1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
package hotline
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestMemClientMgr_Add(t *testing.T) {
mgr := NewMemClientMgr()
c1 := &ClientConn{}
c2 := &ClientConn{}
mgr.Add(c1)
mgr.Add(c2)
assert.NotEqual(t, c1.ID, c2.ID)
assert.Len(t, mgr.List(), 2)
}
func TestMemClientMgr_Get(t *testing.T) {
mgr := NewMemClientMgr()
assert.Nil(t, mgr.Get(ClientID{0xFF, 0xFF}))
c := &ClientConn{}
mgr.Add(c)
assert.Equal(t, c, mgr.Get(c.ID))
}
func TestMemClientMgr_Delete(t *testing.T) {
mgr := NewMemClientMgr()
c := &ClientConn{}
mgr.Add(c)
id := c.ID
mgr.Delete(id)
assert.Nil(t, mgr.Get(id))
}
func TestMemClientMgr_List_Sorted(t *testing.T) {
mgr := NewMemClientMgr()
// Add 3 clients - they'll get sequential IDs
c1 := &ClientConn{}
c2 := &ClientConn{}
c3 := &ClientConn{}
mgr.Add(c1)
mgr.Add(c2)
mgr.Add(c3)
list := mgr.List()
assert.Len(t, list, 3)
// Verify sorted by ID
for i := 1; i < len(list); i++ {
assert.True(t, list[i-1].ID[0] < list[i].ID[0] ||
(list[i-1].ID[0] == list[i].ID[0] && list[i-1].ID[1] <= list[i].ID[1]),
"clients should be sorted by ID")
}
}
|