11
11
import logging
12
12
13
13
import six
14
+ from select import POLLIN
14
15
15
16
from imapclient .exceptions import (
16
17
CapabilityError , IMAPClientError , ProtocolError
@@ -379,14 +380,23 @@ def setUp(self):
379
380
super (TestIdleAndNoop , self ).setUp ()
380
381
self .client ._cached_capabilities = [b'IDLE' ]
381
382
382
- def assert_sock_calls (self , sock ):
383
+ def assert_sock_select_calls (self , sock ):
383
384
self .assertListEqual (sock .method_calls , [
384
385
('settimeout' , (None ,), {}),
385
386
('setblocking' , (0 ,), {}),
386
387
('setblocking' , (1 ,), {}),
387
388
('settimeout' , (None ,), {}),
388
389
])
389
390
391
+ def assert_sock_poll_calls (self , sock ):
392
+ self .assertListEqual (sock .method_calls , [
393
+ ('settimeout' , (None ,), {}),
394
+ ('setblocking' , (0 ,), {}),
395
+ ('fileno' , (), {}),
396
+ ('setblocking' , (1 ,), {}),
397
+ ('settimeout' , (None ,), {}),
398
+ ])
399
+
390
400
def test_idle (self ):
391
401
self .client ._imap ._command .return_value = sentinel .tag
392
402
self .client ._imap ._get_response .return_value = None
@@ -396,6 +406,7 @@ def test_idle(self):
396
406
self .client ._imap ._command .assert_called_with ('IDLE' )
397
407
self .assertEqual (self .client ._idle_tag , sentinel .tag )
398
408
409
+ @patch ('imapclient.imapclient.POLL_SUPPORT' , False )
399
410
@patch ('imapclient.imapclient.select.select' )
400
411
def test_idle_check_blocking (self , mock_select ):
401
412
mock_sock = Mock ()
@@ -416,9 +427,10 @@ def fake_get_line():
416
427
responses = self .client .idle_check ()
417
428
418
429
mock_select .assert_called_once_with ([mock_sock ], [], [], None )
419
- self .assert_sock_calls (mock_sock )
430
+ self .assert_sock_select_calls (mock_sock )
420
431
self .assertListEqual ([(1 , b'EXISTS' ), (0 , b'EXPUNGE' )], responses )
421
432
433
+ @patch ('imapclient.imapclient.POLL_SUPPORT' , False )
422
434
@patch ('imapclient.imapclient.select.select' )
423
435
def test_idle_check_timeout (self , mock_select ):
424
436
mock_sock = Mock ()
@@ -428,9 +440,10 @@ def test_idle_check_timeout(self, mock_select):
428
440
responses = self .client .idle_check (timeout = 0.5 )
429
441
430
442
mock_select .assert_called_once_with ([mock_sock ], [], [], 0.5 )
431
- self .assert_sock_calls (mock_sock )
443
+ self .assert_sock_select_calls (mock_sock )
432
444
self .assertListEqual ([], responses )
433
445
446
+ @patch ('imapclient.imapclient.POLL_SUPPORT' , False )
434
447
@patch ('imapclient.imapclient.select.select' )
435
448
def test_idle_check_with_data (self , mock_select ):
436
449
mock_sock = Mock ()
@@ -449,7 +462,80 @@ def fake_get_line():
449
462
responses = self .client .idle_check ()
450
463
451
464
mock_select .assert_called_once_with ([mock_sock ], [], [], None )
452
- self .assert_sock_calls (mock_sock )
465
+ self .assert_sock_select_calls (mock_sock )
466
+ self .assertListEqual ([(99 , b'EXISTS' )], responses )
467
+
468
+ @patch ('imapclient.imapclient.POLL_SUPPORT' , True )
469
+ @patch ('imapclient.imapclient.select.poll' )
470
+ def test_idle_check_blocking (self , mock_poll_module ):
471
+ mock_sock = Mock (fileno = Mock (return_value = 1 ))
472
+ self .client ._imap .sock = self .client ._imap .sslobj = mock_sock
473
+
474
+ mock_poller = Mock (poll = Mock (return_value = [(1 , POLLIN )]))
475
+ mock_poll_module .return_value = mock_poller
476
+ counter = itertools .count ()
477
+
478
+ def fake_get_line ():
479
+ count = six .next (counter )
480
+ if count == 0 :
481
+ return b'* 1 EXISTS'
482
+ elif count == 1 :
483
+ return b'* 0 EXPUNGE'
484
+ else :
485
+ raise socket .timeout
486
+
487
+ self .client ._imap ._get_line = fake_get_line
488
+
489
+ responses = self .client .idle_check ()
490
+
491
+ assert mock_poll_module .call_count == 1
492
+ mock_poller .register .assert_called_once_with (1 , POLLIN )
493
+ mock_poller .poll .assert_called_once_with (None )
494
+ self .assert_sock_poll_calls (mock_sock )
495
+ self .assertListEqual ([(1 , b'EXISTS' ), (0 , b'EXPUNGE' )], responses )
496
+
497
+ @patch ('imapclient.imapclient.POLL_SUPPORT' , True )
498
+ @patch ('imapclient.imapclient.select.poll' )
499
+ def test_idle_check_timeout (self , mock_poll_module ):
500
+ mock_sock = Mock (fileno = Mock (return_value = 1 ))
501
+ self .client ._imap .sock = self .client ._imap .sslobj = mock_sock
502
+
503
+ mock_poller = Mock (poll = Mock (return_value = []))
504
+ mock_poll_module .return_value = mock_poller
505
+
506
+ responses = self .client .idle_check (timeout = 0.5 )
507
+
508
+ assert mock_poll_module .call_count == 1
509
+ mock_poller .register .assert_called_once_with (1 , POLLIN )
510
+ mock_poller .poll .assert_called_once_with (500 )
511
+ self .assert_sock_poll_calls (mock_sock )
512
+ self .assertListEqual ([], responses )
513
+
514
+ @patch ('imapclient.imapclient.POLL_SUPPORT' , True )
515
+ @patch ('imapclient.imapclient.select.poll' )
516
+ def test_idle_check_with_data (self , mock_poll_module ):
517
+ mock_sock = Mock (fileno = Mock (return_value = 1 ))
518
+ self .client ._imap .sock = self .client ._imap .sslobj = mock_sock
519
+
520
+ mock_poller = Mock (poll = Mock (return_value = [(1 , POLLIN )]))
521
+ mock_poll_module .return_value = mock_poller
522
+ counter = itertools .count ()
523
+
524
+ def fake_get_line ():
525
+ count = six .next (counter )
526
+ if count == 0 :
527
+ return b'* 99 EXISTS'
528
+ else :
529
+ raise socket .timeout
530
+
531
+ self .client ._imap ._get_line = fake_get_line
532
+
533
+ responses = self .client .idle_check ()
534
+
535
+ assert mock_poll_module .call_count == 1
536
+ mock_poller .register .assert_called_once_with (1 , POLLIN )
537
+ mock_poller .poll .assert_called_once_with (None )
538
+ self .assert_sock_poll_calls (mock_sock )
453
539
self .assertListEqual ([(99 , b'EXISTS' )], responses )
454
540
455
541
def test_idle_done (self ):
@@ -835,4 +921,4 @@ def test_tagged_response_with_parse_error(self):
835
921
client ._imap ._get_response = lambda : b'NOT-A-STAR 99 EXISTS'
836
922
837
923
with self .assertRaises (ProtocolError ):
838
- client ._consume_until_tagged_response (sentinel .tag , b'IDLE' )
924
+ client ._consume_until_tagged_response (sentinel .tag , b'IDLE' )
0 commit comments