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(inproc.NewTransport()) s1.AddTransport(ipc.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()) }
// AddTransports adds all known transports to the given socket. func AddTransports(sock mangos.Socket) { sock.AddTransport(tcp.NewTransport()) sock.AddTransport(inproc.NewTransport()) sock.AddTransport(ipc.NewTransport()) sock.AddTransport(tlstcp.NewTransport()) sock.AddTransport(ws.NewTransport()) sock.AddTransport(wss.NewTransport()) }
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 TestExpireDrop(t *testing.T) { inp := inproc.NewTransport() addr := AddrTestInp + "EXPIRE" if runtime.GOOS == "windows" { t.Skip("Windows clock resolution too coarse") return } srv, err := pair.NewSocket() if err != nil { t.Errorf("Failed to make server: %v", err) return } defer srv.Close() srv.AddTransport(inp) err = srv.Listen(addr) if err != nil { t.Errorf("Failed listen: %v", err) return } cli, err := pair.NewSocket() if err != nil { t.Errorf("Failed to make client: %v", err) return } defer cli.Close() cli.AddTransport(inp) err = cli.Dial(addr) if err != nil { t.Errorf("Failed dial: %v", err) return } time.Sleep(time.Millisecond * 100) err = srv.SetOption(mangos.OptionRecvDeadline, time.Millisecond*10) if err != nil { t.Errorf("Failed set recv deadline") return } if err = cli.Send([]byte("GOOD")); err != nil { t.Errorf("Failed first send: %v", err) return } // Now try setting the option. We set it to a very short window. // Note that we default to have a non-zero queue depth, so we will // still queue the message, but then when we retrieve the message // it will be found to be stale. err = cli.SetOption(mangos.OptionSendDeadline, time.Nanosecond*1) if err != nil { t.Errorf("Failed set send deadline") return } if err = cli.Send([]byte("DROP")); err != nil { t.Errorf("Failed send drop: %v", err) return } v, err := srv.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) } v, err = srv.Recv() switch err { case mangos.ErrRecvTimeout: // expected t.Logf("Deadline honored") case nil: t.Errorf("Message not dropped: %v", v) default: t.Errorf("Got unexpected error: %v", err) } }
// 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 } } // Wait for the various connections to plumb up time.Sleep(time.Millisecond * 100) // 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) } }