Skip to content

Commit

Permalink
Merge pull request #406 from mackerelio/stabilize-check-log-testCancel
Browse files Browse the repository at this point in the history
[check-log] stabilize time-dependent tests
  • Loading branch information
astj committed Aug 28, 2020
2 parents 832aa61 + 70ace34 commit a586b10
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions check-log/lib/check-log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,21 +245,20 @@ func TestRun(t *testing.T) {
}, 1)
}
testCancel := func() {
// This test checks searchLog keeps reading until EOL even if ctx is cancelled.
// This test checks searchLog keeps reading until the first EOL even if ctx is cancelled.
// To guarantee to read at least once, a timeout sec
// should be choice it is greater than reading the state file.
fh.WriteString("OK\nFATAL\nFATAL\n")
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
fh.WriteString("FATAL\nFATAL\nFATAL\n")
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Millisecond)
defer cancel()

expected := time.Now().Add(30 * time.Millisecond)
w, c, errLines, err := opts.searchLog(ctx, logf)
assert.WithinDuration(t, expected, time.Now(), 18*time.Millisecond, "searching time exceeded")

// searchLog should read only first line, so the result counts only the first `FATAL`
assert.Equal(t, err, nil, "err should be nil")
assert.Equal(t, int64(0), w, "something went wrong")
assert.Equal(t, int64(0), c, "something went wrong")
assert.Equal(t, "", errLines, "something went wrong")
assert.Equal(t, int64(1), w, "something went wrong")
assert.Equal(t, int64(1), c, "something went wrong")
assert.Equal(t, "FATAL\n", errLines, "something went wrong")
}
testCancel()
opts.testHookNewBufferedReader = nil
Expand Down

0 comments on commit a586b10

Please sign in to comment.