File: select_connection_ioloop_tests.py

package info (click to toggle)
python-pika 1.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,064 kB
  • sloc: python: 20,886; makefile: 136
file content (1110 lines) | stat: -rw-r--r-- 38,602 bytes parent folder | download | duplicates (2)
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
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
# -*- coding: utf-8 -*-
"""
Tests for SelectConnection IOLoops

"""

from __future__ import print_function

import errno
import datetime
import functools
import logging
import os
import platform
import select
import signal
import socket
import sys
import time
import threading
import unittest
from unittest import mock

import pika
from pika import compat
from pika.adapters import select_connection


# protected-access
# pylint: disable=W0212

# missing-docstring
# pylint: disable=C0111

# invalid-name
# pylint: disable=C0103

# attribute-defined-outside-init
# pylint: disable=W0201


LOGGER = logging.getLogger(__name__)

EPOLL_SUPPORTED = hasattr(select, 'epoll')
POLL_SUPPORTED = hasattr(select, 'poll') and hasattr(select.poll(), 'modify')
KQUEUE_SUPPORTED = hasattr(select, 'kqueue')

POLLIN = getattr(select, 'POLLIN', 0) or 1
POLLOUT = getattr(select, 'POLLOUT', 0) or 4
POLLERR = getattr(select, 'POLLERR', 0) or 8

POLLHUP = getattr(select, 'POLLHUP', 0) or 16
POLLNVAL = getattr(select, 'POLLNVAL', 0) or 32
POLLPRI = getattr(select, 'POLLPRI', 0) or 2


def _trace_stderr(fmt, *args):
    """Format and output the text to stderr"""
    print((fmt % args) + "\n", end="", file=sys.stderr)


def _fd_events_to_str(events):
    str_events = '{}: '.format(events)

    if events & POLLIN:
        str_events += "In."
    if events & POLLOUT:
        str_events += "Out."
    if events & POLLERR:
        str_events += "Err."
    if events & POLLHUP:
        str_events += "Hup."
    if events & POLLNVAL:
        str_events += "Inval."
    if events & POLLPRI:
        str_events += "Pri."


    remainig_events = events & ~(POLLIN |
                                 POLLOUT |
                                 POLLERR |
                                 POLLHUP |
                                 POLLNVAL |
                                 POLLPRI)
    if remainig_events:
        str_events += '+{}'.format(bin(remainig_events))

    return str_events


class IOLoopBaseTest(unittest.TestCase):
    SELECT_POLLER = None
    TIMEOUT = 1.5

    def setUp(self):
        select_type_patch = mock.patch.multiple(
            select_connection, SELECT_TYPE=self.SELECT_POLLER)
        select_type_patch.start()
        self.addCleanup(select_type_patch.stop)

        self.ioloop = select_connection.IOLoop()
        self.addCleanup(setattr, self, 'ioloop', None)
        self.addCleanup(self.ioloop.close)

        activate_poller_patch = mock.patch.object(
            self.ioloop._poller,
            'activate_poller',
            wraps=self.ioloop._poller.activate_poller)
        activate_poller_patch.start()
        self.addCleanup(activate_poller_patch.stop)

        deactivate_poller_patch = mock.patch.object(
            self.ioloop._poller,
            'deactivate_poller',
            wraps=self.ioloop._poller.deactivate_poller)
        deactivate_poller_patch.start()
        self.addCleanup(deactivate_poller_patch.stop)

    def shortDescription(self):
        method_desc = super(IOLoopBaseTest, self).shortDescription()
        return '%s (%s)' % (method_desc, self.SELECT_POLLER)

    def start(self):
        """Setup timeout handler for detecting 'no-activity'
        and start polling.

        """
        fail_timer = self.ioloop.call_later(self.TIMEOUT, self.on_timeout)
        self.addCleanup(self.ioloop.remove_timeout, fail_timer)
        self.ioloop.start()

        self.ioloop._poller.activate_poller.assert_called_once_with()  # pylint: disable=E1101
        self.ioloop._poller.deactivate_poller.assert_called_once_with()  # pylint: disable=E1101

    def on_timeout(self):
        """Called when stuck waiting for connection to close"""
        self.ioloop.stop()  # force the ioloop to stop
        self.fail('Test timed out')


class IOLoopCloseClosesSubordinateObjectsTestSelect(IOLoopBaseTest):
    """ Test ioloop being closed """
    SELECT_POLLER = 'select'

    def start_test(self):
        with mock.patch.multiple(self.ioloop,
                                 _timer=mock.DEFAULT,
                                 _poller=mock.DEFAULT,
                                 _callbacks=mock.DEFAULT) as mocks:
            self.ioloop.close()
            mocks['_timer'].close.assert_called_once_with()
            mocks['_poller'].close.assert_called_once_with()
            self.assertEqual(self.ioloop._callbacks, [])


