forked from lesismal/arpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_test.go
124 lines (110 loc) · 2.69 KB
/
server_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
// Copyright 2020 lesismal. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package arpc
import (
"context"
"fmt"
"log"
"net"
"testing"
"time"
)
var testServerAddr = "localhost:12000"
func TestServer_addLoad(t *testing.T) {
var (
half int64 = 10000
total int64 = half * 2
)
s := NewServer()
for i := int64(0); i < total; i++ {
s.addLoad()
}
if s.CurrLoad != total {
t.Fatalf("addLoad failed: %v != %v", s.CurrLoad, total)
}
}
func TestServer_subLoad(t *testing.T) {
var (
half int64 = 10000
total int64 = half * 2
)
s := NewServer()
for i := int64(0); i < total; i++ {
s.addLoad()
}
if s.CurrLoad != total {
t.Fatalf("addLoad failed: %v != %v", s.CurrLoad, total)
}
for i := int64(0); i < half; i++ {
s.subLoad()
}
if s.CurrLoad != half {
t.Fatalf("subLoad failed: %v != %v", s.CurrLoad, half)
}
for i := int64(0); i < half; i++ {
s.subLoad()
}
if s.CurrLoad != 0 {
t.Fatalf("subLoad failed: %v != %v", s.CurrLoad, half)
}
}
func TestServer_NewMessage(t *testing.T) {
s := &Server{}
for cmd := byte(1); cmd <= 3; cmd++ {
method := fmt.Sprintf("method_%v", cmd)
message := fmt.Sprintf("message_%v", cmd)
msg := s.NewMessage(cmd, method, message)
if msg == nil {
t.Fatalf("Server.NewMessage() = nil")
}
if msg.Cmd() != cmd {
t.Fatalf("Server.NewMessage() error, cmd is: %v, want: %v", msg.Cmd(), cmd)
}
if msg.Method() != method {
t.Fatalf("Server.NewMessage() error, cmd is: %v, want: %v", msg.Method(), method)
}
if msg.Method() != method {
t.Fatalf("Server.NewMessage() error, cmd is: %v, want: %v", string(msg.Data()), message)
}
}
}
func TestServer_Serve(t *testing.T) {
ln, err := net.Listen("tcp", testServerAddr)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
svr := NewServer()
svr.MaxLoad = 3
go svr.Serve(ln)
time.Sleep(time.Second / 100)
for i := 0; i < 3; i++ {
if _, err := net.Dial("tcp", testServerAddr); err != nil {
log.Fatalf("failed to Dial: %v", err)
}
}
for i := 0; i < 3; i++ {
if conn, err := net.Dial("tcp", testServerAddr); err != nil {
log.Fatalf("failed to Dial: %v", err)
} else {
conn.SetReadDeadline(time.Now().Add(time.Second / 10))
if _, err = conn.Read([]byte{1}); err == nil {
log.Fatalf("conn.Read success, should be closed by server(limited by MaxLoad)")
}
}
}
svr.Stop()
}
func TestServer_Run(t *testing.T) {
svr := NewServer()
go svr.Run(testServerAddr)
go svr.Run(testServerAddr)
time.Sleep(time.Second / 100)
svr.Stop()
}
func TestServer_Shutdown(t *testing.T) {
svr := NewServer()
go svr.Run(":8899")
time.Sleep(time.Second / 100)
svr.Shutdown(context.Background())
}