Skip to content

Commit

Permalink
fix(tests): Fix flaky TestCloneGlobalLogger (#2775)
Browse files Browse the repository at this point in the history
* fix(tests): Fix flaky TestCloneGlobalLogger

* Rename test hook
  • Loading branch information
e-sumin authored Mar 21, 2024
1 parent 35043cf commit 67985d8
Showing 1 changed file with 18 additions and 28 deletions.
46 changes: 18 additions & 28 deletions pkg/log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,28 +197,7 @@ func (s *LogSuite) TestLogLevel(c *C) {
}

func (s *LogSuite) TestCloneGlobalLogger(c *C) {
done := make(chan struct{})
defer close(done)

capturedMessages := make([]*logrus.Entry, 0)
newTestHook := func(ctx context.Context) *FluentbitHook {
ec := make(chan *logrus.Entry, defaultEntryBufferCount)

go func(in <-chan *logrus.Entry) {
for {
select {
case e := <-in:
capturedMessages = append(capturedMessages, e)
case <-done:
return
}
}
}(ec)

return &FluentbitHook{logs: ec}
}

hook := newTestHook(context.TODO())
hook := newTestLogHook()
log.AddHook(hook)
actual := cloneGlobalLogger()
c.Assert(actual.Formatter, Equals, log.Formatter)
Expand All @@ -232,7 +211,7 @@ func (s *LogSuite) TestCloneGlobalLogger(c *C) {
actual.SetReportCaller(true)
actual.SetLevel(logrus.ErrorLevel)
actual.SetOutput(&bytes.Buffer{})
actual.AddHook(&testLogHook{})
actual.AddHook(&logHook{})

c.Assert(actual.Formatter, Not(Equals), log.Formatter)
c.Assert(actual.ReportCaller, Not(Equals), log.ReportCaller)
Expand All @@ -241,19 +220,30 @@ func (s *LogSuite) TestCloneGlobalLogger(c *C) {
c.Assert(actual.Hooks, Not(DeepEquals), log.Hooks)

log.Println("Test message")
c.Assert(len(capturedMessages), Equals, 1)
c.Assert(capturedMessages[0].Message, Equals, "Test message")
c.Assert(len(hook.capturedMessages), Equals, 1)
c.Assert(hook.capturedMessages[0].Message, Equals, "Test message")
}

type testLogHook struct{}
type logHook struct {
capturedMessages []*logrus.Entry
}

func (t *testLogHook) Levels() []logrus.Level {
func newTestLogHook() *logHook {
return &logHook{
capturedMessages: make([]*logrus.Entry, 0),
}
}

func (t *logHook) Levels() []logrus.Level {
return []logrus.Level{
logrus.InfoLevel,
logrus.DebugLevel,
}
}

func (t *testLogHook) Fire(*logrus.Entry) error {
func (t *logHook) Fire(entry *logrus.Entry) error {
if t.capturedMessages != nil {
t.capturedMessages = append(t.capturedMessages, entry)
}
return nil
}

0 comments on commit 67985d8

Please sign in to comment.