class IOLoopCloseAfterStartReturnsTest(IOLoopBaseTest):
    """ Test IOLoop.close() after normal return from start(). """
    SELECT_POLLER = 'select'

    def start_test(self):
        self.ioloop.stop() # so start will terminate quickly
        self.start()
        self.ioloop.close()
        self.assertEqual(self.ioloop._callbacks, [])


class IOLoopStartReentrancyNotAllowedTestSelect(IOLoopBaseTest):
    """ Test calling IOLoop.start() while arleady in start() raises exception. """
    SELECT_POLLER = 'select'

    def start_test(self):
        callback_completed = []

        def call_close_from_callback():
            with self.assertRaises(RuntimeError) as cm:
                self.ioloop.start()

            self.assertEqual(cm.exception.args[0],
                             'IOLoop is not reentrant and is already running')
            self.ioloop.stop()
            callback_completed.append(1)

        self.ioloop.add_callback_threadsafe(call_close_from_callback)
        self.start()
        self.assertEqual(callback_completed, [1])


class IOLoopCloseBeforeStartReturnsTestSelect(IOLoopBaseTest):
    """ Test calling IOLoop.close() before return from start() raises exception. """
    SELECT_POLLER = 'select'

    def start_test(self):
        callback_completed = []

        def call_close_from_callback():
            with self.assertRaises(AssertionError) as cm:
                self.ioloop.close()

            self.assertEqual(cm.exception.args[0],
                             'Cannot call close() before start() unwinds.')
            self.ioloop.stop()
            callback_completed.append(1)

        self.ioloop.add_callback_threadsafe(call_close_from_callback)
        self.start()
        self.assertEqual(callback_completed, [1])


class IOLoopThreadStopTestSelect(IOLoopBaseTest):
    """ Test ioloop being stopped by another Thread. """
    SELECT_POLLER = 'select'

    def start_test(self):
        """Starts a thread that stops ioloop after a while and start polling"""
        timer = threading.Timer(
            0.1,
            lambda: self.ioloop.add_callback_threadsafe(self.ioloop.stop))
        self.addCleanup(timer.cancel)
        timer.start()
        self.start() # NOTE: Normal return from `start()` constitutes success


@unittest.skipIf(not POLL_SUPPORTED, 'poll not supported')
class IOLoopThreadStopTestPoll(IOLoopThreadStopTestSelect):
    """Same as IOLoopThreadStopTestSelect but uses 'poll' syscall."""
    SELECT_POLLER = 'poll'


@unittest.skipIf(not EPOLL_SUPPORTED, 'epoll not supported')
class IOLoopThreadStopTestEPoll(IOLoopThreadStopTestSelect):
    """Same as IOLoopThreadStopTestSelect but uses 'epoll' syscall."""
    SELECT_POLLER = 'epoll'


@unittest.skipIf(not KQUEUE_SUPPORTED, 'kqueue not supported')
class IOLoopThreadStopTestKqueue(IOLoopThreadStopTestSelect):
    """Same as IOLoopThreadStopTestSelect but uses 'kqueue' syscall."""
    SELECT_POLLER = 'kqueue'


class IOLoopAddCallbackAfterCloseDoesNotRaiseTestSelect(IOLoopBaseTest):
    """ Test ioloop add_callback_threadsafe() after ioloop close doesn't raise exception. """
    SELECT_POLLER = 'select'

    def start_test(self):
        # Simulate closing after start returns
        self.ioloop.stop()  # so that start() returns ASAP
        self.start() # NOTE: Normal return from `start()` constitutes success
        self.ioloop.close()

        # Expect: add_callback_threadsafe() won't raise after ioloop.close()
        self.ioloop.add_callback_threadsafe(lambda: None)


