This repository has been archived by the owner on May 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient_test.go
166 lines (146 loc) · 3 KB
/
client_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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package synapse
import (
"bytes"
"net"
"sync"
"testing"
"time"
)
func isCode(err error, c Status) bool {
if reserr, ok := err.(*ResponseError); ok && reserr.Code == c {
return true
}
return false
}
// open up a client and server; make
// some concurrent requests
func TestClient(t *testing.T) {
const concurrent = 5
wg := new(sync.WaitGroup)
wg.Add(concurrent)
for i := 0; i < concurrent; i++ {
go func() {
instr := testData("hello, world!")
var outstr testData
err := tcpClient.Call(Echo, &instr, &outstr)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal([]byte(instr), []byte(outstr)) {
t.Fatal("input and output not equal")
}
wg.Done()
}()
}
wg.Wait()
// test for NotFound
err := tcpClient.Call(100, nil, nil)
if !isCode(err, StatusNotFound) {
t.Errorf("expected not-found error; got %#v", err)
}
}
// the output of the debug handler
// is only visible if '-v' is set
func TestDebugClient(t *testing.T) {
instr := String("here's a message body!")
var outstr String
err := tcpClient.Call(DebugEcho, &instr, &outstr)
if err != nil {
t.Fatal(err)
}
if instr != outstr {
t.Fatal("input and output not equal")
}
}
// test that 'nil' is a safe
// argument to requests and responses
func TestNop(t *testing.T) {
err := tcpClient.Call(Nop, nil, nil)
if err != nil {
t.Fatal(err)
}
}
// benchmarks the test case above
func BenchmarkTCPEcho(b *testing.B) {
l, err := net.Listen("tcp", "localhost:7000")
if err != nil {
b.Fatal(err)
}
defer func() {
l.Close()
time.Sleep(1 * time.Millisecond)
}()
go Serve(l, EchoHandler{})
cl, err := Dial("tcp", "localhost:7000", 50*time.Millisecond)
if err != nil {
b.Fatal(err)
}
defer cl.Close()
b.ResetTimer()
b.ReportAllocs()
b.SetParallelism(20)
b.RunParallel(func(pb *testing.PB) {
instr := testData("hello, world!")
var outstr testData
for pb.Next() {
err := cl.Call(Echo, &instr, &outstr)
if err != nil {
b.Fatal(err)
}
if !bytes.Equal([]byte(instr), []byte(outstr)) {
b.Fatalf("%q in; %q out", instr, outstr)
}
}
})
b.StopTimer()
}
func BenchmarkUnixNoop(b *testing.B) {
l, err := net.Listen("unix", "bench")
if err != nil {
b.Fatal(err)
}
defer func() {
l.Close()
time.Sleep(1 * time.Millisecond)
}()
go Serve(l, NopHandler{})
cl, err := Dial("unix", "bench", 50*time.Millisecond)
if err != nil {
b.Fatal(err)
}
defer cl.Close()
b.ResetTimer()
b.ReportAllocs()
b.SetParallelism(20)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
err := cl.Call(Echo, nil, nil)
if err != nil {
b.Fatal(err)
}
}
})
b.StopTimer()
}
func BenchmarkPipeNoop(b *testing.B) {
srv, cln := net.Pipe()
go ServeConn(srv, NopHandler{})
defer srv.Close()
cl, err := NewClient(cln, 50*time.Millisecond)
if err != nil {
b.Fatal(err)
}
defer cl.Close()
b.ResetTimer()
b.ReportAllocs()
b.SetParallelism(20)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
err := cl.Call(0, nil, nil)
if err != nil {
b.Fatal(err)
}
}
})
b.StopTimer()
}