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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
#!/usr/bin/perl
use warnings;
use strict;
use Test::More;
use POSIX;
use FindBin;
use Time::HiRes 'sleep';
use Mojo::File qw(tempfile path);
use lib ("$FindBin::Bin/lib", "../lib", "lib");
use Mojo::IOLoop::ReadWriteProcess qw(queue process);
use Mojo::IOLoop::ReadWriteProcess::Session;
subtest queues => sub {
my $q = queue;
$q->pool->maximum_processes(3);
$q->queue->maximum_processes(800);
my $proc = 10;
my $fired;
my $i = 1;
for (1 .. $proc) {
$q->add(process(sub { shift; return shift() })->set_pipes(0)->args($i));
$i++;
}
my %output;
$q->once(
stop => sub {
$fired++;
$output{shift->return_status}++;
});
is $q->queue->size, $proc - $q->pool->maximum_processes;
is $q->pool->size, 3;
is $q->pool->maximum_processes, 3;
$q->consume;
is $fired, $proc;
is $q->queue->size, 0;
is $q->pool->size, 0;
is $q->done->size, $proc;
$i = 1;
for (1 .. $proc) {
is $output{$i}, 1;
$i++;
}
};
subtest test_2 => sub {
my $q = queue;
$q->pool->maximum_processes(2);
$q->queue->maximum_processes(800);
my $proc = 10;
my $fired;
my $i = 1;
for (1 .. $proc) {
$q->add(process(sub { shift; return shift() })->set_pipes(0)->args($i));
$i++;
}
my %output;
$q->once(
stop => sub {
$fired++;
$output{shift->return_status}++;
});
is $q->queue->size, $proc - $q->pool->maximum_processes;
is $q->pool->size, 2;
is $q->pool->maximum_processes, 2;
$q->consume;
is $fired, $proc;
is $q->queue->size, 0;
is $q->pool->size, 0;
is $q->done->size, $proc;
$i = 1;
for (1 .. $proc) {
is $output{$i}, 1;
$i++;
}
};
subtest atomic_queues => sub {
my $q = queue;
$q->pool->maximum_processes(1);
$q->queue->maximum_processes(800);
my $proc = 10;
my $fired;
my $i = 1;
for (1 .. $proc) {
$q->add(process(sub { shift; return shift() })->set_pipes(0)->args($i));
$i++;
}
my %output;
$q->once(
stop => sub {
$fired++;
$output{shift->return_status}++;
});
is $q->queue->size, $proc - $q->pool->maximum_processes;
is $q->pool->size, 1;
is $q->pool->maximum_processes, 1;
$q->consume;
is $fired, $proc;
is $q->queue->size, 0;
is $q->pool->size, 0;
is $q->done->size, $proc;
$i = 1;
for (1 .. $proc) {
is $output{$i}, 1;
$i++;
}
};
subtest test_3 => sub {
my $q = queue();
$q->pool->maximum_processes(2);
$q->queue->maximum_processes(100000);
my $proc = 10;
my $fired;
my %output;
my $i = 1;
# Started as long as resources allows (maximum_processes of the main pool)
# That requires then to subscribe for each process event's separately (manually)
for (1 .. $proc) {
my $p = process(sub { shift; return shift() + 42 })->set_pipes(0)->args($i);
$p->once(
stop => sub {
$fired++;
$output{shift->return_status}++;
});
$q->add($p);
$i++;
}
is $q->pool->maximum_processes, 2;
$q->consume;
is $q->queue->size, 0;
is $q->pool->size, 0;
is $q->done->size, $proc;
is $fired, $proc;
$i = 1;
for (1 .. $proc) {
is $output{$i + 42}, 1 or diag explain \%output;
$i++;
}
};
is(Mojo::IOLoop::ReadWriteProcess::Session->singleton->all->size, 40);
is(Mojo::IOLoop::ReadWriteProcess::Session->singleton->all_orphans->size, 0);
subtest stress_test => sub {
plan skip_all => "set STRESS_TEST=1 (be careful)" unless $ENV{STRESS_TEST};
Mojo::IOLoop::ReadWriteProcess::Session->singleton->reset;
my $q = queue();
$q->pool->maximum_processes(50);
$q->queue->maximum_processes(100000);
my $proc = 200;
my $fired;
my %output;
my $i = 1;
# Started as long as resources allows (maximum_processes of the main pool)
# That requires then to subscribe for each process event's separately (manually)
for (1 .. $proc) {
my $p
= process(sub { shift; sleep 4; exit shift() })
->set_pipes(0)
->internal_pipes(0)
->args($i);
$p->once(
stop => sub {
$fired++;
$output{shift->exit_status}++;
});
$q->add($p);
$i++;
}
is $q->pool->maximum_processes, 50;
$q->consume;
is $q->queue->size, 0;
is $q->pool->size, 0;
is $q->done->size, $proc;
is $fired, $proc;
$i = 1;
for (1 .. $proc) {
is $output{$i}, 1 or diag explain \%output;
$i++;
}
is(Mojo::IOLoop::ReadWriteProcess::Session->singleton->all->size, 200);
};
done_testing;
|