@@ -22,19 +22,42 @@ import (
2222// requests to the mock heimdal server for specific functions. Add more handlers
2323// according to requirements.
2424type HttpHandlerFake struct {
25+ mu sync.RWMutex
2526 handleFetchCheckpoint http.HandlerFunc
2627 handleFetchMilestone http.HandlerFunc
2728}
2829
30+ func (h * HttpHandlerFake ) SetCheckpointHandler (handler http.HandlerFunc ) {
31+ h .mu .Lock ()
32+ defer h .mu .Unlock ()
33+ h .handleFetchCheckpoint = handler
34+ }
35+
36+ func (h * HttpHandlerFake ) SetMilestoneHandler (handler http.HandlerFunc ) {
37+ h .mu .Lock ()
38+ defer h .mu .Unlock ()
39+ h .handleFetchMilestone = handler
40+ }
41+
2942func (h * HttpHandlerFake ) GetCheckpointHandler () http.HandlerFunc {
3043 return func (w http.ResponseWriter , r * http.Request ) {
31- h .handleFetchCheckpoint .ServeHTTP (w , r )
44+ h .mu .RLock ()
45+ handler := h .handleFetchCheckpoint
46+ h .mu .RUnlock ()
47+ if handler != nil {
48+ handler .ServeHTTP (w , r )
49+ }
3250 }
3351}
3452
3553func (h * HttpHandlerFake ) GetMilestoneHandler () http.HandlerFunc {
3654 return func (w http.ResponseWriter , r * http.Request ) {
37- h .handleFetchMilestone .ServeHTTP (w , r )
55+ h .mu .RLock ()
56+ handler := h .handleFetchMilestone
57+ h .mu .RUnlock ()
58+ if handler != nil {
59+ handler .ServeHTTP (w , r )
60+ }
3861 }
3962}
4063
@@ -97,7 +120,7 @@ func TestFetchCheckpointFromMockHeimdall(t *testing.T) {
97120 }
98121
99122 handler := & HttpHandlerFake {}
100- handler .handleFetchCheckpoint = func (w http.ResponseWriter , _ * http.Request ) {
123+ handler .SetCheckpointHandler ( func (w http.ResponseWriter , _ * http.Request ) {
101124 err := json .NewEncoder (w ).Encode (gRPCGatewayCheckpointResponseV2 {
102125 Result : gRPCGatewayCheckpointV2 {
103126 Proposer : common.Address {},
@@ -112,7 +135,7 @@ func TestFetchCheckpointFromMockHeimdall(t *testing.T) {
112135 if err != nil {
113136 w .WriteHeader (500 ) // Return 500 Internal Server Error.
114137 }
115- }
138+ })
116139
117140 // Fetch available port
118141 port , listener , err := network .FindAvailablePort ()
@@ -165,7 +188,7 @@ func TestFetchMilestoneFromMockHeimdall(t *testing.T) {
165188 }
166189
167190 handler := & HttpHandlerFake {}
168- handler .handleFetchMilestone = func (w http.ResponseWriter , _ * http.Request ) {
191+ handler .SetMilestoneHandler ( func (w http.ResponseWriter , _ * http.Request ) {
169192 err := json .NewEncoder (w ).Encode (gRPCGatewayMilestoneResponseV2 {
170193 Result : gRPCGatewayMilestoneV2 {
171194 Proposer : common.Address {},
@@ -181,7 +204,7 @@ func TestFetchMilestoneFromMockHeimdall(t *testing.T) {
181204 if err != nil {
182205 w .WriteHeader (500 ) // Return 500 Internal Server Error.
183206 }
184- }
207+ })
185208
186209 // Fetch available port
187210 port , listener , err := network .FindAvailablePort ()
@@ -222,7 +245,7 @@ func TestFetchShutdown(t *testing.T) {
222245 // Case1 - Testing context timeout: Create delay in serving requests for simulating timeout. Add delay slightly
223246 // greater than `retryDelay`. This should cause the request to timeout and trigger shutdown
224247 // due to `ctx.Done()`. Expect context timeout error.
225- handler .handleFetchCheckpoint = func (w http.ResponseWriter , _ * http.Request ) {
248+ handler .SetCheckpointHandler ( func (w http.ResponseWriter , _ * http.Request ) {
226249 time .Sleep (100 * time .Millisecond )
227250
228251 err := json .NewEncoder (w ).Encode (checkpoint.CheckpointResponse {
@@ -239,7 +262,7 @@ func TestFetchShutdown(t *testing.T) {
239262 if err != nil {
240263 w .WriteHeader (500 ) // Return 500 Internal Server Error.
241264 }
242- }
265+ })
243266
244267 // Fetch available port
245268 port , listener , err := network .FindAvailablePort ()
@@ -264,10 +287,10 @@ func TestFetchShutdown(t *testing.T) {
264287 // Case2 - Testing context cancellation. Pass a context with timeout to the request and
265288 // cancel it before timeout. This should cause the request to timeout and trigger shutdown
266289 // due to `ctx.Done()`. Expect context cancellation error.
267- handler .handleFetchCheckpoint = func (w http.ResponseWriter , _ * http.Request ) {
290+ handler .SetCheckpointHandler ( func (w http.ResponseWriter , _ * http.Request ) {
268291 time .Sleep (10 * time .Millisecond )
269292 w .WriteHeader (500 ) // Return 500 Internal Server Error.
270- }
293+ })
271294
272295 ctx , cancel = context .WithTimeout (t .Context (), 50 * time .Millisecond ) // Use some high value for timeout
273296
@@ -285,9 +308,9 @@ func TestFetchShutdown(t *testing.T) {
285308 // Case3 - Testing interrupt: Closing the `closeCh` in heimdall client simulating interrupt. This
286309 // should cause the request to fail and throw an error due to `<-closeCh` in fetchWithRetry.
287310 // Expect shutdown detected error.
288- handler .handleFetchCheckpoint = func (w http.ResponseWriter , _ * http.Request ) {
311+ handler .SetCheckpointHandler ( func (w http.ResponseWriter , _ * http.Request ) {
289312 w .WriteHeader (500 ) // Return 500 Internal Server Error.
290- }
313+ })
291314
292315 // Close the channel after a delay until we make request
293316 go func () {
0 commit comments