-
Notifications
You must be signed in to change notification settings - Fork 94
/
sock_draft_test.go
147 lines (120 loc) · 2.89 KB
/
sock_draft_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
// +build draft
package goczmq
import (
"fmt"
"testing"
)
func TestScatterGather(t *testing.T) {
bogusScatter, err := NewScatter("bogus://bogus")
if err == nil {
t.Error(err)
}
defer bogusScatter.Destroy()
bogusGather, err := NewGather("bogus://bogus")
if err == nil {
t.Error(err)
}
defer bogusGather.Destroy()
scatter, err := NewScatter("inproc://scatter1,inproc://scatter2")
if err != nil {
t.Error(err)
}
defer scatter.Destroy()
gather, err := NewGather("inproc://scatter1,inproc://scatter2")
if err != nil {
t.Error(err)
}
defer gather.Destroy()
err = scatter.SendFrame([]byte("Hello World"), FlagNone)
if err != nil {
t.Error(err)
}
frame, _, err := gather.RecvFrame()
if err != nil {
t.Error(err)
}
if want, have := "Hello World", string(frame); want != have {
t.Errorf("want %#v, have %#v", want, have)
}
}
func benchmarkScatterGather(size int, b *testing.B) {
gatherSock := NewSock(Gather)
defer gatherSock.Destroy()
_, err := gatherSock.Bind(fmt.Sprintf("inproc://benchScatterGather%#v", size))
if err != nil {
panic(err)
}
scatterSock := NewSock(Scatter)
defer scatterSock.Destroy()
err = scatterSock.Connect(fmt.Sprintf("inproc://benchScatterGather%#v", size))
if err != nil {
panic(err)
}
go func() {
payload := make([]byte, size)
for i := 0; i < b.N; i++ {
err = scatterSock.SendFrame(payload, FlagNone)
if err != nil {
panic(err)
}
}
}()
for i := 0; i < b.N; i++ {
msg, _, err := gatherSock.RecvFrame()
if err != nil {
panic(err)
}
if len(msg) != size {
panic("msg too small")
}
b.SetBytes(int64(size))
}
}
func BenchmarkScatterGather1k(b *testing.B) { benchmarkScatterGather(1024, b) }
func BenchmarkScatterGather4k(b *testing.B) { benchmarkScatterGather(4096, b) }
func BenchmarkScatterGather16k(b *testing.B) { benchmarkScatterGather(16384, b) }
func TestClientServer(t *testing.T) {
bogusClient, err := NewClient("bogus://bogus")
if err == nil {
t.Error(err)
}
defer bogusClient.Destroy()
bogusServer, err := NewServer("bogus://bogus")
if err == nil {
t.Error(err)
}
defer bogusServer.Destroy()
client, err := NewClient("inproc://server")
if err != nil {
t.Error(err)
}
defer client.Destroy()
server, err := NewServer("inproc://server")
if err != nil {
t.Error(err)
}
defer server.Destroy()
err = client.SendFrame([]byte("Hello World"), FlagNone)
if err != nil {
t.Error(err)
}
frame, routing_id, err := server.RecvServerFrame()
if err != nil {
t.Error(err)
}
t.Logf("routing_id %d", routing_id)
if want, have := "Hello World", string(frame); want != have {
t.Errorf("want %#v, have %#v", want, have)
}
err = server.SendServerFrame([]byte("Hi World"), routing_id)
if err != nil {
t.Error(err)
}
frame, _, err = client.RecvFrame()
if err != nil {
t.Error(err)
}
if want, have := "Hi World", string(frame); want != have {
t.Errorf("want %#v, have %#v", want, have)
}
}