# TODO FUTURE - fix this flaky test
@unittest.skipIf(platform.python_implementation() == 'PyPy', 'test is flaky on PyPy')
class IOLoopTimerTestSelect(IOLoopBaseTest):
    """Set a bunch of very short timers to fire in reverse order and check
    that they fire in order of time, not

    """
    NUM_TIMERS = 5
    TIMER_INTERVAL = 0.25
    SELECT_POLLER = 'select'

    def set_timers(self):
        """Set timers that timers that fires in succession with the specified
        interval.

        """
        self.timer_stack = list()
        for i in range(self.NUM_TIMERS, 0, -1):
            deadline = i * self.TIMER_INTERVAL
            self.ioloop.call_later(
                deadline, functools.partial(self.on_timer, i))
            self.timer_stack.append(i)

    def start_test(self):
        """Set timers and start ioloop."""
        self.set_timers()
        self.start()

    def on_timer(self, val):
        """A timeout handler that verifies that the given parameter matches
        what is expected.

        """
        self.assertEqual(val, self.timer_stack.pop())
        if not self.timer_stack:
            self.ioloop.stop()

    def test_normal(self):
        """Setup 5 timeout handlers and observe them get invoked one by one."""
        self.start_test()

    def test_timer_for_deleting_itself(self):
        """Verifies that an attempt to delete a timeout within the
        corresponding handler generates no exceptions.

        """
        self.timer_stack = list()
        handle_holder = []
        self.timer_got_fired = False
        self.handle = self.ioloop.call_later(
            0.1, functools.partial(
                self._on_timer_delete_itself, handle_holder))
        handle_holder.append(self.handle)
        self.start()
        self.assertTrue(self.timer_got_called)

    def _on_timer_delete_itself(self, handle_holder):
        """A timeout handler that tries to remove itself."""
        self.assertEqual(self.handle, handle_holder.pop())
        # This removal here should not raise exception by itself nor
        # in the caller SelectPoller._process_timeouts().
        self.timer_got_called = True
        self.ioloop.remove_timeout(self.handle)
        self.ioloop.stop()

    def test_timer_delete_another(self):
        """Verifies that an attempt by a timeout handler to delete another,
        that  is ready to run, cancels the execution of the latter without
        generating an exception. This should pose no issues.

        """
        holder_for_target_timer = []
        self.ioloop.call_later(
            0.01, functools.partial(
                self._on_timer_delete_another, holder_for_target_timer))
        timer_2 = self.ioloop.call_later(0.02, self._on_timer_no_call)
        holder_for_target_timer.append(timer_2)
        time.sleep(0.03)  # so that timer_1 and timer_2 fires at the same time.
        self.start()
        self.assertTrue(self.deleted_another_timer)
        self.assertTrue(self.concluded)

    def _on_timer_delete_another(self, holder):
        """A timeout handler that tries to remove another timeout handler
        that is ready to run. This should pose no issues.

        """
        target_timer = holder[0]
        self.ioloop.remove_timeout(target_timer)
        self.deleted_another_timer = True

        def _on_timer_conclude():
            """A timeout handler that is called to verify outcome of calling
            or not calling of previously set handlers.

            """
            self.concluded = True
            self.assertTrue(self.deleted_another_timer)
            self.assertIsNone(target_timer.callback)
            self.ioloop.stop()

        self.ioloop.call_later(0.01, _on_timer_conclude)

    def _on_timer_no_call(self):
        """A timeout handler that is used when it's assumed not be called."""
        self.fail('deleted timer callback was called.')


@unittest.skipIf(not POLL_SUPPORTED, 'poll not supported')
class IOLoopTimerTestPoll(IOLoopTimerTestSelect):
    """Same as IOLoopTimerTestSelect but uses 'poll' syscall"""
    SELECT_POLLER = 'poll'


@unittest.skipIf(not EPOLL_SUPPORTED, 'epoll not supported')
class IOLoopTimerTestEPoll(IOLoopTimerTestSelect):
    """Same as IOLoopTimerTestSelect but uses 'epoll' syscall"""
    SELECT_POLLER = 'epoll'


@unittest.skipIf(not KQUEUE_SUPPORTED, 'kqueue not supported')
class IOLoopTimerTestKqueue(IOLoopTimerTestSelect):
    """Same as IOLoopTimerTestSelect but uses 'kqueue' syscall"""
    SELECT_POLLER = 'kqueue'


class IOLoopSleepTimerTestSelect(IOLoopTimerTestSelect):
    """Sleep until all the timers should have passed and check they still
        fire in deadline order"""

    def start_test(self):
        """ Setup timers, sleep and start polling """
        self.set_timers()
        time.sleep(self.NUM_TIMERS * self.TIMER_INTERVAL)
        self.start()


@unittest.skipIf(not POLL_SUPPORTED, 'poll not supported')
class IOLoopSleepTimerTestPoll(IOLoopSleepTimerTestSelect):
    """Same as IOLoopSleepTimerTestSelect but uses 'poll' syscall"""
    SELECT_POLLER = 'poll'


@unittest.skipIf(not EPOLL_SUPPORTED, 'epoll not supported')
class IOLoopSleepTimerTestEPoll(IOLoopSleepTimerTestSelect):
    """Same as IOLoopSleepTimerTestSelect but uses 'epoll' syscall"""
    SELECT_POLLER = 'epoll'


@unittest.skipIf(not KQUEUE_SUPPORTED, 'kqueue not supported')
class IOLoopSleepTimerTestKqueue(IOLoopSleepTimerTestSelect):
    """Same as IOLoopSleepTimerTestSelect but uses 'kqueue' syscall"""
    SELECT_POLLER = 'kqueue'


