@@ -7,39 +7,105 @@ import (
77func TestAllocateCookie (t * testing.T ) {
88 allocator := NewAllocator (0 )
99
10- cookie := allocator .RequestCookie ()
10+ cookie , err := allocator .RequestCookie ()
11+ if err != nil {
12+ t .Errorf ("unexpectd return err %s" , err )
13+ }
1114 if cookie != 0x1 {
1215 t .Errorf ("request cookie fail, expect 0x1, got %x" , cookie )
1316 }
1417
15- cookie = allocator .RequestCookie ()
18+ cookie , err = allocator .RequestCookie ()
19+ if err != nil {
20+ t .Errorf ("unexpectd return err %s" , err )
21+ }
1622 if cookie != 0x2 {
1723 t .Errorf ("request cookie fail, expect 0x2, got %x" , cookie )
1824 }
1925
2026 allocator .SetFixedMask (0x10 )
2127
22- cookie = allocator .RequestCookie ()
28+ cookie , err = allocator .RequestCookie ()
29+ if err != nil {
30+ t .Errorf ("unexpectd return err %s" , err )
31+ }
2332 if cookie != 0x13 {
2433 t .Errorf ("request cookie fail, expect 0x13, got %x" , cookie )
2534 }
2635
2736 allocator = NewAllocator (1 )
2837
29- cookie = allocator .RequestCookie ()
38+ cookie , err = allocator .RequestCookie ()
39+ if err != nil {
40+ t .Errorf ("unexpectd return err %s" , err )
41+ }
3042 if cookie != 0x10000001 {
3143 t .Errorf ("request cookie fail, expect 0x10000001, got %x" , cookie )
3244 }
3345
34- cookie = allocator .RequestCookie ()
46+ cookie , err = allocator .RequestCookie ()
47+ if err != nil {
48+ t .Errorf ("unexpectd return err %s" , err )
49+ }
3550 if cookie != 0x10000002 {
3651 t .Errorf ("request cookie fail, expect 0x10000002, got %x" , cookie )
3752 }
3853
3954 allocator .SetFixedMask (0x100000000 )
4055
41- cookie = allocator .RequestCookie ()
56+ cookie , err = allocator .RequestCookie ()
57+ if err != nil {
58+ t .Errorf ("unexpectd return err %s" , err )
59+ }
4260 if cookie != 0x110000003 {
4361 t .Errorf ("request cookie fail, expect 0x110000003, got %x" , cookie )
4462 }
63+
64+ allocator = NewAllocator (1 , SetFlowIDRange (10 , 9 ))
65+ if allocator != nil {
66+ t .Errorf ("expect allocator is nil, but real is not" )
67+ }
68+
69+ allocator = NewAllocator (1 , SetFlowIDRange (0 , 9 ))
70+ if allocator != nil {
71+ t .Errorf ("expect allocator is nil, but real is not" )
72+ }
73+
74+ allocator = NewAllocator (1 , SetFlowIDRange (10 , 1 << BitWidthFlowId ))
75+ if allocator != nil {
76+ t .Errorf ("expect allocator is nil, but real is not" )
77+ }
78+
79+ allocator = NewAllocator (1 , SetFlowIDRange (10 , 11 ))
80+
81+ cookie , err = allocator .RequestCookie ()
82+ if err != nil {
83+ t .Errorf ("unexpectd return err %s" , err )
84+ }
85+ if cookie != 0x1000000a {
86+ t .Errorf ("request cookie fail, expect 0x1000000a , got %x" , cookie )
87+ }
88+ cookie , err = allocator .RequestCookie ()
89+ if err != nil {
90+ t .Errorf ("unexpectd return err %s" , err )
91+ }
92+ if cookie != 0x1000000b {
93+ t .Errorf ("request cookie fail, expect 0x1000000b, got %x" , cookie )
94+ }
95+ cookie , err = allocator .RequestCookie ()
96+ if err == nil {
97+ t .Errorf ("expect allocate cookie failed, but success" )
98+ }
99+
100+ allocator = NewAllocator (1 , SetDefaultFlowIDRange ())
101+ if allocator == nil {
102+ t .Errorf ("new allocator failed, is unexpected" )
103+ }
104+ cookie , err = allocator .RequestCookie ()
105+ if err != nil {
106+ t .Errorf ("unexpectd return err %s" , err )
107+ }
108+ if cookie != 0x10000001 {
109+ t .Errorf ("request cookie fail, expect 0x10000001, got %x" , cookie )
110+ }
45111}
0 commit comments