github.com/gdamore/mangos@v1.4.0/test/push_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/pull"
    22  	"nanomsg.org/go-mangos/protocol/push"
    23  )
    24  
    25  type PushTest struct {
    26  	T
    27  }
    28  
    29  type PullTest struct {
    30  	T
    31  }
    32  
    33  func (pt *PushTest) Init(t *testing.T, addr string) bool {
    34  	var err error
    35  	if pt.Sock, err = push.NewSocket(); err != nil {
    36  		pt.Errorf("NewSocket(): %v", err)
    37  		return false
    38  	}
    39  	return pt.T.Init(t, addr)
    40  }
    41  
    42  func (pt *PushTest) SendHook(m *mangos.Message) bool {
    43  	m.Body = append(m.Body, byte(pt.GetSend()))
    44  	return pt.T.SendHook(m)
    45  }
    46  
    47  func (pt *PullTest) Init(t *testing.T, addr string) bool {
    48  	var err error
    49  	if pt.Sock, err = pull.NewSocket(); err != nil {
    50  		pt.Errorf("NewSocket(): %v", err)
    51  		return false
    52  	}
    53  	return pt.T.Init(t, addr)
    54  }
    55  
    56  func (pt *PullTest) RecvHook(m *mangos.Message) bool {
    57  	if len(m.Body) != 1 {
    58  		pt.Errorf("Recv message length %d != 1", len(m.Body))
    59  		return false
    60  	}
    61  	if m.Body[0] != byte(pt.GetRecv()) {
    62  		pt.Errorf("Wrong message: %d != %d", m.Body[0], byte(pt.GetRecv()))
    63  		return false
    64  	}
    65  	return pt.T.RecvHook(m)
    66  }
    67  
    68  func pushCases() []TestCase {
    69  	push := &PushTest{}
    70  	push.ID = 0
    71  	push.MsgSize = 1
    72  	push.WantTx = 200
    73  	push.WantRx = 0
    74  	push.Server = true
    75  
    76  	pull := &PullTest{}
    77  	pull.ID = 1
    78  	pull.MsgSize = 1
    79  	pull.WantTx = 0
    80  	pull.WantRx = 200
    81  
    82  	return []TestCase{push, pull}
    83  }
    84  
    85  func TestPushPullInp(t *testing.T) {
    86  	RunTestsInp(t, pushCases())
    87  }
    88  
    89  func TestPushPullTCP(t *testing.T) {
    90  	RunTestsTCP(t, pushCases())
    91  }
    92  
    93  func TestPushPullIPC(t *testing.T) {
    94  	RunTestsIPC(t, pushCases())
    95  }
    96  
    97  func TestPushPullTLS(t *testing.T) {
    98  	RunTestsTLS(t, pushCases())
    99  }
   100  
   101  func TestPushPullWS(t *testing.T) {
   102  	RunTestsWS(t, pushCases())
   103  }
   104  
   105  func TestPushPullWSS(t *testing.T) {
   106  	RunTestsWSS(t, pushCases())
   107  }