nanomsg.org/go/mangos/v2@v2.0.9-0.20200203084354-8a092611e461/protocol/pub/pub_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 pub
    16  
    17  import (
    18  	"nanomsg.org/go/mangos/v2/protocol/sub"
    19  	"sync"
    20  	"testing"
    21  	"time"
    22  
    23  	"nanomsg.org/go/mangos/v2"
    24  	. "nanomsg.org/go/mangos/v2/internal/test"
    25  	_ "nanomsg.org/go/mangos/v2/transport/inproc"
    26  )
    27  
    28  func TestPubIdentity(t *testing.T) {
    29  	s, e := NewSocket()
    30  	MustSucceed(t, e)
    31  	id := s.Info()
    32  	MustBeTrue(t, id.Self == mangos.ProtoPub)
    33  	MustBeTrue(t, id.Peer == mangos.ProtoSub)
    34  	MustBeTrue(t, id.SelfName == "pub")
    35  	MustBeTrue(t, id.PeerName == "sub")
    36  	MustSucceed(t, s.Close())
    37  }
    38  
    39  func TestPubCooked(t *testing.T) {
    40  	VerifyCooked(t, NewSocket)
    41  }
    42  
    43  func TestPubNoRecv(t *testing.T) {
    44  	CannotRecv(t, NewSocket)
    45  }
    46  
    47  func TestPubClosed(t *testing.T) {
    48  	VerifyClosedSend(t, NewSocket)
    49  	VerifyClosedClose(t, NewSocket)
    50  	VerifyClosedDial(t, NewSocket)
    51  	VerifyClosedListen(t, NewSocket)
    52  }
    53  
    54  func TestPubOptions(t *testing.T) {
    55  	VerifyInvalidOption(t, NewSocket)
    56  	// Note we can't test the actual impact yet, as we need to have
    57  	// a way to block a sending pipe.
    58  	VerifyOptionQLen(t, NewSocket, mangos.OptionWriteQLen)
    59  }
    60  
    61  func TestPubNonBlock(t *testing.T) {
    62  	maxqlen := 2
    63  
    64  	p, err := NewSocket()
    65  	MustSucceed(t, err)
    66  	MustNotBeNil(t, p)
    67  
    68  	MustSucceed(t, p.SetOption(mangos.OptionWriteQLen, maxqlen))
    69  	MustSucceed(t, p.Listen(AddrTestInp()))
    70  
    71  	msg := []byte{'A', 'B', 'C'}
    72  
    73  	for i := 0; i < maxqlen*10; i++ {
    74  		MustSucceed(t, p.Send(msg))
    75  	}
    76  	MustSucceed(t, p.Close())
    77  }
    78  
    79  func TestPubBroadcast(t *testing.T) {
    80  	p, err := NewSocket()
    81  	MustSucceed(t, err)
    82  	MustNotBeNil(t, p)
    83  
    84  	s1, err := sub.NewSocket()
    85  	MustSucceed(t, err)
    86  	MustNotBeNil(t, s1)
    87  
    88  	s2, err := sub.NewSocket()
    89  	MustSucceed(t, err)
    90  	MustNotBeNil(t, s2)
    91  
    92  	a := AddrTestInp()
    93  	MustSucceed(t, p.Listen(a))
    94  	MustSucceed(t, s1.Dial(a))
    95  	MustSucceed(t, s2.Dial(a))
    96  
    97  	time.Sleep(time.Millisecond * 50)
    98  
    99  	MustSucceed(t, s1.SetOption(mangos.OptionSubscribe, "topic1"))
   100  	MustSucceed(t, s2.SetOption(mangos.OptionSubscribe, "topic2"))
   101  	MustSucceed(t, s1.SetOption(mangos.OptionSubscribe, "both"))
   102  	MustSucceed(t, s2.SetOption(mangos.OptionSubscribe, "both"))
   103  	MustSucceed(t, s1.SetOption(mangos.OptionRecvDeadline, time.Millisecond*50))
   104  	MustSucceed(t, s2.SetOption(mangos.OptionRecvDeadline, time.Millisecond*50))
   105  
   106  	MustSucceed(t, p.Send([]byte("topic1one")))
   107  	MustSucceed(t, p.Send([]byte("neither")))
   108  	MustSucceed(t, p.Send([]byte("topic2two")))
   109  	MustSucceed(t, p.Send([]byte("bothan")))
   110  	MustSucceed(t, p.Send([]byte("topic2again")))
   111  	MustSucceed(t, p.Send([]byte("topic1again")))
   112  	MustSucceed(t, p.Send([]byte("garbage")))
   113  
   114  	var wg sync.WaitGroup
   115  	wg.Add(2)
   116  	pass1 := false
   117  	pass2 := false
   118  
   119  	go func() { // Subscriber one
   120  		defer wg.Done()
   121  		v, e := s1.Recv()
   122  		MustSucceed(t, e)
   123  		MustBeTrue(t, string(v) == "topic1one")
   124  
   125  		v, e = s1.Recv()
   126  		MustSucceed(t, e)
   127  		MustBeTrue(t, string(v) == "bothan")
   128  
   129  		v, e = s1.Recv()
   130  		MustSucceed(t, e)
   131  		MustBeTrue(t, string(v) == "topic1again")
   132  
   133  		v, e = s1.Recv()
   134  		MustBeError(t, e, mangos.ErrRecvTimeout)
   135  		MustBeNil(t, v)
   136  		pass1 = true
   137  	}()
   138  
   139  	go func() { // Subscriber two
   140  		defer wg.Done()
   141  		v, e := s2.Recv()
   142  		MustSucceed(t, e)
   143  		MustBeTrue(t, string(v) == "topic2two")
   144  
   145  		v, e = s2.Recv()
   146  		MustSucceed(t, e)
   147  		MustBeTrue(t, string(v) == "bothan")
   148  
   149  		v, e = s2.Recv()
   150  		MustSucceed(t, e)
   151  		MustBeTrue(t, string(v) == "topic2again")
   152  
   153  		v, e = s2.Recv()
   154  		MustBeError(t, e, mangos.ErrRecvTimeout)
   155  		MustBeNil(t, v)
   156  		pass2 = true
   157  	}()
   158  
   159  	wg.Wait()
   160  
   161  	MustBeTrue(t, pass1)
   162  	MustBeTrue(t, pass2)
   163  
   164  	MustSucceed(t, p.Close())
   165  	MustSucceed(t, s1.Close())
   166  	MustSucceed(t, s2.Close())
   167  }