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 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
|
#!/usr/bin/perl
use warnings;
use strict;
use Test::More;
use POSIX;
use FindBin;
use Mojo::File qw(tempfile tempdir path);
use lib ("$FindBin::Bin/lib", "../lib", "lib");
use Mojo::IOLoop::ReadWriteProcess
qw(process queue shared_memory lock semaphore);
use Mojo::IOLoop::ReadWriteProcess::Shared::Semaphore;
use Mojo::IOLoop::ReadWriteProcess::Shared::Lock;
use Mojo::IOLoop::ReadWriteProcess::Shared::Memory;
use Data::Dumper;
use constant DEBUG => $ENV{MOJO_PROCESS_DEBUG};
plan skip_all => "Skipped unless TEST_SHARED is set" unless $ENV{TEST_SHARED};
subtest 'semaphore' => sub {
my $sem_key = 33131;
my $sem = Mojo::IOLoop::ReadWriteProcess::Shared::Semaphore::semaphore(
key => $sem_key);
ok(defined $sem->id, ' We have semaphore id ( ' . $sem->id . ' )');
ok(defined $sem->stat,
' We have semaphore stats ( ' . Dumper($sem->stat) . ' )');
is($sem->stat->[7], 1, 'Default semaphore size is 1');
$sem->setval(0, 1);
is $sem->getval(0), 1, 'Semaphore value set to 1';
$sem->setval(0, 0);
is $sem->getval(0), 0, 'Semaphore value set 0';
$sem->setval(0, 1);
is $sem->getval(0), 1, 'Semaphore value set to 1';
$sem->setall(0);
is $sem->getval(0), 0, 'Semaphore value set 0';
$sem->setval(0, 1);
is $sem->getall, 1, 'We have one semaphore, which is free to go';
is $sem->getncnt, 0, '0 Processes waiting for the semaphore';
my $q = queue;
$q->pool->maximum_processes(10);
$q->queue->maximum_processes(50);
$q->add(
process(
sub {
my $sem = semaphore(key => $sem_key);
my $e = 1;
if ($sem->acquire({wait => 1, undo => 0})) {
$e = 0;
$sem->release();
}
Devel::Cover::report() if Devel::Cover->can('report');
exit($e);
}
)->set_pipes(0)->internal_pipes(0)) for 1 .. 20;
$q->consume();
is $q->done->size, 20, '20 Processes consumed';
$q->done->each(
sub {
is $_[0]->exit_status, 0,
"Process: "
. shift->pid
. " exited with 0 (semaphore acquired at least once)";
});
$sem->remove;
};
subtest 'lock' => sub {
my $k = 2342385;
my $lock
= Mojo::IOLoop::ReadWriteProcess::Shared::Lock::shared_lock(key => $k);
my $q = queue;
$q->pool->maximum_processes(10);
$q->queue->maximum_processes(50);
$q->add(
process(
sub {
my $l = lock(key => $k);
my $e = 1;
if ($l->lock) {
$e = 0;
$l->unlock;
}
Devel::Cover::report() if Devel::Cover->can('report');
exit($e);
}
)->set_pipes(0)->internal_pipes(0)) for 1 .. 20;
$q->consume();
is $q->done->size, 20, '20 Processes consumed';
$q->done->each(
sub {
is $_[0]->exit_status, 0,
"Process: "
. shift->pid
. " exited with 0 (semaphore acquired at least once)";
});
$lock->remove();
};
subtest 'lock section' => sub {
my $lock
= Mojo::IOLoop::ReadWriteProcess::Shared::Memory::shared_lock(key => 3331);
my $q = queue;
$q->pool->maximum_processes(10);
$q->queue->maximum_processes(50);
$q->add(
process(
sub {
my $l = lock(key => 3331);
my $e = 1;
$l->section(sub { $e = 0 });
Devel::Cover::report() if Devel::Cover->can('report');
exit($e);
}
)->set_pipes(0)->internal_pipes(0)) for 1 .. 20;
$q->consume();
is $q->done->size, 20, '20 Processes consumed';
$q->done->each(
sub {
is $_[0]->exit_status, 0,
"Process: "
. shift->pid
. " exited with 0 (semaphore acquired at least once)";
});
$lock->remove;
};
subtest 'concurrent memory read/write' => sub {
use IPC::SysV 'ftok';
my $k = ftok($0, 0);
my $mem = shared_memory(key => $k);
$mem->_lock->remove;
my $default = shared_memory;
is $default->key, $k, "Default memory key is : $k";
$mem = shared_memory(key => $k);
$mem->clean;
$mem->_lock->remove;
$mem = shared_memory(key => $k);
$mem->lock_section(sub { $mem->buffer('start') });
my $q = queue;
$q->pool->maximum_processes(10);
$q->queue->maximum_processes(50);
$q->add(
process(
sub {
my $mem = shared_memory(key => $k);
srand time;
$mem->lock_section(
sub {
# Random sleeps to try to make threads race into lock section
unless (DEBUG) {
do {
note "$$: Sleeping inside locked section";
sleep rand(int(2));
}
for 1 .. 5;
}
my $b = $mem->buffer;
$mem->buffer($$ . " $b");
Devel::Cover::report() if Devel::Cover->can('report');
});
}
)->set_pipes(0)->internal_pipes(0)) for 1 .. 20;
$q->consume();
$mem = shared_memory(key => $k);
$mem->lock_section(
sub {
ok((length $mem->buffer > 0), 'Buffer is there');
});
$mem->lock_section(
sub {
my @pids = split(/ /, $mem->buffer);
is scalar @pids, 21, 'There are 20 pids and the start word (21)';
});
$mem->_lock->remove;
$mem->remove;
};
sub free_mem {
my $mem = shared_memory;
$mem->_lock->remove;
$mem->remove;
$mem = shared_memory;
$mem->clean;
$mem->_lock->remove;
$mem = shared_memory;
if ($mem->try_lock) {
$mem->buffer(freeze({}));
$mem->unlock;
}
}
sub test_mem {
my $mem = shared_memory(destroy => 1);
$mem->lock_section(
sub {
ok((length $mem->buffer > 0), 'Buffer is there');
my $data = thaw($mem->buffer);
my @pids = keys %{$data};
is scalar @pids, 20, 'There are 20 pids';
diag explain $data;
});
is $mem->stat->[8], 0, 'No process attached to memory';
}
subtest 'storable' => sub {
use Storable qw(freeze thaw);
use Mojo::IOLoop::ReadWriteProcess::Shared::Memory
qw(shared_lock shared_memory semaphore);
free_mem;
my $q = queue;
$q->pool->maximum_processes(10);
$q->queue->maximum_processes(50);
$q->add(
process(
sub {
my $mem = shared_memory;
$mem->lock_section(
sub {
unless (DEBUG) {
do {
note "$$: Sleeping inside locked section";
sleep rand(int(2));
}
for 1 .. 5;
}
my $data = thaw($mem->buffer);
$data->{$$}++;
$mem->buffer(freeze($data));
Devel::Cover::report() if Devel::Cover->can('report');
});
}
)->set_pipes(0)->internal_pipes(0)) for 1 .. 20;
$q->consume();
is $q->done->size, 20, 'Queue consumed 20 processes';
test_mem;
};
#
# subtest 'locking with undo' => sub {
# use Storable qw(freeze thaw);
#
# free_mem;
#
# my $q = queue;
# $q->pool->maximum_processes(10);
# $q->queue->maximum_processes(50);
#
# $q->add(
# process(
# sub {
# my $mem = shared_memory;
#
# if ($mem->lock(undo => 1, wait => 1))
# { # Do not unlock/release with undo => 1
# eval { my $data = thaw($mem->buffer);
# $data->{$$}++;
# $mem->buffer(freeze($data));
# $mem->save();
# };
# warn "FAILED UNDO $@" if $@;
#
# # $mem->unlock();
# }
# Devel::Cover::report() if Devel::Cover->can('report');
# exit(0);
# }
# )->set_pipes(0)->internal_pipes(0)) for 1 .. 20;
#
# $q->consume();
# is $q->done->size, 20, 'Queue consumed 20 processes';
#
# test_mem;
# };
subtest 'dying in locked section' => sub {
use Storable qw(freeze thaw);
free_mem;
my $q = queue;
$q->pool->maximum_processes(10);
$q->queue->maximum_processes(20);
$q->add(
process(
sub {
my $mem = shared_memory;
$mem->lock_section(
sub {
unless (DEBUG) {
do {
note "$$: Sleeping inside locked section";
sleep rand(int(2));
}
for 1 .. 5;
}
my $data = thaw($mem->buffer);
$data->{$$}++;
$mem->buffer(freeze($data));
Devel::Cover::report() if Devel::Cover->can('report');
die("Process failed!");
});
Devel::Cover::report() if Devel::Cover->can('report');
}
)->set_pipes(0)->internal_pipes(0)) for 1 .. 20;
$q->consume();
is $q->done->size, 20, 'Queue consumed 20 processes';
test_mem;
};
done_testing();
|