github.com/gdamore/mangos@v1.4.0/test/survey_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 "encoding/binary" 19 "testing" 20 "time" 21 22 "nanomsg.org/go-mangos" 23 "nanomsg.org/go-mangos/protocol/respondent" 24 "nanomsg.org/go-mangos/protocol/surveyor" 25 ) 26 27 type surveyTest struct { 28 nresp int32 29 resp map[uint32]bool 30 T 31 } 32 33 type responderTest struct { 34 T 35 } 36 37 func (st *surveyTest) Init(t *testing.T, addr string) bool { 38 var err error 39 st.resp = make(map[uint32]bool) 40 if st.Sock, err = surveyor.NewSocket(); err != nil { 41 st.Errorf("NewSocket(): %v", err) 42 return false 43 } 44 return st.T.Init(t, addr) 45 } 46 47 func (st *surveyTest) WantRecvStart() bool { 48 return false 49 } 50 51 func (st *surveyTest) SendHook(m *mangos.Message) bool { 52 m.Body = m.Body[0:4] 53 binary.BigEndian.PutUint32(m.Body, uint32(st.GetSend())) 54 return st.T.SendHook(m) 55 } 56 57 func (st *surveyTest) RecvHook(m *mangos.Message) bool { 58 if len(m.Body) != 4 { 59 st.Errorf("Recv message length %d != 4", len(m.Body)) 60 return false 61 } 62 v := binary.BigEndian.Uint32(m.Body) 63 if st.resp[v] { 64 st.Logf("Duplicate response from id %d", v) 65 } else { 66 st.Logf("Response from id %d", v) 67 st.resp[v] = true 68 st.BumpRecv() 69 } 70 return true 71 } 72 73 func (rt *responderTest) Init(t *testing.T, addr string) bool { 74 var err error 75 if rt.Sock, err = respondent.NewSocket(); err != nil { 76 rt.Errorf("NewSocket(): %v", err) 77 return false 78 } 79 return rt.T.Init(t, addr) 80 } 81 82 func (rt *responderTest) RecvHook(m *mangos.Message) bool { 83 if len(m.Body) < 4 { 84 rt.Errorf("Recv message length %d < 4", len(m.Body)) 85 return false 86 } 87 rt.Logf("Got survey ID %d", binary.BigEndian.Uint32(m.Body)) 88 89 // reply 90 newm := rt.NewMessage() 91 newm.Body = newm.Body[0:4] 92 binary.BigEndian.PutUint32(newm.Body, uint32(rt.GetID())) 93 rt.SendMsg(newm) 94 return rt.T.RecvHook(m) 95 } 96 97 func (rt *responderTest) RecvStart() bool { 98 m, err := rt.RecvMsg() 99 if err != nil { 100 rt.Errorf("RecvMsg failed: %v", err) 101 return false 102 } 103 defer m.Free() 104 if _, ok := ParseStart(m); !ok { 105 rt.Errorf("Unexpected survey message: %v", m) 106 return false 107 } 108 109 rm := MakeStart(uint32(rt.GetID())) 110 rt.Debugf("Sending START reply") 111 rt.SendMsg(rm) 112 return true 113 } 114 115 func surveyCases() []TestCase { 116 var nresp int32 = 3 117 118 cases := make([]TestCase, nresp+1) 119 surv := &surveyTest{nresp: nresp} 120 surv.Server = true 121 surv.ID = 0 122 surv.MsgSize = 8 123 surv.WantTx = 1 124 surv.WantRx = int32(nresp) 125 surv.txdelay = 1000 * time.Millisecond 126 surv.Synch = true 127 surv.NReply = int(nresp) 128 cases[0] = surv 129 130 for i := 0; i < int(nresp); i++ { 131 resp := &responderTest{} 132 resp.ID = i + 1 133 resp.MsgSize = 8 134 resp.WantTx = 0 // reply is done in response to receipt 135 resp.WantRx = 1 136 cases[i+1] = resp 137 } 138 139 return cases 140 } 141 142 func TestSurveyTCP(t *testing.T) { 143 RunTestsTCP(t, surveyCases()) 144 } 145 146 func TestSurveyIPC(t *testing.T) { 147 RunTestsIPC(t, surveyCases()) 148 } 149 150 func TestSurveyInp(t *testing.T) { 151 RunTestsInp(t, surveyCases()) 152 } 153 154 func TestSurveyTLS(t *testing.T) { 155 RunTestsTLS(t, surveyCases()) 156 } 157 158 func TestSurveyWS(t *testing.T) { 159 RunTestsWS(t, surveyCases()) 160 } 161 162 func TestSurveyWSS(t *testing.T) { 163 RunTestsWSS(t, surveyCases()) 164 } 165 166 func TestSurveyTTLZero(t *testing.T) { 167 SetTTLZero(t, respondent.NewSocket) 168 } 169 170 func TestSurveyTTLNegative(t *testing.T) { 171 SetTTLNegative(t, respondent.NewSocket) 172 } 173 174 func TestSurveyTTLTooBig(t *testing.T) { 175 SetTTLTooBig(t, respondent.NewSocket) 176 } 177 178 func TestSurveyTTLNotInt(t *testing.T) { 179 SetTTLNotInt(t, respondent.NewSocket) 180 } 181 182 func TestSurveyTTLSet(t *testing.T) { 183 SetTTL(t, respondent.NewSocket) 184 } 185 186 func TestSurveyTTLDrop(t *testing.T) { 187 TTLDropTest(t, surveyor.NewSocket, respondent.NewSocket) 188 }