test(dnstap): fix flaky TestReconnect (#7982)

This commit is contained in:
Ville Vesilehto
2026-03-30 03:03:08 +03:00
committed by GitHub
parent 0e9a51410a
commit 0ba8e3c850

View File

@@ -20,13 +20,28 @@ var (
) )
type MockLogger struct { type MockLogger struct {
WarnCount int mu sync.Mutex
WarnLog string warnCount int
warnLog string
} }
func (l *MockLogger) Warningf(format string, v ...any) { func (l *MockLogger) Warningf(format string, v ...any) {
l.WarnCount++ l.mu.Lock()
l.WarnLog += fmt.Sprintf(format, v...) defer l.mu.Unlock()
l.warnCount++
l.warnLog += fmt.Sprintf(format, v...)
}
func (l *MockLogger) WarnCount() int {
l.mu.Lock()
defer l.mu.Unlock()
return l.warnCount
}
func (l *MockLogger) WarnLog() string {
l.mu.Lock()
defer l.mu.Unlock()
return l.warnLog
} }
func accept(t *testing.T, l net.Listener, count int) { func accept(t *testing.T, l net.Listener, count int) {
@@ -172,9 +187,10 @@ func TestReconnect(t *testing.T) {
// THEN // THEN
// DnsTap is able to reconnect // DnsTap is able to reconnect
// Messages can be sent eventually // Messages can be sent eventually
require.NotNil(t, dio.enc) require.Eventually(t, func() bool {
require.Equal(t, 0, len(dio.queue)) return len(dio.queue) == 0
require.Less(t, logger.WarnCount, messageCount) }, time.Second, 10*time.Millisecond, "queue should be drained by serve goroutine")
require.Less(t, logger.WarnCount(), messageCount)
}) })
t.Run("NotConnectedOnStart", func(t *testing.T) { t.Run("NotConnectedOnStart", func(t *testing.T) {
@@ -225,9 +241,10 @@ func TestReconnect(t *testing.T) {
// THEN // THEN
// DnsTap is able to reconnect // DnsTap is able to reconnect
// Messages can be sent eventually // Messages can be sent eventually
require.NotNil(t, dio.enc) require.Eventually(t, func() bool {
require.Equal(t, 0, len(dio.queue)) return len(dio.queue) == 0
require.Less(t, logger.WarnCount, messageCount) }, time.Second, 10*time.Millisecond, "queue should be drained by serve goroutine")
require.Less(t, logger.WarnCount(), messageCount)
}) })
} }
@@ -264,6 +281,6 @@ func TestFullQueueWriteFail(t *testing.T) {
// THEN // THEN
// Dropped messages are logged // Dropped messages are logged
require.NotEqual(t, 0, logger.WarnCount) require.NotEqual(t, 0, logger.WarnCount())
require.Contains(t, logger.WarnLog, "Dropped dnstap messages") require.Contains(t, logger.WarnLog(), "Dropped dnstap messages")
} }