From 4f1696fa0bfa23072925b6f333fc867c3bb6649d Mon Sep 17 00:00:00 2001 From: Gareth Kirwan Date: Fri, 19 Jul 2024 01:36:59 +0100 Subject: [PATCH] Subscriptions: Fix subscription.Clone Params and Pairs (#1582) * Subscriptions: Fix subscription.Clone Params and Pairs * fixup! Subscriptions: Fix subscription.Clone Params and Pairs --- exchanges/subscription/subscription.go | 6 ++---- exchanges/subscription/subscription_test.go | 9 ++++++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/exchanges/subscription/subscription.go b/exchanges/subscription/subscription.go index 695eb420..51646636 100644 --- a/exchanges/subscription/subscription.go +++ b/exchanges/subscription/subscription.go @@ -132,16 +132,14 @@ func (s *Subscription) Clone() *Subscription { Enabled: s.Enabled, Channel: s.Channel, Asset: s.Asset, - Params: s.Params, + Params: maps.Clone(s.Params), Interval: s.Interval, Levels: s.Levels, Authenticated: s.Authenticated, state: s.state, - Pairs: s.Pairs, + Pairs: slices.Clone(s.Pairs), QualifiedChannel: s.QualifiedChannel, } - s.Pairs = slices.Clone(s.Pairs) - s.Params = maps.Clone(s.Params) s.m.RUnlock() return c } diff --git a/exchanges/subscription/subscription_test.go b/exchanges/subscription/subscription_test.go index a724b2d4..bd9a9a27 100644 --- a/exchanges/subscription/subscription_test.go +++ b/exchanges/subscription/subscription_test.go @@ -93,21 +93,24 @@ func TestSubscriptionMarshaling(t *testing.T) { // TestClone exercises Clone func TestClone(t *testing.T) { t.Parallel() + params := map[string]any{"a": 42} a := &Subscription{ Channel: TickerChannel, Interval: kline.OneHour, Pairs: currency.Pairs{btcusdtPair}, - Params: map[string]any{"a": 42}, + Params: params, } a.EnsureKeyed() b := a.Clone() assert.IsType(t, new(Subscription), b, "Clone must return a Subscription pointer") assert.NotSame(t, a, b, "Clone should return a new Subscription") assert.Nil(t, b.Key, "Clone should have a nil key") - b.Pairs[0] = ethusdcPair - assert.Equal(t, btcusdtPair, a.Pairs[0], "Pairs should be (relatively) deep copied") + b.Pairs[0].Delimiter = "🐳" + assert.Empty(t, a.Pairs[0].Delimiter, "Pairs should be (relatively) deep copied") b.Params["a"] = 12 assert.Equal(t, 42, a.Params["a"], "Params should be (relatively) deep copied") + assert.NotEqual(t, params, b.Params, "Params should be cloned") + assert.Equal(t, params, a.Params, "Original Params should be left alone") a.m.Lock() assert.True(t, b.m.TryLock(), "Clone must use a different Mutex") }