Skip to content

Commit

Permalink
Subscriptions: Fix all asset subs erroring on no enabled assets
Browse files Browse the repository at this point in the history
Only subs with Empty asset should do anything when assets are empty.
If asset is set to all and no assets are enabled, we should return
nothing
  • Loading branch information
gbjk committed Nov 21, 2024
1 parent e58f029 commit a291786
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
6 changes: 6 additions & 0 deletions exchanges/bitmex/bitmex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,12 @@ func TestGenerateSubscriptions(t *testing.T) {
subs, err := b.generateSubscriptions()
require.NoError(t, err, "generateSubscriptions must not error")
testsubs.EqualLists(t, exp, subs)

for _, a := range b.GetAssetTypes(true) {
b.CurrencyPairs.SetAssetEnabled(a, false)

Check failure on line 1132 in exchanges/bitmex/bitmex_test.go

View workflow job for this annotation

GitHub Actions / lint

Error return value of `b.CurrencyPairs.SetAssetEnabled` is not checked (errcheck)
}
_, err = b.generateSubscriptions()
require.NoError(t, err, "generateSubscriptions must not error when no assets are enabled")
}

func TestSubscribe(t *testing.T) {
Expand Down
14 changes: 7 additions & 7 deletions exchanges/bitmex/bitmex_websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -650,16 +650,16 @@ func channelName(s *subscription.Subscription, a asset.Item) string {

const subTplText = `
{{- if $.S.Asset }}
{{ range $asset, $pairs := $.AssetPairs }}
{{- range $asset, $pairs := $.AssetPairs }}
{{- with $name := channelName $.S $asset }}
{{- range $i, $p := $pairs -}}
{{- $name -}} : {{- $p -}}
{{ $.PairSeparator }}
{{- range $i, $p := $pairs }}
{{- $name -}} : {{- $p }}
{{- $.PairSeparator }}
{{- end }}
{{- end }}
{{ $.AssetSeparator }}
{{- $.AssetSeparator }}
{{- end }}
{{- else -}}
{{ channelName $.S $.S.Asset }}
{{- else }}
{{- channelName $.S $.S.Asset }}
{{- end }}
`
9 changes: 6 additions & 3 deletions exchanges/subscription/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,19 @@ func expandTemplate(e iExchange, s *Subscription, ap assetPairs, assets asset.It

switch s.Asset {
case asset.All:
if len(ap) == 0 {
return List{}, nil // No assets enabled; only asset.Empty subs may continue
}
subCtx.AssetPairs = ap
default:
if s.Asset != asset.Empty && len(ap[s.Asset]) == 0 {
return List{}, nil // No pairs enabled for this sub asset
}
// This deliberately includes asset.Empty to harmonise handling
subCtx.AssetPairs = assetPairs{
s.Asset: ap[s.Asset],
}
assets = asset.Items{s.Asset}
if s.Asset != asset.Empty && len(ap[s.Asset]) == 0 {
return List{}, nil // Nothing is enabled for this sub asset
}
}

buf := &bytes.Buffer{}
Expand Down

0 comments on commit a291786

Please sign in to comment.