class IOLoopSocketBaseSelect(IOLoopBaseTest):
    """A base class for setting up a communicating pair of sockets."""
    SELECT_POLLER = 'select'
    READ_SIZE = 1024

    def save_sock(self, sock):
        """Store 'sock' in self.sock_map and return the fileno."""
        fd_ = sock.fileno()
        self.sock_map[fd_] = sock
        return fd_

    def setUp(self):
        super(IOLoopSocketBaseSelect, self).setUp()
        self.sock_map = dict()
        self.create_accept_socket()

    def tearDown(self):
        for fd_ in self.sock_map:
            self.ioloop.remove_handler(fd_)
            self.sock_map[fd_].close()
        super(IOLoopSocketBaseSelect, self).tearDown()

    def create_accept_socket(self):
        """Create a socket and setup 'accept' handler"""
        listen_sock = socket.socket()
        listen_sock.setblocking(0)
        listen_sock.bind(('localhost', 0))
        listen_sock.listen(1)
        fd_ = self.save_sock(listen_sock)
        self.listen_addr = listen_sock.getsockname()
        self.ioloop.add_handler(fd_, self.do_accept, self.ioloop.READ)

    def create_write_socket(self, on_connected):
        """ Create a pair of socket and setup 'connected' handler """
        write_sock = socket.socket()
        write_sock.setblocking(0)
        err = write_sock.connect_ex(self.listen_addr)
        # NOTE we get errno.EWOULDBLOCK 10035 on Windows
        self.assertIn(err, (errno.EINPROGRESS, errno.EWOULDBLOCK))
        fd_ = self.save_sock(write_sock)
        self.ioloop.add_handler(fd_, on_connected, self.ioloop.WRITE)
        return write_sock

    def do_accept(self, fd_, events):
        """ Create socket from the given fd_ and setup 'read' handler """
        self.assertEqual(events, self.ioloop.READ)
        listen_sock = self.sock_map[fd_]
        read_sock, _ = listen_sock.accept()
        fd_ = self.save_sock(read_sock)
        self.ioloop.add_handler(fd_, self.do_read, self.ioloop.READ)

    def connected(self, _fd, _events):
        """ Create socket from given _fd and respond to 'connected'.
            Implemenation is subclass's responsibility. """
        self.fail("IOLoopSocketBase.connected not extended")

    def do_read(self, fd_, events):
        """ read from fd and check the received content """
        self.assertEqual(events, self.ioloop.READ)
        # NOTE Use socket.recv instead of os.read for Windows compatibility
        self.verify_message(self.sock_map[fd_].recv(self.READ_SIZE))

    def verify_message(self, _msg):
        """ See if 'msg' matches what is expected. This is a stub.
            Real implementation is subclass's responsibility """
        self.fail("IOLoopSocketBase.verify_message not extended")

    def on_timeout(self):
        """called when stuck waiting for connection to close"""
        # force the ioloop to stop
        self.ioloop.stop()
        self.fail('Test timed out')


@unittest.skipIf(not POLL_SUPPORTED, 'poll not supported')
class IOLoopSocketBasePoll(IOLoopSocketBaseSelect):
    """Same as IOLoopSocketBaseSelect but uses 'poll' syscall"""
    SELECT_POLLER = 'poll'


@unittest.skipIf(not EPOLL_SUPPORTED, 'epoll not supported')
class IOLoopSocketBaseEPoll(IOLoopSocketBaseSelect):
    """Same as IOLoopSocketBaseSelect but uses 'epoll' syscall"""
    SELECT_POLLER = 'epoll'


@unittest.skipIf(not KQUEUE_SUPPORTED, 'kqueue not supported')
class IOLoopSocketBaseKqueue(IOLoopSocketBaseSelect):
    """ Same as IOLoopSocketBaseSelect but uses 'kqueue' syscall """
    SELECT_POLLER = 'kqueue'


class IOLoopSimpleMessageTestCaseSelect(IOLoopSocketBaseSelect):
    """Test read/write by creating a pair of sockets, writing to one
    end and reading from the other

    """
    def start(self):
        """Create a pair of sockets and poll"""
        self.create_write_socket(self.connected)
        super(IOLoopSimpleMessageTestCaseSelect, self).start()

    def connected(self, fd, events):
        """Respond to 'connected' event by writing to the write-side."""
        self.assertEqual(events, self.ioloop.WRITE)
        # NOTE Use socket.send instead of os.write for Windows compatibility
        self.sock_map[fd].send(b'X')
        self.ioloop.update_handler(fd, 0)

    def verify_message(self, msg):
        """Make sure we get what is expected and stop polling """
        self.assertEqual(msg, b'X')
        self.ioloop.stop()

    def start_test(self):
        """Simple message Test"""
        self.start()


@unittest.skipIf(not POLL_SUPPORTED, 'poll not supported')
class IOLoopSimpleMessageTestCasetPoll(IOLoopSimpleMessageTestCaseSelect):
    """Same as IOLoopSimpleMessageTestCaseSelect but uses 'poll' syscall"""
    SELECT_POLLER = 'poll'


@unittest.skipIf(not EPOLL_SUPPORTED, 'epoll not supported')
class IOLoopSimpleMessageTestCasetEPoll(IOLoopSimpleMessageTestCaseSelect):
    """Same as IOLoopSimpleMessageTestCaseSelect but uses 'epoll' syscall"""
    SELECT_POLLER = 'epoll'


@unittest.skipIf(not KQUEUE_SUPPORTED, 'kqueue not supported')
class IOLoopSimpleMessageTestCasetKqueue(IOLoopSimpleMessageTestCaseSelect):
    """Same as IOLoopSimpleMessageTestCaseSelect but uses 'kqueue' syscall"""
    SELECT_POLLER = 'kqueue'


