Skip to content

Commit

Permalink
testing: fix telemetry unit tests (algorand#2321)
Browse files Browse the repository at this point in the history
Fix and re-enable async telemetry unit test TestAsyncTelemetryHook_CloseDrop.
  • Loading branch information
winder authored Jun 24, 2021
1 parent b55b53f commit f870265
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
2 changes: 1 addition & 1 deletion logging/telemetryhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func createAsyncHook(wrappedHook logrus.Hook, channelDepth uint, maxQueueDepth i
}

func createAsyncHookLevels(wrappedHook logrus.Hook, channelDepth uint, maxQueueDepth int, levels []logrus.Level) *asyncTelemetryHook {
// one time check to see if the wrappedHook is ready (true for mocked telemetry)
// needed by 'makeTelemetryTestFixtureWithConfig' to mark ready in unit tests.
tfh, ok := wrappedHook.(*telemetryFilteredHook)
ready := ok && tfh.wrappedHook != nil

Expand Down
24 changes: 10 additions & 14 deletions logging/telemetryhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"os"
"path/filepath"
"testing"
"time"

"github.com/sirupsen/logrus"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -170,36 +169,32 @@ func TestSaveLoadConfig(t *testing.T) {
os.RemoveAll(configDir)
}

func TestAsyncTelemetryHook_Close(t *testing.T) {
t.Skip("We no longer ensure 100% delivery. To not block, we drop messages when they come in faster than the network sends them.")
a := require.New(t)
t.Parallel()

func TestAsyncTelemetryHook_CloseDrop(t *testing.T) {
const entryCount = 100

filling := make(chan struct{})

testHook := makeMockTelemetryHook(logrus.DebugLevel)
testHook.cb = func(entry *logrus.Entry) {
// Inject a delay to ensure we buffer entries
time.Sleep(1 * time.Millisecond)
<-filling // Block while filling
}
hook := createAsyncHook(&testHook, 4, entryCount)
hook.ready = true
for i := 0; i < entryCount; i++ {
entry := logrus.Entry{
Level: logrus.ErrorLevel,
}
hook.Fire(&entry)
}

close(filling)
hook.Close()

a.Equal(entryCount, len(testHook.entries()))
// To not block, we drop messages when they come in faster than the network sends them.
require.Less(t, len(testHook.entries()), entryCount)
}

func TestAsyncTelemetryHook_QueueDepth(t *testing.T) {
t.Skip("flakey test can fail on slow test systems")
a := require.New(t)
t.Parallel()

const entryCount = 100
const maxDepth = 10

Expand All @@ -211,6 +206,7 @@ func TestAsyncTelemetryHook_QueueDepth(t *testing.T) {
}

hook := createAsyncHook(&testHook, entryCount, maxDepth)
hook.ready = true
for i := 0; i < entryCount; i++ {
entry := logrus.Entry{
Level: logrus.ErrorLevel,
Expand All @@ -221,5 +217,5 @@ func TestAsyncTelemetryHook_QueueDepth(t *testing.T) {
close(filling)
hook.Close()

a.Equal(maxDepth, len(testHook.entries()))
require.Equal(t, maxDepth, len(testHook.entries()))
}

0 comments on commit f870265

Please sign in to comment.