nanomsg.org/go/mangos/v2@v2.0.9-0.20200203084354-8a092611e461/test/device_test.go (about) 1 // Copyright 2018 The Mangos Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use file except in compliance with the License. 5 // You may obtain a copy of the license at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package test 16 17 import ( 18 "strings" 19 "testing" 20 "time" 21 22 "nanomsg.org/go/mangos/v2" 23 "nanomsg.org/go/mangos/v2/protocol/pair" 24 "nanomsg.org/go/mangos/v2/protocol/xpair" 25 _ "nanomsg.org/go/mangos/v2/transport/all" 26 ) 27 28 type devTest struct { 29 T 30 } 31 32 func (dt *devTest) Init(t *testing.T, addr string) bool { 33 var err error 34 if dt.Sock, err = pair.NewSocket(); err != nil { 35 t.Fatalf("pair.NewSocket(): %v", err) 36 } 37 return dt.T.Init(t, addr) 38 } 39 40 func (dt *devTest) SendHook(m *mangos.Message) bool { 41 m.Body = append(m.Body, byte(dt.GetSend())) 42 return dt.T.SendHook(m) 43 } 44 45 func (dt *devTest) RecvHook(m *mangos.Message) bool { 46 if len(m.Body) != 1 { 47 dt.Errorf("Recv message length %d != 1", len(m.Body)) 48 return false 49 } 50 if m.Body[0] != byte(dt.GetRecv()) { 51 dt.Errorf("Wrong message: %d != %d", m.Body[0], byte(dt.GetRecv())) 52 return false 53 } 54 return dt.T.RecvHook(m) 55 } 56 57 func deviceCaseClient() []TestCase { 58 dev := &devTest{} 59 dev.ID = 0 60 dev.MsgSize = 4 61 dev.WantTx = 50 62 dev.WantRx = 50 63 cases := []TestCase{dev} 64 return cases 65 } 66 67 func testDevLoop(t *testing.T, addr string) { 68 s1, err := xpair.NewSocket() 69 if err != nil { 70 t.Errorf("Failed to open S1: %v", err) 71 return 72 } 73 defer s1.Close() 74 75 options := make(map[string]interface{}) 76 if strings.HasPrefix(addr, "wss://") || strings.HasPrefix(addr, "tls+tcp://") { 77 options[mangos.OptionTLSConfig] = srvCfg 78 } 79 80 if err := s1.ListenOptions(addr, options); err != nil { 81 t.Errorf("Failed listening to %s: %v", addr, err) 82 return 83 } 84 85 if err := mangos.Device(s1, s1); err != nil { 86 t.Errorf("Device failed: %v", err) 87 return 88 } 89 90 RunTests(t, addr, deviceCaseClient()) 91 } 92 93 func testDevChain(t *testing.T, addr1 string, addr2 string, addr3 string) { 94 // This tests using multiple devices across a few transports. 95 // It looks like this: addr1->addr2->addr3 <==> addr3->addr2->addr1 96 var err error 97 s := make([]mangos.Socket, 5) 98 for i := 0; i < 5; i++ { 99 if s[i], err = xpair.NewSocket(); err != nil { 100 t.Errorf("Failed to open S1_1: %v", err) 101 return 102 } 103 defer s[i].Close() 104 } 105 106 if err = s[0].Listen(addr1); err != nil { 107 t.Errorf("s[0] Listen: %v", err) 108 return 109 } 110 if err = s[2].Listen(addr2); err != nil { 111 t.Errorf("s[2] Listen: %v", err) 112 return 113 } 114 if err = s[4].Listen(addr3); err != nil { 115 t.Errorf("s[4] Listen: %v", err) 116 return 117 } 118 if err = s[1].Dial(addr2); err != nil { 119 t.Errorf("s[1] Dial: %v", err) 120 return 121 } 122 if err = s[3].Dial(addr3); err != nil { 123 t.Errorf("s[3] Dial: %v", err) 124 return 125 } 126 if err = mangos.Device(s[0], s[1]); err != nil { 127 t.Errorf("s[0],s[1] Device: %v", err) 128 return 129 } 130 if err = mangos.Device(s[2], s[3]); err != nil { 131 t.Errorf("s[2],s[3] Device: %v", err) 132 return 133 } 134 if err = mangos.Device(s[4], nil); err != nil { 135 t.Errorf("s[4] Device: %v", err) 136 return 137 } 138 RunTests(t, addr1, deviceCaseClient()) 139 } 140 141 func TestDeviceChain(t *testing.T) { 142 testDevChain(t, AddrTestTCP(), AddrTestWS(), AddrTestInp()) 143 // Some platforms (windows) need a little time to wind up the close 144 time.Sleep(100 * time.Millisecond) 145 } 146 147 func TestDeviceLoopTCP(t *testing.T) { 148 testDevLoop(t, AddrTestTCP()) 149 } 150 151 func TestDeviceLoopInp(t *testing.T) { 152 testDevLoop(t, AddrTestInp()) 153 } 154 155 func TestDeviceLoopIPC(t *testing.T) { 156 testDevLoop(t, AddrTestIPC()) 157 } 158 159 func TestDeviceLoopTLS(t *testing.T) { 160 testDevLoop(t, AddrTestTLS()) 161 } 162 163 func TestDeviceLoopWS(t *testing.T) { 164 testDevLoop(t, AddrTestWS()) 165 } 166 167 func TestDeviceLoopWSS(t *testing.T) { 168 testDevLoop(t, AddrTestWSS()) 169 }