go.nanomsg.org/mangos/v3@v3.4.3-0.20240217232803-46464076f1f5/internal/test/device_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  	"sync"
    19  	"testing"
    20  	"time"
    21  
    22  	"go.nanomsg.org/mangos/v3"
    23  	"go.nanomsg.org/mangos/v3/protocol/pair"
    24  	"go.nanomsg.org/mangos/v3/protocol/xpair"
    25  	_ "go.nanomsg.org/mangos/v3/transport/all"
    26  )
    27  
    28  func TestDeviceBadPair(t *testing.T) {
    29  	s1 := GetMockSocketRaw(1, 1, "mock1", "mock1", true, nil)
    30  	defer MustClose(t, s1)
    31  	s2 := GetMockSocketRaw(2, 2, "mock2", "mock2", true, nil)
    32  	defer MustClose(t, s2)
    33  
    34  	MustBeError(t, mangos.Device(s1, s2), mangos.ErrBadProto)
    35  }
    36  
    37  func TestDeviceBadSingle(t *testing.T) {
    38  	s1 := GetMockSocketRaw(1, 2, "mock1", "mock2", true, nil)
    39  	defer MustClose(t, s1)
    40  	MustBeError(t, mangos.Device(s1, s1), mangos.ErrBadProto)
    41  }
    42  
    43  func TestDeviceFirstNil(t *testing.T) {
    44  	s1 := GetMockSocketRaw(1, 1, "m", "m", true, nil)
    45  	defer MustClose(t, s1)
    46  	MustSucceed(t, mangos.Device(nil, s1))
    47  }
    48  
    49  func TestDeviceSecondNil(t *testing.T) {
    50  	s1 := GetMockSocketRaw(1, 1, "m", "m", true, nil)
    51  	defer MustClose(t, s1)
    52  	MustSucceed(t, mangos.Device(s1, nil))
    53  }
    54  
    55  func TestDeviceBothNil(t *testing.T) {
    56  	MustBeError(t, mangos.Device(nil, nil), mangos.ErrClosed)
    57  }
    58  
    59  func TestDeviceRawError(t *testing.T) {
    60  	s0 := GetMockSocketRaw(1, 1, "m", "m", true, nil)
    61  	defer MustClose(t, s0)
    62  	s1 := GetMockSocketRaw(1, 1, "m", "m", nil, mangos.ErrCanceled)
    63  	defer MustClose(t, s1)
    64  	MustBeError(t, mangos.Device(s1, s0), mangos.ErrCanceled)
    65  	MustBeError(t, mangos.Device(s0, s1), mangos.ErrCanceled)
    66  	s2 := GetMockSocketRaw(1, 1, "m", "m", 5, nil)
    67  	defer MustClose(t, s2)
    68  	MustBeError(t, mangos.Device(s2, s0), mangos.ErrNotRaw)
    69  	MustBeError(t, mangos.Device(s0, s2), mangos.ErrNotRaw)
    70  }
    71  
    72  func TestDeviceCookedFirst(t *testing.T) {
    73  	s1 := GetMockSocketRaw(1, 2, "m1", "m2", false, nil)
    74  	defer MustClose(t, s1)
    75  	s2 := GetMockSocketRaw(2, 1, "m2", "m1", true, nil)
    76  	defer MustClose(t, s2)
    77  
    78  	MustBeError(t, mangos.Device(s1, s2), mangos.ErrNotRaw)
    79  }
    80  func TestDeviceCookedSecond(t *testing.T) {
    81  	s1 := GetMockSocketRaw(1, 2, "m1", "m2", true, nil)
    82  	defer MustClose(t, s1)
    83  	s2 := GetMockSocketRaw(2, 1, "m2", "m1", false, nil)
    84  	defer MustClose(t, s2)
    85  
    86  	MustBeError(t, mangos.Device(s1, s2), mangos.ErrNotRaw)
    87  }
    88  
    89  func TestDeviceCookedBoth(t *testing.T) {
    90  	s1 := GetMockSocketRaw(1, 2, "m1", "m2", false, nil)
    91  	defer MustClose(t, s1)
    92  	s2 := GetMockSocketRaw(2, 1, "m2", "m1", false, nil)
    93  	defer MustClose(t, s2)
    94  
    95  	MustBeError(t, mangos.Device(s1, s2), mangos.ErrNotRaw)
    96  }
    97  func TestDeviceCookedNeither(t *testing.T) {
    98  	s1 := GetMockSocketRaw(1, 2, "m1", "m2", true, nil)
    99  	defer MustClose(t, s1)
   100  	s2 := GetMockSocketRaw(2, 1, "m2", "m1", true, nil)
   101  	defer MustClose(t, s2)
   102  
   103  	MustSucceed(t, mangos.Device(s1, s2))
   104  }
   105  
   106  func TestDevicePass(t *testing.T) {
   107  	s1 := GetSocket(t, xpair.NewSocket)
   108  	defer MustClose(t, s1)
   109  	s2 := GetSocket(t, xpair.NewSocket)
   110  	defer MustClose(t, s2)
   111  	tx := GetSocket(t, pair.NewSocket)
   112  	defer MustClose(t, tx)
   113  	rx := GetSocket(t, pair.NewSocket)
   114  	defer MustClose(t, rx)
   115  
   116  	ConnectPair(t, tx, s1)
   117  	ConnectPair(t, rx, s2)
   118  
   119  	MustSucceed(t, mangos.Device(s1, s2))
   120  	var wg sync.WaitGroup
   121  	wg.Add(1)
   122  
   123  	pass := false
   124  	go func() {
   125  		defer wg.Done()
   126  		MustRecvString(t, rx, "ping")
   127  		pass = true
   128  	}()
   129  
   130  	MustSendString(t, tx, "ping")
   131  	wg.Wait()
   132  	MustBeTrue(t, pass)
   133  }
   134  
   135  func TestDeviceCloseSend(t *testing.T) {
   136  	s1 := GetSocket(t, xpair.NewSocket)
   137  	defer MustClose(t, s1)
   138  	s2 := GetSocket(t, xpair.NewSocket)
   139  	defer MustClose(t, s2)
   140  	tx := GetSocket(t, pair.NewSocket)
   141  	rx := GetSocket(t, pair.NewSocket)
   142  
   143  	ConnectPair(t, tx, s1)
   144  	ConnectPair(t, rx, s2)
   145  	var wg sync.WaitGroup
   146  
   147  	MustSucceed(t, mangos.Device(s1, s2))
   148  
   149  	pass := false
   150  	wg.Add(1)
   151  	go func() {
   152  		defer wg.Done()
   153  		for {
   154  			e := tx.Send([]byte{})
   155  			if e != nil {
   156  				MustBeError(t, e, mangos.ErrClosed)
   157  				pass = true
   158  				break
   159  			}
   160  		}
   161  	}()
   162  
   163  	time.Sleep(time.Millisecond * 100)
   164  	MustClose(t, rx)
   165  	time.Sleep(time.Millisecond * 20)
   166  	MustClose(t, tx)
   167  	wg.Wait()
   168  	MustBeTrue(t, pass)
   169  }