Skip to content

Commit b01a3cd

Browse files
authored
Merge pull request #232 from thirdweb-dev/06-27-fix_race_condition_in_tests
fix race condition in tests
2 parents f909dbb + f0325ba commit b01a3cd

File tree

1 file changed

+36
-8
lines changed

1 file changed

+36
-8
lines changed

internal/handlers/search_handlers_test.go

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,13 @@ func TestSearch_TransactionHash(t *testing.T) {
7070

7171
txHash := "0x1234567890123456789012345678901234567890123456789012345678901234"
7272

73+
// Mock the 3 GetTransactions calls for different time ranges
74+
// 1. Past 5 days (startOffsetDays=5, endOffsetDays=0)
7375
mockStorage.EXPECT().GetTransactions(mock.MatchedBy(func(filter storage.QueryFilter) bool {
7476
return filter.ChainId.Cmp(big.NewInt(1)) == 0 &&
75-
filter.FilterParams["hash"] == txHash
77+
filter.FilterParams["hash"] == txHash &&
78+
filter.FilterParams["block_timestamp_gte"] != "" &&
79+
filter.FilterParams["block_timestamp_lte"] == ""
7680
})).Return(storage.QueryResult[common.Transaction]{
7781
Data: []common.Transaction{{
7882
Hash: txHash,
@@ -84,9 +88,33 @@ func TestSearch_TransactionHash(t *testing.T) {
8488
}},
8589
}, nil)
8690

87-
// Mock other necessary calls for block and logs search
88-
mockStorage.EXPECT().GetBlocks(mock.Anything).Return(storage.QueryResult[common.Block]{}, nil)
89-
mockStorage.EXPECT().GetLogs(mock.Anything).Return(storage.QueryResult[common.Log]{}, nil)
91+
// 2. 5-30 days (startOffsetDays=30, endOffsetDays=5)
92+
mockStorage.EXPECT().GetTransactions(mock.MatchedBy(func(filter storage.QueryFilter) bool {
93+
return filter.ChainId.Cmp(big.NewInt(1)) == 0 &&
94+
filter.FilterParams["hash"] == txHash &&
95+
filter.FilterParams["block_timestamp_gte"] != "" &&
96+
filter.FilterParams["block_timestamp_lte"] != ""
97+
})).Return(storage.QueryResult[common.Transaction]{}, nil)
98+
99+
// 3. More than 30 days (startOffsetDays=0, endOffsetDays=30)
100+
mockStorage.EXPECT().GetTransactions(mock.MatchedBy(func(filter storage.QueryFilter) bool {
101+
return filter.ChainId.Cmp(big.NewInt(1)) == 0 &&
102+
filter.FilterParams["hash"] == txHash &&
103+
filter.FilterParams["block_timestamp_gte"] == "" &&
104+
filter.FilterParams["block_timestamp_lte"] != ""
105+
})).Return(storage.QueryResult[common.Transaction]{}, nil)
106+
107+
// Mock the GetBlocks call for block hash search
108+
mockStorage.EXPECT().GetBlocks(mock.MatchedBy(func(filter storage.QueryFilter) bool {
109+
return filter.ChainId.Cmp(big.NewInt(1)) == 0 &&
110+
filter.FilterParams["hash"] == txHash
111+
})).Return(storage.QueryResult[common.Block]{}, nil)
112+
113+
// Mock the GetLogs call for topic_0 search
114+
mockStorage.EXPECT().GetLogs(mock.MatchedBy(func(filter storage.QueryFilter) bool {
115+
return filter.ChainId.Cmp(big.NewInt(1)) == 0 &&
116+
filter.Signature == txHash
117+
})).Return(storage.QueryResult[common.Log]{}, nil)
90118

91119
w := httptest.NewRecorder()
92120
req, _ := http.NewRequest("GET", "/v1/search/1/"+txHash, nil)
@@ -112,7 +140,7 @@ func TestSearch_Address(t *testing.T) {
112140
router, mockStorage := setupTestRouter()
113141

114142
address := "0x1234567890123456789012345678901234567890"
115-
mockStorage.On("GetTransactions", mock.MatchedBy(func(filter storage.QueryFilter) bool {
143+
mockStorage.EXPECT().GetTransactions(mock.MatchedBy(func(filter storage.QueryFilter) bool {
116144
return filter.ChainId.Cmp(big.NewInt(1)) == 0 &&
117145
filter.ContractAddress == address
118146
})).Return(storage.QueryResult[common.Transaction]{
@@ -126,7 +154,7 @@ func TestSearch_Address(t *testing.T) {
126154
}},
127155
}, nil)
128156

129-
mockStorage.On("GetTransactions", mock.MatchedBy(func(filter storage.QueryFilter) bool {
157+
mockStorage.EXPECT().GetTransactions(mock.MatchedBy(func(filter storage.QueryFilter) bool {
130158
return filter.ChainId.Cmp(big.NewInt(1)) == 0 &&
131159
filter.FromAddress == address
132160
})).Return(storage.QueryResult[common.Transaction]{
@@ -164,7 +192,7 @@ func TestSearch_Contract(t *testing.T) {
164192
router, mockStorage := setupTestRouter()
165193

166194
address := "0x1234567890123456789012345678901234567890"
167-
mockStorage.On("GetTransactions", mock.MatchedBy(func(filter storage.QueryFilter) bool {
195+
mockStorage.EXPECT().GetTransactions(mock.MatchedBy(func(filter storage.QueryFilter) bool {
168196
return filter.ChainId.Cmp(big.NewInt(1)) == 0 &&
169197
filter.ContractAddress == address
170198
})).Return(storage.QueryResult[common.Transaction]{
@@ -204,7 +232,7 @@ func TestSearch_FunctionSignature(t *testing.T) {
204232
router, mockStorage := setupTestRouter()
205233

206234
signature := "0x12345678"
207-
mockStorage.On("GetTransactions", mock.MatchedBy(func(filter storage.QueryFilter) bool {
235+
mockStorage.EXPECT().GetTransactions(mock.MatchedBy(func(filter storage.QueryFilter) bool {
208236
return filter.ChainId.Cmp(big.NewInt(1)) == 0 &&
209237
filter.Signature == signature
210238
})).Return(storage.QueryResult[common.Transaction]{

0 commit comments

Comments
 (0)