func testDevChain(t *testing.T, addr1 string, addr2 string, addr3 string) { // This tests using multiple devices across a few transports. // It looks like this: addr1->addr2->addr3 <==> addr3->addr2->addr1 var err error s := make([]mangos.Socket, 5) for i := 0; i < 5; i++ { if s[i], err = pair.NewSocket(); err != nil { t.Errorf("Failed to open S1_1: %v", err) return } defer s[i].Close() s[i].AddTransport(tcp.NewTransport()) s[i].AddTransport(inproc.NewTransport()) s[i].AddTransport(tlstcp.NewTransport()) s[i].AddTransport(ipc.NewTransport()) s[i].AddTransport(ws.NewTransport()) } if err = s[0].Listen(addr1); err != nil { t.Errorf("s[0] Listen: %v", err) return } if err = s[1].Dial(addr2); err != nil { t.Errorf("s[1] Dial: %v", err) return } if err = s[2].Listen(addr2); err != nil { t.Errorf("s[2] Listen: %v", err) return } if err = s[3].Dial(addr3); err != nil { t.Errorf("s[3] Dial: %v", err) return } if err = s[4].Listen(addr3); err != nil { t.Errorf("s[4] Listen: %v", err) return } if err = mangos.Device(s[0], s[1]); err != nil { t.Errorf("s[0],s[1] Device: %v", err) return } if err = mangos.Device(s[2], s[3]); err != nil { t.Errorf("s[2],s[3] Device: %v", err) return } if err = mangos.Device(s[4], nil); err != nil { t.Errorf("s[4] Device: %v", err) return } RunTests(t, addr1, deviceCaseClient()) }
func TestDeviceBadPair(t *testing.T) { s1, err := req.NewSocket() if err != nil { t.Errorf("Failed to open S1: %v", err) return } defer s1.Close() s2, err := pair.NewSocket() if err != nil { t.Errorf("Failed to open S2: %v", err) return } defer s2.Close() switch err := mangos.Device(s1, s2); err { case mangos.ErrBadProto: t.Logf("Got expected err: %v", err) return case nil: t.Errorf("Matching incompatible types succeeded") return default: t.Errorf("Got unexpected err: %v", err) return } }
func testDevLoop(t *testing.T, addr string) { s1, err := pair.NewSocket() if err != nil { t.Errorf("Failed to open S1: %v", err) return } defer s1.Close() s1.AddTransport(tcp.NewTransport()) s1.AddTransport(ipc.NewTransport()) s1.AddTransport(inproc.NewTransport()) s1.AddTransport(tlstcp.NewTransport()) s1.AddTransport(ws.NewTransport()) s1.AddTransport(wss.NewTransport()) options := make(map[string]interface{}) if strings.HasPrefix(addr, "wss://") || strings.HasPrefix(addr, "tls+tcp://") { options[mangos.OptionTlsConfig] = srvCfg } if err := s1.ListenOptions(addr, options); err != nil { t.Errorf("Failed listening to %s: %v", addr, err) return } if err := mangos.Device(s1, s1); err != nil { t.Errorf("Device failed: %v", err) return } RunTests(t, addr, deviceCaseClient()) }
func TestDeviceBothNil(t *testing.T) { switch err := mangos.Device(nil, nil); err { case mangos.ErrClosed: t.Logf("Got expected err: %v", err) return case nil: t.Errorf("Matching incompatible types succeeded") return default: t.Errorf("Got unexpected err: %v", err) return } }
func TestDeviceSecondNil(t *testing.T) { s1, err := pair.NewSocket() if err != nil { t.Errorf("Failed to open S1: %v", err) return } defer s1.Close() switch err := mangos.Device(s1, nil); err { case nil: t.Logf("Ok!") return default: t.Errorf("Got unexpected err: %v", err) return } }
func TestDeviceReqRep(t *testing.T) { s1, err := req.NewSocket() if err != nil { t.Errorf("Failed to open S1: %v", err) return } defer s1.Close() s2, err := rep.NewSocket() if err != nil { t.Errorf("Failed to open S2: %v", err) return } defer s2.Close() switch err := mangos.Device(s1, s2); err { case nil: t.Logf("Matching req/rep ok!") return default: t.Errorf("Got unexpected err: %v", err) return } }
// TTLDropTest is a generic test for dropping based on TTL expiration. // F1 makes the client socket, f2 makes the server socket. func TTLDropTest(t *testing.T, cli newSockFunc, srv newSockFunc) { nhop := 3 clis := make([]mangos.Socket, 0, nhop) srvs := make([]mangos.Socket, 0, nhop) inp := inproc.NewTransport() for i := 0; i < nhop; i++ { s, err := srv() if err != nil { t.Errorf("Failed to make server: %v", err) return } defer s.Close() s.AddTransport(inp) err = s.Listen(AddrTestInp + fmt.Sprintf("HOP%d", i)) if err != nil { t.Errorf("Failed listen: %v", err) return } err = s.SetOption(mangos.OptionRaw, true) if err != nil { t.Errorf("Failed set raw mode: %v", err) return } srvs = append(srvs, s) } for i := 0; i < nhop; i++ { s, err := cli() if err != nil { t.Errorf("Failed to make client: %v", err) return } defer s.Close() s.AddTransport(inp) err = s.Dial(AddrTestInp + fmt.Sprintf("HOP%d", i)) if err != nil { t.Errorf("Failed dial: %v", err) return } clis = append(clis, s) } // Now make the device chain for i := 0; i < nhop-1; i++ { err := mangos.Device(srvs[i], clis[i+1]) if err != nil { t.Errorf("Device failed: %v", err) return } } // At this point, we can issue requests on clis[0], and read them from // srvs[nhop-1]. rq := clis[0] rp := srvs[nhop-1] err := rp.SetOption(mangos.OptionRecvDeadline, time.Millisecond*20) if err != nil { t.Errorf("Failed set recv deadline") return } t.Logf("Socket for sending is %s", rq.GetProtocol().Name()) if err = rq.Send([]byte("GOOD")); err != nil { t.Errorf("Failed first send: %v", err) return } t.Logf("Socket for receiving is %s", rp.GetProtocol().Name()) v, err := rp.Recv() if err != nil { t.Errorf("Failed first recv: %v", err) return } else if !bytes.Equal(v, []byte("GOOD")) { t.Errorf("Got wrong message: %v", v) return } else { t.Logf("Got good message: %v", v) } // Now try setting the option. err = rp.SetOption(mangos.OptionTTL, nhop-1) if err != nil { t.Errorf("Failed set TTL: %v", err) return } if err = rq.Send([]byte("DROP")); err != nil { t.Errorf("Failed send drop: %v", err) return } v, err = rp.Recv() switch err { case mangos.ErrRecvTimeout: // expected t.Logf("TTL honored") case nil: t.Errorf("Message not dropped: %v", v) default: t.Errorf("Got unexpected error: %v", err) } }
func TestTtlDrop(t *testing.T) { nhop := 3 srep := make([]mangos.Socket, 0, nhop) sreq := make([]mangos.Socket, 0, nhop) inp := inproc.NewTransport() for i := 0; i < nhop; i++ { s, err := rep.NewSocket() if err != nil { t.Errorf("Failed to make REP: %v", err) return } defer s.Close() s.AddTransport(inp) err = s.Listen(AddrTestInp + fmt.Sprintf("HOP%d", i)) if err != nil { t.Errorf("Failed listen: %v", err) return } err = s.SetOption(mangos.OptionRaw, true) if err != nil { t.Errorf("Failed set raw mode: %v", err) return } srep = append(srep, s) } for i := 0; i < nhop; i++ { s, err := req.NewSocket() if err != nil { t.Errorf("Failed to make REQ: %v", err) return } defer s.Close() s.AddTransport(inp) err = s.Dial(AddrTestInp + fmt.Sprintf("HOP%d", i)) if err != nil { t.Errorf("Failed dial: %v", err) return } sreq = append(sreq, s) } // Now make the device chain for i := 0; i < nhop-1; i++ { err := mangos.Device(srep[i], sreq[i+1]) if err != nil { t.Errorf("Device failed: %v", err) return } } // At this point, we can issue requests on sreq[0], and read them from // srep[nhop-1]. rq := sreq[0] rp := srep[nhop-1] err := rp.SetOption(mangos.OptionRecvDeadline, time.Millisecond*20) if err != nil { t.Errorf("Failed set recv deadline") return } if err = rq.Send([]byte("GOOD")); err != nil { t.Errorf("Failed first send: %v", err) return } v, err := rp.Recv() if err != nil { t.Errorf("Failed first recv: %v", err) return } else if !bytes.Equal(v, []byte("GOOD")) { t.Errorf("Got wrong message: %v", v) return } else { t.Logf("Got good message: %v", v) } // Now try setting the option err = rp.SetOption(mangos.OptionTtl, nhop-1) if err != nil { t.Errorf("Failed set TTL: %v", err) return } if err = rq.Send([]byte("DROP")); err != nil { t.Errorf("Failed send drop: %v", err) return } v, err = rp.Recv() switch err { case mangos.ErrRecvTimeout: // expected t.Logf("TTL honored") case nil: t.Errorf("Message not dropped: %v", v) default: t.Errorf("Got unexpected error: %v", err) } }