-
Notifications
You must be signed in to change notification settings - Fork 50
/
poll_test.go
68 lines (59 loc) · 1.38 KB
/
poll_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
// Go binding for nanomsg
package nanomsg
import (
"testing"
"time"
)
func TestPoller(t *testing.T) {
sa, err := NewPairSocket()
if err != nil {
t.Fatal(err)
}
defer sa.Close()
if _, err := sa.Bind("inproc://a"); err != nil {
t.Fatal(err)
}
sb, err := NewPairSocket()
if err != nil {
t.Fatal(err)
}
defer sb.Close()
if _, err := sb.Connect("inproc://a"); err != nil {
t.Fatal(err)
}
// Create a poller which makes sure it can send on both sockets.
var poller Poller
pia := poller.Add(sa.Socket, true, true)
pib := poller.Add(sb.Socket, true, true)
if i, err := poller.Poll(10 * time.Millisecond); err != nil {
t.Fatal(err)
} else if i != 2 {
t.Error("should be able to send", i)
}
// Remove the send check and pass in a message on one of the sockets.
pia.PollSend(false)
pib.PollSend(false)
if _, err := sa.Send([]byte("abc"), DontWait); err != nil {
t.Fatal(err)
}
if i, err := poller.Poll(10 * time.Millisecond); err != nil {
t.Fatal(err)
} else if i != 1 {
t.Error("no events", i)
}
if pia.CanRecv() {
t.Error("unexpected to recv")
}
if !pib.CanRecv() {
t.Error("expected recv")
}
// Receieve the message and allow the poller to timeout.
if _, err := sb.Recv(DontWait); err != nil {
t.Fatal(err)
}
if i, err := poller.Poll(1 * time.Millisecond); err != nil {
t.Fatal(err)
} else if i != 0 {
t.Error("no events should be available")
}
}