class IOLoopEintrTestCaseSelect(IOLoopBaseTest):
    """ Tests if EINTR is properly caught and polling gets resumed. """
    SELECT_POLLER = 'select'
    MSG_CONTENT = b'hello'

    @staticmethod
    def signal_handler(signum, interrupted_stack):
        """A signal handler that gets called in response to
           os.kill(signal.SIGUSR1)."""
        pass

    def _eintr_read_handler(self, fileno, events):
        """Read from within poll loop that gets receives eintr error."""
        self.assertEqual(events, self.ioloop.READ)

        sock = socket.fromfd(
            os.dup(fileno), socket.AF_INET, socket.SOCK_STREAM)
        self.addCleanup(sock.close)

        mesg = sock.recv(256)
        self.assertEqual(mesg, self.MSG_CONTENT)
        self.poller.stop()
        self._eintr_read_handler_is_called = True

    def _eintr_test_fail(self):
        """This function gets called when eintr-test failed to get
           _eintr_read_handler called."""
        self.poller.stop()
        self.fail('Eintr-test timed out')

    @unittest.skipUnless(compat.HAVE_SIGNAL,
                         "This platform doesn't support posix signals")
    @mock.patch('pika.adapters.select_connection._is_resumable')
    def test_eintr(
            self,
            is_resumable_mock,
            is_resumable_raw=pika.adapters.select_connection._is_resumable):
        """Test that poll() is properly restarted after receiving EINTR error.
           Class of an exception raised to signal the error differs in one
           implementation of polling mechanism and another."""
        is_resumable_mock.side_effect = is_resumable_raw

        timer = select_connection._Timer()
        self.poller = self.ioloop._get_poller(timer.get_remaining_interval,
                                              timer.process_timeouts)
        self.addCleanup(self.poller.close)

        sockpair = self.poller._get_interrupt_pair()
        self.addCleanup(sockpair[0].close)
        self.addCleanup(sockpair[1].close)

        self._eintr_read_handler_is_called = False
        self.poller.add_handler(sockpair[0].fileno(), self._eintr_read_handler,
                                self.ioloop.READ)

        self.ioloop.call_later(self.TIMEOUT, self._eintr_test_fail)

        original_signal_handler = \
            signal.signal(signal.SIGUSR1, self.signal_handler)
        self.addCleanup(signal.signal, signal.SIGUSR1, original_signal_handler)

        tmr_k = threading.Timer(0.1,
                                lambda: os.kill(os.getpid(), signal.SIGUSR1))
        self.addCleanup(tmr_k.cancel)
        tmr_w = threading.Timer(0.2,
                                lambda: sockpair[1].send(self.MSG_CONTENT))
        self.addCleanup(tmr_w.cancel)
        tmr_k.start()
        tmr_w.start()
        self.poller.start()
        self.assertTrue(self._eintr_read_handler_is_called)
        if pika.compat.EINTR_IS_EXPOSED:
            self.assertEqual(is_resumable_mock.call_count, 1)
        else:
            self.assertEqual(is_resumable_mock.call_count, 0)


@unittest.skipIf(not POLL_SUPPORTED, 'poll not supported')
class IOLoopEintrTestCasePoll(IOLoopEintrTestCaseSelect):
    """Same as IOLoopEintrTestCaseSelect but uses poll syscall"""
    SELECT_POLLER = 'poll'


@unittest.skipIf(not EPOLL_SUPPORTED, 'epoll not supported')
class IOLoopEintrTestCaseEPoll(IOLoopEintrTestCaseSelect):
    """Same as IOLoopEINTRrTestCaseSelect but uses epoll syscall"""
    SELECT_POLLER = 'epoll'


@unittest.skipIf(not KQUEUE_SUPPORTED, 'kqueue not supported')
class IOLoopEintrTestCaseKqueue(IOLoopEintrTestCaseSelect):
    """Same as IOLoopEINTRTestCaseSelect but uses kqueue syscall"""
    SELECT_POLLER = 'kqueue'


class SelectPollerTestPollWithoutSockets(unittest.TestCase):
    def start_test(self):
        timer = select_connection._Timer()
        poller = select_connection.SelectPoller(
            get_wait_seconds=timer.get_remaining_interval,
            process_timeouts=timer.process_timeouts)
        self.addCleanup(poller.close)

        timer_call_container = []
        timer.call_later(0.00001, lambda: timer_call_container.append(1))
        poller.poll()

        delay = poller._get_wait_seconds()
        self.assertIsNotNone(delay)
        deadline = pika.compat.time_now() + delay

        while True:
            poller._process_timeouts()

            if pika.compat.time_now() < deadline:
                self.assertEqual(timer_call_container, [])
            else:
                # One last time in case deadline reached after previous
                # processing cycle
                poller._process_timeouts()
                break

        self.assertEqual(timer_call_container, [1])


