nanomsg.org/go/mangos/v2@v2.0.9-0.20200203084354-8a092611e461/protocol/xreq/xreq_test.go (about) 1 // Copyright 2019 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 xreq 16 17 import ( 18 "nanomsg.org/go/mangos/v2" 19 "nanomsg.org/go/mangos/v2/protocol/rep" 20 "nanomsg.org/go/mangos/v2/protocol/req" 21 "nanomsg.org/go/mangos/v2/protocol/xrep" 22 "sync" 23 "testing" 24 "time" 25 26 . "nanomsg.org/go/mangos/v2/internal/test" 27 _ "nanomsg.org/go/mangos/v2/transport/inproc" 28 ) 29 30 func TestXReqIdentity(t *testing.T) { 31 id := MustGetInfo(t, NewSocket) 32 MustBeTrue(t, id.Self == mangos.ProtoReq) 33 MustBeTrue(t, id.SelfName == "req") 34 MustBeTrue(t, id.Peer == mangos.ProtoRep) 35 MustBeTrue(t, id.PeerName == "rep") 36 } 37 38 func TestXReqRaw(t *testing.T) { 39 VerifyRaw(t, NewSocket) 40 } 41 42 func TestXReqClosed(t *testing.T) { 43 VerifyClosedRecv(t, NewSocket) 44 VerifyClosedSend(t, NewSocket) 45 VerifyClosedClose(t, NewSocket) 46 VerifyClosedDial(t, NewSocket) 47 VerifyClosedListen(t, NewSocket) 48 VerifyClosedAddPipe(t, NewSocket) 49 } 50 51 func TestXReqOptions(t *testing.T) { 52 VerifyInvalidOption(t, NewSocket) 53 VerifyOptionDuration(t, NewSocket, mangos.OptionRecvDeadline) 54 VerifyOptionDuration(t, NewSocket, mangos.OptionSendDeadline) 55 VerifyOptionInt(t, NewSocket, mangos.OptionReadQLen) 56 VerifyOptionInt(t, NewSocket, mangos.OptionWriteQLen) 57 VerifyOptionBool(t, NewSocket, mangos.OptionBestEffort) 58 } 59 60 func TestXReqBestEffort(t *testing.T) { 61 timeout := time.Millisecond 62 msg := []byte{'0', '1', '2', '3'} 63 64 s := GetSocket(t, NewSocket) 65 MustSucceed(t, s.SetOption(mangos.OptionWriteQLen, 0)) 66 MustSucceed(t, s.SetOption(mangos.OptionSendDeadline, timeout)) 67 MustSucceed(t, s.Listen(AddrTestInp())) 68 MustSucceed(t, s.SetOption(mangos.OptionBestEffort, true)) 69 MustSucceed(t, s.Send(msg)) 70 MustSucceed(t, s.Send(msg)) 71 MustSucceed(t, s.SetOption(mangos.OptionBestEffort, false)) 72 MustBeError(t, s.Send(msg), mangos.ErrSendTimeout) 73 MustBeError(t, s.Send(msg), mangos.ErrSendTimeout) 74 } 75 76 func TestXReqDevice(t *testing.T) { 77 r1, e := req.NewSocket() 78 MustSucceed(t, e) 79 r2, e := rep.NewSocket() 80 MustSucceed(t, e) 81 82 r3, e := xrep.NewSocket() 83 MustSucceed(t, e) 84 r4, e := NewSocket() 85 MustSucceed(t, e) 86 87 a1 := AddrTestInp() 88 a2 := AddrTestInp() 89 90 MustSucceed(t, mangos.Device(r3, r4)) 91 92 // r1 -> r3 / r4 -> r2 93 MustSucceed(t, r3.Listen(a1)) 94 MustSucceed(t, r4.Listen(a2)) 95 96 MustSucceed(t, r1.Dial(a1)) 97 MustSucceed(t, r2.Dial(a2)) 98 99 MustSucceed(t, r1.SetOption(mangos.OptionRecvDeadline, time.Second)) 100 MustSucceed(t, r1.SetOption(mangos.OptionSendDeadline, time.Second)) 101 MustSucceed(t, r2.SetOption(mangos.OptionRecvDeadline, time.Second)) 102 MustSucceed(t, r2.SetOption(mangos.OptionSendDeadline, time.Second)) 103 MustSucceed(t, r1.Send([]byte("PING"))) 104 ping, e := r2.Recv() 105 MustSucceed(t, e) 106 MustBeTrue(t, string(ping) == "PING") 107 MustSucceed(t, r2.Send([]byte("PONG"))) 108 pong, e := r1.Recv() 109 MustSucceed(t, e) 110 MustBeTrue(t, string(pong) == "PONG") 111 _ = r1.Close() 112 _ = r2.Close() 113 _ = r3.Close() 114 _ = r4.Close() 115 } 116 117 func TestXReqRecvDeadline(t *testing.T) { 118 s, e := NewSocket() 119 MustSucceed(t, e) 120 e = s.SetOption(mangos.OptionRecvDeadline, time.Millisecond) 121 MustSucceed(t, e) 122 m, e := s.RecvMsg() 123 MustFail(t, e) 124 MustBeTrue(t, e == mangos.ErrRecvTimeout) 125 MustBeNil(t, m) 126 _ = s.Close() 127 } 128 129 func TestXReqResizeSendDiscard(t *testing.T) { 130 self := GetSocket(t, NewSocket) 131 MustSucceed(t, self.SetOption(mangos.OptionWriteQLen, 1)) 132 MustSucceed(t, self.Send([]byte{})) 133 MustSucceed(t, self.SetOption(mangos.OptionSendDeadline, time.Millisecond*10)) 134 MustBeError(t, self.Send([]byte{}), mangos.ErrSendTimeout) 135 MustSucceed(t, self.SetOption(mangos.OptionSendDeadline, time.Millisecond*50)) 136 time.AfterFunc(time.Millisecond*5, func() { 137 MustSucceed(t, self.SetOption(mangos.OptionWriteQLen, 1)) 138 }) 139 MustSucceed(t, self.Send([]byte{})) 140 MustSucceed(t, self.Close()) 141 } 142 143 func TestXReqResizeRecvOk(t *testing.T) { 144 self := GetSocket(t, NewSocket) 145 mock, _ := MockConnect(t, self) 146 var wg sync.WaitGroup 147 wg.Add(1) 148 pass := false 149 MustSucceed(t, self.SetOption(mangos.OptionRecvDeadline, time.Second)) 150 go func() { 151 defer wg.Done() 152 _ = MustRecvMsg(t, self) 153 pass = true 154 }() 155 time.Sleep(time.Millisecond * 50) 156 MustSucceed(t, self.SetOption(mangos.OptionReadQLen, 1)) 157 time.Sleep(time.Millisecond * 10) 158 MockMustSend(t, mock, []byte{0x80, 0, 0, 1}, time.Second) 159 wg.Wait() 160 MustSucceed(t, self.Close()) 161 MustBeTrue(t, pass) 162 } 163 164 func TestXReqRecvNoHeader(t *testing.T) { 165 self := GetSocket(t, NewSocket) 166 mock, _ := MockConnect(t, self) 167 168 MustSucceed(t, self.SetOption(mangos.OptionReadQLen, 2)) 169 MustSucceed(t, self.SetOption(mangos.OptionRecvDeadline, time.Millisecond*50)) 170 MockMustSend(t, mock, []byte{}, time.Second) 171 MustNotRecv(t, self, mangos.ErrRecvTimeout) 172 MustSucceed(t, self.Close()) 173 } 174 175 func TestXReqRecvResizeDiscard(t *testing.T) { 176 self := GetSocket(t, NewSocket) 177 defer MustClose(t, self) 178 mock, _ := MockConnect(t, self) 179 MustSucceed(t, self.SetOption(mangos.OptionReadQLen, 1)) 180 181 var wg sync.WaitGroup 182 wg.Add(1) 183 184 go func() { 185 defer wg.Done() 186 for { 187 m := mangos.NewMessage(0) 188 m.Body = append(m.Body, 0x80, 0, 0, 1) 189 e := mock.MockSendMsg(m, time.Second) 190 if e != nil { 191 MustBeError(t, e, mangos.ErrClosed) 192 break 193 } 194 195 } 196 }() 197 time.Sleep(time.Millisecond * 50) 198 MustSucceed(t, self.SetOption(mangos.OptionReadQLen, 2)) 199 time.Sleep(time.Millisecond * 50) 200 MustSucceed(t, mock.Close()) 201 wg.Wait() 202 } 203 204 func TestXReqCloseRecv(t *testing.T) { 205 self := GetSocket(t, NewSocket) 206 MustSucceed(t, self.SetOption(mangos.OptionReadQLen, 0)) 207 mock, pipe := MockConnect(t, self) 208 MockMustSend(t, mock, []byte{0x80, 0, 0, 1}, time.Second) 209 time.Sleep(time.Millisecond * 10) 210 MustSucceed(t, pipe.Close()) 211 time.Sleep(time.Millisecond * 10) 212 MustSucceed(t, self.Close()) 213 } 214 215 func TestXReqCloseSend0(t *testing.T) { 216 self := GetSocket(t, NewSocket) 217 _, pipe := MockConnect(t, self) 218 time.Sleep(time.Millisecond * 10) 219 MustSucceed(t, pipe.Close()) 220 time.Sleep(time.Millisecond * 10) 221 } 222 223 func TestXReqCloseSend1(t *testing.T) { 224 self := GetSocket(t, NewSocket) 225 _, _ = MockConnect(t, self) 226 time.Sleep(time.Millisecond * 10) 227 MustSucceed(t, self.Close()) 228 time.Sleep(time.Millisecond * 10) 229 } 230 231 func TestXReqCloseSend2(t *testing.T) { 232 self := GetSocket(t, NewSocket) 233 MustSucceed(t, self.SetOption(mangos.OptionWriteQLen, 2)) 234 _, _ = MockConnect(t, self) 235 MustSucceed(t, self.Send([]byte{0x80, 1, 1, 1})) 236 MustSucceed(t, self.Send([]byte{0x80, 1, 1, 2})) 237 time.Sleep(time.Millisecond * 10) 238 MustSucceed(t, self.Close()) 239 time.Sleep(time.Millisecond * 10) 240 }