github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/pkg/mux/client/client_test.go (about) 1 package client_test 2 3 import ( 4 "sort" 5 "testing" 6 7 "github.com/bitfinexcom/bitfinex-api-go/pkg/models/event" 8 "github.com/bitfinexcom/bitfinex-api-go/pkg/mux/client" 9 "github.com/stretchr/testify/assert" 10 ) 11 12 func TestSubsLimitReached(t *testing.T) { 13 cases := map[string]struct { 14 limit int 15 expected bool 16 subs []event.Subscribe 17 }{ 18 "limit unreached": { 19 limit: 20, 20 expected: false, 21 subs: []event.Subscribe{{Event: "foo"}}, 22 }, 23 "limit reached": { 24 limit: 2, 25 expected: true, 26 subs: []event.Subscribe{ 27 {Event: "foo"}, 28 {Event: "bar"}, 29 }, 30 }, 31 "limit unreached #2": { 32 limit: 0, 33 expected: false, 34 subs: []event.Subscribe{ 35 {Event: "foo"}, 36 {Event: "bar"}, 37 }, 38 }, 39 } 40 41 for k, v := range cases { 42 t.Run(k, func(t *testing.T) { 43 c := client.New().WithSubsLimit(v.limit) 44 for _, e := range v.subs { 45 c.AddSub(e) 46 } 47 48 got := c.SubsLimitReached() 49 assert.Equal(t, v.expected, got) 50 }) 51 } 52 } 53 54 func TestSubAdded(t *testing.T) { 55 cases := map[string]struct { 56 expected bool 57 subs []event.Subscribe 58 pld event.Subscribe 59 }{ 60 "not added": { 61 expected: false, 62 subs: []event.Subscribe{{Event: "foo"}}, 63 pld: event.Subscribe{Event: "bar"}, 64 }, 65 "added": { 66 expected: true, 67 subs: []event.Subscribe{{Event: "foo"}}, 68 pld: event.Subscribe{Event: "foo"}, 69 }, 70 } 71 72 for k, v := range cases { 73 t.Run(k, func(t *testing.T) { 74 c := client.New() 75 for _, e := range v.subs { 76 c.AddSub(e) 77 } 78 79 got := c.SubAdded(v.pld) 80 assert.Equal(t, v.expected, got) 81 }) 82 } 83 } 84 85 func TestRemoveSub(t *testing.T) { 86 cases := map[string]struct { 87 expected bool 88 subs []event.Subscribe 89 pld event.Subscribe 90 }{ 91 "removing existing sub": { 92 expected: false, 93 subs: []event.Subscribe{{Event: "foo"}}, 94 pld: event.Subscribe{Event: "foo"}, 95 }, 96 "removing unexisting sub": { 97 expected: true, 98 subs: []event.Subscribe{{Event: "foo"}}, 99 pld: event.Subscribe{Event: "bar"}, 100 }, 101 } 102 103 for k, v := range cases { 104 t.Run(k, func(t *testing.T) { 105 c := client.New() 106 for _, e := range v.subs { 107 c.AddSub(e) 108 } 109 110 c.RemoveSub(v.pld) 111 got := c.SubAdded(v.subs[0]) 112 assert.Equal(t, v.expected, got) 113 }) 114 } 115 } 116 117 type byEvent []event.Subscribe 118 119 func (x byEvent) Len() int { 120 return len(x) 121 } 122 123 func (x byEvent) Less(i, j int) bool { 124 return x[i].Event < x[j].Event 125 } 126 127 func (x byEvent) Swap(i, j int) { 128 x[i], x[j] = x[j], x[i] 129 } 130 131 func TestGetAllSubs(t *testing.T) { 132 cases := map[string]struct { 133 expected []event.Subscribe 134 subs []event.Subscribe 135 }{ 136 "get all subs": { 137 expected: []event.Subscribe{{Event: "bar"}, {Event: "foo"}}, 138 subs: []event.Subscribe{{Event: "bar"}, {Event: "foo"}}, 139 }, 140 } 141 142 for k, v := range cases { 143 t.Run(k, func(t *testing.T) { 144 c := client.New() 145 for _, e := range v.subs { 146 c.AddSub(e) 147 } 148 149 got := c.GetAllSubs() 150 sort.Sort(byEvent(got)) 151 assert.Equal(t, v.expected, got) 152 }) 153 } 154 }