github.com/anycable/anycable-go@v1.5.1/node/subscription_state_test.go (about) 1 package node 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 ) 8 9 func TestSubscriptionStateChannels(t *testing.T) { 10 t.Run("with different channels", func(t *testing.T) { 11 subscriptions := NewSubscriptionState() 12 13 subscriptions.AddChannel("{\"channel\":\"SystemNotificationChannel\"}") 14 subscriptions.AddChannel("{\"channel\":\"DressageChannel\",\"id\":23376}") 15 16 expected := []string{ 17 "{\"channel\":\"SystemNotificationChannel\"}", 18 "{\"channel\":\"DressageChannel\",\"id\":23376}", 19 } 20 21 actual := subscriptions.Channels() 22 23 for _, key := range expected { 24 assert.Contains(t, actual, key) 25 } 26 }) 27 28 t.Run("with the same channel", func(t *testing.T) { 29 subscriptions := NewSubscriptionState() 30 31 subscriptions.AddChannel( 32 "{\"channel\":\"GraphqlChannel\",\"channelId\":\"165d8949069\"}", 33 ) 34 subscriptions.AddChannel( 35 "{\"channel\":\"GraphqlChannel\",\"channelId\":\"165d8941e62\"}", 36 ) 37 38 expected := []string{ 39 "{\"channel\":\"GraphqlChannel\",\"channelId\":\"165d8949069\"}", 40 "{\"channel\":\"GraphqlChannel\",\"channelId\":\"165d8941e62\"}", 41 } 42 43 actual := subscriptions.Channels() 44 45 for _, key := range expected { 46 assert.Contains(t, actual, key) 47 } 48 }) 49 } 50 51 func TestSubscriptionStreamsFor(t *testing.T) { 52 subscriptions := NewSubscriptionState() 53 54 subscriptions.AddChannel("chat_1") 55 subscriptions.AddChannel("presence_1") 56 57 subscriptions.AddChannelStream("chat_1", "a") 58 subscriptions.AddChannelStream("chat_1", "b") 59 subscriptions.AddChannelStream("presence_1", "z") 60 61 assert.Contains(t, subscriptions.StreamsFor("chat_1"), "a") 62 assert.Contains(t, subscriptions.StreamsFor("chat_1"), "b") 63 assert.Equal(t, []string{"z"}, subscriptions.StreamsFor("presence_1")) 64 65 subscriptions.RemoveChannelStreams("chat_1") 66 assert.Empty(t, subscriptions.StreamsFor("chat_1")) 67 assert.Equal(t, []string{"z"}, subscriptions.StreamsFor("presence_1")) 68 69 subscriptions.AddChannelStream("presence_1", "y") 70 subscriptions.RemoveChannelStream("presence_1", "z") 71 subscriptions.RemoveChannelStream("presence_1", "t") 72 assert.Equal(t, []string{"y"}, subscriptions.StreamsFor("presence_1")) 73 }