github.com/gdamore/mangos@v1.4.0/test/pair_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/pair" 22 ) 23 24 type pairTest struct { 25 T 26 } 27 28 func (pt *pairTest) Init(t *testing.T, addr string) bool { 29 var err error 30 if pt.Sock, err = pair.NewSocket(); err != nil { 31 pt.Errorf("NewSocket failed: %v", err) 32 return false 33 } 34 return pt.T.Init(t, addr) 35 } 36 37 func (pt *pairTest) SendHook(m *mangos.Message) bool { 38 m.Body = append(m.Body, byte(pt.GetSend())) 39 return pt.T.SendHook(m) 40 } 41 42 func (pt *pairTest) RecvHook(m *mangos.Message) bool { 43 if len(m.Body) != 1 { 44 pt.Errorf("Recv message length %d != 1", len(m.Body)) 45 return false 46 } 47 if m.Body[0] != byte(pt.GetRecv()) { 48 pt.Errorf("Wrong message: %d != %d", m.Body[0], byte(pt.GetRecv())) 49 return false 50 } 51 return pt.T.RecvHook(m) 52 } 53 54 func pairCases() []TestCase { 55 snd := &pairTest{} 56 snd.ID = 0 57 snd.MsgSize = 1 58 snd.WantTx = 200 59 snd.WantRx = 300 60 snd.Server = true 61 62 rcv := &pairTest{} 63 rcv.ID = 1 64 rcv.MsgSize = 1 65 rcv.WantTx = 300 66 rcv.WantRx = 200 67 68 return []TestCase{snd, rcv} 69 } 70 71 func TestPairInp(t *testing.T) { 72 RunTestsInp(t, pairCases()) 73 } 74 75 func TestPairTCP(t *testing.T) { 76 RunTestsTCP(t, pairCases()) 77 } 78 79 func TestPairIPC(t *testing.T) { 80 RunTestsIPC(t, pairCases()) 81 } 82 83 func TestPairTLS(t *testing.T) { 84 RunTestsTLS(t, pairCases()) 85 } 86 87 func TestPairWS(t *testing.T) { 88 RunTestsWS(t, pairCases()) 89 } 90 91 func TestPairWSS(t *testing.T) { 92 RunTestsWSS(t, pairCases()) 93 }