Skip to content

Commit ee4c9e4

Browse files
authored
Merge pull request #6822 from onflow/petera/fix-proxy-unittest
[Access] Fix mocking in connection unittests
2 parents 088596f + 6810ae4 commit ee4c9e4

File tree

1 file changed

+115
-37
lines changed

1 file changed

+115
-37
lines changed

engine/access/rpc/connection/connection_test.go

Lines changed: 115 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ func TestProxyAccessAPI(t *testing.T) {
4040

4141
req := &access.PingRequest{}
4242
expected := &access.PingResponse{}
43-
cn.handler.On("Ping", testifymock.Anything, req).Return(expected, nil)
43+
cn.handler.
44+
On("Ping",
45+
testifymock.Anything,
46+
testifymock.AnythingOfType("*access.PingRequest")).
47+
Return(expected, nil)
4448

4549
// create the factory
4650
connectionFactory := new(ConnectionFactoryImpl)
@@ -85,7 +89,11 @@ func TestProxyExecutionAPI(t *testing.T) {
8589

8690
req := &execution.PingRequest{}
8791
expected := &execution.PingResponse{}
88-
en.handler.On("Ping", testifymock.Anything, req).Return(expected, nil)
92+
en.handler.
93+
On("Ping",
94+
testifymock.Anything,
95+
testifymock.AnythingOfType("*execution.PingRequest")).
96+
Return(expected, nil)
8997

9098
// create the factory
9199
connectionFactory := new(ConnectionFactoryImpl)
@@ -130,7 +138,11 @@ func TestProxyAccessAPIConnectionReuse(t *testing.T) {
130138

131139
req := &access.PingRequest{}
132140
expected := &access.PingResponse{}
133-
cn.handler.On("Ping", testifymock.Anything, req).Return(expected, nil)
141+
cn.handler.
142+
On("Ping",
143+
testifymock.Anything,
144+
testifymock.AnythingOfType("*access.PingRequest")).
145+
Return(expected, nil)
134146

135147
// create the factory
136148
connectionFactory := new(ConnectionFactoryImpl)
@@ -188,7 +200,11 @@ func TestProxyExecutionAPIConnectionReuse(t *testing.T) {
188200

189201
req := &execution.PingRequest{}
190202
expected := &execution.PingResponse{}
191-
en.handler.On("Ping", testifymock.Anything, req).Return(expected, nil)
203+
en.handler.
204+
On("Ping",
205+
testifymock.Anything,
206+
testifymock.AnythingOfType("*execution.PingRequest")).
207+
Return(expected, nil)
192208

193209
// create the factory
194210
connectionFactory := new(ConnectionFactoryImpl)
@@ -250,7 +266,12 @@ func TestExecutionNodeClientTimeout(t *testing.T) {
250266
// setup the handler mock to not respond within the timeout
251267
req := &execution.PingRequest{}
252268
resp := &execution.PingResponse{}
253-
en.handler.On("Ping", testifymock.Anything, req).After(timeout+time.Second).Return(resp, nil)
269+
en.handler.
270+
On("Ping",
271+
testifymock.Anything,
272+
testifymock.AnythingOfType("*execution.PingRequest")).
273+
After(timeout+time.Second).
274+
Return(resp, nil)
254275

255276
// create the factory
256277
connectionFactory := new(ConnectionFactoryImpl)
@@ -302,7 +323,12 @@ func TestCollectionNodeClientTimeout(t *testing.T) {
302323
// setup the handler mock to not respond within the timeout
303324
req := &access.PingRequest{}
304325
resp := &access.PingResponse{}
305-
cn.handler.On("Ping", testifymock.Anything, req).After(timeout+time.Second).Return(resp, nil)
326+
cn.handler.
327+
On("Ping",
328+
testifymock.Anything,
329+
testifymock.AnythingOfType("*access.PingRequest")).
330+
After(timeout+time.Second).
331+
Return(resp, nil)
306332

307333
// create the factory
308334
connectionFactory := new(ConnectionFactoryImpl)
@@ -353,11 +379,22 @@ func TestConnectionPoolFull(t *testing.T) {
353379
defer cn2.stop(t)
354380
defer cn3.stop(t)
355381

356-
req := &access.PingRequest{}
357382
expected := &access.PingResponse{}
358-
cn1.handler.On("Ping", testifymock.Anything, req).Return(expected, nil)
359-
cn2.handler.On("Ping", testifymock.Anything, req).Return(expected, nil)
360-
cn3.handler.On("Ping", testifymock.Anything, req).Return(expected, nil)
383+
cn1.handler.
384+
On("Ping",
385+
testifymock.Anything,
386+
testifymock.AnythingOfType("*access.PingRequest")).
387+
Return(expected, nil)
388+
cn2.handler.
389+
On("Ping",
390+
testifymock.Anything,
391+
testifymock.AnythingOfType("*access.PingRequest")).
392+
Return(expected, nil)
393+
cn3.handler.
394+
On("Ping",
395+
testifymock.Anything,
396+
testifymock.AnythingOfType("*access.PingRequest")).
397+
Return(expected, nil)
361398

362399
// create the factory
363400
connectionFactory := new(ConnectionFactoryImpl)
@@ -436,7 +473,11 @@ func TestConnectionPoolStale(t *testing.T) {
436473

437474
req := &access.PingRequest{}
438475
expected := &access.PingResponse{}
439-
cn.handler.On("Ping", testifymock.Anything, req).Return(expected, nil)
476+
cn.handler.
477+
On("Ping",
478+
testifymock.Anything,
479+
testifymock.AnythingOfType("*access.PingRequest")).
480+
Return(expected, nil)
440481

441482
// create the factory
442483
connectionFactory := new(ConnectionFactoryImpl)
@@ -525,9 +566,14 @@ func TestExecutionNodeClientClosedGracefully(t *testing.T) {
525566
req := &execution.PingRequest{}
526567
resp := &execution.PingResponse{}
527568
respSent := atomic.NewUint64(0)
528-
en.handler.On("Ping", testifymock.Anything, req).Run(func(_ testifymock.Arguments) {
529-
respSent.Inc()
530-
}).Return(resp, nil)
569+
en.handler.
570+
On("Ping",
571+
testifymock.Anything,
572+
testifymock.AnythingOfType("*execution.PingRequest")).
573+
Run(func(_ testifymock.Arguments) {
574+
respSent.Inc()
575+
}).
576+
Return(resp, nil)
531577

532578
// create the factory
533579
connectionFactory := new(ConnectionFactoryImpl)
@@ -615,14 +661,18 @@ func TestEvictingCacheClients(t *testing.T) {
615661
// Set up mock handlers for Ping and GetNetworkParameters
616662
pingReq := &access.PingRequest{}
617663
pingResp := &access.PingResponse{}
618-
cn.handler.On("Ping", testifymock.Anything, pingReq).Return(
619-
func(context.Context, *access.PingRequest) *access.PingResponse {
620-
close(startPing)
621-
<-returnFromPing // keeps request open until returnFromPing is closed
622-
return pingResp
623-
},
624-
func(context.Context, *access.PingRequest) error { return nil },
625-
)
664+
cn.handler.
665+
On("Ping",
666+
testifymock.Anything,
667+
testifymock.AnythingOfType("*access.PingRequest")).
668+
Return(
669+
func(context.Context, *access.PingRequest) *access.PingResponse {
670+
close(startPing)
671+
<-returnFromPing // keeps request open until returnFromPing is closed
672+
return pingResp
673+
},
674+
func(context.Context, *access.PingRequest) error { return nil },
675+
)
626676

627677
netReq := &access.GetNetworkParametersRequest{}
628678
netResp := &access.GetNetworkParametersResponse{}
@@ -748,7 +798,9 @@ func TestConcurrentConnections(t *testing.T) {
748798
requestCount := rapid.IntRange(50, 1000).Draw(tt, "r")
749799
responsesSent := atomic.NewInt32(0)
750800
en.handler.
751-
On("Ping", testifymock.Anything, req).
801+
On("Ping",
802+
testifymock.Anything,
803+
testifymock.AnythingOfType("*execution.PingRequest")).
752804
Return(func(_ context.Context, _ *execution.PingRequest) (*execution.PingResponse, error) {
753805
time.Sleep(getSleep() * time.Microsecond)
754806

@@ -891,7 +943,13 @@ func TestCircuitBreakerExecutionNode(t *testing.T) {
891943
ctx := context.Background()
892944

893945
// Set up the handler mock to not respond within the requestTimeout.
894-
en.handler.On("Ping", testifymock.Anything, req).After(2*requestTimeout).Return(resp, nil)
946+
en.handler.
947+
On("Ping",
948+
testifymock.Anything,
949+
testifymock.AnythingOfType("*execution.PingRequest")).
950+
After(2*requestTimeout).
951+
Return(resp, nil).
952+
Once()
895953

896954
// Call and measure the duration for the first invocation.
897955
duration, err := callAndMeasurePingDuration(ctx)
@@ -900,28 +958,35 @@ func TestCircuitBreakerExecutionNode(t *testing.T) {
900958

901959
// Call and measure the duration for the second invocation (circuit breaker state is now "Open").
902960
duration, err = callAndMeasurePingDuration(ctx)
903-
assert.Equal(t, gobreaker.ErrOpenState, err)
961+
assert.ErrorIs(t, err, gobreaker.ErrOpenState)
904962
assert.Greater(t, requestTimeout, duration)
905963

906-
// Reset the mock Ping for the next invocation to return response without delay
907-
en.handler.On("Ping", testifymock.Anything, req).Unset()
908-
en.handler.On("Ping", testifymock.Anything, req).Return(resp, nil)
964+
en.handler.
965+
On("Ping",
966+
testifymock.Anything,
967+
testifymock.AnythingOfType("*execution.PingRequest")).
968+
Return(resp, nil).
969+
Once()
909970

910971
// Wait until the circuit breaker transitions to the "HalfOpen" state.
911972
time.Sleep(circuitBreakerRestoreTimeout + (500 * time.Millisecond))
912973

913974
// Call and measure the duration for the third invocation (circuit breaker state is now "HalfOpen").
914975
duration, err = callAndMeasurePingDuration(ctx)
915976
assert.Greater(t, requestTimeout, duration)
916-
assert.Equal(t, nil, err)
977+
assert.NoError(t, err)
917978
})
918979

919980
for _, code := range successCodes {
920981
t.Run(fmt.Sprintf("test error %s treated as a success for circuit breaker ", code.String()), func(t *testing.T) {
921982
ctx := context.Background()
922983

923-
en.handler.On("Ping", testifymock.Anything, req).Unset()
924-
en.handler.On("Ping", testifymock.Anything, req).Return(nil, status.Error(code, code.String()))
984+
en.handler.
985+
On("Ping",
986+
testifymock.Anything,
987+
testifymock.AnythingOfType("*execution.PingRequest")).
988+
Return(nil, status.Error(code, code.String())).
989+
Once()
925990

926991
duration, err := callAndMeasurePingDuration(ctx)
927992
require.Error(t, err)
@@ -997,7 +1062,13 @@ func TestCircuitBreakerCollectionNode(t *testing.T) {
9971062
ctx := context.Background()
9981063

9991064
// Set up the handler mock to not respond within the requestTimeout.
1000-
cn.handler.On("Ping", testifymock.Anything, req).After(2*requestTimeout).Return(resp, nil)
1065+
cn.handler.
1066+
On("Ping",
1067+
testifymock.Anything,
1068+
testifymock.AnythingOfType("*access.PingRequest")).
1069+
After(2*requestTimeout).
1070+
Return(resp, nil).
1071+
Once()
10011072

10021073
// Call and measure the duration for the first invocation.
10031074
duration, err := callAndMeasurePingDuration(ctx)
@@ -1009,9 +1080,12 @@ func TestCircuitBreakerCollectionNode(t *testing.T) {
10091080
assert.Equal(t, gobreaker.ErrOpenState, err)
10101081
assert.Greater(t, requestTimeout, duration)
10111082

1012-
// Reset the mock Ping for the next invocation to return response without delay
1013-
cn.handler.On("Ping", testifymock.Anything, req).Unset()
1014-
cn.handler.On("Ping", testifymock.Anything, req).Return(resp, nil)
1083+
cn.handler.
1084+
On("Ping",
1085+
testifymock.Anything,
1086+
testifymock.AnythingOfType("*access.PingRequest")).
1087+
Return(resp, nil).
1088+
Once()
10151089

10161090
// Wait until the circuit breaker transitions to the "HalfOpen" state.
10171091
time.Sleep(circuitBreakerRestoreTimeout + (500 * time.Millisecond))
@@ -1026,8 +1100,12 @@ func TestCircuitBreakerCollectionNode(t *testing.T) {
10261100
t.Run(fmt.Sprintf("test error %s treated as a success for circuit breaker ", code.String()), func(t *testing.T) {
10271101
ctx := context.Background()
10281102

1029-
cn.handler.On("Ping", testifymock.Anything, req).Unset()
1030-
cn.handler.On("Ping", testifymock.Anything, req).Return(nil, status.Error(code, code.String()))
1103+
cn.handler.
1104+
On("Ping",
1105+
testifymock.Anything,
1106+
testifymock.AnythingOfType("*access.PingRequest")).
1107+
Return(nil, status.Error(code, code.String())).
1108+
Once()
10311109

10321110
duration, err := callAndMeasurePingDuration(ctx)
10331111
require.Error(t, err)

0 commit comments

Comments
 (0)