Skip to content

Flaky test: TestMemoryStorage_CleanupLoop/cleanup_runs_periodically #4096

@JAORMX

Description

@JAORMX

Summary

TestMemoryStorage_CleanupLoop/cleanup_runs_periodically in pkg/authserver/storage/memory_test.go is flaky due to timing sensitivity.

Details

The test creates a storage with a 50ms cleanup interval, inserts an expired auth code, then sleeps 100ms and asserts the code was cleaned up:

storage := NewMemoryStorage(WithCleanupInterval(50 * time.Millisecond))
// ... insert expired session ...
time.Sleep(100 * time.Millisecond)
assert.Equal(t, 0, storage.Stats().AuthCodes) // fails intermittently

Under load (e.g., -race with many parallel tests), the cleanup goroutine may not have fired within the 100ms sleep window, causing the assertion to fail with expected: 0, actual: 1.

Location: pkg/authserver/storage/memory_test.go:716-729

Suggested fix

Replace the fixed sleep with a polling loop using require.Eventually or assert.Eventually:

require.Eventually(t, func() bool {
    return storage.Stats().AuthCodes == 0
}, 2*time.Second, 25*time.Millisecond, "expired auth code should be cleaned up")

This is deterministic regardless of system load while still testing the same cleanup behavior.

Metadata

Metadata

Assignees

Labels

bugSomething isn't workinggoPull requests that update go code

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions