nanomsg.org/go/mangos/v2@v2.0.9-0.20200203084354-8a092611e461/protocol/xpull/xpull_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 xpull
    16  
    17  import (
    18  	"testing"
    19  	"time"
    20  
    21  	"nanomsg.org/go/mangos/v2"
    22  	"nanomsg.org/go/mangos/v2/protocol/push"
    23  
    24  	. "nanomsg.org/go/mangos/v2/internal/test"
    25  	_ "nanomsg.org/go/mangos/v2/transport/inproc"
    26  )
    27  
    28  func TestXPullIdentity(t *testing.T) {
    29  	s := GetSocket(t, NewSocket)
    30  	id := s.Info()
    31  	MustBeTrue(t, id.Self == mangos.ProtoPull)
    32  	MustBeTrue(t, id.SelfName == "pull")
    33  	MustBeTrue(t, id.Peer == mangos.ProtoPush)
    34  	MustBeTrue(t, id.PeerName == "push")
    35  	MustSucceed(t, s.Close())
    36  }
    37  
    38  func TestXPullRaw(t *testing.T) {
    39  	VerifyRaw(t, NewSocket)
    40  }
    41  
    42  func TestXPullReadOnly(t *testing.T) {
    43  	CannotSend(t, NewSocket)
    44  }
    45  
    46  func TestXPullClosed(t *testing.T) {
    47  	VerifyClosedListen(t, NewSocket)
    48  	VerifyClosedDial(t, NewSocket)
    49  	VerifyClosedClose(t, NewSocket)
    50  	VerifyClosedRecv(t, NewSocket)
    51  	VerifyClosedAddPipe(t, NewSocket)
    52  }
    53  
    54  func TestXPullOptions(t *testing.T) {
    55  	VerifyInvalidOption(t, NewSocket)
    56  	VerifyOptionDuration(t, NewSocket, mangos.OptionRecvDeadline)
    57  	VerifyOptionQLen(t, NewSocket, mangos.OptionReadQLen)
    58  
    59  }
    60  
    61  func TestXPullRecvDeadline(t *testing.T) {
    62  	s := GetSocket(t, NewSocket)
    63  	MustSucceed(t, s.SetOption(mangos.OptionRecvDeadline, time.Millisecond))
    64  	d, e := s.Recv()
    65  	MustBeError(t, e, mangos.ErrRecvTimeout)
    66  	MustBeNil(t, d)
    67  	MustSucceed(t, s.Close())
    68  }
    69  
    70  func TestXPullRecvClosePipe(t *testing.T) {
    71  	s := GetSocket(t, NewSocket)
    72  	p := GetSocket(t, push.NewSocket)
    73  	MustSucceed(t, p.SetOption(mangos.OptionWriteQLen, 1))
    74  	MustSucceed(t, s.SetOption(mangos.OptionReadQLen, 3))
    75  	MustSucceed(t, s.SetOption(mangos.OptionRecvDeadline, time.Minute))
    76  	ConnectPair(t, s, p)
    77  	MustSucceed(t, p.Send([]byte{}))
    78  	m, e := s.RecvMsg()
    79  	MustSucceed(t, e)
    80  	MustSucceed(t, p.SetOption(mangos.OptionSendDeadline, time.Millisecond))
    81  
    82  	// Fill the pipe
    83  	for i := 0; i < 20; i++ {
    84  		// These all will work, but the back-pressure will go all the
    85  		// way to the sender.
    86  		if e := p.Send([]byte{byte(i)}); e != nil {
    87  			MustBeError(t, e, mangos.ErrSendTimeout)
    88  			break
    89  		}
    90  	}
    91  
    92  	time.Sleep(time.Millisecond * 10)
    93  	MustSucceed(t, m.Pipe.Close())
    94  
    95  	time.Sleep(time.Millisecond * 10)
    96  	MustSucceed(t, s.Close())
    97  }
    98  
    99  func TestXPullRecvCloseSocket(t *testing.T) {
   100  	s := GetSocket(t, NewSocket)
   101  	p := GetSocket(t, push.NewSocket)
   102  	MustSucceed(t, p.SetOption(mangos.OptionWriteQLen, 1))
   103  	MustSucceed(t, s.SetOption(mangos.OptionReadQLen, 3))
   104  	MustSucceed(t, s.SetOption(mangos.OptionRecvDeadline, time.Minute))
   105  	MustSucceed(t, p.SetOption(mangos.OptionSendDeadline, time.Millisecond))
   106  	ConnectPair(t, s, p)
   107  
   108  	// Fill the pipe
   109  	for i := 0; i < 20; i++ {
   110  		// These all will work, but the back-pressure will go all the
   111  		// way to the sender.
   112  		if e := p.Send([]byte{byte(i)}); e != nil {
   113  			MustBeError(t, e, mangos.ErrSendTimeout)
   114  			break
   115  		}
   116  	}
   117  
   118  	time.Sleep(time.Millisecond * 10)
   119  	MustSucceed(t, s.Close())
   120  }
   121  
   122  func TestXPullRecv(t *testing.T) {
   123  	s := GetSocket(t, NewSocket)
   124  	p := GetSocket(t, push.NewSocket)
   125  	MustSucceed(t, s.SetOption(mangos.OptionRecvDeadline, time.Second))
   126  	ConnectPair(t, s, p)
   127  
   128  	for i := 0; i < 100; i++ {
   129  		MustSucceed(t, p.Send([]byte{byte(i)}))
   130  		v, e := s.Recv()
   131  		MustSucceed(t, e)
   132  		MustBeTrue(t, len(v) == 1)
   133  		MustBeTrue(t, v[0] == byte(i))
   134  	}
   135  	MustSucceed(t, s.Close())
   136  	MustSucceed(t, p.Close())
   137  }
   138  
   139  func TestXPullResizeRecv(t *testing.T) {
   140  	s := GetSocket(t, NewSocket)
   141  	p := GetSocket(t, push.NewSocket)
   142  	MustSucceed(t, p.SetOption(mangos.OptionWriteQLen, 1))
   143  	MustSucceed(t, s.SetOption(mangos.OptionReadQLen, 20))
   144  	MustSucceed(t, s.SetOption(mangos.OptionRecvDeadline, time.Millisecond))
   145  	ConnectPair(t, s, p)
   146  
   147  	// Fill the pipe
   148  	for i := 0; i < 20; i++ {
   149  		// These all will work, but the back-pressure will go all the
   150  		// way to the sender.
   151  		MustSucceed(t, p.Send([]byte{byte(i)}))
   152  	}
   153  
   154  	time.Sleep(time.Millisecond * 10)
   155  	MustSucceed(t, s.SetOption(mangos.OptionReadQLen, 1))
   156  	// Sleep so the resize filler finishes
   157  	time.Sleep(time.Millisecond * 20)
   158  
   159  	for i := 0; i < 20; i++ {
   160  		if _, e := s.Recv(); e != nil {
   161  			MustBeError(t, e, mangos.ErrRecvTimeout)
   162  			break
   163  		}
   164  	}
   165  	MustSucceed(t, s.Close())
   166  }
   167  
   168  func TestXPullResizeRecv2(t *testing.T) {
   169  	s := GetSocket(t, NewSocket)
   170  	p := GetSocket(t, push.NewSocket)
   171  	MustSucceed(t, p.SetOption(mangos.OptionWriteQLen, 1))
   172  	MustSucceed(t, s.SetOption(mangos.OptionReadQLen, 3))
   173  	MustSucceed(t, s.SetOption(mangos.OptionRecvDeadline, time.Millisecond))
   174  	MustSucceed(t, p.SetOption(mangos.OptionSendDeadline, time.Millisecond))
   175  	ConnectPair(t, s, p)
   176  
   177  	// Fill the pipe
   178  	for i := 0; i < 20; i++ {
   179  		// These all will work, but the back-pressure will go all the
   180  		// way to the sender.
   181  		if e := p.Send([]byte{byte(i)}); e != nil {
   182  			MustBeError(t, e, mangos.ErrSendTimeout)
   183  			break
   184  		}
   185  	}
   186  
   187  	MustSucceed(t, p.SetOption(mangos.OptionSendDeadline, time.Minute))
   188  	go func() {
   189  		MustSucceed(t, p.Send([]byte{'A'}))
   190  	}()
   191  
   192  	time.Sleep(time.Millisecond * 10)
   193  	MustSucceed(t, s.SetOption(mangos.OptionReadQLen, 10))
   194  	// Sleep so the resize filler finishes
   195  	time.Sleep(time.Millisecond * 20)
   196  
   197  	for i := 0; i < 20; i++ {
   198  		if _, e := s.Recv(); e != nil {
   199  			MustBeError(t, e, mangos.ErrRecvTimeout)
   200  			break
   201  		}
   202  	}
   203  	MustSucceed(t, s.Close())
   204  }
   205  
   206  func TestXPullResizeRecv3(t *testing.T) {
   207  	s := GetSocket(t, NewSocket)
   208  	p := GetSocket(t, push.NewSocket)
   209  	MustSucceed(t, p.SetOption(mangos.OptionWriteQLen, 1))
   210  	MustSucceed(t, s.SetOption(mangos.OptionReadQLen, 3))
   211  	MustSucceed(t, s.SetOption(mangos.OptionRecvDeadline, time.Minute))
   212  	MustSucceed(t, p.SetOption(mangos.OptionSendDeadline, time.Millisecond))
   213  	MustSucceed(t, s.SetOption("_resizeDiscards", true))
   214  	ConnectPair(t, s, p)
   215  
   216  	time.AfterFunc(time.Millisecond*20, func() {
   217  		MustSucceed(t, s.SetOption(mangos.OptionReadQLen, 10))
   218  		time.Sleep(time.Millisecond * 10)
   219  		MustSucceed(t, p.Send([]byte{}))
   220  	})
   221  
   222  	b, e := s.Recv()
   223  	MustSucceed(t, e)
   224  	MustNotBeNil(t, b)
   225  	MustSucceed(t, s.Close())
   226  }
   227  
   228  func TestXPullResizeRecv4(t *testing.T) {
   229  	s := GetSocket(t, NewSocket)
   230  	p := GetSocket(t, push.NewSocket)
   231  	MustSucceed(t, p.SetOption(mangos.OptionWriteQLen, 1))
   232  	MustSucceed(t, s.SetOption(mangos.OptionReadQLen, 3))
   233  	MustSucceed(t, s.SetOption(mangos.OptionRecvDeadline, time.Minute))
   234  	MustSucceed(t, p.SetOption(mangos.OptionSendDeadline, time.Millisecond))
   235  	MustSucceed(t, s.SetOption("_resizeDiscards", true))
   236  	ConnectPair(t, s, p)
   237  
   238  	// Fill the pipe
   239  	for i := 0; i < 20; i++ {
   240  		if e := p.Send([]byte{byte(i)}); e != nil {
   241  			MustBeError(t, e, mangos.ErrSendTimeout)
   242  			break
   243  		}
   244  	}
   245  
   246  	time.AfterFunc(time.Millisecond*20, func() {
   247  		MustSucceed(t, s.SetOption(mangos.OptionReadQLen, 10))
   248  	})
   249  
   250  	time.Sleep(time.Millisecond * 50)
   251  	MustSucceed(t, s.Close())
   252  }