-
Notifications
You must be signed in to change notification settings - Fork 822
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add WS Batch support with ctx mocking
- Loading branch information
Ryan O'Hara-Reid
committed
Jan 17, 2025
1 parent
9567ef0
commit c5b9558
Showing
21 changed files
with
291 additions
and
15 deletions.
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
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package request | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"io" | ||
"net/http" | ||
) | ||
|
||
var mockResponseFlag = struct{ name string }{name: "mockResponse"} | ||
|
||
// IsMockResponse returns true if the request has a mock response set | ||
func IsMockResponse(ctx context.Context) bool { | ||
return ctx.Value(mockResponseFlag) != nil | ||
} | ||
|
||
// WithMockResponse sets the mock response for a request. This is used for testing purposes. | ||
// REST response is single. Websocket response can be multiple. This allows expected responses to be set for a request if required. | ||
func WithMockResponse(ctx context.Context, mockResponse ...[]byte) context.Context { | ||
return context.WithValue(ctx, mockResponseFlag, mockResponse) | ||
} | ||
|
||
// GetMockResponse returns the mock response for a request | ||
func GetMockResponse(ctx context.Context) [][]byte { | ||
mockResponse, _ := ctx.Value(mockResponseFlag).([][]byte) | ||
return mockResponse | ||
} | ||
|
||
func getRESTResponseFromMock(ctx context.Context) *http.Response { | ||
mockResp := GetMockResponse(ctx) | ||
if len(mockResp) != 1 { | ||
panic("mock REST response invalid, requires exactly one response") | ||
} | ||
return &http.Response{ | ||
StatusCode: http.StatusOK, | ||
Body: io.NopCloser(io.Reader(io.LimitReader(bytes.NewBuffer(mockResp[0]), drainBodyLimit))), | ||
} | ||
} |
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,27 @@ | ||
package request | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestMockResponse(t *testing.T) { | ||
t.Parallel() | ||
|
||
ctx := context.Background() | ||
require.False(t, IsMockResponse(ctx)) | ||
require.Nil(t, GetMockResponse(ctx)) | ||
require.Panics(t, func() { getRESTResponseFromMock(ctx) }) | ||
mockCtx := WithMockResponse(ctx, []byte("test")) | ||
require.True(t, IsMockResponse(mockCtx)) | ||
require.NotNil(t, GetMockResponse(mockCtx)) | ||
got := getRESTResponseFromMock(mockCtx) | ||
require.NotNil(t, got) | ||
require.Equal(t, 200, got.StatusCode) | ||
hotBod, err := io.ReadAll(got.Body) | ||
require.NoError(t, err) | ||
require.Equal(t, []byte("test"), hotBod) | ||
} |
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
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,33 @@ | ||
package stream | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/thrasher-corp/gocryptotrader/exchanges/request" | ||
) | ||
|
||
// MockWebsocketConnection is a mock websocket connection | ||
type MockWebsocketConnection struct { | ||
WebsocketConnection | ||
} | ||
|
||
// SendMessageReturnResponse returns a mock response from context | ||
func (m *MockWebsocketConnection) SendMessageReturnResponse(ctx context.Context, epl request.EndpointLimit, signature, payload any) ([]byte, error) { | ||
resps, _ := m.SendMessageReturnResponses(ctx, epl, signature, payload, 1) | ||
return resps[0], nil | ||
} | ||
|
||
// SendMessageReturnResponses returns a mock response from context | ||
func (m *MockWebsocketConnection) SendMessageReturnResponses(ctx context.Context, epl request.EndpointLimit, signature, payload any, expected int) ([][]byte, error) { | ||
return m.SendMessageReturnResponsesWithInspector(ctx, epl, signature, payload, expected, nil) | ||
} | ||
|
||
// SendMessageReturnResponsesWithInspector returns a mock response from context | ||
func (*MockWebsocketConnection) SendMessageReturnResponsesWithInspector(ctx context.Context, _ request.EndpointLimit, _, _ any, _ int, _ Inspector) ([][]byte, error) { | ||
return request.GetMockResponse(ctx), nil | ||
} | ||
|
||
// newMockConnection returns a new mock websocket connection, used so that the websocket does not need to be connected | ||
func newMockWebsocketConnection() Connection { | ||
return &MockWebsocketConnection{} | ||
} |
Oops, something went wrong.