github.com/gdamore/mangos@v1.4.0/test/reqrep_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 "testing" 19 20 "nanomsg.org/go-mangos" 21 "nanomsg.org/go-mangos/protocol/rep" 22 "nanomsg.org/go-mangos/protocol/req" 23 ) 24 25 type reqTest struct { 26 cur int32 27 tot int32 28 T 29 } 30 31 type repTest struct { 32 T 33 } 34 35 // For now, we take a very simple pairwise approach to req/rep. We should 36 // consider additional tests for raw mode multiple responders. 37 38 func (rt *reqTest) Init(t *testing.T, addr string) bool { 39 var err error 40 if rt.Sock, err = req.NewSocket(); err != nil { 41 rt.Errorf("NewSocket(): %v", err) 42 return false 43 } 44 rt.cur = 0 45 rt.tot = 0 46 return rt.T.Init(t, addr) 47 } 48 49 func (rt *reqTest) SendHook(m *mangos.Message) bool { 50 m.Body = append(m.Body, byte(rt.GetSend())) 51 rt.tot = rt.GetSend() 52 return rt.T.SendHook(m) 53 } 54 55 func (rt *reqTest) RecvHook(m *mangos.Message) bool { 56 if len(m.Body) != 1 { 57 rt.Errorf("Recv message length %d != 1", len(m.Body)) 58 return false 59 } 60 if m.Body[0] != byte(rt.GetRecv()) { 61 rt.Errorf("Wrong message: %d != %d", m.Body[0], byte(rt.GetRecv())) 62 return false 63 } 64 rt.tot++ 65 rt.cur++ 66 rt.T.RecvHook(m) 67 return rt.cur == rt.tot 68 } 69 70 func (rt *repTest) Init(t *testing.T, addr string) bool { 71 var err error 72 if rt.Sock, err = rep.NewSocket(); err != nil { 73 rt.Errorf("NewSocket(): %v", err) 74 return false 75 } 76 return rt.T.Init(t, addr) 77 } 78 79 func (rt *repTest) RecvHook(m *mangos.Message) bool { 80 if len(m.Body) != 1 { 81 rt.Errorf("Recv message length %d != 1", len(m.Body)) 82 return false 83 } 84 85 // reply 86 newm := rt.NewMessage() 87 newm.Body = append(newm.Body, m.Body...) 88 rt.SendMsg(newm) 89 return rt.T.RecvHook(m) 90 } 91 92 // NO SLOW START FOR REQ/REP PROTOCOLS!! 93 94 func (*repTest) WantRecvStart() bool { 95 return false 96 } 97 98 func (*reqTest) WantRecvStart() bool { 99 return false 100 } 101 102 func reqRepCases() []TestCase { 103 var nresp int32 104 105 nresp = 100 106 107 repc := &repTest{} 108 repc.Server = true 109 repc.ID = 0 110 repc.MsgSize = 1 111 repc.WantTx = 0 112 repc.WantRx = nresp 113 repc.debug = true 114 115 reqc := &reqTest{} 116 reqc.debug = true 117 reqc.ID = 1 118 reqc.MsgSize = 1 119 reqc.WantTx = nresp 120 reqc.WantRx = nresp 121 reqc.tot = nresp 122 reqc.Synch = true 123 reqc.NReply = 1 124 125 return []TestCase{repc, reqc} 126 } 127 128 func TestReqRepTCP(t *testing.T) { 129 RunTestsTCP(t, reqRepCases()) 130 } 131 132 func TestReqRepIPC(t *testing.T) { 133 RunTestsIPC(t, reqRepCases()) 134 } 135 136 func TestReqRepInp(t *testing.T) { 137 RunTestsInp(t, reqRepCases()) 138 } 139 140 func TestReqRepTLS(t *testing.T) { 141 RunTestsTLS(t, reqRepCases()) 142 } 143 144 func TestReqRepWS(t *testing.T) { 145 RunTestsWS(t, reqRepCases()) 146 } 147 148 func TestReqRepWSS(t *testing.T) { 149 RunTestsWSS(t, reqRepCases()) 150 } 151 152 func TestReqRepTTLZero(t *testing.T) { 153 SetTTLZero(t, rep.NewSocket) 154 } 155 156 func TestReqRepTTLNegative(t *testing.T) { 157 SetTTLNegative(t, rep.NewSocket) 158 } 159 160 func TestReqRepTTLTooBig(t *testing.T) { 161 SetTTLTooBig(t, rep.NewSocket) 162 } 163 164 func TestReqRepTTLNotInt(t *testing.T) { 165 SetTTLNotInt(t, rep.NewSocket) 166 } 167 168 func TestReqRepTTLSet(t *testing.T) { 169 SetTTL(t, rep.NewSocket) 170 } 171 172 func TestReqRepTTLDrop(t *testing.T) { 173 TTLDropTest(t, req.NewSocket, rep.NewSocket) 174 }