user/dev discussion of public-inbox itself
 help / color / mirror / code / Atom feed
blob 90356b57b3e7ae5ed66435b4c3cfe8ff41bef89b 4495 bytes (raw)
name: t/lei-refresh-mail-sync.t 	 # note: path name is non-authoritative(*)

  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
 
#!perl -w
# Copyright (C) all contributors <meta@public-inbox.org>
# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
use strict; use v5.10.1; use PublicInbox::TestCommon;
require_mods(qw(lei));
use File::Path qw(remove_tree);

my $stop_daemon = sub { # needed since we don't have inotify
	lei_ok qw(daemon-pid);
	chomp(my $pid = $lei_out);
	$pid > 0 or xbail "bad pid: $pid";
	kill('TERM', $pid) or xbail "kill: $!";
	for (0..10) {
		tick;
		kill(0, $pid) or last;
	}
	kill(0, $pid) and xbail "daemon still running (PID:$pid)";
};

test_lei({ daemon_only => 1 }, sub {
	my $d = "$ENV{HOME}/d";
	my ($ro_home, $cfg_path) = setup_public_inboxes;
	lei_ok qw(daemon-pid);
	lei_ok qw(add-external), "$ro_home/t2";
	lei_ok qw(q mid:testmessage@example.com -o), "Maildir:$d";
	my (@o) = glob("$d/*/*");
	scalar(@o) == 1 or xbail('multiple results', \@o);
	my ($bn0) = ($o[0] =~ m!/([^/]+)\z!);

	my $oid = '9bf1002c49eb075df47247b74d69bcd555e23422';
	lei_ok 'inspect', "blob:$oid";
	my $before = json_utf8->decode($lei_out);
	my $exp0 = { 'mail-sync' => { "maildir:$d" => [ $bn0 ] } };
	is_deeply($before, $exp0, 'inspect shows expected');

	$stop_daemon->();
	my $dst = $o[0];
	$dst =~ s/:2,.*\z// and $dst =~ s!/cur/!/new/! and
		rename($o[0], $dst) or xbail "rename($o[0] => $dst): $!";

	lei_ok 'inspect', "blob:$oid";
	is_deeply(json_utf8->decode($lei_out),
		$before, 'inspect unchanged immediately after restart');
	lei_ok 'refresh-mail-sync', '--all';
	lei_ok 'inspect', "blob:$oid";
	my ($bn1) = ($dst =~ m!/([^/]+)\z!);
	my $exp1 = { 'mail-sync' => { "maildir:$d" => [ $bn1 ] } };
	is_deeply(json_utf8->decode($lei_out), $exp1,
		'refresh-mail-sync updated location');

	$stop_daemon->();
	rename($dst, "$d/unwatched") or xbail "rename $dst out-of-the-way $!";

	lei_ok 'refresh-mail-sync', $d;
	lei_ok 'inspect', "blob:$oid";
	is($lei_out, '{}', 'no known locations after "removal"');
	lei_ok 'refresh-mail-sync', "Maildir:$d";

	$stop_daemon->();
	rename("$d/unwatched", $dst) or xbail "rename $dst back";

	lei_ok 'refresh-mail-sync', "Maildir:$d";
	lei_ok 'inspect', "blob:$oid";
	is_deeply(json_utf8->decode($lei_out), $exp1,
		'replaced file noted again');

	$stop_daemon->();

	remove_tree($d);
	lei_ok 'refresh-mail-sync', '--all';
	lei_ok 'inspect', "blob:$oid";
	is($lei_out, '{}', 'no known locations after "removal"');
	lei_ok 'ls-mail-sync';
	is($lei_out, '', 'no sync left when folder is gone');

SKIP: {
	require_mods(qw(-imapd -nntpd Mail::IMAPClient Net::NNTP), 1);
	require File::Copy; # stdlib
	my $home = $ENV{HOME};
	my $srv;
	my $cfg_path2 = "$home/cfg2";
	File::Copy::cp($cfg_path, $cfg_path2);
	my $env = { PI_CONFIG => $cfg_path2 };
	for my $x (qw(imapd)) {
		my $s = tcp_server;
		my $cmd = [ "-$x", '-W0', "--stdout=$home/$x.out",
			"--stderr=$home/$x.err" ];
		my $td = start_script($cmd, $env, { 3 => $s}) or xbail("-$x");
		$srv->{$x} = {
			addr => (my $scalar = tcp_host_port($s)),
			td => $td,
			cmd => $cmd,
		};
	}
	my $url = "imap://$srv->{imapd}->{addr}/t.v1.0";
	lei_ok 'import', $url, '+L:v1';
	lei_ok 'inspect', "blob:$oid";
	$before = json_utf8->decode($lei_out);
	my @f = grep(m!\Aimap://;AUTH=ANONYMOUS\@\Q$srv->{imapd}->{addr}\E!,
		keys %{$before->{'mail-sync'}});
	is(scalar(@f), 1, 'got IMAP folder') or xbail(\@f);
	xsys([qw(git config), '-f', $cfg_path2,
		qw(--unset publicinbox.t1.newsgroup)]) and
		xbail "git config $?";
	$stop_daemon->(); # drop IMAP IDLE
	$srv->{imapd}->{td}->kill('HUP');
	tick; # wait for HUP
	lei_ok 'refresh-mail-sync', $url;
	lei_ok 'inspect', "blob:$oid";
	my $after = json_utf8->decode($lei_out);
	ok(!$after->{'mail-sync'}, 'no sync info for non-existent mailbox');
	lei_ok 'ls-mail-sync';
	unlike $lei_out, qr!^\Q$f[0]\E!, 'IMAP folder gone from mail_sync';

	# simulate server downtime
	$url = "imap://$srv->{imapd}->{addr}/t.v2.0";
	lei_ok 'import', $url, '+L:v2';

	lei_ok 'inspect', "blob:$oid";
	$before = $lei_out;
	delete $srv->{imapd}->{td}; # kill + join daemon

	ok(!(lei 'refresh-mail-sync', $url), 'URL fails on dead -imapd');
	ok(!(lei 'refresh-mail-sync', '--all'), '--all fails on dead -imapd');

	# restart server (somewhat dangerous since we released the socket)
	my $cmd = $srv->{imapd}->{cmd};
	push @$cmd, '-l', $srv->{imapd}->{addr};
	$srv->{imapd}->{td} = start_script($cmd, $env) or xbail "@$cmd";

	lei_ok 'refresh-mail-sync', '--all';
	lei_ok 'inspect', "blob:$oid";
	is($lei_out, $before, 'no changes when server was down');
}; # imapd+nntpd stuff
});

done_testing;

debug log:

solving 90356b57 ...
found 90356b57 in https://80x24.org/public-inbox.git

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://80x24.org/public-inbox.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).