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
|
#!/usr/bin/perl
use warnings;
use strict;
use utf8;
use open qw(:std :utf8);
use lib qw(lib ../lib);
use Test::More tests => 9;
use Encode qw(decode encode);
use Time::HiRes qw(time);
BEGIN {
# Подготовка объекта тестирования для работы с utf8
my $builder = Test::More->builder;
binmode $builder->output, ":utf8";
binmode $builder->failure_output, ":utf8";
binmode $builder->todo_output, ":utf8";
use_ok 'AnyEvent';
use_ok 'AnyEvent::Tools', ':mutex';
}
{
my $mutex = mutex;
my ($counter, $total) = (0, 0);
my $cv = condvar AnyEvent;
my ($timer1, $timer2, $timer3);
$timer1 = AE::timer 0, 0.2 => sub {
$total++;
if ($mutex->is_locked) {
$counter++;
}
};
$timer2 = AE::timer 1, 0 => sub {
$mutex->lock(sub {
my ($g) = @_;
undef $timer2;
my $timer;
$timer = AE::timer 2, 0 => sub {
undef $g;
undef $timer;
};
});
return;
};
$timer3 = AE::timer 5, 0 => sub {
$cv->send;
};
$cv->recv;
ok $counter < 13 && $counter > 8,
"Mutex was locked correct time ($counter/$total)";
}
{
my $cv = condvar AnyEvent;
my $mutex = mutex;
my $idle;
my %res;
$mutex->lock(sub {
my $start_time = time;
my $mutex_guard = shift;
my $timer;
$timer = AE::timer 0.1, 0 => sub {
$res{1} = { start => $start_time, stop => time};
undef $timer;
undef $mutex_guard;
};
});
$mutex->lock(sub {
my $start_time = time;
my $mutex_guard = shift;
my $timer;
$timer = AE::timer 0.1, 0 => sub {
$res{2} = { start => $start_time, stop => time};
undef $timer;
undef $mutex_guard;
};
});
$mutex->lock(sub {
my $start_time = time;
my $mutex_guard = shift;
my $timer;
$timer = AE::timer 0.1, 0 => sub {
$res{3} = { start => $start_time, stop => time};
undef $timer;
undef $mutex_guard;
};
});
$idle = AE::timer 0, 0.05 => sub {
return unless 3 == keys %res;
undef $idle;
$cv->send;
};
$cv->recv;
ok abs($res{1}{start} - $res{2}{start}) > 0.09,
"First and second processes followed sequentially";
ok $res{1}{stop} < $res{2}{start},
"Second process was started after first had finished";
ok abs($res{2}{start} - $res{3}{start}) > 0.09,
"Second and third processes followed sequentially";
ok $res{2}{stop} < $res{3}{start},
"Third process was started after second had finished";
}
{
my $cv = condvar AnyEvent;
my $error;
my $mutex = mutex;
my $counter = 0;
$mutex->lock(sub {
my ($guard) = @_;
my $timer;
$timer = AE::timer .1, 0 => sub {
undef $guard;
undef $timer;
$counter++;
};
});
my $mguard = $mutex->lock(sub {
$error = 1;
});
$mutex->lock(sub {
$counter++;
});
my $timer;
$timer = AE::timer .05, 0 => sub {
undef $timer;
undef $mguard;
};
my $timer2 = AE::timer 0.5, 0 => sub {
$cv->send;
};
$cv->recv;
ok !$error, "Cancel lock request";
ok $counter == 2, "All lock requests were handled";
}
|