diff options
author | Florian Pritz <bluewind@xinu.at> | 2018-07-22 16:02:55 +0200 |
---|---|---|
committer | Florian Pritz <bluewind@xinu.at> | 2018-07-22 16:02:55 +0200 |
commit | 5a3717ef847517c3e1a903badd31bf12831a7171 (patch) | |
tree | 81d94ad81b2eadf28b94c78c88b5b3760db85a07 | |
parent | 13cad1519b679526b2b8f5f38dd388d0ed3a4dcd (diff) | |
download | App-ImapNotify-5a3717ef847517c3e1a903badd31bf12831a7171.tar.gz App-ImapNotify-5a3717ef847517c3e1a903badd31bf12831a7171.tar.xz |
Expect BYE from server and return gracefully
Signed-off-by: Florian Pritz <bluewind@xinu.at>
-rw-r--r-- | README.md | 7 | ||||
-rw-r--r-- | lib/App/ImapNotify.pm | 8 | ||||
-rw-r--r-- | t/08_BYE.t | 47 |
3 files changed, 56 insertions, 6 deletions
@@ -36,6 +36,7 @@ Same as loop(), but automatcally calls loop() again if the connection is lost. Open a connection and wait for NOTIFY notifications. When a NOTIFY notification arrives, show a notification to the user. -This method throws an exception when the connection to the server is lost. If -you want to continue waiting for new notifications, you may call this method -again. Also look at loop\_reconnect(). +This method either returns when the server closes the connection or throws an +exception when the connection to the server is lost. If you want to continue +waiting for new notifications, you may call this method again. Also look at +loop\_reconnect(). diff --git a/lib/App/ImapNotify.pm b/lib/App/ImapNotify.pm index cd81538..c67c903 100644 --- a/lib/App/ImapNotify.pm +++ b/lib/App/ImapNotify.pm @@ -89,9 +89,10 @@ method loop_reconnect() { Open a connection and wait for NOTIFY notifications. When a NOTIFY notification arrives, show a notification to the user. -This method throws an exception when the connection to the server is lost. If -you want to continue waiting for new notifications, you may call this method -again. Also look at loop_reconnect(). +This method either returns when the server closes the connection or throws an +exception when the connection to the server is lost. If you want to continue +waiting for new notifications, you may call this method again. Also look at +loop_reconnect(). =cut @@ -124,6 +125,7 @@ method loop() { next if $line =~ /\* \d+ RECENT/; next if $line =~ /\* \d+ EXISTS/; next if $line =~ /\* \d+ EXPUNGE/; + last if $line =~ /\* BYE /; confess(sprintf("Got unexpected line: '%s'", $line)); } diff --git a/t/08_BYE.t b/t/08_BYE.t new file mode 100644 index 0000000..650e46f --- /dev/null +++ b/t/08_BYE.t @@ -0,0 +1,47 @@ +use strict; +use warnings; +use Test::Differences; +use Test::More; +use Test::MockObject; + +use Log::Any::Adapter ('Stderr', log_level => "warn"); + +use App::ImapNotify; +use App::ImapNotify::ImapClient; + + +my $notifier = Test::MockObject->new(); +$notifier->set_true("notify"); + +my $socket = Test::MockObject->new(); +my @input_lines = ( + "* OK [CAPABILITY IMAP4rev1 SASL-IR LOGIN-REFERRALS ID ENABLE IDLE LITERAL+ AUTH=PLAIN AUTH=LOGIN] Fake server ready.\r\n", + "CMD-0 OK [CAPABILITY IMAP4rev1 SASL-IR LOGIN-REFERRALS ID ENABLE IDLE SORT SORT=DISPLAY THREAD=REFERENCES THREAD=REFS THREAD=ORDEREDSUBJECT MULTIAPPEND URL-PARTIAL CATENATE UNSELECT CHILDREN NAMESPACE UIDPLUS LIST-EXTENDED I18NLEVEL=1 CONDSTORE QRESYNC ESEARCH ESORT SEARCHRES WITHIN CONTEXT=SEARCH LIST-STATUS BINARY MOVE SNIPPET=FUZZY LITERAL+ NOTIFY SPECIAL-USE COMPRESS=DEFLATE] Logged in\r\n", + "CMD-1 OK [READ-WRITE] Select completed (0.001 + 0.000 secs).\r\n", + "CMD-2 OK NOTIFY completed (0.001 + 0.000 secs).\r\n", + "* BYE Server shutting down.\r\n", + sub {fail("This should not be read"); }, +); +$socket->mock('readline', sub {my $x = shift @input_lines; ref($x) eq "CODE" ? $x->() : $x;}); + +$socket->set_true(qw(writeline reconnect)); + +my $config = { + log_id => 'test-id1', + host => 'localhost.localdomain', + port => 993, + username => 'tester1', + password => 'secretPW42', + mailboxes => [qw(INBOX INBOX.test)], + keepalive_timeout => 300, +}; + +my $imap_client = App::ImapNotify::ImapClient->new_no_defaults($config, {sock => $socket}); + +my $app = App::ImapNotify->new_no_defaults($config, {imap_client => $imap_client, notifier => $notifier}); + +$app->loop(); + +is(scalar(@input_lines), 1, "line after BYE should not be read"); + +done_testing; |