File: 02_guards.t

package info (click to toggle)
libanyevent-aggressiveidle-perl 0.04-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 80 kB
  • sloc: perl: 145; makefile: 2
file content (56 lines) | stat: -rw-r--r-- 1,152 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/perl

use warnings;
use strict;
use utf8;
use open qw(:std :utf8);
use lib qw(lib ../lib);

use Test::More tests    => 6;
use Encode qw(decode encode);


BEGIN {
    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::AggressiveIdle', ':all';

}

{
    my $cv = condvar AnyEvent;

    my ($cnt1, $cnt2, $cnt2_t, $cnt3) = (0, 0, 0, 0);

    my $idle1 = aggressive_idle {
        $cnt1++;

    };

    my $idle2 = aggressive_idle {
        my ($pid, $guard) = @_;

        $cnt2++;
        my $t;
        $t = AE::timer 0.1, 0 => sub {
            undef $t;
            $cnt2_t++;
            $cv->send if $cnt2 == 10;
            undef $guard;
        }
    };

    my $idle3 = aggressive_idle { $cnt3++ };

    $cv->recv;

    ok $cnt1 == $cnt3, 'Two aggressive_idle counters are equal';
    ok $cnt1 > $cnt2,  'Timer works properly';
    ok $cnt2_t == 10,  'Subroutine was called 10 times';
    ok abs($cnt2 - $cnt2_t) <= 1, 'Idle process was called 10 or 11 times';
}