From 26d72d1a731de84f1ed5fa603cf7960105bb780f Mon Sep 17 00:00:00 2001 From: Eugen Sumin Date: Thu, 21 Mar 2024 11:51:57 +0100 Subject: [PATCH 1/2] fix(tests): Fix flaky TestCloneGlobalLogger --- pkg/log/log_test.go | 42 ++++++++++++++++-------------------------- 1 file changed, 16 insertions(+), 26 deletions(-) diff --git a/pkg/log/log_test.go b/pkg/log/log_test.go index b6ccf5f52f..3ddb8efb45 100644 --- a/pkg/log/log_test.go +++ b/pkg/log/log_test.go @@ -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) @@ -241,11 +220,19 @@ 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 testLogHook struct { + capturedMessages []*logrus.Entry +} + +func newTestLogHook() *testLogHook { + return &testLogHook{ + capturedMessages: make([]*logrus.Entry, 0), + } +} func (t *testLogHook) Levels() []logrus.Level { return []logrus.Level{ @@ -254,6 +241,9 @@ func (t *testLogHook) Levels() []logrus.Level { } } -func (t *testLogHook) Fire(*logrus.Entry) error { +func (t *testLogHook) Fire(entry *logrus.Entry) error { + if t.capturedMessages != nil { + t.capturedMessages = append(t.capturedMessages, entry) + } return nil } From fdd4b042d81349689627289aadf371b55abf5c71 Mon Sep 17 00:00:00 2001 From: Eugen Sumin Date: Thu, 21 Mar 2024 14:12:22 +0100 Subject: [PATCH 2/2] Rename test hook --- pkg/log/log_test.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pkg/log/log_test.go b/pkg/log/log_test.go index 3ddb8efb45..96c2e91c6d 100644 --- a/pkg/log/log_test.go +++ b/pkg/log/log_test.go @@ -211,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) @@ -224,24 +224,24 @@ func (s *LogSuite) TestCloneGlobalLogger(c *C) { c.Assert(hook.capturedMessages[0].Message, Equals, "Test message") } -type testLogHook struct { +type logHook struct { capturedMessages []*logrus.Entry } -func newTestLogHook() *testLogHook { - return &testLogHook{ +func newTestLogHook() *logHook { + return &logHook{ capturedMessages: make([]*logrus.Entry, 0), } } -func (t *testLogHook) Levels() []logrus.Level { +func (t *logHook) Levels() []logrus.Level { return []logrus.Level{ logrus.InfoLevel, logrus.DebugLevel, } } -func (t *testLogHook) Fire(entry *logrus.Entry) error { +func (t *logHook) Fire(entry *logrus.Entry) error { if t.capturedMessages != nil { t.capturedMessages = append(t.capturedMessages, entry) }