-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
34eddbb
commit 6aa5f96
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package logstate | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"net/http/httptest" | ||
"time" | ||
|
||
"github.com/canonical/pebble/internals/plan" | ||
"github.com/canonical/pebble/internals/servicelog" | ||
. "gopkg.in/check.v1" | ||
) | ||
|
||
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") | ||
} | ||
} |