TestPromtail: avoid hanging when test fails midway (#5837)

* Allow Promtail.Shutdown to be called again

I want to do this in a test

* TestPromtail: avoid hanging when test fails midway

Also call Shutdown via defer so we clean up those resources when the test fails
pull/5692/head^2
Bryan Boreham 4 years ago committed by GitHub
parent 27a027824f
commit 1329ea165c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      clients/pkg/promtail/promtail.go
  2. 4
      clients/pkg/promtail/promtail_test.go

@ -114,6 +114,9 @@ func (p *Promtail) Client() client.Client {
func (p *Promtail) Shutdown() {
p.mtx.Lock()
defer p.mtx.Unlock()
if p.stopped {
return
}
p.stopped = true
if p.server != nil {
p.server.Shutdown()

@ -86,6 +86,9 @@ func TestPromtail(t *testing.T) {
server = &http.Server{Addr: "localhost:3100", Handler: nil}
)
defer func() {
if t.Failed() {
return // Test has already failed; don't wait for everything to shut down.
}
fmt.Fprintf(os.Stdout, "wait close")
wg.Wait()
if err != nil {
@ -117,6 +120,7 @@ func TestPromtail(t *testing.T) {
err = errors.Wrap(err, "Failed to start promtail")
}
}()
defer p.Shutdown() // In case the test fails before the call to Shutdown below.
expectedCounts := map[string]int{}

Loading…
Cancel
Save