-
Notifications
You must be signed in to change notification settings - Fork 104
/
SessionIDManager_test.go
126 lines (117 loc) · 2.72 KB
/
SessionIDManager_test.go
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"testing"
)
func TestSessionIDManager16Bits(t *testing.T) {
m, err := NewSessionIDManager(0xfffe)
if err != nil {
t.Errorf("NewSessionIDManager return an error: %s", err.Error())
return
}
// fill all session ids
{
var i uint16
for i = 0; i <= 0xfffe; i++ {
id, err := m.AllocSessionID()
if err != nil {
t.Errorf("AllocSessionID return an error: %s", err.Error())
return
}
if i != id {
t.Errorf("checking AllocSessionID failed, expected ID: %x, returned ID: %x", i, id)
return
}
}
id, err := m.AllocSessionID()
if err == nil {
t.Errorf("AllocSessionID should return an error because session ID is full, but it returned a session ID: %x", id)
return
}
}
// free the first one
{
var id1, id2 uint16
id1 = 0x0000
m.FreeSessionID(id1)
id2, err := m.AllocSessionID()
if err != nil {
t.Errorf("AllocSessionID return an error: %s", err.Error())
return
}
if id1 != id2 {
t.Errorf("checking FreeSessionID and AllocSessionID failed, expected ID: %x, returned ID: %x", id1, id2)
return
}
}
// free the second one
{
var id1, id2 uint16
id1 = 0x0001
m.FreeSessionID(id1)
id2, err := m.AllocSessionID()
if err != nil {
t.Errorf("AllocSessionID return an error: %s", err.Error())
return
}
if id1 != id2 {
t.Errorf("checking FreeSessionID and AllocSessionID failed, expected ID: %x, returned ID: %x", id1, id2)
return
}
}
// free the one in the middle
{
var id1, id2 uint16
id1 = 0x5024
m.FreeSessionID(id1)
id2, err := m.AllocSessionID()
if err != nil {
t.Errorf("AllocSessionID return an error: %s", err.Error())
return
}
if id1 != id2 {
t.Errorf("checking FreeSessionID and AllocSessionID failed, expected ID: %x, returned ID: %x", id1, id2)
return
}
}
// free the penult one
{
var id1, id2 uint16
id1 = 0xfffd
m.FreeSessionID(id1)
id2, err := m.AllocSessionID()
if err != nil {
t.Errorf("AllocSessionID return an error: %s", err.Error())
return
}
if id1 != id2 {
t.Errorf("checking FreeSessionID and AllocSessionID failed, expected ID: %x, returned ID: %x", id1, id2)
return
}
}
// free the last one
{
var id1, id2 uint16
id1 = 0x00fffe
m.FreeSessionID(id1)
id2, err := m.AllocSessionID()
if err != nil {
t.Errorf("AllocSessionID return an error: %s", err.Error())
return
}
if id1 != id2 {
t.Errorf("checking FreeSessionID and AllocSessionID failed, expected ID: %x, returned ID: %x", id1, id2)
return
}
}
// free all ids
{
var i uint16
for i = 0x0000; i <= 0xfffe; i++ {
m.FreeSessionID(i)
}
if m.count != 0 {
t.Errorf("checking FreeSessionID failed, m.count should be 0 after free all ids, but it is %d", m.count)
return
}
}
}