mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-22 15:10:13 +00:00
subscriptions: Add templating support and integrate with Binance (#1568)
* Subscriptions: Add List.AssetPairs * Subscriptions: Add Template and QualifiedChannel These fields separate the concept of what the channel is from the qualified resource name * Subscriptions: Add List.SetStates() * Subscriptions: Add List.QualifiedChannels * Subscriptions: Rename testsubs.EqualLists * Binance: Switch to ExpandTemplates * Binance: Update ConfigTest format * Subscriptions: Test Coverage improvements * Subscriptions: Reenterant List.ExpandTemplates * Subscriptions: Move templates from subscriptions to exchanges * Binance: Inline subscription template and improvements
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package subscriptionstest
|
||||
|
||||
import (
|
||||
"maps"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -9,10 +10,14 @@ import (
|
||||
"github.com/thrasher-corp/gocryptotrader/exchanges/subscription"
|
||||
)
|
||||
|
||||
// Equal is a utility function to compare subscription lists and show a pretty failure message
|
||||
// EqualLists is a utility function to compare subscription lists and show a pretty failure message
|
||||
// It overcomes the verbose depth of assert.ElementsMatch spewConfig
|
||||
func Equal(tb testing.TB, a, b subscription.List) {
|
||||
// Duplicate of exchange/subscription/subscription:equalList
|
||||
func EqualLists(tb testing.TB, a, b subscription.List) bool {
|
||||
tb.Helper()
|
||||
for _, sub := range append(a, b...) {
|
||||
sub.Key = &StrictKey{&subscription.ExactKey{Subscription: sub}}
|
||||
}
|
||||
s, err := subscription.NewStoreFromList(a)
|
||||
require.NoError(tb, err, "NewStoreFromList must not error")
|
||||
added, missing := s.Diff(b)
|
||||
@@ -25,5 +30,35 @@ func Equal(tb testing.TB, a, b subscription.List) {
|
||||
fail = fail + "\n - " + strings.Join(missing.Strings(), "\n - ")
|
||||
}
|
||||
assert.Fail(tb, fail, "Subscriptions should be equal")
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// StrictKey is key type for subscriptions where all the pairs, QualifiedChannel and Params in a Subscription must match exactly
|
||||
type StrictKey struct {
|
||||
*subscription.ExactKey
|
||||
}
|
||||
|
||||
var _ subscription.MatchableKey = StrictKey{} // Enforce StrictKey must implement MatchableKey
|
||||
|
||||
// Match implements MatchableKey
|
||||
// Returns true if the key fields exactly matches the subscription, including all Pairs, QualifiedChannel and Params
|
||||
func (k StrictKey) Match(eachKey subscription.MatchableKey) bool {
|
||||
if !k.ExactKey.Match(eachKey) {
|
||||
return false
|
||||
}
|
||||
eachSub := eachKey.GetSubscription()
|
||||
return eachSub.QualifiedChannel == k.QualifiedChannel &&
|
||||
maps.Equal(eachSub.Params, k.Params)
|
||||
}
|
||||
|
||||
// String implements Stringer; returns the Asset, Channel and Pairs
|
||||
// Does not provide concurrency protection on the subscription it points to
|
||||
func (k StrictKey) String() string {
|
||||
s := k.Subscription
|
||||
if s == nil {
|
||||
return "Uninitialised StrictKey"
|
||||
}
|
||||
return s.QualifiedChannel + " " + subscription.ExactKey{Subscription: s}.String()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user