crossroads / tests / test_low_level.py

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
# -*- coding: utf-8 -*-
'''Low level tests for bindings to Crossroads.I/O.'''

from stuf.six import unittest
from stuf.utils import lrange


class BaseLow(object):

    def setUp(self):
        import crossroads.lowest
        self.xs = crossroads.lowest
        import crossroads.constants
        self.XS = crossroads.constants
        from crossroads.low import Context
        self.context_class = Context

    def time_assert(self, actual, expected):
        '''
        Check whether measured time is the expected time (in milliseconds). The
        upper tolerance is 1/2 sec so that the test doesn't fail even on very
        slow or very loaded systems.
        '''
        self.assertTrue(
            actual > ((expected) - 50) and actual < ((expected) + 500)
        )


class TestLowLevelNoCtx(BaseLow, unittest.TestCase):

    def test_linger(self):
        ctx = self.context_class()
        s = ctx.push(linger=100)
        with s.connect_tcp('127.0.0.1:5560'):
            # send a message.
            s.send('r')
            self.assertEqual(s.last_rc, 1)
        # terminate the context. this should take 0.1 second.
        watch = self.xs.stopwatch_start()
        ctx.close()
        self.time_assert(self.xs.stopwatch_stop(watch) / 1000, 100)

    def test_max_sockets(self):
        try:
            # create context and set MAX_SOCKETS to 1
            ctx = self.context_class(max_sockets=1)
            # first socket should be created OK
            s1 = ctx.push()
            # creation of second socket should fail
            try:
                ctx.push()
            except self.xs.XSError as e:
                self.assertEqual(e.errno, self.XS.EMFILE)
        except:
            raise
        finally:
            # clean up
            self.assertEqual(s1.close(), 0)
            self.assertEqual(ctx.close(), 0)

    def test_shutdown(self):
        from time import sleep
        # create infrastructure
        ctx = self.context_class()
        push = ctx.push()
        pull = ctx.pull()
        with push.bind_tcp('127.0.0.1:5560'), pull.connect_tcp('127.0.0.1:5560'):
            push_id = push.last_rc
            self.assertNotEqual(push_id, -1)
            # pass one message through to ensure the connection is established
            self.assertEqual(push.send('ABC').last_rc, 3)
            pull.recv(3)
            self.assertEqual(pull.last_rc, 3)
            # shut down the bound endpoint
            self.assertEqual(push.shutdown(push_id).last_rc, 0)
            sleep(1)
            # check that sending would block (there's no outbound connection)
            try:

                push.send('ABC', nowait=True)
            except self.xs.XSError as e:
                self.assertTrue(e.errno, self.XS.EAGAIN)
        # now the other way round...Create infrastructure
        ctx = self.context_class()
        pull = ctx.pull()
        push = ctx.push()
        with pull.bind_tcp('127.0.0.1:5560'), push.connect_tcp('127.0.0.1:5560'):
            push_id = push.last_rc
            self.assertNotEqual(push_id, -1)
            # pass one message through to ensure the connection is established
            self.assertEqual(push.send('ABC').last_rc, 3)
            pull.recv(3)
            self.assertEqual(pull.last_rc, 3)
            # shut down the bound endpoint
            self.assertEqual(push.shutdown(push_id).last_rc, 0)
            sleep(1)
            # check that sending would block (there's no outbound connection)
            try:
                push.send('ABC', nowait=True)
            except self.xs.XSError as e:
                self.assertTrue(e.errno, self.XS.EAGAIN)


