forked from valyala/fasthttp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
peripconn_test.go
59 lines (48 loc) · 1.07 KB
/
peripconn_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
package fasthttp
import (
"testing"
)
func TestIPxUint32(t *testing.T) {
testIPxUint32(t, 0)
testIPxUint32(t, 10)
testIPxUint32(t, 0x12892392)
}
func testIPxUint32(t *testing.T, n uint32) {
ip := uint322ip(n)
nn := ip2uint32(ip)
if n != nn {
t.Fatalf("Unexpected value=%d for ip=%s. Expected %d", nn, ip, n)
}
}
func TestPerIPConnCounter(t *testing.T) {
var cc perIPConnCounter
expectPanic(t, func() { cc.Unregister(123) })
for i := 1; i < 100; i++ {
if n := cc.Register(123); n != i {
t.Fatalf("Unexpected counter value=%d. Expected %d", n, i)
}
}
n := cc.Register(456)
if n != 1 {
t.Fatalf("Unexpected counter value=%d. Expected 1", n)
}
for i := 1; i < 100; i++ {
cc.Unregister(123)
}
cc.Unregister(456)
expectPanic(t, func() { cc.Unregister(123) })
expectPanic(t, func() { cc.Unregister(456) })
n = cc.Register(123)
if n != 1 {
t.Fatalf("Unexpected counter value=%d. Expected 1", n)
}
cc.Unregister(123)
}
func expectPanic(t *testing.T, f func()) {
defer func() {
if r := recover(); r == nil {
t.Fatalf("Expecting panic")
}
}()
f()
}