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
|
#!/usr/bin/perl
use warnings;
use strict;
use utf8;
use open qw(:std :utf8);
use lib qw(lib ../lib);
use Test::More tests => 8;
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 = rw_mutex;
my $cv = condvar AnyEvent;
my $counter = 0;
my $done_counter = 0;
my $timer;
$timer = AE::timer 0.13, 0 => sub { $cv->send };
$mutex->rlock(sub {
my ($g) = @_;
my $t;
my $mcounter = 0;
$t = AE::timer 0.01, 0.01 => sub {
$mcounter++;
if ($mcounter++ >= 10) {
undef $t;
undef $g;
$done_counter++;
$cv->send if $done_counter == 2;
return;
}
$counter++;
};
});
$mutex->rlock(sub {
my ($g) = @_;
my $t;
my $mcounter = 0;
$t = AE::timer 0.01, 0.01 => sub {
$mcounter++;
if ($mcounter++ >= 10) {
undef $t;
undef $g;
$done_counter++;
$cv->send if $done_counter == 2;
return;
}
$counter++;
};
});
$cv->recv;
ok $counter == 10, "Two rlock work properly";
}
{
my $mutex = rw_mutex;
my $cv = condvar AnyEvent;
my %res;
my $time = time;
$mutex->rlock(sub {
my ($g) = @_;
$res{'first-start'} = time - $time;
my $t;
$t = AE::timer 0.3, 0 => sub {
$res{'first-stop'} = time - $time;
undef $g;
undef $t;
};
});
$mutex->rlock(sub {
$res{'second'} = time - $time;
});
$mutex->wlock(sub {
my ($g) = @_;
$res{'third-start'} = time - $time;
my $t;
$t = AE::timer 0.2, 0 => sub {
$res{'third-stop'} = time - $time;
undef $g;
undef $t;
};
});
$mutex->rlock(sub {
my ($g) = @_;
$res{'fourth-start'} = time - $time;
my $t;
$t = AE::timer 0.2, 0 => sub {
$res{'fourth-stop'} = time - $time;
undef $g;
undef $t;
$cv->send;
};
});
$mutex->rlock(sub {
$res{'fifth'} = time - $time;
});
$cv->recv;
ok abs($res{'first-start'} - $res{second}) < .001,
"First and second started simultaneously";
ok $res{'third-start'} > $res{'first-stop'},
"Write lock was after all rlock were freed";
ok $res{'fourth-start'} > $res{'third-stop'},
"Read lock waited until write lock is done";
ok abs($res{fifth} - $res{'fourth-start'}) < .001,
"Waited rlocks sarted simultaneously";
}
{
my $cv = condvar AnyEvent;
my $mutex = rw_mutex;
$mutex->rlock_limit(2);
my @res;
for my $step (1 .. 20) {
$mutex->rlock(sub {
my ($g) = @_;
my $t;
$t = AE::timer .1, 0, sub {
push @res, time;
undef $t;
undef $g;
$cv->send if $step == 20;
};
});
}
$cv->recv;
my $ok = 1;
for (my $i = 0; $i < @res - 2; $i += 2) {
$ok = $res[$i + 2] - $res[$i] >= .095;
last unless $ok;
}
ok $ok, "rlock_limit works fine";
}
|