func TestSocketEvent(t *testing.T) { var rep *zmq.Socket defer func() { if rep != nil { rep.SetLinger(0) rep.Close() } }() // REP socket rep, err := zmq.NewSocket(zmq.REP) if err != nil { t.Fatal("NewSocket:", err) } // REP socket monitor, all events err = rep.Monitor("inproc://monitor.rep", zmq.EVENT_ALL) if err != nil { t.Fatal("rep.Monitor:", err) } chMsg := make(chan string, 10) go rep_socket_monitor("inproc://monitor.rep", chMsg) time.Sleep(time.Second) // Generate an event err = rep.Bind("tcp://*:9689") if err != nil { t.Fatal("rep.Bind:", err) } rep.Close() rep = nil expect := []string{ "EVENT_LISTENING tcp://0.0.0.0:9689", "EVENT_CLOSED tcp://0.0.0.0:9689", "Done", } i := 0 for msg := range chMsg { if i < len(expect) { if msg != expect[i] { t.Errorf("Expected message %q, got %q", expect[i], msg) } i++ } else { t.Error("Unexpected message: %q", msg) } } for ; i < len(expect); i++ { t.Errorf("Expected message %q, got nothing", expect[i]) } }
func TestPoller(t *testing.T) { var sb, sc *zmq.Socket defer func() { for _, s := range []*zmq.Socket{sb, sc} { if s != nil { s.SetLinger(0) s.Close() } } }() sb, err := zmq.NewSocket(zmq.PAIR) if err != nil { t.Fatal("NewSocket:", err) } err = sb.Bind("tcp://127.0.0.1:9737") if err != nil { t.Fatal("sb.Bind:", err) } sc, err = zmq.NewSocket(zmq.PAIR) if err != nil { t.Fatal("NewSocket:", err) } err = sc.Connect("tcp://127.0.0.1:9737") if err != nil { t.Fatal("sc.Connect:", err) } poller := zmq.NewPoller() idxb := poller.Add(sb, 0) idxc := poller.Add(sc, 0) if idxb != 0 || idxc != 1 { t.Errorf("idxb=%d idxc=%d", idxb, idxc) } if pa, err := poller.PollAll(100 * time.Millisecond); err != nil { t.Error("PollAll 1:", err) } else if len(pa) != 2 { t.Errorf("PollAll 1 len = %d", len(pa)) } else if pa[0].Events != 0 || pa[1].Events != 0 { t.Errorf("PollAll 1 events = %v, %v", pa[0], pa[1]) } poller.Update(idxb, zmq.POLLOUT) poller.UpdateBySocket(sc, zmq.POLLIN) if pa, err := poller.PollAll(100 * time.Millisecond); err != nil { t.Error("PollAll 2:", err) } else if len(pa) != 2 { t.Errorf("PollAll 2 len = %d", len(pa)) } else if pa[0].Events != zmq.POLLOUT || pa[1].Events != 0 { t.Errorf("PollAll 2 events = %v, %v", pa[0], pa[1]) } poller.UpdateBySocket(sb, 0) content := "12345678ABCDEFGH12345678ABCDEFGH" // Send message from client to server if rc, err := sb.Send(content, zmq.DONTWAIT); err != nil { t.Error("sb.Send DONTWAIT:", err) } else if rc != 32 { t.Error("sb.Send DONTWAIT:", err32) } if pa, err := poller.PollAll(100 * time.Millisecond); err != nil { t.Error("PollAll 3:", err) } else if len(pa) != 2 { t.Errorf("PollAll 3 len = %d", len(pa)) } else if pa[0].Events != 0 || pa[1].Events != zmq.POLLIN { t.Errorf("PollAll 3 events = %v, %v", pa[0], pa[1]) } // Receive message if msg, err := sc.Recv(zmq.DONTWAIT); err != nil { t.Error("sb.Recv DONTWAIT:", err) } else if msg != content { t.Error("sb.Recv msg != content") } poller.UpdateBySocket(sb, zmq.POLLOUT) poller.Update(idxc, zmq.POLLIN) if pa, err := poller.PollAll(100 * time.Millisecond); err != nil { t.Error("PollAll 4:", err) } else if len(pa) != 2 { t.Errorf("PollAll 4 len = %d", len(pa)) } else if pa[0].Events != zmq.POLLOUT || pa[1].Events != 0 { t.Errorf("PollAll 4 events = %v, %v", pa[0], pa[1]) } err = sc.Close() sc = nil if err != nil { t.Error("sc.Close:", err) } err = sb.Close() sb = nil if err != nil { t.Error("sb.Close:", err) } }