Skip to content

Commit

Permalink
add TestLokiFlushCancelContext
Browse files Browse the repository at this point in the history
  • Loading branch information
barrettj12 committed Aug 3, 2023
1 parent 34eddbb commit 6aa5f96
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions internals/overlord/logstate/loki_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package logstate

import (
"context"
"net/http"
"net/http/httptest"
"time"

Check failure on line 8 in internals/overlord/logstate/loki_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s Prefix(github.com/canonical/pebble) (gci)
"github.com/canonical/pebble/internals/plan"
"github.com/canonical/pebble/internals/servicelog"
. "gopkg.in/check.v1"

Check failure on line 11 in internals/overlord/logstate/loki_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gci`-ed with --skip-generated -s standard -s default -s Prefix(github.com/canonical/pebble) (gci)
)

type lokiSuite struct{}

var _ = Suite(&lokiSuite{})

func (*lokiSuite) TestLokiFlushCancelContext(c *C) {
serverCtx, killServer := context.WithCancel(context.Background())
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
select {
case <-serverCtx.Done():
// Simulate a slow-responding server
case <-time.After(10 * time.Second):
}
}))
defer s.Close()
defer killServer()

cl := newLokiClient(&plan.LogTarget{Location: s.URL})
err := cl.Write(context.Background(), servicelog.Entry{
Time: time.Now(),
Service: "svc1",
Message: "this is a log line\n",
})
c.Assert(err, IsNil)

// Cancel the Flush context quickly
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Millisecond)
defer cancel()

flushReturned := make(chan struct{})
go func() {
err = cl.Flush(ctx)
close(flushReturned)
c.Assert(err, ErrorMatches, ".*context deadline exceeded")
}()

// Check Flush returns quickly after context timeout
select {
case <-flushReturned:
case <-time.After(2 * time.Millisecond):
c.Fatal("lokiClient.Flush took too long to return after context timeout")
}
}

0 comments on commit 6aa5f96

Please sign in to comment.