go.nanomsg.org/mangos/v3@v3.4.3-0.20240217232803-46464076f1f5/protocol/xpub/xpub_test.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 xpub
    16  
    17  import (
    18  	"testing"
    19  	"time"
    20  
    21  	"go.nanomsg.org/mangos/v3"
    22  	. "go.nanomsg.org/mangos/v3/internal/test"
    23  	_ "go.nanomsg.org/mangos/v3/transport/inproc"
    24  )
    25  
    26  func TestXPubRaw(t *testing.T) {
    27  	VerifyRaw(t, NewSocket)
    28  }
    29  
    30  func TestXPubNoRecv(t *testing.T) {
    31  	CannotRecv(t, NewSocket)
    32  }
    33  
    34  func TestXPubClosed(t *testing.T) {
    35  	VerifyClosedSend(t, NewSocket)
    36  	VerifyClosedClose(t, NewSocket)
    37  	VerifyClosedDial(t, NewSocket)
    38  	VerifyClosedListen(t, NewSocket)
    39  	VerifyClosedAddPipe(t, NewSocket)
    40  }
    41  
    42  func TestXPubOptions(t *testing.T) {
    43  	VerifyInvalidOption(t, NewSocket)
    44  	VerifyOptionQLen(t, NewSocket, mangos.OptionWriteQLen)
    45  }
    46  
    47  func TestXPubNonBlock(t *testing.T) {
    48  	maxqlen := 2
    49  
    50  	p, err := NewSocket()
    51  	MustSucceed(t, err)
    52  	MustNotBeNil(t, p)
    53  
    54  	MustSucceed(t, p.SetOption(mangos.OptionWriteQLen, maxqlen))
    55  	MustSucceed(t, p.Listen(AddrTestInp()))
    56  
    57  	msg := []byte{'A', 'B', 'C'}
    58  
    59  	for i := 0; i < maxqlen*10; i++ {
    60  		MustSucceed(t, p.Send(msg))
    61  	}
    62  	MustSucceed(t, p.Close())
    63  }
    64  
    65  func TestXPubNonBlock2(t *testing.T) {
    66  	self := GetSocket(t, NewSocket)
    67  	MustSucceed(t, self.SetOption(mangos.OptionWriteQLen, 2))
    68  
    69  	_, _ = MockConnect(t, self)
    70  
    71  	for i := 0; i < 100; i++ {
    72  		MustSendString(t, self, "yep")
    73  	}
    74  	MustSucceed(t, self.Close())
    75  }
    76  
    77  func TestXPubSendClose(t *testing.T) {
    78  	self := GetSocket(t, NewSocket)
    79  	MustSucceed(t, self.SetOption(mangos.OptionWriteQLen, 2))
    80  
    81  	_, _ = MockConnect(t, self)
    82  
    83  	time.AfterFunc(time.Millisecond*10, func() {
    84  		MustSucceed(t, self.Close())
    85  	})
    86  	for {
    87  		e := self.Send([]byte{})
    88  		if e != nil {
    89  			MustBeError(t, e, mangos.ErrClosed)
    90  			break
    91  		}
    92  	}
    93  	time.Sleep(time.Millisecond * 40)
    94  	// MustSucceed(t, self.Close())
    95  }
    96  
    97  func TestXPubSendClose2(t *testing.T) {
    98  	self := GetSocket(t, NewSocket)
    99  	MustSucceed(t, self.SetOption(mangos.OptionWriteQLen, 2))
   100  	_, _ = MockConnect(t, self)
   101  	time.Sleep(time.Millisecond * 10)
   102  	MustSucceed(t, self.Close())
   103  }
   104  
   105  func TestXPubRecvDiscard(t *testing.T) {
   106  	self := GetSocket(t, NewSocket)
   107  	MustSucceed(t, self.SetOption(mangos.OptionWriteQLen, 2))
   108  	mock, _ := MockConnect(t, self)
   109  	time.Sleep(time.Millisecond * 10)
   110  	MockMustSendStr(t, mock, "garbage", time.Second)
   111  	time.Sleep(time.Millisecond * 10)
   112  	MustSucceed(t, self.Close())
   113  }