-
Notifications
You must be signed in to change notification settings - Fork 10
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
Petera/handle resourceexhaused ingestion #686
Changes from 5 commits
79a0de7
138e285
70a8358
157ae34
737c88f
c27ad32
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package bootstrap | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
) | ||
|
||
func TestRetryInterceptor(t *testing.T) { | ||
expecterErr := status.Error(codes.ResourceExhausted, "resource exhausted") | ||
interceptor := retryInterceptor(100*time.Millisecond, 10*time.Millisecond) | ||
|
||
testCases := []struct { | ||
name string | ||
invoker func(callCount int) error | ||
maxRequestTime time.Duration | ||
callCount int // expect exact count | ||
minCallCount int // min, for when using a timeout | ||
expectedErr error | ||
}{ | ||
{ | ||
name: "no error", | ||
invoker: func(callCount int) error { | ||
return nil | ||
}, | ||
maxRequestTime: 10 * time.Millisecond, | ||
callCount: 1, | ||
expectedErr: nil, | ||
}, | ||
{ | ||
name: "succeeds on 3rd attempt", | ||
invoker: func(callCount int) error { | ||
if callCount >= 3 { | ||
return nil | ||
} | ||
return expecterErr | ||
}, | ||
maxRequestTime: 40 * time.Millisecond, | ||
callCount: 3, | ||
expectedErr: nil, | ||
}, | ||
{ | ||
name: "fails after timeout", | ||
invoker: func(callCount int) error { | ||
return expecterErr | ||
}, | ||
maxRequestTime: 150 * time.Millisecond, // add a buffer for test slowness | ||
minCallCount: 10, | ||
expectedErr: expecterErr, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
tc := tc | ||
t.Run(tc.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
callCount := 0 | ||
invoker := func(context.Context, string, any, any, *grpc.ClientConn, ...grpc.CallOption) error { | ||
callCount++ | ||
return tc.invoker(callCount) | ||
} | ||
|
||
start := time.Now() | ||
err := interceptor( | ||
context.Background(), "", nil, nil, nil, | ||
invoker, | ||
) | ||
if tc.expectedErr != nil { | ||
assert.ErrorIs(t, err, tc.expectedErr) | ||
} else { | ||
assert.NoError(t, err) | ||
} | ||
|
||
if tc.minCallCount > 0 { | ||
assert.GreaterOrEqual(t, callCount, tc.minCallCount) | ||
} else { | ||
assert.Equal(t, callCount, tc.callCount) | ||
} | ||
assert.LessOrEqual(t, time.Since(start), tc.maxRequestTime) | ||
}) | ||
} | ||
} | ||
Comment on lines
+58
to
+88
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix potential race condition in parallel tests. Since tests are running in parallel ( t.Run(tc.name, func(t *testing.T) {
t.Parallel()
- callCount := 0
+ var callCount atomic.Int32
invoker := func(context.Context, string, any, any, *grpc.ClientConn, ...grpc.CallOption) error {
- callCount++
+ currentCount := callCount.Add(1)
- return tc.invoker(callCount)
+ return tc.invoker(int(currentCount))
} Don't forget to add
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this really needed? It looks like the the variable is scoped to a single test run There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @peterargue, you’re correct that each subtest has its own |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not including this was causing lint errors which is strange since the loop var update should be included since go1.22.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is due to the
golangci-lint
version that we use: https://github.com/onflow/flow-evm-gateway/blob/main/.github/workflows/ci.yml#L41. I tried with the latest ([email protected]
), and it doesn't produce this lint error. I will open up a new PR to update it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Opened #718 .