forked from panjf2000/gnet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
conn_matrix_test.go
115 lines (100 loc) · 2.61 KB
/
conn_matrix_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
//go:build (linux || freebsd || dragonfly || netbsd || openbsd || darwin) && gc_opt
// +build linux freebsd dragonfly netbsd openbsd darwin
// +build gc_opt
package gnet
import (
"net"
"testing"
"golang.org/x/sys/unix"
goPool "github.com/panjf2000/gnet/v2/pkg/pool/goroutine"
)
var testVastConns = false
func TestConnMatrix(t *testing.T) {
t.Run("1k-connections", func(t *testing.T) {
testConnMatrix(t, 1000)
})
t.Run("10k-connections", func(t *testing.T) {
testConnMatrix(t, 10000)
})
t.Run("100k-connections", func(t *testing.T) {
if !testVastConns {
t.Skip("skipped because testVastConns is set to false")
}
testConnMatrix(t, 100000)
})
t.Run("1m-connections", func(t *testing.T) {
if !testVastConns {
t.Skip("skipped because testVastConns is set to false")
}
testConnMatrix(t, 1000000)
})
}
const (
actionAdd = iota + 1
actionDel
)
type handleConn struct {
c *conn
action int
}
func testConnMatrix(t *testing.T, n int) {
handleConns := make(chan *handleConn, 1024)
connections := connMatrix{}
connections.init()
el := eventloop{engine: &engine{opts: &Options{}}}
done := make(chan struct{})
go func() {
for i := 0; i < n+n/2; i++ {
v := <-handleConns
switch v.action {
case actionAdd:
connections.addConn(v.c, 0)
case actionDel:
connections.delConn(v.c)
}
}
close(done)
}()
pool := goPool.Default()
for i := 0; i < n; i++ {
c := newTCPConn(i, &el, &unix.SockaddrInet4{}, &net.TCPAddr{}, &net.TCPAddr{})
handleConns <- &handleConn{c, actionAdd}
if i%2 == 0 {
pool.Submit(func() {
handleConns <- &handleConn{c, actionDel}
})
}
}
m := n / 2
<-done
if count := connections.loadCount(); count != int32(n)/2 {
t.Fatalf("unexpected conn count %d, expected %d", count, int32(n)/2)
}
for i := 0; i < len(connections.table); i++ {
if connections.connCounts[i] == 0 {
continue
}
for j := 0; j < len(connections.table[i]) && m > 0; j++ {
m--
c := connections.table[i][j]
if c == nil {
t.Fatalf("unexpected nil connection at row %d, column %d", i, j)
}
if c.fd != c.gfd.Fd() {
t.Fatalf("unexpected fd %d, expected fd %d", c.gfd.Fd(), c.fd)
}
if i != c.gfd.ConnMatrixRow() || j != c.gfd.ConnMatrixColumn() {
t.Fatalf("unexpected row %d, column %d, expected row %d, column %d",
c.gfd.ConnMatrixRow(), c.gfd.ConnMatrixColumn(), i, j)
}
gfd, ok := connections.fd2gfd[c.fd]
if !ok {
t.Fatalf("missing gfd for fd %d", c.fd)
}
if gfd != c.gfd {
t.Fatalf("expected gfd: %v, but got gfd: %v", c.gfd, gfd)
}
}
}
t.Log("connMatrix remains compact after many additions and deletions, test done!")
}