github.com/gdamore/mangos@v1.4.0/test/reqretry_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  	"testing"
    19  
    20  	"time"
    21  
    22  	"nanomsg.org/go-mangos"
    23  	"nanomsg.org/go-mangos/protocol/rep"
    24  	"nanomsg.org/go-mangos/protocol/req"
    25  	"nanomsg.org/go-mangos/transport/inproc"
    26  
    27  	. "github.com/smartystreets/goconvey/convey"
    28  )
    29  
    30  func TestReqRetry(t *testing.T) {
    31  	Convey("Testing Req Retry", t, func() {
    32  		addr := "inproc://port"
    33  
    34  		// Let's first try request issued with no connection, and
    35  		// completing immediately after connect is established.
    36  		// Req will have multiple connections to separate servers,
    37  		Convey("Given a REQ & REP sockets", func() {
    38  			sockreq, err := req.NewSocket()
    39  			So(err, ShouldBeNil)
    40  			So(sockreq, ShouldNotBeNil)
    41  
    42  			defer sockreq.Close()
    43  			sockreq.AddTransport(inproc.NewTransport())
    44  
    45  			sockrep, err := rep.NewSocket()
    46  			So(err, ShouldBeNil)
    47  			So(sockrep, ShouldNotBeNil)
    48  			defer sockrep.Close()
    49  			sockrep.AddTransport(inproc.NewTransport())
    50  
    51  			d, err := sockreq.NewDialer(addr, nil)
    52  			So(err, ShouldBeNil)
    53  			So(d, ShouldNotBeNil)
    54  
    55  			l, err := sockrep.NewListener(addr, nil)
    56  			So(err, ShouldBeNil)
    57  			So(l, ShouldNotBeNil)
    58  
    59  			err = d.Dial()
    60  			So(err, ShouldBeNil)
    61  
    62  			Convey("A request is issued on late server connect", func() {
    63  				m := mangos.NewMessage(0)
    64  				m.Body = append(m.Body, []byte("hello")...)
    65  				err = sockreq.SendMsg(m)
    66  				So(err, ShouldBeNil)
    67  
    68  				err = l.Listen()
    69  				So(err, ShouldBeNil)
    70  
    71  				m, err = sockrep.RecvMsg()
    72  				So(m, ShouldNotBeNil)
    73  				So(err, ShouldBeNil)
    74  
    75  				m.Body = append(m.Body, []byte(" there")...)
    76  				err = sockrep.SendMsg(m)
    77  				So(err, ShouldBeNil)
    78  
    79  				m, err = sockreq.RecvMsg()
    80  				So(m, ShouldNotBeNil)
    81  				So(err, ShouldBeNil)
    82  
    83  				m.Free()
    84  			})
    85  
    86  			Convey("A request is reissued on server re-connect", func() {
    87  
    88  				rep2, err := rep.NewSocket()
    89  				So(err, ShouldBeNil)
    90  				So(rep2, ShouldNotBeNil)
    91  				defer rep2.Close()
    92  				rep2.AddTransport(inproc.NewTransport())
    93  
    94  				l2, err := rep2.NewListener(addr, nil)
    95  				So(err, ShouldBeNil)
    96  				So(l2, ShouldNotBeNil)
    97  
    98  				err = l.Listen()
    99  				time.Sleep(time.Millisecond * 50)
   100  
   101  				m := mangos.NewMessage(0)
   102  				m.Body = append(m.Body, []byte("hello")...)
   103  				err = sockreq.SendMsg(m)
   104  				So(err, ShouldBeNil)
   105  
   106  				So(err, ShouldBeNil)
   107  
   108  				m, err = sockrep.RecvMsg()
   109  				So(m, ShouldNotBeNil)
   110  				So(err, ShouldBeNil)
   111  
   112  				// Now close the connection -- no reply!
   113  				sockrep.Close()
   114  				l.Close()
   115  
   116  				// Open the new one on the other socket
   117  				err = l2.Listen()
   118  				m, err = rep2.RecvMsg()
   119  				So(m, ShouldNotBeNil)
   120  				So(err, ShouldBeNil)
   121  
   122  				m.Body = append(m.Body, []byte(" again")...)
   123  				err = rep2.SendMsg(m)
   124  				So(err, ShouldBeNil)
   125  
   126  				m, err = sockreq.RecvMsg()
   127  				So(m, ShouldNotBeNil)
   128  				So(err, ShouldBeNil)
   129  
   130  				m.Free()
   131  			})
   132  
   133  		})
   134  	})
   135  }