-
Notifications
You must be signed in to change notification settings - Fork 905
websocket, internal/testing: support multiconn WS helpers #2205
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
Merged
thrasher-
merged 4 commits into
thrasher-corp:master
from
thrasher-:jarvis/issue-2151-multiconn-test-helper
Mar 11, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
cd4f8f4
websocket, internal/testing: support multiconn WS helpers
thrasher- 669594f
internal/testing: remove redundant subs override
thrasher- ba823c2
websocket: harden multiconn test helpers
thrasher- 9332623
websocket: move test hooks into helper file
thrasher- 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 hidden or 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 hidden or 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,82 @@ | ||
| package websocket | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/thrasher-corp/gocryptotrader/common" | ||
| "github.com/thrasher-corp/gocryptotrader/exchanges/subscription" | ||
| "github.com/thrasher-corp/gocryptotrader/log" | ||
| ) | ||
|
|
||
| // SetSubscriptionsNotRequired configures the manager to connect without | ||
| // generating default subscriptions. | ||
| // | ||
| // This exported helper exists for cross-package test harnesses only. It is a | ||
| // pre-connect test-mode mutation used by websocket unit-test helpers that need | ||
| // request-response connectivity without normal stream bootstraps. Tests that | ||
| // later need the manager's default subscription generation should use a fresh | ||
| // exchange/manager instance. | ||
| func (m *Manager) SetSubscriptionsNotRequired() { | ||
| m.m.Lock() | ||
| defer m.m.Unlock() | ||
|
|
||
| if !m.useMultiConnectionManagement { | ||
| m.GenerateSubs = func() (subscription.List, error) { return subscription.List{}, nil } | ||
| return | ||
| } | ||
|
|
||
| for _, ws := range m.connectionManager { | ||
| if ws.setup == nil { | ||
| log.Warnf(log.WebsocketMgr, "%s websocket: missing connection setup while disabling required subscriptions; creating empty setup", m.exchangeName) | ||
| ws.setup = &ConnectionSetup{} | ||
| } | ||
| ws.setup.SubscriptionsNotRequired = true | ||
| } | ||
| } | ||
|
|
||
| // SetAllConnectionURLs configures every managed websocket connection to use the | ||
| // same URL. | ||
| // | ||
| // This exported helper exists for cross-package test harnesses only. It is a | ||
| // pre-connect test-mode mutation used by websocket unit-test helpers to | ||
| // redirect all websocket traffic through a single mock server before | ||
| // connecting. Calling this after Connect has started returns an error. | ||
| func (m *Manager) SetAllConnectionURLs(u string) error { | ||
| if err := common.NilGuard(m); err != nil { | ||
| return err | ||
| } | ||
| if err := checkWebsocketURL(u); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| m.m.Lock() | ||
| defer m.m.Unlock() | ||
|
|
||
| if m.IsConnecting() { | ||
| return fmt.Errorf("%v %w: SetAllConnectionURLs must be called before Connect", m.exchangeName, errAlreadyReconnecting) | ||
| } | ||
| if m.IsConnected() { | ||
| return fmt.Errorf("%v %w: SetAllConnectionURLs must be called before Connect", m.exchangeName, errAlreadyConnected) | ||
| } | ||
|
|
||
| if !m.useMultiConnectionManagement { | ||
| m.runningURL = u | ||
| m.runningURLAuth = u | ||
| if m.Conn != nil { | ||
| m.Conn.SetURL(u) | ||
| } | ||
| if m.AuthConn != nil { | ||
| m.AuthConn.SetURL(u) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| for _, ws := range m.connectionManager { | ||
| if ws.setup == nil { | ||
| log.Warnf(log.WebsocketMgr, "%s websocket: missing connection setup while updating connection URLs; creating empty setup", m.exchangeName) | ||
| ws.setup = &ConnectionSetup{} | ||
| } | ||
| ws.setup.URL = u | ||
| } | ||
| return nil | ||
| } |
This file contains hidden or 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,44 @@ | ||
| package poloniex | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| testexch "github.com/thrasher-corp/gocryptotrader/internal/testing/exchange" | ||
| mockws "github.com/thrasher-corp/gocryptotrader/internal/testing/websocket" | ||
| ) | ||
|
|
||
| func TestSetupWsSupportsMultiConnectionManagement(t *testing.T) { | ||
| server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| mockws.WsMockUpgrader(t, w, r, mockws.EchoHandler) | ||
| })) | ||
| t.Cleanup(server.Close) | ||
|
|
||
| e := new(Exchange) | ||
| require.NoError(t, testexch.Setup(e), "Test instance Setup must not error") | ||
|
|
||
| wsURL := "ws" + strings.TrimPrefix(server.URL, "http") | ||
| err := e.Websocket.SetAllConnectionURLs(wsURL) | ||
| require.NoError(t, err, "SetAllConnectionURLs must not error for Poloniex") | ||
|
|
||
| testexch.SetupWs(t, e) | ||
| t.Cleanup(func() { | ||
| if e.Websocket.IsConnected() { | ||
| assert.NoError(t, e.Websocket.Shutdown(), "Websocket shutdown should not error") | ||
| } | ||
| }) | ||
|
|
||
| assert.Empty(t, e.Features.Subscriptions, "Features.Subscriptions should be cleared by SetupWs") | ||
| assert.True(t, e.Websocket.IsConnected(), "Websocket manager should be connected after SetupWs") | ||
|
|
||
| for _, messageFilter := range []string{connSpotPublic, connSpotPrivate, connFuturesPublic, connFuturesPrivate} { | ||
| conn, connErr := e.Websocket.GetConnection(messageFilter) | ||
| require.NoErrorf(t, connErr, "GetConnection must not error for message filter %s", messageFilter) | ||
| assert.Equalf(t, wsURL, conn.GetURL(), "Connection URL should be redirected for message filter %s", messageFilter) | ||
| assert.Emptyf(t, conn.Subscriptions().List(), "Connection subscriptions should remain empty for message filter %s", messageFilter) | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.