github.com/gdamore/mangos@v1.4.0/test/pubsub_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  	"bytes"
    19  	"testing"
    20  
    21  	"nanomsg.org/go-mangos"
    22  	"nanomsg.org/go-mangos/protocol/pub"
    23  	"nanomsg.org/go-mangos/protocol/sub"
    24  )
    25  
    26  var publish = []string{
    27  	"/some/like/it/hot",
    28  	"/some/where",
    29  	"/over/the",
    30  	"/rainbow",
    31  	"\\\\C\\SPOT\\RUN",
    32  	"The Quick Brown Fox",
    33  	"END"}
    34  
    35  type subTest struct {
    36  	T
    37  }
    38  
    39  type pubTest struct {
    40  	pubidx int
    41  	T
    42  }
    43  
    44  func (st *subTest) Init(t *testing.T, addr string) bool {
    45  	var err error
    46  	if st.Sock, err = sub.NewSocket(); err != nil {
    47  		st.Errorf("NewSocket(): %v", err)
    48  		return false
    49  	}
    50  	rv := st.T.Init(t, addr)
    51  	// We have to subscribe to the START message!
    52  	if !rv {
    53  		return false
    54  	}
    55  	err = st.Sock.SetOption(mangos.OptionSubscribe, []byte("START"))
    56  	if err != nil {
    57  		st.Errorf("Failed subscription to START: %v", err)
    58  		return false
    59  	}
    60  
    61  	err = st.Sock.SetOption(mangos.OptionSubscribe, []byte("END"))
    62  	if err != nil {
    63  		st.Errorf("Failed to subscribe to END: %v", err)
    64  		return false
    65  	}
    66  
    67  	err = st.Sock.SetOption(mangos.OptionSubscribe, []byte("/rain"))
    68  	if err != nil {
    69  		st.Errorf("Failed subscribing to the rain: %v", err)
    70  		return false
    71  	}
    72  
    73  	return true
    74  }
    75  
    76  func (pt *pubTest) Init(t *testing.T, addr string) bool {
    77  	pt.pubidx = 0
    78  	var err error
    79  	if pt.Sock, err = pub.NewSocket(); err != nil {
    80  		pt.Errorf("NewSocket(): %v", err)
    81  		return false
    82  	}
    83  	return pt.T.Init(t, addr)
    84  }
    85  
    86  func (st *subTest) RecvHook(m *mangos.Message) bool {
    87  	switch {
    88  	case bytes.HasPrefix(m.Body, []byte("END")):
    89  		st.BumpRecv()
    90  		return true
    91  	case bytes.HasPrefix(m.Body, []byte("/rain")):
    92  		st.BumpRecv()
    93  		return true
    94  	default:
    95  		st.Errorf("Unexpected message! %v", m)
    96  		return false
    97  	}
    98  }
    99  
   100  func (pt *pubTest) SendHook(m *mangos.Message) bool {
   101  	if pt.pubidx >= len(publish) {
   102  		pt.Errorf("Nothing left to send! (%d/%d)", pt.pubidx, len(publish))
   103  		return false
   104  	}
   105  	m.Body = append(m.Body, []byte(publish[pt.pubidx])...)
   106  	pt.Debugf("Sending %d, %s", pt.pubidx, string(m.Body))
   107  	pt.pubidx++
   108  	return pt.T.SendHook(m)
   109  }
   110  
   111  func pubCases() []TestCase {
   112  
   113  	nsub := 5
   114  	cases := make([]TestCase, nsub+1)
   115  
   116  	pub := &pubTest{}
   117  	pub.WantTx = int32(len(publish))
   118  	pub.WantRx = 0
   119  	pub.Server = true
   120  	cases[0] = pub
   121  
   122  	for i := 0; i < nsub; i++ {
   123  		sub := &subTest{}
   124  		sub.WantRx = 2
   125  		sub.ID = i + 1
   126  		cases[i+1] = sub
   127  	}
   128  
   129  	return cases
   130  
   131  }
   132  
   133  func TestPubSubTCP(t *testing.T) {
   134  	RunTestsTCP(t, pubCases())
   135  }
   136  
   137  func TestPubSubIPC(t *testing.T) {
   138  	RunTestsIPC(t, pubCases())
   139  }
   140  
   141  func TestPubSubInp(t *testing.T) {
   142  	RunTestsInp(t, pubCases())
   143  }
   144  
   145  func TestPubSubTLS(t *testing.T) {
   146  	RunTestsTLS(t, pubCases())
   147  }
   148  
   149  func TestPubSubWS(t *testing.T) {
   150  	RunTestsWS(t, pubCases())
   151  }
   152  
   153  func TestPubSubWSS(t *testing.T) {
   154  	RunTestsWSS(t, pubCases())
   155  }