go.nanomsg.org/mangos/v3@v3.4.3-0.20240217232803-46464076f1f5/internal/test/operations.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  	"sync"
    19  	"testing"
    20  	"time"
    21  
    22  	"go.nanomsg.org/mangos/v3"
    23  )
    24  
    25  // CannotSend verifies that the socket cannot send.
    26  func CannotSend(t *testing.T, f func() (mangos.Socket, error)) {
    27  	s := GetSocket(t, f)
    28  
    29  	// Not all protocols support this option, but try.
    30  	_ = s.SetOption(mangos.OptionSendDeadline, time.Millisecond)
    31  
    32  	MustBeError(t, s.Send([]byte{0, 1, 2, 3}), mangos.ErrProtoOp)
    33  	MustSucceed(t, s.Close())
    34  }
    35  
    36  // CannotRecv verifies that the socket cannot recv.
    37  func CannotRecv(t *testing.T, f func() (mangos.Socket, error)) {
    38  	s := GetSocket(t, f)
    39  	_ = s.SetOption(mangos.OptionRecvDeadline, time.Millisecond)
    40  
    41  	v, err := s.Recv()
    42  	MustBeError(t, err, mangos.ErrProtoOp)
    43  	MustBeNil(t, v)
    44  	MustSucceed(t, s.Close())
    45  }
    46  
    47  // GetSocket returns a socket using the constructor function.
    48  func GetSocket(t *testing.T, f func() (mangos.Socket, error)) mangos.Socket {
    49  	s, err := f()
    50  	MustSucceed(t, err)
    51  	MustNotBeNil(t, s)
    52  	return s
    53  }
    54  
    55  // MustClose closes the socket.
    56  func MustClose(t *testing.T, s mangos.Socket) {
    57  	MustSucceed(t, s.Close())
    58  }
    59  
    60  // MustGetInfo returns the Info for the socket.
    61  func MustGetInfo(t *testing.T, f func() (mangos.Socket, error)) mangos.ProtocolInfo {
    62  	s := GetSocket(t, f)
    63  	id := s.Info()
    64  	MustClose(t, s)
    65  	return id
    66  }
    67  
    68  // ConnectPairVia connects two sockets using the given address.  The pipe event
    69  // hook is used for this operation, and the function does not return until both
    70  // sockets have seen the connection.
    71  func ConnectPairVia(t *testing.T, addr string, s1, s2 mangos.Socket, o1, o2 map[string]interface{}) {
    72  	wg1 := sync.WaitGroup{}
    73  	wg2 := sync.WaitGroup{}
    74  	wg1.Add(1)
    75  	wg2.Add(1)
    76  	h1 := s1.SetPipeEventHook(func(ev mangos.PipeEvent, p mangos.Pipe) {
    77  		if ev == mangos.PipeEventAttached {
    78  			wg1.Done()
    79  		}
    80  	})
    81  	h2 := s2.SetPipeEventHook(func(ev mangos.PipeEvent, p mangos.Pipe) {
    82  		if ev == mangos.PipeEventAttached {
    83  			wg2.Done()
    84  		}
    85  	})
    86  	MustSucceed(t, s1.ListenOptions(addr, o1))
    87  	MustSucceed(t, s2.SetOption(mangos.OptionDialAsynch, true))
    88  	MustSucceed(t, s2.DialOptions(addr, o2))
    89  
    90  	wg1.Wait()
    91  	wg2.Wait()
    92  	s1.SetPipeEventHook(h1)
    93  	s2.SetPipeEventHook(h2)
    94  }
    95  
    96  // ConnectPair is like ConnectPairVia but uses inproc.
    97  func ConnectPair(t *testing.T, s1 mangos.Socket, s2 mangos.Socket) {
    98  	ConnectPairVia(t, AddrTestInp(), s1, s2, nil, nil)
    99  }
   100  
   101  func MustRecv(t *testing.T, s mangos.Socket) []byte {
   102  	m, e := s.Recv()
   103  	MustSucceed(t, e)
   104  	MustNotBeNil(t, m)
   105  	return m
   106  }
   107  
   108  func MustNotRecv(t *testing.T, s mangos.Socket, err error) {
   109  	m, e := s.Recv()
   110  	MustBeError(t, e, err)
   111  	MustBeNil(t, m)
   112  }
   113  
   114  func MustRecvMsg(t *testing.T, s mangos.Socket) *mangos.Message {
   115  	m, e := s.RecvMsg()
   116  	MustSucceed(t, e)
   117  	MustNotBeNil(t, m)
   118  	return m
   119  }
   120  
   121  func MustSendMsg(t *testing.T, s mangos.Socket, m *mangos.Message) {
   122  	MustSucceed(t, s.SendMsg(m))
   123  }
   124  
   125  func MustSend(t *testing.T, s mangos.Socket, b []byte) {
   126  	MustSucceed(t, s.Send(b))
   127  }
   128  
   129  func MustSendString(t *testing.T, s mangos.Socket, m string) {
   130  	MustSend(t, s, []byte(m))
   131  }
   132  
   133  func MustRecvString(t *testing.T, s mangos.Socket, m string) {
   134  	b := MustRecv(t, s)
   135  	MustBeTrue(t, string(b) == m)
   136  }