go.nanomsg.org/mangos/v3@v3.4.3-0.20240217232803-46464076f1f5/internal/test/listener_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 test 16 17 import ( 18 "reflect" 19 "testing" 20 "time" 21 22 "go.nanomsg.org/mangos/v3" 23 _ "go.nanomsg.org/mangos/v3/transport/inproc" 24 ) 25 26 func TestListenerBadScheme(t *testing.T) { 27 self := GetMockSocket() 28 defer MustClose(t, self) 29 30 l, e := self.NewListener("bad://nothere", nil) 31 MustBeError(t, e, mangos.ErrBadTran) 32 MustBeTrue(t, l == nil) 33 34 // Malformed, needs :// bit 35 l, e = self.NewListener("inproc:nothere", nil) 36 MustBeError(t, e, mangos.ErrBadTran) 37 MustBeTrue(t, l == nil) 38 } 39 40 func TestListenerAddress(t *testing.T) { 41 AddMockTransport() 42 self := GetMockSocket() 43 defer MustClose(t, self) 44 45 l, e := self.NewListener(AddrMock(), nil) 46 MustSucceed(t, e) 47 MustBeTrue(t, l.Address() == AddrMock()) 48 } 49 50 func TestListenerSocketOptions(t *testing.T) { 51 AddMockTransport() 52 53 VerifyOptionInt(t, NewMockSocket, mangos.OptionMaxRecvSize) 54 } 55 56 func TestListenerOptions(t *testing.T) { 57 AddMockTransport() 58 sock := GetMockSocket() 59 defer MustClose(t, sock) 60 61 l, e := sock.NewListener(AddrMock(), nil) 62 MustSucceed(t, e) 63 MustBeTrue(t, l != nil) 64 65 MustBeError(t, l.SetOption("bogus", nil), mangos.ErrBadOption) 66 _, e = l.GetOption("bogus") 67 MustBeError(t, e, mangos.ErrBadOption) 68 69 val, e := l.GetOption(mangos.OptionMaxRecvSize) 70 MustSucceed(t, e) 71 MustBeTrue(t, reflect.TypeOf(val) == reflect.TypeOf(0)) 72 73 val, e = l.GetOption("mockError") 74 MustBeError(t, e, mangos.ErrProtoState) 75 MustBeTrue(t, val == nil) 76 77 MustBeError(t, l.SetOption(mangos.OptionMaxRecvSize, "a"), mangos.ErrBadValue) 78 MustBeError(t, l.SetOption(mangos.OptionMaxRecvSize, -100), mangos.ErrBadValue) 79 MustBeError(t, l.SetOption("mockError", mangos.ErrCanceled), mangos.ErrCanceled) 80 81 MustSucceed(t, l.SetOption(mangos.OptionMaxRecvSize, 1024)) 82 val, e = l.GetOption(mangos.OptionMaxRecvSize) 83 MustSucceed(t, e) 84 sz, ok := val.(int) 85 MustBeTrue(t, ok) 86 MustBeTrue(t, sz == 1024) 87 } 88 89 func TestListenerOptionsMap(t *testing.T) { 90 AddMockTransport() 91 sock := GetMockSocket() 92 defer MustClose(t, sock) 93 addr := AddrMock() 94 95 opts := make(map[string]interface{}) 96 opts[mangos.OptionMaxRecvSize] = "garbage" 97 l, e := sock.NewListener(addr, opts) 98 MustBeError(t, e, mangos.ErrBadValue) 99 MustBeTrue(t, l == nil) 100 opts[mangos.OptionMaxRecvSize] = -1 101 l, e = sock.NewListener(addr, opts) 102 MustBeError(t, e, mangos.ErrBadValue) 103 MustBeTrue(t, l == nil) 104 105 opts = make(map[string]interface{}) 106 opts["JUNKOPT"] = "yes" 107 l, e = sock.NewListener(addr, opts) 108 MustBeError(t, e, mangos.ErrBadOption) 109 MustBeTrue(t, l == nil) 110 111 opts = make(map[string]interface{}) 112 opts["mockError"] = mangos.ErrCanceled 113 l, e = sock.NewListener(addr, opts) 114 MustBeError(t, e, mangos.ErrCanceled) 115 MustBeTrue(t, l == nil) 116 117 // Now a good option 118 opts = make(map[string]interface{}) 119 opts[mangos.OptionMaxRecvSize] = 3172 120 l, e = sock.NewListener(addr, opts) 121 MustSucceed(t, e) 122 MustBeTrue(t, l != nil) 123 v, e := l.GetOption(mangos.OptionMaxRecvSize) 124 MustSucceed(t, e) 125 sz, ok := v.(int) 126 MustBeTrue(t, ok) 127 MustBeTrue(t, sz == 3172) 128 129 } 130 131 func TestListenerOptionsInherit(t *testing.T) { 132 AddMockTransport() 133 sock := GetMockSocket() 134 defer MustClose(t, sock) 135 addr := AddrMock() 136 137 // This should force listener not to alloc (bad option value) 138 MustSucceed(t, sock.SetOption(mangos.OptionMaxRecvSize, 1001)) 139 l, e := sock.NewListener(addr, nil) 140 MustBeError(t, e, mangos.ErrBadValue) 141 MustBeTrue(t, l == nil) 142 MustSucceed(t, sock.SetOption(mangos.OptionMaxRecvSize, 1002)) 143 l, e = sock.NewListener(addr, nil) 144 MustSucceed(t, e) 145 MustBeTrue(t, l != nil) 146 147 MustSucceed(t, sock.SetOption(mangos.OptionMaxRecvSize, 500)) 148 v, e := l.GetOption(mangos.OptionMaxRecvSize) 149 MustSucceed(t, e) 150 MustBeTrue(t, v.(int) == 500) 151 } 152 153 func TestListenerClosed(t *testing.T) { 154 AddMockTransport() 155 sock := GetMockSocket() 156 defer MustClose(t, sock) 157 158 l, e := sock.NewListener(AddrMock(), nil) 159 MustSucceed(t, e) 160 MustBeTrue(t, l != nil) 161 162 MustSucceed(t, l.Close()) 163 164 MustBeError(t, l.Listen(), mangos.ErrClosed) 165 MustBeError(t, l.Close(), mangos.ErrClosed) 166 } 167 168 func TestListenerCloseAbort(t *testing.T) { 169 addr := AddrTestInp() 170 sock := GetMockSocket() 171 defer MustClose(t, sock) 172 173 l, e := sock.NewListener(addr, nil) 174 MustSucceed(t, e) 175 MustBeTrue(t, l != nil) 176 177 MustSucceed(t, l.Listen()) 178 time.Sleep(time.Millisecond * 50) 179 MustSucceed(t, l.Close()) 180 } 181 182 func TestListenerAcceptClose(t *testing.T) { 183 AddMockTransport() 184 sock := GetMockSocket() 185 defer MustClose(t, sock) 186 187 l, ml := GetMockListener(t, sock) 188 189 MustSucceed(t, l.Listen()) 190 MustSucceed(t, ml.Close()) 191 } 192 193 func TestListenerListenFail(t *testing.T) { 194 AddMockTransport() 195 sock := GetMockSocket() 196 defer MustClose(t, sock) 197 198 l, ml := GetMockListener(t, sock) 199 200 ml.InjectError(mangos.ErrCanceled) 201 MustBeError(t, l.Listen(), mangos.ErrCanceled) 202 } 203 204 func TestListenerPipe(t *testing.T) { 205 sock1 := GetMockSocket() 206 defer MustClose(t, sock1) 207 sock2 := GetMockSocket() 208 defer MustClose(t, sock2) 209 addr := AddrTestInp() 210 211 MustSucceed(t, sock1.SetOption(mangos.OptionRecvDeadline, time.Second)) 212 MustSucceed(t, sock2.SetOption(mangos.OptionSendDeadline, time.Second)) 213 214 l, e := sock1.NewListener(addr, nil) 215 MustSucceed(t, e) 216 MustSucceed(t, l.Listen()) 217 MustSucceed(t, sock2.Dial(addr)) 218 219 MustSendString(t, sock2, "junk") 220 m := MustRecvMsg(t, sock1) 221 222 MustBeTrue(t, m.Pipe.Dialer() == nil) 223 MustBeTrue(t, m.Pipe.Listener() == l) 224 MustBeTrue(t, m.Pipe.Address() == addr) 225 m.Free() 226 } 227 228 func TestListenerAcceptOne(t *testing.T) { 229 AddMockTransport() 230 sock := GetMockSocket() 231 defer MustClose(t, sock) 232 233 l, ml := GetMockListener(t, sock) 234 MustSucceed(t, l.Listen()) 235 236 mp := ml.NewPipe(sock.Info().Peer) 237 MustSucceed(t, ml.AddPipe(mp)) 238 time.Sleep(time.Millisecond * 50) 239 MustSucceed(t, ml.Close()) 240 } 241 242 func TestListenerAcceptFail(t *testing.T) { 243 AddMockTransport() 244 sock := GetMockSocket() 245 defer MustClose(t, sock) 246 247 l, ml := GetMockListener(t, sock) 248 249 MustSucceed(t, l.Listen()) 250 ml.InjectError(mangos.ErrCanceled) 251 time.Sleep(time.Millisecond * 50) 252 MustSucceed(t, ml.Close()) 253 } 254 255 func TestListenerReuse(t *testing.T) { 256 AddMockTransport() 257 sock := GetMockSocket() 258 defer MustClose(t, sock) 259 260 l, e := sock.NewListener(AddrMock(), nil) 261 MustSucceed(t, e) 262 MustBeTrue(t, l != nil) 263 264 MustSucceed(t, l.Listen()) 265 MustBeError(t, l.Listen(), mangos.ErrAddrInUse) 266 267 MustSucceed(t, l.Close()) 268 }