crossroads / tests / test_lowest_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
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
# -*- coding: utf-8 -*-
'''Lowest level tests for bindings to Crossroads.I/O.'''

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

from operator import attrgetter

twoget = attrgetter(*'XS xs'.split())


class TestLowestLevel(unittest.TestCase):

    def setUp(self):
        import crossroads.lowest
        self.xs = crossroads.lowest
        import crossroads.constants
        self.XS = crossroads.constants

#        static void *thread_routine (void *arg_)
#        {
#            arg_t *arg = (arg_t*) arg_;
#            arg->fn (arg->arg);
#            return NULL;
#        }
#    }
#
#    void *thread_create (void (*fn_) (void *arg_), void *arg_)
#    {
#        arg_t *arg = (arg_t*) malloc (sizeof (arg_t));
#        assert (arg);
#        arg->fn = fn_;
#        arg->arg = arg_;
#        int rc = pthread_create (&arg->handle, NULL, thread_routine, (void*) arg);
#        assert (rc == 0);
#        return (void*) arg;
#    }
#
#    void thread_join (void *thread_)
#    {
#        arg_t *arg = (arg_t*) thread_;
#        int rc = pthread_join (arg->handle, NULL);
#        assert (rc == 0);
#        free (arg);
#    }

    def bounce(self, sb, sc):
        from ctypes import c_char_p, c_int, sizeof, c_size_t, c_ubyte
        from crossroads.lowest import array
        XS, xs = twoget(self)
        content = c_char_p(b'12345678ABCDEFGH12345678abcdefgh')
        # send the message.
        self.assertEqual(xs.send(sc, content, 32, XS.SNDMORE), 32)
        self.assertEqual(xs.send(sc, content, 32, 0), 32)
        # bounce the message back.
        buf1 = array(c_ubyte, 32)
        self.assertEqual(xs.recv(sb, buf1, 32, 0), 32)
        rcvmore = c_int()
        sz = c_size_t(sizeof(rcvmore))
        self.assertEqual(xs.getsockopt(sb, XS.RCVMORE, rcvmore, sz), 0)
        self.assertTrue(rcvmore)
        self.assertEqual(xs.recv(sb, buf1, 32, 0), 32)
        self.assertEqual(xs.getsockopt(sb, XS.RCVMORE, rcvmore, sz), 0)
        self.assertFalse(rcvmore)
        self.assertEqual(xs.send(sb, buf1, 32, XS.SNDMORE), 32)
        self.assertEqual(xs.send(sb, buf1, 32, 0), 32)
        # receive the bounced message.
        buf2 = array(c_ubyte, 32)
        self.assertEqual(xs.recv(sc, buf2, 32, 0), 32)
        self.assertEqual(xs.getsockopt(sc, XS.RCVMORE, rcvmore, sz), 0)
        self.assertTrue(rcvmore)
        self.assertEqual(xs.recv(sc, buf2, 32, 0), 32)
        self.assertEqual(xs.getsockopt(sc, XS.RCVMORE, rcvmore, sz), 0)
        self.assertFalse(rcvmore)
        # check whether the message is still the same.
        self.assertEqual(bytearray(buf2), content.value)

    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)
        )

    def test_empty(self):
        xs = self.xs
        # this is a very simple test to check whether everything works OK when
        # context is terminated even before I/O threads were launched.
        ctx = xs.init()
        self.assertTrue(ctx)
        self.assertEqual(xs.term(ctx), 0)

    def test_max_sockets(self):
        from ctypes import sizeof, c_int
        XS, xs = twoget(self)
        # create context and set MAX_SOCKETS to 1.
        ctx = xs.init()
        self.assertTrue(ctx)
        max_sockets = c_int(1)
        self.assertEqual(xs.setctxopt(
            ctx, XS.MAX_SOCKETS, max_sockets, sizeof(max_sockets)
        ), 0)
        # First socket should be created OK.
        s1 = xs.socket(ctx, XS.PUSH)
        self.assertTrue(s1)
        # creation of second socket should fail.
        try:
            xs.socket(ctx, XS.PUSH)
        except xs.XSError as e:
            self.assertEqual(e.errno, XS.EMFILE)
        # clean up.
        self.assertEqual(xs.close(s1), 0)
        self.assertEqual(xs.term(ctx), 0)

    def test_pair_ipc(self):
        XS, xs = twoget(self)
        ctx = xs.init()
        self.assertTrue(ctx)
        sb = xs.socket(ctx, XS.PAIR)
        self.assertTrue(sb)
        self.assertNotEqual(xs.bind(sb, b'ipc:///tmp/tester'), -1)
        sc = xs.socket(ctx, XS.PAIR)
        self.assertTrue(sc)
        self.assertNotEqual(xs.connect(sc, b'ipc:///tmp/tester'), -1)
        self.bounce(sb, sc)
        self.assertEqual(xs.close(sc), 0)
        self.assertEqual(xs.close(sb), 0)
        self.assertEqual(xs.term(ctx), 0)

    def test_pair_inproc(self):
        XS, xs = twoget(self)
        ctx = xs.init()
        self.assertTrue(ctx)
        sb = xs.socket(ctx, XS.PAIR)
        self.assertTrue(sb)
        self.assertNotEqual(xs.bind(sb, b'inproc://a'), -1)
        sc = xs.socket(ctx, XS.PAIR)
        self.assertTrue(sc)
        self.assertNotEqual(xs.connect(sc, b'inproc://a'), -1)
        self.bounce(sb, sc)
        self.assertEqual(xs.close(sc), 0)
        self.assertEqual(xs.close(sb), 0)
        self.assertEqual(xs.term(ctx), 0)

    def test_pair_tcp(self):
        from time import sleep
        XS, xs = twoget(self)
        ctx = xs.init()
        self.assertTrue(ctx)
        sb = xs.socket(ctx, XS.PAIR)
        self.assertTrue(sb)
        self.assertNotEqual(xs.bind(sb, b'tcp://127.0.0.1:5560'), -1)
        sc = xs.socket(ctx, XS.PAIR)
        self.assertTrue(sc)
        self.assertNotEqual(xs.connect(sc, b'tcp://127.0.0.1:5560'), -1)
        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 = xs.socket(ctx, XS.PAIR)
        self.assertTrue(sc2)
        self.assertNotEqual(xs.connect(sc2, b'tcp://127.0.0.1:5560'), -1)
        sleep(1)
        self.assertEqual(xs.close(sc2), 0)
        self.assertEqual(xs.close(sc), 0)
        self.assertEqual(xs.close(sb), 0)
        self.assertEqual(xs.term(ctx), 0)

    def test_regrep_inproc(self):
        XS, xs = twoget(self)
        ctx = xs.init()
        self.assertTrue(ctx)
        sb = xs.socket(ctx, XS.REP)
        self.assertTrue(sb)
        self.assertNotEqual(xs.bind(sb, b'inproc://a'), -1)
        sc = xs.socket(ctx, XS.REQ)
        self.assertTrue(sc)
        self.assertNotEqual(xs.connect(sc, b'inproc://a'), -1)
        self.bounce(sb, sc)
        self.assertEqual(xs.close(sc), 0)
        self.assertEqual(xs.close(sb), 0)
        self.assertEqual(xs.term(ctx), 0)

    def test_regrep_ipc(self):
        XS, xs = twoget(self)
        ctx = xs.init()
        self.assertTrue(ctx)
        sb = xs.socket(ctx, XS.REP)
        self.assertTrue(sb)
        self.assertNotEqual(xs.bind(sb, b'ipc:///tmp/tester'), -1)
        sc = xs.socket(ctx, XS.REQ)
        self.assertTrue(sc)
        self.assertNotEqual(xs.connect(sc, b'ipc:///tmp/tester'), -1)
        self.bounce(sb, sc)
        self.assertEqual(xs.close(sc), 0)
        self.assertEqual(xs.close(sb), 0)
        self.assertEqual(xs.term(ctx), 0)

    def test_regrep_tcp(self):
        XS, xs = twoget(self)
        ctx = xs.init()
        self.assertTrue(ctx)
        sb = xs.socket(ctx, XS.REP)
        self.assertTrue(sb)
        self.assertNotEqual(xs.bind(sb, b'tcp://127.0.0.1:5560'), -1)
        sc = xs.socket(ctx, XS.REQ)
        self.assertTrue(sc)
        self.assertNotEqual(xs.connect(sc, b'tcp://127.0.0.1:5560'), -1)
        self.bounce(sb, sc)
        self.assertEqual(xs.close(sc), 0)
        self.assertEqual(xs.close(sb), 0)
        self.assertEqual(xs.term(ctx), 0)

    def test_regrep_device(self):
        from ctypes import sizeof, c_int, c_size_t, c_ubyte
        from crossroads.lowest import array
        XS, xs = twoget(self)
        ctx = xs.init()
        self.assertTrue(ctx)
        # create a req/rep device.
        xreq = xs.socket(ctx, XS.XREQ)
        self.assertTrue(xreq)
        self.assertNotEqual(xs.bind(xreq, b'tcp://127.0.0.1:5560'), -1)
        xrep = xs.socket(ctx, XS.XREP)
        self.assertTrue(xrep)
        self.assertNotEqual(xs.bind(xrep, b'tcp://127.0.0.1:5561'), -1)
        # create a worker.
        rep = xs.socket(ctx, XS.REP)
        self.assertTrue(rep)
        self.assertNotEqual(xs.connect(rep, b'tcp://127.0.0.1:5560'), -1)
        # create a client.
        req = xs.socket(ctx, XS.REQ)
        self.assertTrue(req)
        self.assertNotEqual(xs.connect(req, b'tcp://127.0.0.1:5561'), -1)
        # send a request.
        self.assertEqual(xs.send(req, b'ABC', 3, XS.SNDMORE), 3)
        self.assertEqual(xs.send(req, b'DEF', 3, 0), 3)
        # pass the request through the device.
        for i in lrange(4):  #  @UnusedVariable
            msg = xs.msg_t()
            self.assertEqual(xs.msg_init(msg), 0)
            rc = xs.recvmsg(xrep, msg, 0)
            self.assertTrue(rc >= 0)
            rcvmore = c_int()
            sz = c_size_t(sizeof(rcvmore))
            self.assertEqual(xs.getsockopt(xrep, XS.RCVMORE, rcvmore, sz), 0)
            rc = xs.sendmsg(xreq, msg, XS.SNDMORE if rcvmore else 0)
            self.assertTrue(rc >= 0)
        # receive the request.
        buff = array(c_ubyte, 3)
        self.assertEqual(xs.recv(rep, buff, 3, 0), 3)
        self.assertEqual(bytearray(buff), b'ABC')
        rcvmore = c_int()
        sz = c_size_t(sizeof(rcvmore))
        self.assertEqual(xs.getsockopt(rep, XS.RCVMORE, rcvmore, sz), 0)
        self.assertTrue(rcvmore)
        self.assertEqual(xs.recv(rep, buff, 3, 0), 3)
        self.assertEqual(bytearray(buff), b'DEF')
        self.assertEqual(xs.getsockopt(rep, XS.RCVMORE, rcvmore, sz), 0)
        self.assertFalse(rcvmore)
        # send the reply.
        self.assertEqual(xs.send(rep, b'GHI', 3, XS.SNDMORE), 3)
        self.assertEqual(xs.send(rep, b'JKL', 3, 0), 3)
        # pass the reply through the device.
        for i in lrange(4):  #  @UnusedVariable
            msg = xs.msg_t()
            self.assertEqual(xs.msg_init(msg), 0)
            self.assertTrue(xs.recvmsg(xreq, msg, 0) >= 0)
            self.assertEqual(xs.getsockopt(xreq, XS.RCVMORE, rcvmore, sz), 0)
            self.assertTrue(
                xs.sendmsg(xrep, msg, XS.SNDMORE if rcvmore else 0) >= 0
            )
        # receive the reply.
        self.assertEqual(xs.recv(req, buff, 3, 0), 3)
        self.assertEqual(bytearray(buff), b'GHI')
        self.assertEqual(xs.getsockopt(req, XS.RCVMORE, rcvmore, sz), 0
        )
        self.assertTrue(rcvmore)
        self.assertEqual(xs.recv(req, buff, 3, 0), 3)
        self.assertEqual(bytearray(buff), b'JKL')
        self.assertEqual(xs.getsockopt(req, XS.RCVMORE, rcvmore, sz), 0)
        self.assertFalse(rcvmore)
        # clean up.
        self.assertEqual(xs.close(req), 0)
        self.assertEqual(xs.close(rep), 0)
        self.assertEqual(xs.close(xrep), 0)
        self.assertEqual(xs.close(xreq), 0)
        self.assertEqual(xs.term(ctx), 0)

    def test_linger(self):
        from ctypes import byref, sizeof, c_int
        XS, xs = twoget(self)
        # create socket.
        ctx = xs.init()
        self.assertTrue(ctx)
        s = xs.socket(ctx, XS.PUSH)
        self.assertTrue(s)
        # set linger to 0.1 second.
        linger = c_int(100)
        # connect to non-existent endpoing.
        self.assertEqual(
            xs.setsockopt(s, XS.LINGER, byref(linger), sizeof(linger)), 0
        )
        self.assertNotEqual(xs.connect(s, b'tcp://127.0.0.1:5560'), -1)
        # send a message.
        self.assertEqual(xs.send(s, b'r', 1, 0), 1)
        # close the socket.
        self.assertEqual(xs.close(s), 0)
        # terminate the context. This should take 0.1 second.
        watch = xs.stopwatch_start()
        self.assertEqual(xs.term(ctx), 0)
        self.time_assert(xs.stopwatch_stop(watch) / 1000, linger.value)

    def test_msg_flags(self):
        from ctypes import sizeof, c_int, c_size_t
        XS, xs = twoget(self)
        # create the infrastructure
        ctx = xs.init()
        self.assertTrue(ctx)
        sb = xs.socket(ctx, XS.XREP)
        self.assertTrue(sb)
        self.assertNotEqual(xs.bind(sb, b'inproc://a'), -1)
        sc = xs.socket(ctx, XS.XREQ)
        self.assertTrue(sc)
        self.assertNotEqual(xs.connect(sc, b'inproc://a'), -1)
        # send 2 - part message.
        self.assertEqual(xs.send(sc, b'A', 1, XS.SNDMORE), 1)
        self.assertEqual(xs.send(sc, b'B', 1, 0), 1)
        # identity comes first.
        msg = xs.msg_t()
        self.assertEqual(xs.msg_init(msg), 0)
        self.assertTrue(xs.recvmsg(sb, msg, 0) >= 0)
        more = c_int()
        more_size = c_size_t(sizeof(more))
        self.assertEqual(xs.getmsgopt(msg, XS.MORE, more, more_size), 0)
        self.assertEqual(more.value, 1)
        # then the first part of the message body.
        self.assertEqual(xs.recvmsg(sb, msg, 0), 1)
        more_size = c_size_t(sizeof(more))
        self.assertEqual(xs.getmsgopt(msg, XS.MORE, more, more_size), 0)
        self.assertEqual(more.value, 1)
        # and finally, the second part of the message body.
        self.assertEqual(xs.recvmsg(sb, msg, 0), 1)
        more_size = c_size_t(sizeof(more))
        self.assertEqual(xs.getmsgopt(msg, XS.MORE, more, more_size), 0)
        self.assertEqual(more.value, 0)
        # deallocate the infrastructure.
        self.assertEqual(xs.close(sc), 0)
        self.assertEqual(xs.close(sb), 0)
        self.assertEqual(xs.term(ctx), 0)

    def test_invalid_rep(self):
        from stuf.six import int2byte
        from ctypes import byref, sizeof, c_int, c_ubyte
        from crossroads.lowest import array
        XS, xs = twoget(self)
        # create REQ/XREP wiring.
        ctx = xs.init()
        self.assertTrue(ctx)
        xrep_socket = xs.socket(ctx, XS.XREP)
        self.assertTrue(xrep_socket)
        req_socket = xs.socket(ctx, XS.REQ)
        self.assertTrue(req_socket)
        linger = c_int(0)
        self.assertEqual(xs.setsockopt(
            xrep_socket, XS.LINGER, byref(linger), sizeof(linger)
        ), 0)
        self.assertEqual(xs.setsockopt(
            req_socket, XS.LINGER, byref(linger), sizeof(linger)
        ), 0)
        self.assertNotEqual(xs.bind(xrep_socket, b'inproc://hi'), -1)
        self.assertNotEqual(xs.connect(req_socket, b'inproc://hi'), -1)
        # initial request.
        self.assertEqual(xs.send(req_socket, b'r', 1, 0), 1)
        # receive the request.
        addr = array(c_ubyte, 32)
        addr_size = xs.recv(xrep_socket, addr, sizeof(addr), 0)
        self.assertTrue(addr_size >= 0)
        bottom = array(c_ubyte, 1)
        self.assertEqual(xs.recv(xrep_socket, bottom, sizeof(bottom), 0), 0)
        body = array(c_ubyte, 1)
        self.assertEqual(xs.recv(xrep_socket, body, sizeof(body), 0), 1)
        # send invalid reply.
        self.assertEqual(xs.send(xrep_socket, addr, addr_size, 0), addr_size)
        # send valid reply.
        self.assertEqual(
            xs.send(xrep_socket, addr, addr_size, XS.SNDMORE), addr_size
        )
        self.assertEqual(xs.send(xrep_socket, bottom, 0, XS.SNDMORE), 0)
        self.assertEqual(xs.send(xrep_socket, b'b', 1, 0), 1)
        # check whether we've got the valid reply.
        self.assertEqual(xs.recv(req_socket, body, sizeof(body), 0), 1)
        self.assertEqual(int2byte(body[0]), b'b')
        # tear down the wiring.
        self.assertEqual(xs.close(xrep_socket), 0)
        self.assertEqual(xs.close(req_socket), 0)
        self.assertEqual(xs.term(ctx), 0)

    def test_resubscribe(self):
        from stuf.six import int2byte
        from time import sleep
        from ctypes import sizeof, c_ubyte
        from crossroads.lowest import array
        XS, xs = twoget(self)
        # create the basic infrastructure.
        ctx = xs.init()
        self.assertTrue(ctx)
        xpub = xs.socket(ctx, XS.XPUB)
        self.assertTrue(xpub)
        sub = xs.socket(ctx, XS.SUB)
        self.assertTrue(sub)
        # send two subscriptions upstream.
        self.assertNotEqual(xs.bind(xpub, b'tcp://127.0.0.1:5560'), -1)
        self.assertEqual(xs.setsockopt(sub, XS.SUBSCRIBE, b'a', 1), 0)
        self.assertEqual(xs.setsockopt(sub, XS.SUBSCRIBE, b'b', 1), 0)
        self.assertNotEqual(xs.connect(sub, b'tcp://127.0.0.1:5560'), -1)
        # check whether subscriptions are correctly received.
        buf = array(c_ubyte, 5)
        self.assertEqual(xs.recv(xpub, buf, sizeof(buf), 0), 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')
        self.assertEqual(xs.recv(xpub, buf, sizeof(buf), 0), 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(xs.close(xpub), 0)
        sleep(1)
        # re-establish the connection.
        xpub = xs.socket(ctx, XS.XPUB)
        self.assertTrue(xpub)
        self.assertNotEqual(xs.bind(xpub, b'tcp://127.0.0.1:5560'), -1)
        # we have to give control to the SUB socket here so that it has a chance
        # to resend the subscriptions.
        try:
            xs.recv(sub, buf, sizeof(buf), XS.DONTWAIT)
        except xs.XSError as e:
            self.assertEqual(e.errno, XS.EAGAIN)
        # check whether subscriptions are correctly generated.
        self.assertEqual(xs.recv(xpub, buf, sizeof(buf), 0), 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')
        self.assertEqual(xs.recv(xpub, buf, sizeof(buf), 0), 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')
        # clean up.
        self.assertEqual(xs.close(sub), 0)
        self.assertEqual(xs.close(xpub), 0)
        self.assertEqual(xs.term(ctx), 0)

    def test_wireformat(self):
        XS, xs = twoget(self)
        import socket
        from ctypes import sizeof, c_ubyte
        from crossroads.lowest import array
        # create the basic infrastructure.
        ctx = xs.init()
        self.assertTrue(ctx)
        push = xs.socket(ctx, XS.PUSH)
        self.assertTrue(push)
        pull = xs.socket(ctx, XS.PULL)
        self.assertTrue(push)
        # bind the peer and get the message.
        self.assertNotEqual(xs.bind(pull, b'tcp://127.0.0.1:5560'), -1)
        self.assertNotEqual(xs.bind(push, b'tcp://127.0.0.1:5561'), -1)
        # 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 = array(c_ubyte, 3)
        self.assertEqual(xs.recv(pull, buf, sizeof(buf), 0), 3)
        self.assertNotEqual(buf, b'abc')
        # let's push this data into another socket
        self.assertEqual(xs.send(push, buf, sizeof(buf), 0), 3)
        self.assertNotEqual(rpull.recv(3, 0), b'\x04\0abc')
        rpush.close()
        rpull.close()
        self.assertEqual(xs.close(pull), 0)
        self.assertEqual(xs.close(push), 0)
        self.assertEqual(xs.term(ctx), 0)

    def test_reconnect(self):
        import sys
        from time import sleep
        from ctypes import sizeof, c_ubyte
        from crossroads.lowest import array
        XS, xs = twoget(self)
        # create the basic infrastructure.
        ctx = xs.init()
        self.assertTrue(ctx)
        push = xs.socket(ctx, XS.PUSH)
        self.assertTrue(push)
        pull = xs.socket(ctx, XS.PULL)
        self.assertTrue(push)
        # connect before bind was done at the peer and send one message.
        self.assertNotEqual(xs.connect(push, b'tcp://127.0.0.1:5560'), -1)
        self.assertEqual(xs.send(push, b'ABC', 3, 0), 3)
        # wait a while for few attempts to reconnect to happen.
        sleep(1)
        # bind the peer and get the message.
        self.assertNotEqual(xs.bind(pull, b'tcp://127.0.0.1:5560'), -1)
        buf = array(c_ubyte, 3)
        self.assertEqual(xs.recv(pull, buf, sizeof(buf), 0), 3)
        # clean up.
        self.assertEqual(xs.close(push), 0)
        self.assertEqual(xs.close(pull), 0)
        if sys.platform not in ('win32', 'cygwin'):
            # now, let's test the same scenario with IPC.
            push = xs.socket(ctx, XS.PUSH)
            self.assertTrue(push)
            pull = xs.socket(ctx, XS.PULL)
            self.assertTrue(push)
            # connect before bind was done at the peer and send one message.
            self.assertNotEqual(xs.connect(push, b'ipc:///tmp/tester'), -1)
            self.assertEqual(xs.send(push, b'ABC', 3, 0), 3)
            # wait a while for few attempts to reconnect to happen.
            sleep(1)
            # bind the peer and get the message.
            self.assertNotEqual(xs.bind(pull, b'ipc:///tmp/tester'), -1)
            self.assertEqual(xs.recv(pull, buf, sizeof(buf), 0), 3)
            # clean up.
            self.assertEqual(xs.close(push), 0)
            self.assertEqual(xs.close(pull), 0)
        self.assertEqual(xs.term(ctx), 0)

    def test_hwn(self):
        from ctypes import sizeof, byref, c_int, c_ubyte
        from crossroads.lowest import array
        XS, xs = twoget(self)
        ctx = xs.init()
        self.assertTrue(ctx)
        # Create pair of socket, each with high watermark of 2. Thus the total
        # buffer space should be 4 messages.
        sb = xs.socket(ctx, XS.PULL)
        self.assertTrue(sb)
        hwm = c_int(2)
        self.assertEqual(
            xs.setsockopt(sb, XS.RCVHWM, byref(hwm), sizeof(hwm)), 0
        )
        self.assertNotEqual(xs.bind(sb, b'inproc://a'), -1)
        sc = xs.socket(ctx, XS.PUSH)
        self.assertTrue(sc)
        self.assertEqual(
            xs.setsockopt(sc, XS.SNDHWM, byref(hwm), sizeof(hwm)), 0
        )
        self.assertNotEqual(xs.connect(sc, b'inproc://a'), -1)
        # try to send 10 messages. Only 4 should succeed.
        for t in range(10):
            try:
                rc = xs.send(sc, None, 0, XS.DONTWAIT)
                if t < 4:
                    self.assertEqual(rc, 0)
            except xs.XSError as e:
                self.assertTrue(e.errno, XS.EAGAIN)
        # There should be now 4 messages pending, consume them.
        for i in lrange(4):  #  @UnusedVariable
            self.assertEqual(xs.recv(sb, None, 0, 0), 0)
        # Now it should be possible to send one more.
        self.assertEqual(xs.send(sc, None, 0, 0), 0)
        # Consume the remaining message.
        self.assertEqual(xs.recv(sb, None, 0, 0), 0)
        self.assertEqual(xs.close(sc), 0)
        self.assertEqual(xs.close(sb), 0)
        self.assertEqual(xs.term(ctx), 0)
        # Following part of the tests checks whether small HWMs don't interact
        # with command throttling in strange ways.
        ctx = xs.init()
        self.assertTrue(ctx)
        s1 = xs.socket(ctx, XS.PULL)
        self.assertTrue(s1)
        s2 = xs.socket(ctx, XS.PUSH)
        self.assertTrue(s2)
        hwm = c_int(5)
        self.assertEqual(
            xs.setsockopt(s2, XS.SNDHWM, byref(hwm), sizeof(hwm)), 0
        )
        self.assertTrue(xs.bind(s1, b'tcp://127.0.0.1:5858') >= 0)
        self.assertTrue(xs.connect(s2, b'tcp://127.0.0.1:5858') >= 0)
        for i in lrange(10):  #  @UnusedVariable
            self.assertEqual(xs.send(s2, b'test', 4, XS.DONTWAIT), 4)
            buf = array(c_ubyte, 4)
            self.assertEqual(xs.recv(s1, buf, sizeof(buf), 0), 4)
        self.assertEqual(xs.close(s2), 0)
        self.assertEqual(xs.close(s1), 0)
        self.assertEqual(xs.term(ctx), 0)

    def test_sub_forward(self):
        from time import sleep
        from ctypes import sizeof, c_ubyte
        from crossroads.lowest import array
        XS, xs = twoget(self)
        ctx = xs.init()
        self.assertTrue(ctx)
        # First, create an intermediate device.
        xpub = xs.socket(ctx, XS.XPUB)
        self.assertTrue(xpub)
        self.assertNotEqual(xs.bind(xpub, b'tcp://127.0.0.1:5560'), -1)
        xsub = xs.socket(ctx, XS.XSUB)
        self.assertTrue(xsub)
        self.assertNotEqual(xs.bind(xsub, b'tcp://127.0.0.1:5561'), -1)
        # Create a publisher.
        pub = xs.socket(ctx, XS.PUB)
        self.assertTrue(pub)
        self.assertNotEqual(xs.connect(pub, b'tcp://127.0.0.1:5561'), -1)
        # Create a subscriber.
        sub = xs.socket(ctx, XS.SUB)
        self.assertTrue(sub)
        self.assertNotEqual(xs.connect(sub, b'tcp://127.0.0.1:5560'), -1)
        # Subscribe for all messages.
        self.assertEqual(xs.setsockopt(sub, XS.SUBSCRIBE, '', 0), 0)
        # Pass the subscription upstream through the device.
        buff = array(c_ubyte, 32)
        rc = xs.recv(xpub, buff, sizeof(buff), 0)
        self.assertTrue(rc >= 0)
        self.assertTrue(xs.send(xsub, buff, rc, 0) >= 0)
        # Wait a bit till the subscription gets to the publisher.
        sleep(1)
        # Send an empty message.
        self.assertEqual(xs.send(pub, None, 0, 0), 0)
        # Pass the message downstream through the device.
        rc = xs.recv(xsub, buff, sizeof(buff), 0)
        self.assertTrue(rc >= 0)
        self.assertTrue(xs.send(xpub, buff, rc, 0) >= 0)
        # Receive the message in the subscriber.
        self.assertEqual(xs.recv(sub, buff, sizeof(buff), 0), 0)
        # Clean up.
        self.assertEqual(xs.close(xpub), 0)
        self.assertEqual(xs.close(xsub), 0)
        self.assertEqual(xs.close(pub), 0)
        self.assertEqual(xs.close(sub), 0)
        self.assertEqual(xs.term(ctx), 0)

    def test_shutdown(self):
        from time import sleep
        from ctypes import sizeof, c_ubyte
        from crossroads.lowest import array
        XS, xs = twoget(self)
        buf = array(c_ubyte, 32)
        # Create infrastructure.
        ctx = xs.init()
        self.assertTrue(ctx)
        push = xs.socket(ctx, XS.PUSH)
        self.assertTrue(push)
        push_id = xs.bind(push, b'tcp://127.0.0.1:5560')
        self.assertNotEqual(push_id, -1)
        pull = xs.socket(ctx, XS.PULL)
        self.assertTrue(pull)
        self.assertNotEqual(xs.connect(pull, b'tcp://127.0.0.1:5560'), -1)
        # Pass one message through to ensure the connection is established.
        self.assertEqual(xs.send(push, b'ABC', 3, 0), 3)
        self.assertEqual(xs.recv(pull, buf, sizeof(buf), 0), 3)
        # Shut down the bound endpoint.
        self.assertEqual(xs.shutdown(push, push_id), 0)
        sleep(1)
        try:
            # Check that sending would block (there's no outbound connection).
            xs.send(push, b'ABC', 3, XS.DONTWAIT)
        except xs.XSError as e:
            self.assertEqual(e.errno, XS.EAGAIN)
        # Clean up.
        self.assertEqual(xs.close(pull), 0)
        self.assertEqual(xs.close(push), 0)
        self.assertEqual(xs.term(ctx), 0)
        # Now the other way round...Create infrastructure.
        ctx = xs.init()
        self.assertTrue(ctx)
        pull = xs.socket(ctx, XS.PULL)
        self.assertTrue(pull)
        self.assertNotEqual(xs.bind(pull, b'tcp://127.0.0.1:5560'), -1)
        push = xs.socket(ctx, XS.PUSH)
        self.assertTrue(push)
        push_id = xs.connect(push, b'tcp://127.0.0.1:5560')
        self.assertNotEqual(push_id, -1)
        # Pass one message through to ensure the connection is established.
        self.assertEqual(xs.send(push, b'ABC', 3, 0), 3)
        self.assertEqual(xs.recv(pull, buf, sizeof(buf), 0), 3)
        # Shut down the bound endpoint.
        self.assertEqual(xs.shutdown(push, push_id), 0)
        sleep(1)
        try:
            # Check that sending would block (there's no outbound connection).
            xs.send(push, b'ABC', 3, XS.DONTWAIT)
        except xs.XSError as e:
            self.assertEqual(e.errno, XS.EAGAIN)
        # Clean up.
        self.assertEqual(xs.close(pull), 0)
        self.assertEqual(xs.close(push), 0)
        self.assertEqual(xs.term(ctx), 0)

    def test_survey(self):
        from crossroads.lowest import array
        from ctypes import sizeof, byref, c_int, c_ubyte
        XS, xs = twoget(self)
        buf = array(c_ubyte, 32)
        # Create the basic infrastructure.
        ctx = xs.init()
        self.assertTrue(ctx)
        xsurveyor = xs.socket(ctx, XS.XSURVEYOR)
        self.assertTrue(xsurveyor)
        self.assertNotEqual(xs.bind(xsurveyor, b'inproc://a'), -1)
        xrespondent = xs.socket(ctx, XS.XRESPONDENT)
        self.assertTrue(xrespondent)
        self.assertNotEqual(xs.bind(xrespondent, b'inproc://b'), -1)
        surveyor = xs.socket(ctx, XS.SURVEYOR)
        self.assertTrue(surveyor)
        self.assertNotEqual(xs.connect(surveyor, b'inproc://b'), -1)
        respondent1 = xs.socket(ctx, XS.RESPONDENT)
        self.assertTrue(respondent1)
        self.assertNotEqual(xs.connect(respondent1, b'inproc://a'), -1)
        respondent2 = xs.socket(ctx, XS.RESPONDENT)
        self.assertTrue(respondent2)
        self.assertNotEqual(xs.connect(respondent2, b'inproc://a'), -1)
        # Send the survey.
        self.assertEqual(xs.send(surveyor, b'ABC', 3, 0), 3)
        # Forward the survey through the intermediate device.
        # Survey consist of identity(4 bytes), survey ID(4 bytes) and the body.
        self.assertEqual(xs.recv(xrespondent, buf, sizeof(buf), 0), 4)
        self.assertEqual(xs.send(xsurveyor, buf, 4, XS.SNDMORE), 4)
        self.assertEqual(xs.recv(xrespondent, buf, sizeof(buf), 0), 4)
        self.assertEqual(xs.send(xsurveyor, buf, 4, XS.SNDMORE), 4)
        self.assertEqual(xs.recv(xrespondent, buf, sizeof(buf), 0), 3)
        self.assertEqual(xs.send(xsurveyor, buf, 3, 0), 3)
        # Respondent 1 responds to the survey.
        self.assertEqual(xs.recv(respondent1, buf, sizeof(buf), 0), 3)
        self.assertEqual(xs.send(respondent1, b'DE', 2, 0), 2)
        # Forward the response through the intermediate device.
        self.assertEqual(xs.recv(xsurveyor, buf, sizeof(buf), 0), 4)
        self.assertEqual(xs.send(xrespondent, buf, 4, XS.SNDMORE), 4)
        self.assertEqual(xs.recv(xsurveyor, buf, sizeof(buf), 0), 4)
        self.assertEqual(xs.send(xrespondent, buf, 4, XS.SNDMORE), 4)
        self.assertEqual(xs.recv(xsurveyor, buf, sizeof(buf), 0), 2)
        self.assertEqual(xs.send(xrespondent, buf, 2, 0), 2)
        # Surveyor gets the response.
        self.assertEqual(xs.recv(surveyor, buf, sizeof(buf), 0), 2)
        # Respondent 2 responds to the survey.
        self.assertEqual(xs.recv(respondent2, buf, sizeof(buf), 0), 3)
        self.assertEqual(xs.send(respondent2, b'FGHI', 4, 0), 4)
        # Forward the response through the intermediate device.
        self.assertEqual(xs.recv(xsurveyor, buf, sizeof(buf), 0), 4)
        self.assertEqual(xs.send(xrespondent, buf, 4, XS.SNDMORE), 4)
        self.assertEqual(xs.recv(xsurveyor, buf, sizeof(buf), 0), 4)
        self.assertEqual(xs.send(xrespondent, buf, 4, XS.SNDMORE), 4)
        self.assertEqual(xs.recv(xsurveyor, buf, sizeof(buf), 0), 4)
        self.assertEqual(xs.send(xrespondent, buf, 4, 0), 4)
        # Surveyor gets the response.
        self.assertEqual(xs.recv(surveyor, buf, sizeof(buf), 0), 4)
        # Now let's test whether survey timeout works as expected.
        timeout = c_int(100)
        self.assertEqual(xs.setsockopt(
            surveyor, XS.SURVEY_TIMEOUT, byref(timeout), sizeof(timeout)
        ), 0)
        self.assertEqual(xs.send(surveyor, b'ABC', 3, 0), 3)
        watch = xs.stopwatch_start()
        try:
            xs.recv(surveyor, buf, sizeof(buf), 0)
        except xs.XSError as e:
            self.assertEqual(e.errno, XS.ETIMEDOUT)
        self.time_assert(xs.stopwatch_stop(watch) / 1000, timeout.value)
        # Test whether responses for old surveys are discarded. First,
        # initiate new survey.
        self.assertEqual(xs.setsockopt(
            surveyor, XS.SURVEY_TIMEOUT, byref(timeout), sizeof(timeout)
        ), 0)
        self.assertEqual(xs.send(surveyor, b'DE', 2, 0), 2)
        # Read, process and reply to the old survey.
        self.assertEqual(xs.recv(xrespondent, buf, sizeof(buf), 0), 4)
        self.assertEqual(xs.send(xrespondent, buf, 4, XS.SNDMORE), 4)
        self.assertEqual(xs.recv(xrespondent, buf, sizeof(buf), 0), 4)
        self.assertEqual(xs.send(xrespondent, buf, 4, XS.SNDMORE), 4)
        self.assertEqual(xs.recv(xrespondent, buf, sizeof(buf), 0), 3)
        self.assertEqual(xs.send(xrespondent, buf, 3, 0), 3)
        # Read, process and reply to the new survey.
        self.assertEqual(xs.recv(xrespondent, buf, sizeof(buf), 0), 4)
        self.assertEqual(xs.send(xrespondent, buf, 4, XS.SNDMORE), 4)
        self.assertEqual(xs.recv(xrespondent, buf, sizeof(buf), 0), 4)
        self.assertEqual(xs.send(xrespondent, buf, 4, XS.SNDMORE), 4)
        self.assertEqual(xs.recv(xrespondent, buf, sizeof(buf), 0), 2)
        self.assertEqual(xs.send(xrespondent, buf, 2, 0), 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(xs.recv(surveyor, buf, sizeof(buf), 0), 2)
        self.assertEqual(xs.close(respondent2), 0)
        self.assertEqual(xs.close(respondent1), 0)
        self.assertEqual(xs.close(surveyor), 0)
        self.assertEqual(xs.close(xrespondent), 0)
        self.assertEqual(xs.close(xsurveyor), 0)
        self.assertEqual(xs.term(ctx), 0)

    def test_timeo(self):
        from time import sleep
        from ctypes import c_int, byref, c_char_p, c_size_t, sizeof
        XS, xs = twoget(self)

        def timeo_worker(ctx_):
            # Worker thread connects after delay of 1 second. Then it waits
            # for 1 more second, so that async connect has time to succeed.
            sleep(1)
            sc = xs.socket(ctx_, XS.PUSH)
            self.assertTrue(sc)
            self.assertNotEqual(xs.connect(sc, b'inproc://timeout_test'), -1)
            sleep(1)
            self.assertEqual(xs.close(sc), 0)
        ctx = xs.init()
        self.assertTrue(ctx)
        # create a disconnected socket.
        sb = xs.socket(ctx, XS.PULL)
        self.assertTrue(sb)
        self.assertNotEqual(xs.bind(sb, b'inproc://timeout_test'), -1)
        # check whether non-blocking recv returns immediately.
        buf = c_char_p(b'12345678ABCDEFGH12345678abcdefgh')
        try:
            xs.recv(sb, buf, 32, XS.DONTWAIT)
        except xs.XSError as e:
            self.assertEqual(e.errno, XS.EAGAIN)
        # check whether recv timeout is honoured.
        timeout = c_int(500)
        timeout_size = c_size_t(sizeof(timeout))
        self.assertEqual(
            xs.setsockopt(sb, XS.RCVTIMEO, byref(timeout), timeout_size), 0
        )
        watch = xs.stopwatch_start()
        try:
            xs.recv(sb, buf, 32, 0)
        except xs.XSError as e:
            self.assertEqual(e.errno, XS.EAGAIN)
        self.time_assert(xs.stopwatch_stop(watch) / 1000, timeout.value)
        # check whether connection during the wait doesn't distort the timeout.
        timeout = c_int(2000)
        self.assertEqual(
            xs.setsockopt(sb, XS.RCVTIMEO, byref(timeout), timeout_size), 0
        )
#        thread = self.thread_create(timeo_worker, ctx)
#        self.assertTrue(thread)
        watch = xs.stopwatch_start()
        try:
            self.assertEqual(xs.recv(sb, buf, 32, 0), -1)
        except xs.XSError as e:
            self.assertTrue(e.errno, XS.EAGAIN)
        self.time_assert(xs.stopwatch_stop(watch) / 1000, timeout.value)
#        self.thread_join(thread)
        # check that timeouts don't break normal message transfer.
        sc = xs.socket(ctx, XS.PUSH)
        self.assertTrue(sc)
        self.assertEqual(
            xs.setsockopt(sb, XS.RCVTIMEO, byref(timeout), timeout_size), 0
        )
        self.assertEqual(
            xs.setsockopt(sb, XS.SNDTIMEO, byref(timeout), timeout_size), 0
        )
        self.assertNotEqual(xs.connect(sc, b'inproc://timeout_test'), -1)
        self.assertEqual(xs.send(sc, buf, 32, 0), 32)
        self.assertEqual(xs.recv(sb, buf, 32, 0), 32)
        # Clean-up.
        self.assertEqual(xs.close(sc), 0)
        self.assertEqual(xs.close(sb), 0)
        self.assertEqual(xs.term(ctx), 0)

    def test_polltimeo(self):
        from time import sleep
        XS, xs = twoget(self)

        def polltimeo_worker(ctx_):
            # Worker thread connects after delay of 1 second. Then it waits
            # for 1 more second, so that async connect has time to succeed.
            sleep(1)
            sc = xs.socket(ctx_, XS.PUSH)
            self.assertTrue(sc)
            self.assertNotEqual(xs.connect(sc, b'inproc://timeout_test'), -1)
            sleep(1)
            self.assertEqual(xs.close(sc), 0)
        ctx = xs.init()
        self.assertTrue(ctx)
        # create a disconnected socket.
        sb = xs.socket(ctx, XS.PULL)
        self.assertTrue(sb)
        self.assertNotEqual(xs.bind(sb, b'inproc://timeout_test'), -1)
        # check whether timeout is honoured.
        pi = xs.pollitem_t()
        pi.socket = sb
        pi.events = XS.POLLIN
        watch = xs.stopwatch_start()
        self.assertEqual(xs.poll(pi, 1, 500), 0)
        self.time_assert(xs.stopwatch_stop(watch) / 1000, 500)
        # Check whether connection during the wait doesn't distort the timeout.
#        thread = self.thread_create(polltimeo_worker, ctx)
#        self.assertTrue(thread)
        watch = xs.stopwatch_start()
        self.assertEqual(xs.poll(pi, 1, 2000), 0)
        self.time_assert(xs.stopwatch_stop(watch) / 1000, 2000)
#        self.thread_join(thread)
        # clean-up.
        self.assertEqual(xs.close(sb), 0)
        self.assertEqual(xs.term(ctx), 0)

#    def test_shutdown_stress(self):
#        from ctypes import c_int, byref, sizeof, get_errno
#        XS, xs = twoget(self)
#        THREAD_COUNT = 100
#        def shutdown_stress_worker(s_):
#            rc = xs.connect(s_, b'tcp://127.0.0.1:5560')
#            self.assertNotEqual(rc, -1)
#            # Start closing the socket while the connecting process is underway.
#            rc = xs.close(s_)
#            self.assertEqual(rc, 0)
#        threads = []  # THREAD_COUNT
#        for j in lrange(10):
#            # Check the shutdown with many parallel I/O threads.
#            ctx = xs.init()
#            self.assertTrue(ctx)
#            io_threads = c_int(7)
#            rc = xs.setctxopt(
#                ctx, XS.IO_THREADS, byref(io_threads), sizeof(io_threads)
#            )
#            self.assertEqual(rc, 0)
#            s1 = xs.socket(ctx, XS.PUB)
#            self.assertTrue(s1)
#            rc = xs.bind(s1, b'tcp://127.0.0.1:5560')
#            self.assertNotEqual(rc, -1)
#            for i in lrange(THREAD_COUNT):
#                s2 = xs.socket(ctx, XS.SUB)
#                if not s2 and (
#                    get_errno() == XS.EMFILE or get_errno() == XS.ENFILE
#                ):
#                    threads[i] = None
#                else:
#                    self.assertTrue(s2)
#                    threads[i] = self.thread_create(shutdown_stress_worker, s2)
#                    self.assertTrue(threads[i])
#            for i in lrange(THREAD_COUNT):
#                if threads[i]:
#                    self.thread_join(threads[i])
#            rc = xs.close(s1)
#            self.assertEqual(rc, 0)
#        self.assertEqual(xs.term(ctx), 0)
#
#    def test_backlog(self):
#        BACKLOG = 10
#        PARALLEL_CONNECTS = 50
#        def ipc_worker (ctx_):
#            int rc;
#            push;
#            push = xs.socket (ctx_, XS.PUSH);
#            self.assertTrue(push);
#            rc = xs.connect (push, 'ipc:///tmp/test-backlog');
#            self.assertTrue(rc >= 0);
#            rc = xs.send (push, None, 0, 0);
#            self.assertEqual(rc, 0)
#            rc = xs.close (push);
#            self.assertEqual(rc, 0)
#
#        def tcp_worker(ctx_):
#            int rc;
#            push;
#            push = xs.socket (ctx_, XS.PUSH);
#            self.assertTrue(push);
#            rc = xs.connect (push, 'tcp://127.0.0.1:5560');
#            self.assertTrue(rc >= 0);
#            rc = xs.send (push, None, 0, 0);
#            self.assertEqual(rc, 0)
#            rc = xs.close (push);
#            self.assertEqual(rc, 0)
#            int rc;
#            ctx;
#            pull;
#            int i;
#            int backlog;
#            threads [PARALLEL_CONNECTS];
#            #  Test the exhaustion on IPC backlog.
#        #if !defined XS.HAVE_WINDOWS byrefbyref(!defined XS.HAVE_OPENVMS
#            ctx = xs.init ();
#            self.assertTrue(ctx)
#            pull = xs.socket (ctx, XS.PULL);
#            self.assertTrue(pull);
#            backlog = BACKLOG;
#            rc = xs.setsockopt (pull, XS.BACKLOG, byrefbacklog, sizeof (backlog));
#            self.assertEqual(rc, 0)
#            rc = xs.bind (pull, 'ipc:///tmp/test-backlog');
#            self.assertTrue(rc >= 0);
#            for (i = 0; i < PARALLEL_CONNECTS; i++)
#                threads [i] = thread_create (ipc_worker, ctx);
#            for (i = 0; i < PARALLEL_CONNECTS; i++) {
#                rc = xs.recv (pull, None, 0, 0);
#                self.assertEqual(rc, 0)
#            }
#            rc = xs.close (pull);
#            self.assertEqual(rc, 0)
#            rc = xs.term (ctx);
#            self.assertEqual(rc, 0)
#            for (i = 0; i < PARALLEL_CONNECTS; i++)
#                thread_join (threads [i]);
#        #endif
#            # Test the exhaustion on TCP backlog.
#            ctx = xs.init ();
#            self.assertTrue(ctx)
#            pull = xs.socket (ctx, XS.PULL);
#            self.assertTrue(pull);
#            backlog = BACKLOG;
#            rc = xs.setsockopt (pull, XS.BACKLOG, byrefbacklog, sizeof (backlog));
#            self.assertEqual(rc, 0)
#            rc = xs.bind (pull, b'tcp://127.0.0.1:5560');
#            self.assertTrue(rc >= 0);
#            for (i = 0; i < PARALLEL_CONNECTS; i++)
#                threads [i] = thread_create (tcp_worker, ctx);
#            for (i = 0; i < PARALLEL_CONNECTS; i++) {
#                rc = xs.recv (pull, None, 0, 0);
#                self.assertEqual(rc, 0)
#            }
#            rc = xs.close (pull);
#            self.assertEqual(rc, 0)
#            rc = xs.term (ctx);
#            self.assertEqual(rc, 0)
#
#            for (i = 0; i < PARALLEL_CONNECTS; i++)
#                thread_join (threads [i]);
#        #endif
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.