class TestLowLevelCtx(BaseLow, unittest.TestCase):

    def setUp(self):
        super(TestLowLevelCtx, self).setUp()
        self.ctx = self.context_class()

    def tearDown(self):
        try:
            self.ctx.close()
        except self.xs.XSError:
            pass

    def bounce(self, sb, sc):
        from stuf.six import byteme
        content = b'12345678ABCDEFGH12345678abcdefgh'
        # send the message
        self.assertEqual(sc.send(content, more=True).last_rc, 32)
        self.assertEqual(sc.send(content).last_rc, 32)
        # bounce the message back
        buf1 = sb.recv(32)
        self.assertEqual(len(buf1), 32)
        self.assertEqual(buf1.more, 1)
        buf2 = sb.recv(32)
        self.assertEqual(len(buf2), 32)
        self.assertEqual(buf2.more, 0)
        self.assertEqual(sb.send(buf1, more=True).last_rc, 32)
        self.assertEqual(sb.send(buf2).last_rc, 32)
        # receive the bounced message
        buf1 = sc.recv(32)
        self.assertEqual(buf1.last_rc, 32)
        self.assertEqual(buf1.more, 1)
        buf2 = sc.recv(32)
        self.assertEqual(len(buf2), 32)
        self.assertEqual(buf2.more, 0)
        # check whether the message is still the same
        self.assertEqual(byteme(buf1), content)

    def test_pair_ipc(self):
        sb = self.ctx.pair()
        sc = self.ctx.pair()
        with sb.bind_ipc('/tmp/tester'), sc.connect_ipc('/tmp/tester'):
            self.bounce(sb, sc)

    def test_pair_inproc(self):
        sb = self.ctx.pair()
        sc = self.ctx.pair()
        with sb.bind_inproc(b'a'), sc.connect_inproc(b'a'):
            self.bounce(sb, sc)

    def test_pair_tcp(self):
        from time import sleep
        sb = self.ctx.pair()
        sc = self.ctx.pair()
        with sb.bind_tcp('127.0.0.1:5560'), sc.connect_tcp('127.0.0.1:5560'):
            self.bounce(sb, sc)
            # Now let's try to open one more connection to the bound socket. The
            # connection should be silently rejected rather than causing error.
            sc2 = self.ctx.pair()
            with sc2.connect_tcp('127.0.0.1:5560'):
                sleep(1)

    def test_regrep_inproc(self):
        sb = self.ctx.rep()
        sc = self.ctx.req()
        with sb.bind_inproc('a'), sc.connect_inproc('a'):
            self.bounce(sb, sc)

    def test_regrep_ipc(self):
        sb = self.ctx.rep()
        sc = self.ctx.req()
        with sb.bind_ipc('/tmp/tester'), sc.connect_ipc('/tmp/tester'):
            self.bounce(sb, sc)

    def test_regrep_tcp(self):
        sb = self.ctx.rep()
        sc = self.ctx.req()
        with sb.bind_tcp('127.0.0.1:5560'), sc.connect_tcp('127.0.0.1:5560'):
            self.bounce(sb, sc)

    def test_msg_flags(self):
        # create the infrastructure
        sb = self.ctx.xrep()
        sc = self.ctx.xreq()
        with sb.bind_inproc('a'), sc.connect_inproc('a'):
            self.assertNotEqual(sb.last_rc, -1)
            self.assertNotEqual(sc.last_rc, -1)
            # send 2 - part message
            self.assertEqual(sc.send('A', more=True).last_rc, 1)
            self.assertEqual(sc.send('B').last_rc, 1)
            # identity comes first
            msg = sb.recvmsg()
            self.assertTrue(msg.last_rc >= 0)
            self.assertEqual(msg.moremsg, 1)
            # then the first part of the message body
            msg = sb.recvmsg()
            self.assertTrue(msg.last_rc >= 0)
            self.assertEqual(msg.moremsg, 1)
            # and finally, the second part of the message body
            msg = sb.recvmsg()
            self.assertTrue(msg.last_rc >= 0)
            self.assertEqual(msg.moremsg, 0)

    def test_wireformat(self):
        import socket
        from stuf.six import byteme
        # create the basic infrastructure
        push = self.ctx.push()
        pull = self.ctx.pull()
        # bind the peer and get the message
        with pull.bind_tcp('127.0.0.1:5560'), push.bind_tcp('127.0.0.1:5561'):
            # connect to the peer using raw sockets
            rpush = socket.socket(
                socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP
            )
            rpush.connect(('127.0.0.1', 5560))
            rpull = socket.socket(
                socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP,
            )
            rpull.connect(('127.0.0.1', 5561))
            # let's send some data and check if it arrived
            self.assertEqual(rpush.send(b'\x04\0abc', 0), 5)
            buf = pull.recv(3)
            self.assertEqual(pull.last_rc, 3)
            self.assertEqual(byteme(buf), b'abc')
            # let's push this data into another socket
            self.assertEqual(push.send(buf).last_rc, 3)
            self.assertNotEqual(rpull.recv(3), b'\x04\0abc')
            rpush.close()
            rpull.close()

    def test_reconnect(self):
        import sys
        from time import sleep
        # create the basic infrastructure
        push = self.ctx.push()
        pull = self.ctx.pull()
        # connect before bind was done at the peer and send one message
        with push.connect_tcp('127.0.0.1:5560'):
            self.assertEqual(push.send('ABC').last_rc, 3)
            # wait a while for few attempts to reconnect to happen
            sleep(1)
            # bind the peer and get the message
            with pull.bind_tcp('127.0.0.1:5560'):
                msg = pull.recv(3)
                self.assertEqual(msg.last_rc, 3)
        if sys.platform not in ('win32', 'cygwin'):
            # now, let's test the same scenario with IPC
            push = self.ctx.push()
            pull = self.ctx.pull()
            with push.connect_ipc('/tmp/tester'):
                # connect before bind was done at the peer and send one message
                self.assertEqual(push.send('ABC').last_rc, 3)
                # wait a while for few attempts to reconnect to happen
                sleep(1)
                # bind the peer and get the message
                with pull.bind_ipc('/tmp/tester'):
                    pull.recv(3)
                    self.assertEqual(pull.last_rc, 3)

    def test_hwn(self):
        # Create pair of socket, each with high watermark of 2. Thus the
        # total buffer space should be 4 messages.
        sb = self.ctx.pull(rcvhwm=2)
        sc = self.ctx.push(sndhwm=2)
        with sb.bind_inproc('a'), sc.connect_inproc('a'):
            # Try to send 10 messages. Only 4 should succeed.
            for t in range(10):
                try:
                    sc.send(None, 0, nowait=True)
                    if t < 4:
                        self.assertEqual(sc.last_rc, 0)
                except self.xs.XSError as e:
                    self.assertTrue(e.errno, self.XS.EAGAIN)
            # there should be now 4 messages pending, consume them.
            for i in lrange(4):  #  @UnusedVariable
                sb.recv(0)
                self.assertEqual(sb.last_rc, 0)
            # now it should be possible to send one more.
            self.assertEqual(sc.send(None, 0).last_rc, 0)
            # consume the remaining message
            sb.recv(0)
            self.assertEqual(sb.last_rc, 0)
            s1 = self.ctx.pull()
            s2 = self.ctx.push(sndhwm=5)
            # Following part of the tests checks whether small HWMs don't
            # interact with command throttling in strange ways.
            with s1.bind_tcp('127.0.0.1:5858'), s2.connect_tcp('127.0.0.1:5858'):
                self.assertTrue(s1.last_rc >= 0)
                self.assertTrue(s2.last_rc >= 0)
                for i in lrange(10):  #  @UnusedVariable
                    self.assertEqual(s2.send('test', nowait=True).last_rc, 4)
                    s1.recv(4)
                    self.assertEqual(s1.last_rc, 4)

    def test_survey(self):
        # create the basic infrastructure
        xsurveyor = self.ctx.xsurveyor()
        xrespondent = self.ctx.xrespondent()
        surveyor = self.ctx.surveyor()
        respondent1 = self.ctx.respondent()
        respondent2 = self.ctx.respondent()
        with xsurveyor.bind_inproc('a'), xrespondent.bind_inproc('b'), \
            surveyor.connect_inproc('b'), respondent1.connect_inproc('a'), \
            respondent2.connect_inproc('a'):
            # end the survey
            self.assertEqual(surveyor.send('ABC').last_rc, 3)
            # Forward the survey through the intermediate device. Survey consist
            # of identity(4 bytes), survey ID(4 bytes) and the body.
            buf = xrespondent.recv(4)
            self.assertEqual(buf.last_rc, 4)
            self.assertEqual(xsurveyor.send(buf, more=True).last_rc, 4)
            buf = xrespondent.recv(4)
            self.assertEqual(buf.last_rc, 4)
            self.assertEqual(xsurveyor.send(buf, more=True).last_rc, 4)
            buf = xrespondent.recv(3)
            self.assertEqual(buf.last_rc, 3)
            self.assertEqual(xsurveyor.send(buf).last_rc, 3)
            # respondent 1 responds to the survey
            buf = respondent1.recv(3)
            self.assertEqual(buf.last_rc, 3)
            self.assertEqual(respondent1.send('DE').last_rc, 2)
            # forward the response through the intermediate device
            buf = xsurveyor.recv(4)
            self.assertEqual(buf.last_rc, 4)
            self.assertEqual(xrespondent.send(buf, more=True).last_rc, 4)
            buf = xsurveyor.recv(4)
            self.assertEqual(buf.last_rc, 4)
            self.assertEqual(xrespondent.send(buf, more=True).last_rc, 4)
            buf = xsurveyor.recv(2)
            self.assertEqual(buf.last_rc, 2)
            self.assertEqual(xrespondent.send(buf).last_rc, 2)
            # surveyor gets the response
            buf = surveyor.recv(2)
            self.assertEqual(buf.last_rc, 2)
            # respondent 2 responds to the survey
            buf = respondent2.recv(3)
            self.assertEqual(buf.last_rc, 3)
            self.assertEqual(respondent2.send('FGHI').last_rc, 4)
            # forward the response through the intermediate device
            buf = xsurveyor.recv(4)
            self.assertEqual(buf.last_rc, 4)
            self.assertEqual(xrespondent.send(buf, more=True).last_rc, 4)
            buf = xsurveyor.recv(4)
            self.assertEqual(buf.last_rc, 4)
            self.assertEqual(xrespondent.send(buf, more=True).last_rc, 4)
            buf = xsurveyor.recv(4)
            self.assertEqual(buf.last_rc, 4)
            self.assertEqual(xrespondent.send(buf).last_rc, 4)
            # surveyor gets the response
            buf = surveyor.recv(4)
            self.assertEqual(buf.last_rc, 4)
            # now let's test whether survey timeout works as expected
            surveyor.survey_timeout = 100
            self.assertEqual(surveyor.last_rc, 0)
            self.assertEqual(surveyor.send('ABC', 3, 0).last_rc, 3)
            watch = self.xs.stopwatch_start()
            try:
                surveyor.recv(4)
            except self.xs.XSError as e:
                self.assertEqual(e.errno, self.XS.ETIMEDOUT)
            self.time_assert(self.xs.stopwatch_stop(watch) / 1000, 100)
            # Test whether responses for old surveys are discarded. First,
            # initiate new survey.
            surveyor.survey_timeout = 100
            self.assertEqual(surveyor.last_rc, 0)
            self.assertEqual(surveyor.send('DE').last_rc, 2)
            # read, process and reply to the old survey
            buf = xrespondent.recv(4)
            self.assertEqual(buf.last_rc, 4)
            self.assertEqual(xrespondent.send(buf, more=True).last_rc, 4)
            buf = xrespondent.recv(4)
            self.assertEqual(buf.last_rc, 4)
            self.assertEqual(xrespondent.send(buf, more=True).last_rc, 4)
            buf = xrespondent.recv(3)
            self.assertEqual(buf.last_rc, 3)
            self.assertEqual(xrespondent.send(buf).last_rc, 3)
            # read, process and reply to the new survey
            buf = xrespondent.recv(4)
            self.assertEqual(buf.last_rc, 4)
            self.assertEqual(xrespondent.send(buf, more=True).last_rc, 4)
            buf = xrespondent.recv(4)
            self.assertEqual(buf.last_rc, 4)
            self.assertEqual(xrespondent.send(buf, more=True).last_rc, 4)
            buf = xrespondent.recv(2)
            self.assertEqual(buf.last_rc, 2)
            self.assertEqual(xrespondent.send(buf).last_rc, 2)
            # Get the response and check it's the response to the new survey and
            # that response to the old survey was silently discarded.
            self.assertEqual(surveyor.recv(2).last_rc, 2)

    def test_invalid_rep(self):
        from stuf.six import int2byte
        # create REQ/XREP wiring
        xrep_socket = self.ctx.xrep(linger=0)
        req_socket = self.ctx.req(linger=0)
        with xrep_socket.bind_inproc('hi'), req_socket.connect_inproc('hi'):
            # initial request.
            self.assertEqual(req_socket.send('r').last_rc, 1)
            # receive the request
            addr = xrep_socket.recv(32)
            addr_size = addr.last_rc
            self.assertEqual(addr_size, 5)
            bottom = xrep_socket.recv(1)
            self.assertEqual(bottom.last_rc, 0)
            body = xrep_socket.recv(1)
            self.assertEqual(body.last_rc, 1)
            # send invalid reply
            self.assertEqual(xrep_socket.send(addr, 5).last_rc, addr_size)
            # send valid reply
            self.assertEqual(
                xrep_socket.send(addr, 5, True).last_rc, addr_size,
            )
            self.assertEqual(
                xrep_socket.send(bottom, 0, True).last_rc, 0
            )
            self.assertEqual(xrep_socket.send('b').last_rc, 1)
            # check whether we've got the valid reply
            body = req_socket.recv(1, nowait=True)
            self.assertEqual(body.last_rc, 1)
            self.assertEqual(int2byte(body.msg[0]), b'b')

    def test_sub_forward(self):
        from time import sleep
        # first, create an intermediate device
        xpub = self.ctx.xpub()
        xsub = self.ctx.xsub()
        # create a publisher
        pub = self.ctx.pub()
        sub = self.ctx.sub()
        with xpub.bind_tcp('127.0.0.1:5560'), xsub.bind_tcp('127.0.0.1:5561'), \
          pub.connect_tcp('127.0.0.1:5561'), sub.connect_tcp('127.0.0.1:5560'):
            # create a subscriber and subscribe for all messages
            sub.subscribe = ''
            self.assertEqual(sub.last_rc, 0)
            # pass the subscription upstream through the device
            buf = xpub.recv(32)
            self.assertEqual(xpub.last_rc, 4)
            self.assertEqual(xsub.send(buf.msg, 4).last_rc, 4)
            # wait a bit till the subscription gets to the publisher
            sleep(1)
            # send an empty message
            self.assertEqual(pub.send(None, 0).last_rc, 0)
            # pass the message downstream through the device
            buf = xsub.recv(32)
            self.assertTrue(xsub.last_rc >= 0)
            self.assertTrue(xpub.send(buf.msg, buf.last_rc).last_rc >= 0)
            # receive the message in the subscriber
            sub.recv(xpub.last_rc)
            self.assertEqual(sub.last_rc, 0)

    def test_regrep_device(self):
        from stuf.six import byteme
        # create a req/rep device
        xreq = self.ctx.xreq()
        xrep = self.ctx.xrep()
        # create a worker
        rep = self.ctx.rep()
        # create a client
        req = self.ctx.req()
        with xreq.bind_tcp('127.0.0.1:5560'), xrep.bind_tcp('127.0.0.1:5561'),\
           rep.connect_tcp('127.0.0.1:5560'), req.connect_tcp('127.0.0.1:5561'):
            # send a request
            self.assertEqual(req.send(b'ABC', 3, True).last_rc, 3)
            self.assertEqual(req.send(b'DEF', 3).last_rc, 3)
            # pass the reply through the device
            for i in lrange(4):  #  @UnusedVariable
                msg = xrep.recvmsg()
                self.assertTrue(msg.last_rc >= 0)
                rc = xreq.sendmsg(msg, True if msg.more else False).last_rc
                self.assertTrue(rc >= 0)
            self.assertEqual(msg.more, 0)
            # receive the request
            buff = rep.recv(3)
            self.assertEqual(byteme(buff), b'ABC')
            self.assertEqual(buff.last_rc, 3)
            self.assertEqual(buff.more, 1)
            buff = rep.recv(3)
            self.assertEqual(buff.last_rc, 3)
            self.assertEqual(byteme(buff), b'DEF')
            self.assertEqual(buff.more, 0)
            # send the reply
            self.assertEqual(rep.send(b'GHI', 3, more=True).last_rc, 3)
            self.assertEqual(rep.send(b'JKL', 3).last_rc, 3)
            # pass the reply through the device
            for i in lrange(4):  #  @UnusedVariable
                msg = xreq.recvmsg()
                self.assertTrue(msg.last_rc >= 0)
                rc = xrep.sendmsg(msg, True if msg.more else False).last_rc
                self.assertTrue(rc >= 0)
            # receive the reply
            buff = req.recv(3)
            self.assertEqual(buff.last_rc, 3)
            self.assertEqual(byteme(buff), b'GHI')
            self.assertEqual(buff.more, 1)
            buff2 = req.recv(3)
            self.assertEqual(buff2.last_rc, 3)
            self.assertEqual(byteme(buff2), b'JKL')
            self.assertEqual(buff2.more, 0)

    def test_resubscribe(self):
        try:
            from stuf.six import int2byte
            from time import sleep
            # Create the basic infrastructure.
            xpub = self.ctx.xpub()
            sub = self.ctx.sub()
            xpub.bind_tcp('127.0.0.1:5560')
            # Send two subscriptions upstream.
            sub.subscribe = b'a'
            self.assertEqual(sub.last_rc, 0)
            sub.subscribe = b'b'
            self.assertEqual(sub.last_rc, 0)
            sub.connect_tcp('127.0.0.1:5560')
            self.assertTrue(sub.last_rc >= -1)
            # Check whether subscriptions are correctly received.
            buf = xpub.recv(5, char=True).array
            self.assertEqual(xpub.last_rc, 5)
            self.assertEqual(buf[0], 0)
            self.assertEqual(buf[1], 1)
            self.assertEqual(buf[2], 0)
            self.assertEqual(buf[3], 1)
            self.assertEqual(int2byte(buf[4]), b'a')
            buf = xpub.recv(5, char=True).array
            self.assertEqual(xpub.last_rc, 5)
            self.assertEqual(buf[0], 0)
            self.assertEqual(buf[1], 1)
            self.assertEqual(buf[2], 0)
            self.assertEqual(buf[3], 1)
            self.assertEqual(int2byte(buf[4]), b'b')
            # Tear down the connection.
            self.assertEqual(xpub.close(), 0)
            sleep(1)
            # Re-establish the connection.
            xpub = self.ctx.xpub()
            self.assertNotEqual(xpub.bind_tcp('127.0.0.1:5560'), -1)
            # We have to give control to the SUB socket here so that it has
            # chance to resend the subscriptions.
            try:
                sub.recv(3, nowait=True)
            except self.xs.XSError as e:
                self.assertEqual(e.errno, self.XS.EAGAIN)
            # Check whether subscriptions are correctly generated.
            buf = xpub.recv(5, char=True).array
            self.assertEqual(xpub.last_rc, 5)
            self.assertEqual(buf[0], 0)
            self.assertEqual(buf[1], 1)
            self.assertEqual(buf[2], 0)
            self.assertEqual(buf[3], 1)
            self.assertEqual(int2byte(buf[4]), b'a')
            buf = xpub.recv(5, char=True).array
            self.assertEqual(xpub.last_rc, 5)
            self.assertEqual(buf[0], 0)
            self.assertEqual(buf[1], 1)
            self.assertEqual(buf[2], 0)
            self.assertEqual(buf[3], 1)
            self.assertEqual(int2byte(buf[4]), b'b')
        finally:
            xpub.close()
            sub.close()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.