-
Notifications
You must be signed in to change notification settings - Fork 0
/
ngo_test.go
56 lines (50 loc) · 1.04 KB
/
ngo_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
// Go binding for nanomsg
package ngo
import (
"encoding/gob"
"net"
"testing"
)
func TestListenAndDial(t *testing.T) {
// Dial out to the listener and send 10 integers in increasing size. We use
// gob to encode the data sent on the wire for simplicity.
go func() {
conn, err := Dial("inproc", "req", "listen-serve")
if err != nil {
t.Fatal(err)
}
defer conn.Close()
e := gob.NewEncoder(conn)
for i := 0; i < 10; i++ {
e.Encode(i)
}
if err = conn.Close(); err != nil {
t.Fatal(err)
}
}()
// Launch the listener and for each new message decode it using gob and make
// sure the data retrieved is the expected.
l, err := Listen("inproc", "rep", "listen-serve")
if err != nil {
t.Fatal(err)
}
defer l.Close()
for i := 0; i < 10; i++ {
c, err := l.Accept()
if err != nil {
t.Fatal(err)
}
go func(i int, c net.Conn) {
var j int
defer c.Close()
d := gob.NewDecoder(c)
err := d.Decode(&j)
if err != nil {
t.Fatal(err)
}
if i != j {
t.Error("%d != %d", i, j)
}
}(i, c)
}
}