| 1 |
use Test2::V0 -target => 'Test2::Harness::Runner::State'; |
| 2 |
# HARNESS-DURATION-SHORT |
| 3 |
|
| 4 |
use ok $CLASS; |
| 5 |
|
| 6 |
use File::Temp qw/tempdir/; |
| 7 |
|
| 8 |
use Test2::Harness::Settings; |
| 9 |
|
| 10 |
my $RUN_ID = 'test-run'; |
| 11 |
|
| 12 |
# Drive the scheduler by hand and check which tasks it is willing to run |
| 13 |
# together. |
| 14 |
# |
| 15 |
# 'tasks' is an ordered list of NAME => TASK_SPEC pairs; the spec carries only |
| 16 |
# what the case is about (usually 'conflicts' and/or 'shares'), everything else |
| 17 |
# gets a default. |
| 18 |
# |
| 19 |
# 'steps' is an ordered list of pairs describing what should happen: |
| 20 |
# |
| 21 |
# running => \@names Let the scheduler start everything it can, then check |
| 22 |
# that exactly these tasks are running. |
| 23 |
# stop => \@names Pretend these tasks finished. Nothing forks here, so |
| 24 |
# this is the only way a task ever completes. |
| 25 |
# queue => \@tasks Queue more NAME => TASK_SPEC pairs mid-run. |
| 26 |
sub schedule_ok { |
| 27 |
my ($name, %params) = @_; |
| 28 |
|
| 29 |
my @tasks = @{$params{tasks}}; |
| 30 |
my @steps = @{$params{steps}}; |
| 31 |
my $job_count = $params{job_count} // 10; |
| 32 |
|
| 33 |
my $ctx = context(); |
| 34 |
|
| 35 |
my $settings = Test2::Harness::Settings->new( |
| 36 |
runner => { |
| 37 |
job_count => $job_count, |
| 38 |
slots_per_job => $job_count, |
| 39 |
resources => [], |
| 40 |
}, |
| 41 |
); |
| 42 |
|
| 43 |
my $state = $CLASS->new( |
| 44 |
workdir => tempdir(CLEANUP => 1), |
| 45 |
settings => $settings, |
| 46 |
eager_stages => {}, |
| 47 |
); |
| 48 |
|
| 49 |
$state->queue_run({run_id => $RUN_ID}); |
| 50 |
$state->stage_ready('DEFAULT'); |
| 51 |
|
| 52 |
my %name_for; |
| 53 |
my $queue = sub { |
| 54 |
my (@tasks) = @_; |
| 55 |
|
| 56 |
while (@tasks) { |
| 57 |
my ($task_name, $spec) = splice(@tasks, 0, 2); |
| 58 |
|
| 59 |
my $job_id = "job-$task_name"; |
| 60 |
$name_for{$job_id} = $task_name; |
| 61 |
|
| 62 |
$state->queue_task({ |
| 63 |
run_id => $RUN_ID, |
| 64 |
job_id => $job_id, |
| 65 |
category => 'general', |
| 66 |
duration => 'short', |
| 67 |
use_preload => 1, |
| 68 |
%$spec, |
| 69 |
}); |
| 70 |
} |
| 71 |
}; |
| 72 |
|
| 73 |
$queue->(@tasks); |
| 74 |
|
| 75 |
my $out = subtest $name => sub { |
| 76 |
while (@steps) { |
| 77 |
my ($action, $args) = splice(@steps, 0, 2); |
| 78 |
|
| 79 |
if ($action eq 'running') { |
| 80 |
1 while $state->advance; |
| 81 |
|
| 82 |
my @running = map { $name_for{$_} } keys %{$state->running_tasks // {}}; |
| 83 |
is([sort @running], [sort @$args], "Running: " . join(', ' => sort @$args)); |
| 84 |
} |
| 85 |
elsif ($action eq 'stop') { |
| 86 |
$state->stop_task("job-$_") for @$args; |
| 87 |
} |
| 88 |
elsif ($action eq 'queue') { |
| 89 |
$queue->(@$args); |
| 90 |
} |
| 91 |
else { |
| 92 |
die "Unknown step '$action'"; |
| 93 |
} |
| 94 |
} |
| 95 |
}; |
| 96 |
|
| 97 |
$ctx->release; |
| 98 |
|
| 99 |
return $out; |
| 100 |
} |
| 101 |
|
| 102 |
schedule_ok( |
| 103 |
"Two exclusive claims on one name cannot run together", |
| 104 |
tasks => [ |
| 105 |
a => {conflicts => ['db']}, |
| 106 |
b => {conflicts => ['db']}, |
| 107 |
], |
| 108 |
steps => [ |
| 109 |
running => [qw/a/], |
| 110 |
stop => [qw/a/], |
| 111 |
running => [qw/b/], |
| 112 |
], |
| 113 |
); |
| 114 |
|
| 115 |
schedule_ok( |
| 116 |
"Any number of shared claims on one name run together", |
| 117 |
tasks => [ |
| 118 |
a => {shares => ['db']}, |
| 119 |
b => {shares => ['db']}, |
| 120 |
c => {shares => ['db']}, |
| 121 |
], |
| 122 |
steps => [running => [qw/a b c/]], |
| 123 |
); |
| 124 |
|
| 125 |
schedule_ok( |
| 126 |
"Running shared claims keep an exclusive claim waiting, then release it", |
| 127 |
tasks => [ |
| 128 |
a => {shares => ['db']}, |
| 129 |
b => {shares => ['db']}, |
| 130 |
], |
| 131 |
steps => [ |
| 132 |
running => [qw/a b/], |
| 133 |
queue => [c => {conflicts => ['db']}], |
| 134 |
running => [qw/a b/], |
| 135 |
stop => [qw/a/], |
| 136 |
running => [qw/b/], |
| 137 |
stop => [qw/b/], |
| 138 |
running => [qw/c/], |
| 139 |
], |
| 140 |
); |
| 141 |
|
| 142 |
schedule_ok( |
| 143 |
"An exclusive claim is picked ahead of shared claims on the same name", |
| 144 |
tasks => [ |
| 145 |
a => {shares => ['db']}, |
| 146 |
b => {shares => ['db']}, |
| 147 |
c => {conflicts => ['db']}, |
| 148 |
], |
| 149 |
steps => [ |
| 150 |
running => [qw/c/], |
| 151 |
stop => [qw/c/], |
| 152 |
running => [qw/a b/], |
| 153 |
], |
| 154 |
); |
| 155 |
|
| 156 |
schedule_ok( |
| 157 |
"An exclusive claim keeps shared claims waiting", |
| 158 |
tasks => [ |
| 159 |
a => {conflicts => ['db']}, |
| 160 |
b => {shares => ['db']}, |
| 161 |
c => {shares => ['db']}, |
| 162 |
], |
| 163 |
steps => [ |
| 164 |
running => [qw/a/], |
| 165 |
stop => [qw/a/], |
| 166 |
running => [qw/b c/], |
| 167 |
], |
| 168 |
); |
| 169 |
|
| 170 |
schedule_ok( |
| 171 |
"Claims on unrelated names do not interact", |
| 172 |
tasks => [ |
| 173 |
a => {shares => ['db']}, |
| 174 |
b => {conflicts => ['mysql']}, |
| 175 |
c => {shares => ['mysql']}, |
| 176 |
d => {conflicts => ['db']}, |
| 177 |
], |
| 178 |
steps => [ |
| 179 |
running => [qw/b d/], |
| 180 |
stop => [qw/b d/], |
| 181 |
running => [qw/a c/], |
| 182 |
], |
| 183 |
); |
| 184 |
|
| 185 |
schedule_ok( |
| 186 |
"One task can hold an exclusive claim and a shared claim at once", |
| 187 |
tasks => [ |
| 188 |
a => {conflicts => ['db'], shares => ['mysql']}, |
| 189 |
b => {shares => ['mysql']}, |
| 190 |
c => {shares => ['db']}, |
| 191 |
d => {conflicts => ['mysql']}, |
| 192 |
], |
| 193 |
steps => [ |
| 194 |
running => [qw/a b/], |
| 195 |
stop => [qw/a b/], |
| 196 |
running => [qw/c d/], |
| 197 |
], |
| 198 |
); |
| 199 |
|
| 200 |
schedule_ok( |
| 201 |
"A task with no 'shares' key at all still dispatches", |
| 202 |
tasks => [ |
| 203 |
a => {}, |
| 204 |
b => {conflicts => ['db']}, |
| 205 |
], |
| 206 |
steps => [running => [qw/a b/]], |
| 207 |
); |
| 208 |
|
| 209 |
schedule_ok( |
| 210 |
"A name claimed both ways by one task does not block that task", |
| 211 |
tasks => [ |
| 212 |
a => {conflicts => ['db'], shares => ['db']}, |
| 213 |
b => {shares => ['db']}, |
| 214 |
], |
| 215 |
steps => [ |
| 216 |
running => [qw/a/], |
| 217 |
stop => [qw/a/], |
| 218 |
running => [qw/b/], |
| 219 |
], |
| 220 |
); |
| 221 |
|
| 222 |
schedule_ok( |
| 223 |
"The job limiter still caps how many run at once", |
| 224 |
job_count => 2, |
| 225 |
tasks => [ |
| 226 |
a => {shares => ['db']}, |
| 227 |
b => {shares => ['db']}, |
| 228 |
c => {shares => ['db']}, |
| 229 |
], |
| 230 |
steps => [ |
| 231 |
running => [qw/a b/], |
| 232 |
stop => [qw/a b/], |
| 233 |
running => [qw/c/], |
| 234 |
], |
| 235 |
); |
| 236 |
|
| 237 |
done_testing; |