-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsrv_norace_test.go
40 lines (33 loc) · 1.26 KB
/
srv_norace_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
// +build !race test
package cs_test
import (
"github.com/eyasliu/cs"
"github.com/gogf/gf/test/gtest"
"testing"
"time"
)
func TestSrv_MutilServer(t *testing.T) {
server1 := &testAdapter{request: []*cs.Request{{"a", "1", []byte{1}}}, sid: "1"}
server2 := &testAdapter{request: []*cs.Request{{"a", "2", []byte{2}}}, sid: "2"}
server3 := &testAdapter{request: []*cs.Request{{"a", "3", []byte{3}}}, sid: "3"}
server4 := &testAdapter{request: []*cs.Request{{"a", "4", []byte{4}}}, sid: "4"}
server5 := &testAdapter{request: []*cs.Request{{"a", "5", []byte{5}}}, sid: "5"}
gtest.C(t, func(t *gtest.T) {
srv := cs.New(server1, server2)
seqnos := []string{} // 这个变量会产生数据竞争,数据竞争就会导致里面数组的顺序不确定,但这只是测试代码
srv.Handle("a", func(c *cs.Context) {
seqnos = append(seqnos, c.Seqno)
c.OK()
})
srv.AddServer(server3)
go srv.Run()
time.Sleep(50 * time.Millisecond)
t.AssertIN(seqnos, []string{"1", "2", "3"}) // 1 + 2 + 3
srv.AddServer(server4)
time.Sleep(50 * time.Millisecond)
t.AssertIN(seqnos, []string{"1", "2", "3", "4"}) // 1 + 2 + 3 + 4
srv.AddServer(server5)
time.Sleep(50 * time.Millisecond)
t.AssertIN(seqnos, []string{"1", "2", "3", "4", "5"}) // 1 + 2 + 3 + 4 + 5
})
}