From 0231d6f052917badf10ec9240c0db7f004dc06b4 Mon Sep 17 00:00:00 2001 From: Will Winder Date: Fri, 19 Dec 2025 13:47:46 -0500 Subject: [PATCH 1/3] Fix flaky verifier test. --- verifier/verification_coordinator_finality_test.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/verifier/verification_coordinator_finality_test.go b/verifier/verification_coordinator_finality_test.go index 5aa53f250..a82efa3cc 100644 --- a/verifier/verification_coordinator_finality_test.go +++ b/verifier/verification_coordinator_finality_test.go @@ -145,6 +145,7 @@ func TestFinality_CustomFinality(t *testing.T) { require.Equal(t, 1, processedCount, "Should have processed the ready message") } +// TODO: Avoid using sleep to fix timing issues. func TestFinality_WaitingForFinality(t *testing.T) { setup := initializeCoordinator(t, "test-finality-coordinator") @@ -215,6 +216,12 @@ func TestFinality_WaitingForFinality(t *testing.T) { // Update the shared variable to simulate finalized block advancing setup.setFinalizedBlock(uint64(nonFinalizedBlock)) + + // Wait a short while to ensure the SourceReaderService observes the new finalized block + // (poll interval is 50ms in the test). This prevents a race where the event is re-sent + // before the service has updated its view of the finalized block. + time.Sleep(100 * time.Millisecond) + // TODO: This is a hack because the mock doesn't keep on returning the event if it's within range once it's sent to channel. // This is purely a mock limitation. select { From c8b4612ca0628a1ce65be335dd5a2b2ecedc594b Mon Sep 17 00:00:00 2001 From: Will Winder Date: Fri, 19 Dec 2025 13:51:53 -0500 Subject: [PATCH 2/3] Fix flaky verifier test. --- verifier/helpers_test.go | 31 +++++++++++++++++-- .../verification_coordinator_finality_test.go | 7 ----- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/verifier/helpers_test.go b/verifier/helpers_test.go index 8947b72bf..73f1d7f6c 100644 --- a/verifier/helpers_test.go +++ b/verifier/helpers_test.go @@ -123,12 +123,39 @@ func SetupMockSourceReader(t *testing.T) *MockSourceReaderSetup { func (msrs *MockSourceReaderSetup) ExpectFetchMessageSentEvent(maybeVerificationTask bool) { call := msrs.Reader.EXPECT().FetchMessageSentEvents(mock.Anything, mock.Anything, mock.Anything).RunAndReturn(func(ctx context.Context, b, b2 *big.Int) ([]protocol.MessageSentEvent, error) { var events []protocol.MessageSentEvent + fromBlock := uint64(0) + if b != nil { + fromBlock = b.Uint64() + } + + // Wait briefly for at least one event so tests that send right before a fetch aren't lost. + // Use a short timer to avoid hanging tests indefinitely. + timer := time.NewTimer(200 * time.Millisecond) + defer timer.Stop() + + // Try to read an event that matches the fromBlock filter within the timer window. for { select { case event := <-msrs.Channel: - events = append(events, event) - default: + if event.BlockNumber >= fromBlock { + events = append(events, event) + // Drain any additional matching events without blocking + for { + select { + case ev := <-msrs.Channel: + if ev.BlockNumber >= fromBlock { + events = append(events, ev) + } + default: + return events, nil + } + } + } + case <-timer.C: + // no matching events arrived within the window; return whatever we collected (may be empty) return events, nil + case <-ctx.Done(): + return nil, ctx.Err() } } }) diff --git a/verifier/verification_coordinator_finality_test.go b/verifier/verification_coordinator_finality_test.go index a82efa3cc..5aa53f250 100644 --- a/verifier/verification_coordinator_finality_test.go +++ b/verifier/verification_coordinator_finality_test.go @@ -145,7 +145,6 @@ func TestFinality_CustomFinality(t *testing.T) { require.Equal(t, 1, processedCount, "Should have processed the ready message") } -// TODO: Avoid using sleep to fix timing issues. func TestFinality_WaitingForFinality(t *testing.T) { setup := initializeCoordinator(t, "test-finality-coordinator") @@ -216,12 +215,6 @@ func TestFinality_WaitingForFinality(t *testing.T) { // Update the shared variable to simulate finalized block advancing setup.setFinalizedBlock(uint64(nonFinalizedBlock)) - - // Wait a short while to ensure the SourceReaderService observes the new finalized block - // (poll interval is 50ms in the test). This prevents a race where the event is re-sent - // before the service has updated its view of the finalized block. - time.Sleep(100 * time.Millisecond) - // TODO: This is a hack because the mock doesn't keep on returning the event if it's within range once it's sent to channel. // This is purely a mock limitation. select { From 0977f6970a6b3c30f02010d905953d55e036ca27 Mon Sep 17 00:00:00 2001 From: Will Winder Date: Fri, 19 Dec 2025 14:09:33 -0500 Subject: [PATCH 3/3] Simplify. --- verifier/helpers_test.go | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/verifier/helpers_test.go b/verifier/helpers_test.go index 73f1d7f6c..9a14b26a8 100644 --- a/verifier/helpers_test.go +++ b/verifier/helpers_test.go @@ -123,32 +123,23 @@ func SetupMockSourceReader(t *testing.T) *MockSourceReaderSetup { func (msrs *MockSourceReaderSetup) ExpectFetchMessageSentEvent(maybeVerificationTask bool) { call := msrs.Reader.EXPECT().FetchMessageSentEvents(mock.Anything, mock.Anything, mock.Anything).RunAndReturn(func(ctx context.Context, b, b2 *big.Int) ([]protocol.MessageSentEvent, error) { var events []protocol.MessageSentEvent - fromBlock := uint64(0) - if b != nil { - fromBlock = b.Uint64() - } // Wait briefly for at least one event so tests that send right before a fetch aren't lost. // Use a short timer to avoid hanging tests indefinitely. timer := time.NewTimer(200 * time.Millisecond) defer timer.Stop() - // Try to read an event that matches the fromBlock filter within the timer window. for { select { case event := <-msrs.Channel: - if event.BlockNumber >= fromBlock { - events = append(events, event) - // Drain any additional matching events without blocking - for { - select { - case ev := <-msrs.Channel: - if ev.BlockNumber >= fromBlock { - events = append(events, ev) - } - default: - return events, nil - } + events = append(events, event) + // Drain any additional matching events without blocking + for { + select { + case ev := <-msrs.Channel: + events = append(events, ev) + default: + return events, nil } } case <-timer.C: