Skip to content

websocket, internal/testing: support multiconn WS helpers#2205

Open
thrasher- wants to merge 2 commits intothrasher-corp:masterfrom
thrasher-:jarvis/issue-2151-multiconn-test-helper
Open

websocket, internal/testing: support multiconn WS helpers#2205
thrasher- wants to merge 2 commits intothrasher-corp:masterfrom
thrasher-:jarvis/issue-2151-multiconn-test-helper

Conversation

@thrasher-
Copy link
Collaborator

@thrasher- thrasher- commented Mar 10, 2026

PR Description

Fix internal testing websocket helpers so they work with multi-connection managers.

This addresses issue #2151 by updating the shared websocket test helpers instead of relying on per-exchange workarounds.

What changed

  • added SetSubscriptionsNotRequired() support for multi-connection managers
  • added SetAllConnectionURLs() so test helpers can redirect all managed websocket connections to a single mock server
  • updated internal/testing/exchange.SetupWs to use the multi-connection-safe helper path
  • updated internal/testing/exchange.MockWsInstance to use the multi-connection-safe helper path
  • added direct unit coverage for the new manager helper methods
  • added helper-level regression coverage for:
    • MockWsInstance with a real multi-connection exchange
    • SetupWs with a synthetic multi-connection setup
  • added a focused Poloniex proof-of-concept test showing the shared helper works on the exchange that originally triggered the issue

Why

SetupWs and MockWsInstance previously assumed single-connection websocket behaviour. That broke down once exchanges used UseMultiConnectionManagement, because empty generated subscriptions prevented connections from being established and test helpers had no clean way to mark subscriptions as not required.

This change keeps the fix in shared test infrastructure so exchanges do not need ad-hoc helper logic.

Type of change

  • Bug fix
  • Tests

How has this been tested

  • go test ./exchange/websocket -count=1
  • go test ./internal/testing/exchange -count=1 -v
  • go test ./exchanges/bybit -count=1
  • go test ./exchanges/poloniex -count=1
  • golangci-lint run ./exchange/websocket ./internal/testing/exchange ./exchanges/poloniex
  • codespell
  • gofumpt

Closes #2151

Copilot AI review requested due to automatic review settings March 10, 2026 03:50
@thrasher- thrasher- added jarvis Automated updates by Jarvis test(s) fix This is to denote the PR is fixing a build test issue labels Mar 10, 2026
@thrasher- thrasher- self-assigned this Mar 10, 2026
@thrasher- thrasher- added the review me This pull request is ready for review label Mar 10, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes shared websocket test helpers (SetupWs and MockWsInstance) to work with exchanges that use multi-connection websocket management. Previously, these helpers assumed single-connection behavior, which broke for exchanges using UseMultiConnectionManagement.

Changes:

  • Added SetSubscriptionsNotRequired() and SetAllConnectionURLs() methods to the websocket Manager to support both single and multi-connection modes.
  • Updated MockWsInstance and SetupWs test helpers to use the new multi-connection-safe methods.
  • Added unit tests for the new manager methods and integration-level regression tests including a Poloniex proof-of-concept.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
exchange/websocket/manager.go Added SetSubscriptionsNotRequired() and SetAllConnectionURLs() methods to Manager
internal/testing/exchange/exchange.go Updated MockWsInstance and SetupWs to use the new multi-connection-safe helpers
exchange/websocket/manager_test.go Added unit tests for SetSubscriptionsNotRequired and SetAllConnectionURLs
internal/testing/exchange/exchange_test.go Added regression tests for multi-connection support in MockWsInstance and SetupWs
exchanges/poloniex/poloniex_websocket_helper_test.go Added Poloniex-specific proof-of-concept test for multi-connection SetupWs

Comment on lines +790 to +795
for _, ws := range m.connectionManager {
if ws.setup == nil {
ws.setup = &ConnectionSetup{}
}
ws.setup.SubscriptionsNotRequired = true
}
Copy link

Copilot AI Mar 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating a new ConnectionSetup{} for a nil ws.setup silently masks a programming error — a websocket entry in connectionManager should always have been initialized via SetupNewConnection. This guard could hide bugs where the connection manager is in an inconsistent state. Consider returning an error or logging a warning instead of silently initializing an empty setup.

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jarvis: I’m keeping this guard as-is. In normal flow multi-connection entries are created via SetupNewConnection with a non-nil setup, so this is a cheap defensive safeguard rather than a primary code path, and I’d rather keep the helper resilient here than widen it into a broader error-return change.

Comment on lines +824 to +832
for _, ws := range m.connectionManager {
if ws.setup == nil {
ws.setup = &ConnectionSetup{}
}
ws.setup.URL = u
for _, conn := range ws.connections {
conn.SetURL(u)
}
}
Copy link

Copilot AI Mar 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same concern as in SetSubscriptionsNotRequired: silently initializing a nil ws.setup with &ConnectionSetup{} could mask an invalid state. Both methods should handle this consistently — consider extracting the nil-guard into a shared helper or removing it in favor of treating a nil setup as an error.

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jarvis: Same reasoning here — I’m keeping the guard. The nil setup case should not occur in normal multi-connection setup flow, and the defensive check keeps the two helper methods consistent without changing behaviour for valid managers.

Comment on lines 125 to 126
// Exchanges which don't support subscription conf; Can be removed when all exchanges support sub conf
b.Websocket.GenerateSubs = func() (subscription.List, error) { return subscription.List{}, nil }
Copy link

Copilot AI Mar 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In MockWsInstance, GenerateSubs is still explicitly overridden on line 126, even though SetSubscriptionsNotRequired() on line 127 already sets GenerateSubs to return an empty list for single-connection managers. This makes the two lines redundant for the single-connection path. Consider removing the explicit GenerateSubs override or adding a comment explaining why both are needed (e.g., if SetSubscriptionsNotRequired doesn't override GenerateSubs for multi-connection managers and the old override is kept as a safety net for the single-connection case).

Suggested change
// Exchanges which don't support subscription conf; Can be removed when all exchanges support sub conf
b.Websocket.GenerateSubs = func() (subscription.List, error) { return subscription.List{}, nil }
// Ensure no default subscriptions are required for tests, including exchanges without subscription configuration support.

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jarvis: Done in thrasher-@669594f — removed the redundant GenerateSubs override so MockWsInstance now relies on SetSubscriptionsNotRequired() as the single source of that behaviour.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request effectively addresses the issue of supporting multi-connection websocket helpers for testing. The introduction of SetSubscriptionsNotRequired and SetAllConnectionURLs in the websocket manager provides a clean way to configure test environments, and the updates to MockWsInstance and SetupWs correctly leverage these new helpers. The testing strategy is thorough, with new unit tests, regression tests for multi-connection exchanges, and a proof-of-concept test for Poloniex. I have one minor suggestion to improve code clarity in a test helper, as detailed in the specific comment.

Comment on lines +126 to +127
b.Websocket.GenerateSubs = func() (subscription.List, error) { return subscription.List{}, nil }
b.Websocket.SetSubscriptionsNotRequired()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The call to b.Websocket.SetSubscriptionsNotRequired() on line 127 makes the assignment on line 126 redundant. The SetSubscriptionsNotRequired function already handles setting GenerateSubs for single-connection managers. To improve maintainability and avoid confusion, you can remove this redundant line. The preceding comment on line 125 also appears to be obsolete with this change.

Suggested change
b.Websocket.GenerateSubs = func() (subscription.List, error) { return subscription.List{}, nil }
b.Websocket.SetSubscriptionsNotRequired()
b.Websocket.SetSubscriptionsNotRequired()

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Jarvis: Done in thrasher-@669594f — removed the redundant GenerateSubs assignment and the now-obsolete extra comment so this helper only uses SetSubscriptionsNotRequired() for that behaviour.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

jarvis Automated updates by Jarvis review me This pull request is ready for review test(s) fix This is to denote the PR is fixing a build test issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

websocket/manager: internal/testing/exchange support for multi-connection

2 participants