summaryrefslogtreecommitdiffstats
path: root/t/04_reconnect.t
blob: 987db8eac359bdcce6b2985f69e33a6840871bb7 (plain)
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
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;

=head1 DESCRIPTION

Simulate a connection loss (readline on socket returns undef).

=cut

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",
	# "connection loss"
	undef,
	# client should reconnect
	"* 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",
);

my $socket = Test::MockObject->new();
$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});

$app->loop();
$app->loop();

is(scalar(@input_lines), 0, "all input lines are read");

eq_or_diff([grep { $_->[0] eq "writeline" } $socket->_calls()->@*], [
		['writeline', [$socket, "CMD-0 login tester1 secretPW42\r\n"]],
		['writeline', [$socket, "CMD-1 select INBOX\r\n"]],
		['writeline', [$socket, "CMD-2 notify set (selected (MessageExpunge MessageNew (uid body.peek[header.fields (from to subject)]))) (mailboxes (INBOX INBOX.test) (MessageNew MessageExpunge MailboxName))\r\n"]],
		['writeline', [$socket, "CMD-0 login tester1 secretPW42\r\n"]],
		['writeline', [$socket, "CMD-1 select INBOX\r\n"]],
		['writeline', [$socket, "CMD-2 notify set (selected (MessageExpunge MessageNew (uid body.peek[header.fields (from to subject)]))) (mailboxes (INBOX INBOX.test) (MessageNew MessageExpunge MailboxName))\r\n"]],
	], "socket writeline is called correctly");

is(scalar(grep { $_->[0] eq "reconnect" } $socket->_calls()->@*), 2, "socket reconnect is called twice");

done_testing;