github.com/gdamore/mangos@v1.4.0/test/besteffort_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  	"time"
    20  
    21  	"nanomsg.org/go-mangos"
    22  	"nanomsg.org/go-mangos/protocol/pair"
    23  	"nanomsg.org/go-mangos/transport/tcp"
    24  
    25  	. "github.com/smartystreets/goconvey/convey"
    26  )
    27  
    28  func testBestEffort(addr string, tran mangos.Transport) {
    29  	timeout := time.Millisecond * 10
    30  	msg := []byte{'A', 'B', 'C'}
    31  
    32  	Convey("Given a new listening (unconnected) socket", func() {
    33  		rp, err := pair.NewSocket()
    34  		So(err, ShouldBeNil)
    35  		So(rp, ShouldNotBeNil)
    36  
    37  		defer rp.Close()
    38  		rp.AddTransport(tran)
    39  
    40  		err = rp.SetOption(mangos.OptionWriteQLen, 0)
    41  		So(err, ShouldBeNil)
    42  
    43  		err = rp.SetOption(mangos.OptionSendDeadline, timeout)
    44  		So(err, ShouldBeNil)
    45  
    46  		err = rp.Listen(addr)
    47  		So(err, ShouldBeNil)
    48  
    49  		Convey("Messages are discarded in besteffort mode", func() {
    50  			err = rp.SetOption(mangos.OptionBestEffort, true)
    51  			So(err, ShouldBeNil)
    52  
    53  			for i := 0; i < 2; i++ {
    54  				err = rp.Send(msg)
    55  				So(err, ShouldBeNil)
    56  			}
    57  		})
    58  
    59  		Convey("Messages timeout when not in besteffort mode", func() {
    60  			err = rp.SetOption(mangos.OptionBestEffort, false)
    61  			So(err, ShouldBeNil)
    62  
    63  			for i := 0; i < 2; i++ {
    64  				err = rp.Send(msg)
    65  				So(err, ShouldEqual, mangos.ErrSendTimeout)
    66  			}
    67  		})
    68  	})
    69  }
    70  
    71  func TestBestEffortTCP(t *testing.T) {
    72  	Convey("Testing TCP Best Effort", t, func() {
    73  		testBestEffort(AddrTestTCP, tcp.NewTransport())
    74  	})
    75  }