Skip to content

fix test race condition #237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions internal/handlers/search_handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func TestSearch_TransactionHash(t *testing.T) {
txHash := "0x1234567890123456789012345678901234567890123456789012345678901234"

// Mock the 3 GetTransactions calls for different time ranges
// 1. Past 5 days (startOffsetDays=5, endOffsetDays=0)
// 1. Past 5 days (startOffsetDays=5, endOffsetDays=0) - This should always be called first and return a result
mockStorage.EXPECT().GetTransactions(mock.MatchedBy(func(filter storage.QueryFilter) bool {
return filter.ChainId.Cmp(big.NewInt(1)) == 0 &&
filter.FilterParams["hash"] == txHash &&
Expand All @@ -88,33 +88,33 @@ func TestSearch_TransactionHash(t *testing.T) {
}},
}, nil)

// 2. 5-30 days (startOffsetDays=30, endOffsetDays=5)
mockStorage.EXPECT().GetTransactions(mock.MatchedBy(func(filter storage.QueryFilter) bool {
// 2. 5-30 days (startOffsetDays=30, endOffsetDays=5) - This might not be called due to race conditions
mockStorage.On("GetTransactions", mock.MatchedBy(func(filter storage.QueryFilter) bool {
return filter.ChainId.Cmp(big.NewInt(1)) == 0 &&
filter.FilterParams["hash"] == txHash &&
filter.FilterParams["block_timestamp_gte"] != "" &&
filter.FilterParams["block_timestamp_lte"] != ""
})).Return(storage.QueryResult[common.Transaction]{}, nil)
})).Return(storage.QueryResult[common.Transaction]{}, nil).Maybe()

// 3. More than 30 days (startOffsetDays=0, endOffsetDays=30)
mockStorage.EXPECT().GetTransactions(mock.MatchedBy(func(filter storage.QueryFilter) bool {
// 3. More than 30 days (startOffsetDays=0, endOffsetDays=30) - This might not be called due to race conditions
mockStorage.On("GetTransactions", mock.MatchedBy(func(filter storage.QueryFilter) bool {
return filter.ChainId.Cmp(big.NewInt(1)) == 0 &&
filter.FilterParams["hash"] == txHash &&
filter.FilterParams["block_timestamp_gte"] == "" &&
filter.FilterParams["block_timestamp_lte"] != ""
})).Return(storage.QueryResult[common.Transaction]{}, nil)
})).Return(storage.QueryResult[common.Transaction]{}, nil).Maybe()

// Mock the GetBlocks call for block hash search
mockStorage.EXPECT().GetBlocks(mock.MatchedBy(func(filter storage.QueryFilter) bool {
// Mock the GetBlocks call for block hash search - This might not be called due to race conditions
mockStorage.On("GetBlocks", mock.MatchedBy(func(filter storage.QueryFilter) bool {
return filter.ChainId.Cmp(big.NewInt(1)) == 0 &&
filter.FilterParams["hash"] == txHash
})).Return(storage.QueryResult[common.Block]{}, nil)
})).Return(storage.QueryResult[common.Block]{}, nil).Maybe()

// Mock the GetLogs call for topic_0 search
mockStorage.EXPECT().GetLogs(mock.MatchedBy(func(filter storage.QueryFilter) bool {
// Mock the GetLogs call for topic_0 search - This might not be called due to race conditions
mockStorage.On("GetLogs", mock.MatchedBy(func(filter storage.QueryFilter) bool {
return filter.ChainId.Cmp(big.NewInt(1)) == 0 &&
filter.Signature == txHash
})).Return(storage.QueryResult[common.Log]{}, nil)
})).Return(storage.QueryResult[common.Log]{}, nil).Maybe()

w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/v1/search/1/"+txHash, nil)
Expand Down
22 changes: 21 additions & 1 deletion internal/orchestrator/reorg_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -536,10 +536,30 @@ func TestStartReorgHandler(t *testing.T) {

mockOrchestratorStorage.EXPECT().SetLastReorgCheckedBlockNumber(mock.Anything, mock.Anything).Return(nil).Times(2)

go handler.Start(context.Background())
// Create a cancelable context
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

// Start the handler in a goroutine
done := make(chan struct{})
go func() {
handler.Start(ctx)
close(done)
}()

// Allow some time for the goroutine to run
time.Sleep(250 * time.Millisecond)

// Cancel the context to stop the handler
cancel()

// Wait for the handler to stop with a timeout
select {
case <-done:
// Success - handler stopped
case <-time.After(2 * time.Second):
t.Fatal("Handler did not stop within timeout period after receiving cancel signal")
}
}

func TestReorgHandlingIsSkippedIfMostRecentAndLastCheckedBlockAreSame(t *testing.T) {
Expand Down