Skip to content

Commit 59e8864

Browse files
authored
Merge pull request #237 from thirdweb-dev/07-09-fix_test_race_condition
fix test race condition
2 parents 04f775a + b479d94 commit 59e8864

File tree

2 files changed

+34
-14
lines changed

2 files changed

+34
-14
lines changed

internal/handlers/search_handlers_test.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func TestSearch_TransactionHash(t *testing.T) {
7171
txHash := "0x1234567890123456789012345678901234567890123456789012345678901234"
7272

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

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

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

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

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

119119
w := httptest.NewRecorder()
120120
req, _ := http.NewRequest("GET", "/v1/search/1/"+txHash, nil)

internal/orchestrator/reorg_handler_test.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,10 +536,30 @@ func TestStartReorgHandler(t *testing.T) {
536536

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

539-
go handler.Start(context.Background())
539+
// Create a cancelable context
540+
ctx, cancel := context.WithCancel(context.Background())
541+
defer cancel()
542+
543+
// Start the handler in a goroutine
544+
done := make(chan struct{})
545+
go func() {
546+
handler.Start(ctx)
547+
close(done)
548+
}()
540549

541550
// Allow some time for the goroutine to run
542551
time.Sleep(250 * time.Millisecond)
552+
553+
// Cancel the context to stop the handler
554+
cancel()
555+
556+
// Wait for the handler to stop with a timeout
557+
select {
558+
case <-done:
559+
// Success - handler stopped
560+
case <-time.After(2 * time.Second):
561+
t.Fatal("Handler did not stop within timeout period after receiving cancel signal")
562+
}
543563
}
544564

545565
func TestReorgHandlingIsSkippedIfMostRecentAndLastCheckedBlockAreSame(t *testing.T) {

0 commit comments

Comments
 (0)