class PollerTestCaseSelect(unittest.TestCase):
    SELECT_POLLER = 'select'

    def setUp(self):
        select_type_patch = mock.patch.multiple(
            select_connection, SELECT_TYPE=self.SELECT_POLLER)
        select_type_patch.start()
        self.addCleanup(select_type_patch.stop)

        timer = select_connection._Timer()
        self.addCleanup(timer.close)
        self.poller = select_connection.IOLoop._get_poller(
            timer.get_remaining_interval,
            timer.process_timeouts)
        self.addCleanup(self.poller.close)

    def test_poller_close(self):
        self.poller.close()
        self.assertIsNone(self.poller._r_interrupt)
        self.assertIsNone(self.poller._w_interrupt)
        self.assertIsNone(self.poller._fd_handlers)
        self.assertIsNone(self.poller._fd_events)
        self.assertIsNone(self.poller._processing_fd_event_map)


@unittest.skipIf(not POLL_SUPPORTED, 'poll not supported')
class PollerTestCasePoll(PollerTestCaseSelect):
    """Same as PollerTestCaseSelect but uses poll syscall"""
    SELECT_POLLER = 'poll'


@unittest.skipIf(not EPOLL_SUPPORTED, 'epoll not supported')
class PollerTestCaseEPoll(PollerTestCaseSelect):
    """Same as PollerTestCaseSelect but uses epoll syscall"""
    SELECT_POLLER = 'epoll'


@unittest.skipIf(not KQUEUE_SUPPORTED, 'kqueue not supported')
class PollerTestCaseKqueue(PollerTestCaseSelect):
    """Same as PollerTestCaseSelect but uses kqueue syscall"""
    SELECT_POLLER = 'kqueue'


