Skip to content

Commit

Permalink
Fixed integrarion test. Removed timeouts as it is invalid check there.
Browse files Browse the repository at this point in the history
  • Loading branch information
Guitarheroua committed Jul 21, 2023
1 parent 82f8cba commit 0d5a2c4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 25 deletions.
9 changes: 4 additions & 5 deletions engine/access/rpc/backend/connection_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ type CachedClient struct {

// createConnection creates new gRPC connections to remote node
func (cf *ConnectionFactoryImpl) createConnection(address string, timeout time.Duration, clientType clientType) (*grpc.ClientConn, error) {

if timeout == 0 {
timeout = DefaultClientTimeout
}
Expand All @@ -113,8 +112,11 @@ func (cf *ConnectionFactoryImpl) createConnection(address string, timeout time.D

var connInterceptors []grpc.UnaryClientInterceptor

connInterceptors = append(connInterceptors, createClientTimeoutInterceptor(timeout))

// The order in which interceptors are added to the `connInterceptors` slice is important since they will be called
// in the same order during gRPC requests.
// in the opposite order during gRPC requests. See documentation for more info:
// https://grpc.io/blog/grpc-web-interceptor/#binding-interceptors
if cf.CircuitBreakerConfig.Enabled {
// If the circuit breaker interceptor is enabled, it should always be called first before passing control to
// subsequent interceptors.
Expand All @@ -123,8 +125,6 @@ func (cf *ConnectionFactoryImpl) createConnection(address string, timeout time.D
connInterceptors = append(connInterceptors, cf.createClientInvalidationInterceptor(address, clientType))
}

connInterceptors = append(connInterceptors, createClientTimeoutInterceptor(timeout))

// ClientConn's default KeepAlive on connections is indefinite, assuming the timeout isn't reached
// The connections should be safe to be persisted and reused
// https://pkg.go.dev/google.golang.org/grpc#WithKeepaliveParams
Expand Down Expand Up @@ -386,7 +386,6 @@ func (cf *ConnectionFactoryImpl) createCircuitBreakerInterceptor() grpc.UnaryCli
// the "StateClosed" and handles invocations as usual.
_, err := circuitBreaker.Execute(func() (interface{}, error) {
err := invoker(ctx, method, req, reply, cc, opts...)

return nil, err
})
return err
Expand Down
27 changes: 7 additions & 20 deletions integration/tests/access/access_circuit_breaker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ type AccessCircuitBreakerSuite struct {
net *testnet.FlowNetwork
}

var requestTimeout = 3 * time.Second
var cbRestoreTimeout = 6 * time.Second
var requestTimeout = 1500 * time.Millisecond
var cbRestoreTimeout = 3 * time.Second

func (s *AccessCircuitBreakerSuite) TearDownTest() {
s.log.Info().Msg("================> Start TearDownTest")
Expand Down Expand Up @@ -153,36 +153,23 @@ func (s *AccessCircuitBreakerSuite) TestCircuitBreaker() {
SetGasLimit(9999)

// Sign the transaction
childCtx, cancel := context.WithTimeout(s.ctx, time.Second*10)
signedTx, err := accessClient.SignTransaction(createAccountTx)
require.NoError(s.T(), err)
cancel()

// 3. Disconnect the collection node from the network to activate the Circuit Breaker
err = collectionContainer.Disconnect()
require.NoError(s.T(), err, "failed to pause connection node")

// 4. Send a couple of transactions to test if the circuit breaker opens correctly
sendTransaction := func(ctx context.Context, tx *sdk.Transaction) (time.Duration, error) {
childCtx, cancel = context.WithTimeout(ctx, time.Second*10)
start := time.Now()
err := accessClient.SendTransaction(childCtx, tx)
duration := time.Since(start)
defer cancel()

return duration, err
}

// Try to send the transaction for the first time. It should wait at least the timeout time and return Unavailable error
duration, err := sendTransaction(s.ctx, signedTx)
err = accessClient.SendTransaction(s.ctx, signedTx)
assert.Equal(s.T(), codes.Unavailable, status.Code(err))
assert.GreaterOrEqual(s.T(), duration, requestTimeout)

// Try to send the transaction for the second time. It should wait less than a second because the circuit breaker
// is configured to break after the first failure
duration, err = sendTransaction(s.ctx, signedTx)
assert.Equal(s.T(), codes.Unavailable, status.Code(err))
assert.Greater(s.T(), time.Second, duration)
err = accessClient.SendTransaction(s.ctx, signedTx)
//Here we catch the codes.Unknown error, as this is the one that comes from the Circuit Breaker when the state is Open.
assert.Equal(s.T(), codes.Unknown, status.Code(err))

// Reconnect the collection node
err = collectionContainer.Connect()
Expand All @@ -192,6 +179,6 @@ func (s *AccessCircuitBreakerSuite) TestCircuitBreaker() {
time.Sleep(cbRestoreTimeout)

// Try to send the transaction for the third time. The transaction should be sent successfully
_, err = sendTransaction(s.ctx, signedTx)
err = accessClient.SendTransaction(s.ctx, signedTx)
require.NoError(s.T(), err, "transaction should be sent")
}

0 comments on commit 0d5a2c4

Please sign in to comment.