aboutsummaryrefslogtreecommitdiffstats
path: root/t/queue.t
blob: b480e67c6d44e618b3390565e79601984d44996f (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
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
use Iurt::Queue;

use Test::More;

use Cwd;
use File::stat 'stat';
use MDK::Common qw(cat_);

$config = {};

my $media = 'core/release';
my $bot = 'iurt';

my $b1 = {
    bot => $bot,
    host => 'h1',
    date => 20220401,
    pid => 1,
    'arch' => 'a1',
    'time' => 42
};

my $b2 = {
    bot => $bot,
    host => 'h1',
    date => 20220401,
    pid => 2,
    'arch' => 'a2',
    'time' => 42
};

my $b3 = {
    bot => $bot,
    host => 'h2',
    date => 20220401,
    pid => 1,
    'arch' => 'a1',
    'time' => 42
};

sub create_ent {
    my %ent;
    $ent->{media}{$media}{bot} = [$b1, $b2, $b3];
    return $ent;
}

my $ent, $expected_ent;

$ent = create_ent();
remove_bot_from_package($ent, $media, 'h1', 42);
is_deeply $ent->{media}{$media}{bot}, [$b1, $b2, $b3] or diag explain $ent->{media}{$media}{bot};

$ent = create_ent();
remove_bot_from_package($ent, $media, 'h1', 1);
is_deeply $ent->{media}{$media}{bot}, [$b2, $b3] or diag explain $ent->{media}{$media}{bot};

$ent = create_ent();
remove_bot_from_package($ent, $media, 'h2', 1);
is_deeply $ent->{media}{$media}{bot}, [$b1, $b2] or diag explain $ent->{media}{$media}{bot};

$ent = create_ent();
remove_bot_from_package($ent, $media, 'h1', 2);
is_deeply $ent->{media}{$media}{bot}, [$b1, $b3] or diag explain $ent->{media}{$media}{bot};


chdir 't' if -d 't';
mkdir "tmp";
my $dir = Cwd::cwd() . "/tmp";
my $retry_file = "${dir}/test_noarch.retry";
unlink $retry_file;

sub verify_retry_file {
    my ($test, $present, $content, $mtime) = @_;
    if ($present) {
	ok(-f $retry_file, "$test - $retry_file should exist");
    } else {
	ok(!-f $retry_file, "$test - $retry_file should not exist");
	return;
    }
    is(cat_($retry_file), $content, "$test - $retry_file should contain $content");
    if ($mtime) {
	my $t = stat($retry_file)->mtime;
	# Allow 5s difference if running on a very slow machine
	ok($t > $mtime - 5);
	ok($t < $mtime + 5);
    }
}

unlink $retry_file;
ok(schedule_next_retry({'backoff_delays' => [1000, 2000]}, $dir, 'test', 'noarch', 0), "schedule_next_retry - first failure is retried");
verify_retry_file("schedule_next_retry - first failure is retried", 1, 1, time+1000);

unlink $retry_file;
ok(schedule_next_retry({'backoff_delays' => [1000, 2000]}, $dir, 'test', 'noarch', 1), "schedule_next_retry - one retry left is retried");
verify_retry_file("schedule_next_retry - one retry left is retried", 1, 2, time+2000);

unlink $retry_file;
ok(!schedule_next_retry({'backoff_delays' => [120, 1000]}, $dir, 'test', 'noarch', 2), "schedule_next_retry - no retry left is failed");

unlink $retry_file;
ok(!schedule_next_retry({'backoff_delays' => []}, $dir, 'test', 'noarch', 0), "schedule_next_retry - no retry is failed");

unlink $retry_file;
ok(schedule_next_retry({}, $dir, 'test', 'noarch', 0), "schedule_next_retry - always retry is retried");
verify_retry_file("schedule_next_retry - always retry is retried", 0);

done_testing();