class DefaultPollerSocketEventsTestCase(unittest.TestCase):
    """This test suite outputs diagnostic information usful for debugging the
    IOLoop poller's fd watcher

    """

    DEFAULT_TEST_TIMEOUT = 15

    IOLOOP_CLS = select_connection.IOLoop

    READ = IOLOOP_CLS.READ
    WRITE = IOLOOP_CLS.WRITE
    ERROR = IOLOOP_CLS.ERROR

    def create_ioloop_with_timeout(self):
        """Create IOLoop with test timeout and schedule cleanup to close it

        """
        ioloop = select_connection.IOLoop()
        self.addCleanup(ioloop.close)

        def _on_test_timeout():
            """Called when test times out"""
            LOGGER.info('%s TIMED OUT (%s)', datetime.datetime.utcnow(), self)
            self.fail('Test timed out')

        ioloop.call_later(self.DEFAULT_TEST_TIMEOUT, _on_test_timeout)

        return ioloop


    def create_nonblocking_tcp_socket(self):
        """Create a TCP stream socket and schedule cleanup to close it

        """
        sock = socket.socket()
        sock.setblocking(False)
        self.addCleanup(sock.close)
        return sock

    def create_nonblocking_socketpair(self):
        """Creates a non-blocking socket pair and schedules cleanup to close
        them

        :returns: two-tuple of connected non-blocking sockets

        """
        pair = pika.compat._nonblocking_socketpair()
        self.addCleanup(pair[0].close)
        self.addCleanup(pair[1].close)
        return pair

    def create_blocking_socketpair(self):
        """Creates a blocking socket pair and schedules cleanup to close
        them

        :returns: two-tuple of connected non-blocking sockets

        """
        pair = self.create_nonblocking_socketpair()
        pair[0].setblocking(True)  # pylint: disable=E1101
        pair[1].setblocking(True)
        return pair

    @staticmethod
    def safe_connect_nonblocking_socket(sock, addr_pair):
        """Initiate socket connection, suppressing EINPROGRESS/EWOULDBLOCK
        :param socket.socket sock
        :param addr_pair: two tuple of address string and port integer
        """
        try:
            sock.connect(addr_pair)
        except pika.compat.SOCKET_ERROR as error:
            # EINPROGRESS for posix and EWOULDBLOCK for windows
            if error.errno not in (errno.EINPROGRESS, errno.EWOULDBLOCK,):
                raise

    def get_dead_socket_address(self):
        """

        :return: socket address pair (ip-addr, port) that will refuse connection

        """
        s1, s2 = pika.compat._nonblocking_socketpair()
        s2.close()
        self.addCleanup(s1.close)
        return s1.getsockname()  # pylint: disable=E1101

    def which_events_are_set_with_varying_eventmasks(self,
                                                     sock,
                                                     requested_eventmasks,
                                                     msg_prefix):
        """Common logic for which_events_are_set_* tests. Runs the event loop
        while varying eventmasks at each socket event callback

        :param ioloop:
        :param sock:
        :param requested_eventmasks: a mutable list of eventmasks to apply after
                                     each socket event callback
        :param msg_prefix: Message prefix to apply when printing watched vs.
                           indicated events.
        """
        ioloop = self.create_ioloop_with_timeout()

        def handle_socket_events(_fd, in_events):
            socket_error = sock.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR)
            socket_error = 0 if socket_error == 0 else '{} ({})'.format(
                socket_error,
                os.strerror(socket_error))

            _trace_stderr('[%s] %s: watching=%s; indicated=%s; sockerr=%s',
                          ioloop._poller.__class__.__name__,
                          msg_prefix,
                          _fd_events_to_str(requested_eventmasks[0]),
                          _fd_events_to_str(in_events),
                          socket_error)

            # NOTE ERROR may be added automatically by some pollers
            #      without being requested.
            self.assertTrue(
                in_events & (requested_eventmasks[0] | self.ERROR),
                'watching={}; indicated={}'.format(
                    _fd_events_to_str(requested_eventmasks[0]),
                    _fd_events_to_str(in_events)))

            requested_eventmasks.pop(0)

            if requested_eventmasks:
                ioloop.update_handler(sock.fileno(), requested_eventmasks[0])
            else:
                ioloop.stop()

        ioloop.add_handler(sock.fileno(),
                           handle_socket_events,
                           requested_eventmasks[0])
        ioloop.start()


    def test_which_events_are_set_when_failed_to_connect(self):

        msg_prefix = '@ Failed to connect'

        sock = self.create_nonblocking_tcp_socket()

        self.safe_connect_nonblocking_socket(sock,
                                             self.get_dead_socket_address())
        requested_eventmasks = [
            self.READ | self.WRITE | self.ERROR,
            self.READ | self.WRITE,
            self.READ,
            self.WRITE | self.ERROR
        ]

        # NOTE: on OS X, we just get POLLHUP when requesting WRITE in this case
        # with PollPoller. It's documented in `man poll` on OS X as mutually
        # exclusive with POLLOUT; so it looks like PollPoller on OS X needs to
        # translate POLLHUP TO POLLERR and we need to request ERROR just in
        # case.

        # NOTE: Unlike POSIX, Windows select doesn't indicate as
        # readable/writable a socket that failed to connect - it reflects the
        # failure only via exceptfds.
        if platform.system() == 'Windows':
            _trace_stderr(
                '%s: setting `ERROR` to all event filters on '
                'Windows, because its `select()` does not indicate a socket '
                'that failed to connect as readable or writable.', msg_prefix)
            for i in pika.compat.xrange(len(requested_eventmasks)):
                requested_eventmasks[i] |= self.ERROR

        self.which_events_are_set_with_varying_eventmasks(
            sock=sock,
            requested_eventmasks=requested_eventmasks,
            msg_prefix=msg_prefix)

    def test_which_events_are_set_after_remote_end_closes(self):

        s1, s2 = self.create_blocking_socketpair()

        s2.close()

        requested_eventmasks = [
            self.READ | self.WRITE | self.ERROR,
            self.READ | self.WRITE,
            self.READ,
            self.WRITE
        ]

        self.which_events_are_set_with_varying_eventmasks(
            sock=s1,
            requested_eventmasks=requested_eventmasks,
            msg_prefix='@ Remote closed')

    def test_which_events_are_set_after_remote_end_closes_with_pending_data(self):

        s1, s2 = self.create_blocking_socketpair()

        s2.send(b'abc')
        s2.close()

        requested_eventmasks = [
            self.READ | self.WRITE | self.ERROR,
            self.READ | self.WRITE,
            self.READ,
            self.WRITE
        ]

        self.which_events_are_set_with_varying_eventmasks(
            sock=s1,
            requested_eventmasks=requested_eventmasks,
            msg_prefix='@ Remote closed with pending data')

    def test_which_events_are_set_after_remote_shuts_rd(self):

        s1, s2 = self.create_blocking_socketpair()

        s2.shutdown(socket.SHUT_RD)

        requested_eventmasks = [
            self.READ | self.WRITE | self.ERROR,
            self.WRITE
        ]

        self.which_events_are_set_with_varying_eventmasks(
            sock=s1,
            requested_eventmasks=requested_eventmasks,
            msg_prefix='@ Remote shut RD')

    def test_which_events_are_set_after_remote_shuts_wr(self):

        s1, s2 = self.create_blocking_socketpair()

        s2.shutdown(socket.SHUT_WR)

        requested_eventmasks = [
            (self.READ | self.WRITE | self.ERROR),
            self.READ | self.WRITE,
            self.READ,
            self.WRITE
        ]

        self.which_events_are_set_with_varying_eventmasks(
            sock=s1,
            requested_eventmasks=requested_eventmasks,
            msg_prefix='@ Remote shut WR')

    def test_which_events_are_set_after_remote_shuts_wr_with_pending_data(self):

        s1, s2 = self.create_blocking_socketpair()

        s2.send(b'abc')
        s2.shutdown(socket.SHUT_WR)

        requested_eventmasks = [
            self.READ | self.WRITE | self.ERROR,
            self.READ | self.WRITE,
            self.READ,
            self.WRITE
        ]

        self.which_events_are_set_with_varying_eventmasks(
            sock=s1,
            requested_eventmasks=requested_eventmasks,
            msg_prefix='@ Remote shut WR with pending data')

    def test_which_events_are_set_after_remote_shuts_rdwr(self):

        s1, s2 = self.create_blocking_socketpair()

        s2.shutdown(socket.SHUT_RDWR)

        requested_eventmasks = [
            self.READ | self.WRITE | self.ERROR,
            self.READ | self.WRITE,
            self.READ,
            self.WRITE
        ]

        self.which_events_are_set_with_varying_eventmasks(
            sock=s1,
            requested_eventmasks=requested_eventmasks,
            msg_prefix='@ Remote shut RDWR')

    def test_which_events_are_set_after_local_shuts_rd(self):

        msg_prefix = '@ Local shut RD'

        s1, _s2 = self.create_blocking_socketpair()

        s1.shutdown(socket.SHUT_RD)  # pylint: disable=E1101

        requested_eventmasks = [
            self.READ | self.WRITE | self.ERROR,
            self.READ | self.WRITE,
            self.READ,
            self.WRITE
        ]

        # NOTE: Unlike POSIX, Windows select doesn't indicate as readable socket
        #  that was shut down locally with SHUT_RD.
        if platform.system() == 'Windows':
            _trace_stderr(
                '%s: removing check for solo READ on Windows, '
                'because its `select()` does not indicate a socket shut '
                'locally with SHUT_RD as readable.', msg_prefix)
            requested_eventmasks.remove(self.READ)

        self.which_events_are_set_with_varying_eventmasks(
            sock=s1,
            requested_eventmasks=requested_eventmasks,
            msg_prefix=msg_prefix)

    def test_which_events_are_set_after_local_shuts_wr(self):

        s1, _s2 = self.create_blocking_socketpair()

        s1.shutdown(socket.SHUT_WR)  # pylint: disable=E1101

        requested_eventmasks = [
            self.READ | self.WRITE | self.ERROR,
            self.WRITE | self.ERROR
        ]

        # NOTE: on OS X, we just get POLLHUP when requesting WRITE in this case
        # with PollPoller. It's documented in `man poll` on OS X as mutually
        # exclusive with POLLOUT; so it looks like PollPoller on OS X needs to
        # translate POLLHUP TO POLLERR and we need to request ERROR just in
        # case.

        self.which_events_are_set_with_varying_eventmasks(
            sock=s1,
            requested_eventmasks=requested_eventmasks,
            msg_prefix='@ Local shut WR')

    def test_which_events_are_set_after_local_shuts_rdwr(self):

        msg_prefix = '@ Local shut RDWR'

        s1, _s2 = self.create_blocking_socketpair()

        s1.shutdown(socket.SHUT_RDWR)  # pylint: disable=E1101

        requested_eventmasks = [
            self.READ | self.WRITE | self.ERROR,
            self.READ | self.WRITE,
            self.READ,
            self.WRITE | self.ERROR
        ]
        # NOTE: on OS X, we just get POLLHUP when requesting WRITE in this case
        # with PollPoller. It's documented in `man poll` on OS X as mutually
        # exclusive with POLLOUT; so it looks like PollPoller on OS X needs to
        # translate POLLHUP TO POLLERR and we need to request ERROR just in
        # case.

        # NOTE: Unlike POSIX, Windows select doesn't indicate as readable socket
        #  that was shut down locally with SHUT_RDWR.
        if platform.system() == 'Windows':
            _trace_stderr(
                '%s: removing check for solo READ on Windows, '
                'because its `select()` does not indicate a socket shut '
                'locally with SHUT_RDWR as readable.', msg_prefix)
            requested_eventmasks.remove(self.READ)

        self.which_events_are_set_with_varying_eventmasks(
            sock=s1,
            requested_eventmasks=requested_eventmasks,
            msg_prefix=msg_prefix)


