-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
[CAPL-264] Ensure a default timeout is provided for all outgoing requests #15644
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
core/capabilities/webapi/outgoing_connector_handler_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package webapi | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/mock" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/smartcontractkit/chainlink-common/pkg/utils/tests" | ||
"github.com/smartcontractkit/chainlink/v2/core/logger" | ||
|
||
"github.com/smartcontractkit/chainlink/v2/core/services/gateway/api" | ||
gcmocks "github.com/smartcontractkit/chainlink/v2/core/services/gateway/connector/mocks" | ||
ghcapabilities "github.com/smartcontractkit/chainlink/v2/core/services/gateway/handlers/capabilities" | ||
"github.com/smartcontractkit/chainlink/v2/core/services/gateway/handlers/common" | ||
) | ||
|
||
func TestHandleSingleNodeRequest(t *testing.T) { | ||
t.Run("OK-timeout_is_not_specify_default_timeout_is_expected", func(t *testing.T) { | ||
ctx := tests.Context(t) | ||
log := logger.TestLogger(t) | ||
connector := gcmocks.NewGatewayConnector(t) | ||
var defaultConfig = ServiceConfig{ | ||
RateLimiter: common.RateLimiterConfig{ | ||
GlobalRPS: 100.0, | ||
GlobalBurst: 100, | ||
PerSenderRPS: 100.0, | ||
PerSenderBurst: 100, | ||
}, | ||
} | ||
connectorHandler, err := NewOutgoingConnectorHandler(connector, defaultConfig, ghcapabilities.MethodComputeAction, log) | ||
require.NoError(t, err) | ||
|
||
msgID := "msgID" | ||
testURL := "http://localhost:8080" | ||
connector.EXPECT().DonID().Return("donID") | ||
connector.EXPECT().GatewayIDs().Return([]string{"gateway1"}) | ||
|
||
// build the expected body with the default timeout | ||
req := ghcapabilities.Request{ | ||
URL: testURL, | ||
TimeoutMs: defaultFetchTimeoutMs, | ||
} | ||
payload, err := json.Marshal(req) | ||
require.NoError(t, err) | ||
|
||
expectedBody := &api.MessageBody{ | ||
MessageId: msgID, | ||
DonId: connector.DonID(), | ||
Method: ghcapabilities.MethodComputeAction, | ||
Payload: payload, | ||
} | ||
|
||
// expect the request body to contain the default timeout | ||
connector.EXPECT().SignAndSendToGateway(mock.Anything, "gateway1", expectedBody).Run(func(ctx context.Context, gatewayID string, msg *api.MessageBody) { | ||
connectorHandler.HandleGatewayMessage(ctx, "gateway1", gatewayResponse(t, msgID)) | ||
}).Return(nil).Times(1) | ||
|
||
_, err = connectorHandler.HandleSingleNodeRequest(ctx, msgID, ghcapabilities.Request{ | ||
URL: testURL, | ||
}) | ||
require.NoError(t, err) | ||
}) | ||
|
||
t.Run("OK-timeout_is_specified", func(t *testing.T) { | ||
ctx := tests.Context(t) | ||
log := logger.TestLogger(t) | ||
connector := gcmocks.NewGatewayConnector(t) | ||
var defaultConfig = ServiceConfig{ | ||
RateLimiter: common.RateLimiterConfig{ | ||
GlobalRPS: 100.0, | ||
GlobalBurst: 100, | ||
PerSenderRPS: 100.0, | ||
PerSenderBurst: 100, | ||
}, | ||
} | ||
connectorHandler, err := NewOutgoingConnectorHandler(connector, defaultConfig, ghcapabilities.MethodComputeAction, log) | ||
require.NoError(t, err) | ||
|
||
msgID := "msgID" | ||
testURL := "http://localhost:8080" | ||
connector.EXPECT().DonID().Return("donID") | ||
connector.EXPECT().GatewayIDs().Return([]string{"gateway1"}) | ||
|
||
// build the expected body with the defined timeout | ||
req := ghcapabilities.Request{ | ||
URL: testURL, | ||
TimeoutMs: 40000, | ||
} | ||
payload, err := json.Marshal(req) | ||
require.NoError(t, err) | ||
|
||
expectedBody := &api.MessageBody{ | ||
MessageId: msgID, | ||
DonId: connector.DonID(), | ||
Method: ghcapabilities.MethodComputeAction, | ||
Payload: payload, | ||
} | ||
|
||
// expect the request body to contain the defined timeout | ||
connector.EXPECT().SignAndSendToGateway(mock.Anything, "gateway1", expectedBody).Run(func(ctx context.Context, gatewayID string, msg *api.MessageBody) { | ||
connectorHandler.HandleGatewayMessage(ctx, "gateway1", gatewayResponse(t, msgID)) | ||
}).Return(nil).Times(1) | ||
|
||
_, err = connectorHandler.HandleSingleNodeRequest(ctx, msgID, ghcapabilities.Request{ | ||
URL: testURL, | ||
TimeoutMs: 40000, | ||
}) | ||
require.NoError(t, err) | ||
}) | ||
} | ||
|
||
func gatewayResponse(t *testing.T, msgID string) *api.Message { | ||
headers := map[string]string{"Content-Type": "application/json"} | ||
body := []byte("response body") | ||
responsePayload, err := json.Marshal(ghcapabilities.Response{ | ||
StatusCode: 200, | ||
Headers: headers, | ||
Body: body, | ||
ExecutionError: false, | ||
}) | ||
require.NoError(t, err) | ||
return &api.Message{ | ||
Body: api.MessageBody{ | ||
MessageId: msgID, | ||
Method: ghcapabilities.MethodWebAPITarget, | ||
Payload: responsePayload, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@agparadiso I know I didn't ask you to do this originally, but could we also create a subcontext with this timeout plus some margin added to it?
Atm we don't get a response from the gateway if we time out which causes some small bugs; if we add this, the context would fire on line 101 and we'd at least get a response.
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.
added 👍🏼