@mock.patch.multiple(select_connection, SELECT_TYPE='select')
class SelectPollerSocketEventsTestCase(DefaultPollerSocketEventsTestCase):
    """Runs `DefaultPollerSocketEventsTestCase` tests with forced use of
    SelectPoller
    """
    pass


@unittest.skipIf(not POLL_SUPPORTED, 'poll not supported')
@mock.patch.multiple(select_connection, SELECT_TYPE='poll')
class PollPollerSocketEventsTestCase(DefaultPollerSocketEventsTestCase):
    """Same as DefaultPollerSocketEventsTestCase but uses poll syscall"""
    pass


@unittest.skipIf(not EPOLL_SUPPORTED, 'epoll not supported')
@mock.patch.multiple(select_connection, SELECT_TYPE='epoll')
class EpollPollerSocketEventsTestCase(DefaultPollerSocketEventsTestCase):
    """Same as DefaultPollerSocketEventsTestCase but uses epoll syscall"""
    pass


@unittest.skipIf(not KQUEUE_SUPPORTED, 'kqueue not supported')
@mock.patch.multiple(select_connection, SELECT_TYPE='kqueue')
class KqueuePollerSocketEventsTestCase(DefaultPollerSocketEventsTestCase):
    """Same as DefaultPollerSocketEventsTestCase but uses kqueue syscall"""
    pass