Async-Selector

 view release on metacpan or  search on metacpan

t/30-error.t  view on Meta::CPAN

    $s->register(res => sub { return "RES" }, "" => sub { return "EMPTY" });
    is($result, "", "result is empty before trigger");
    $result = "";
    checkWNum $s, 1;
    $s->trigger("res", "");
    checkWNum $s, 0;
    is($result, ":EMPTY,res:RES", "Got resource after the trigger. undef(empty) resource and 'res' resource.");
}

{
    note('--- watch() with invalid callback');
    my $s = new_ok('Async::Selector');
    my $msg = qr/must be a coderef/i;
    throws_ok {$s->watch(res => 100, undef)} $msg, "callback must not be undef";
    throws_ok {$s->watch(res => 100, "string")} $msg, "... or a string";
    throws_ok {$s->watch(res => 100, [1, 2, 10])} $msg, "... or an arrayref";
    throws_ok {$s->watch(res => 100, {hoge => "foo"})} $msg, "... or a hashref.";
    checkWNum $s, 0;
}

{
    note('--- watch() with no resource');
    my $s = new_ok('Async::Selector');
    my $watcher = undef;
    my @result = ();
    warning_is {$watcher = $s->watch(sub { push(@result, 'token'); return 0 })}
        undef, "watch() finishes with no warning even if it is supplied with no resource selection.";
    isa_ok($watcher, "Async::Selector::Watcher", '... it should still return a Watcher object.');
    ok(!$watcher->active, "... but the Watcher is already inactive.");
    ## ok(!defined($selection), "... it returns no selection object. selection is silently rejected.");
    is(int(@result), 0, "... callback is not executed, because the watch is rejected.");
    checkWNum $s, 0;

    @result = ();
    warning_is {$watcher = $s->watch_et(sub { push(@result, 'token'); return 0 })}
        undef, "The behavior is the same for watch_et().";
    isa_ok($watcher, "Async::Selector::Watcher", '... it should still return a Watcher object.');
    ok(!$watcher->active, "... but the Watcher is already inactive.");
    ## ok(!defined($selection), "... it returns no selection object.");
    is(int(@result), 0, "... callback is not executed, because the watch is rejected.");
    checkWNum $s, 0;
}

{
    note('--- watch() without callback');
    my $s = new_ok('Async::Selector');
    throws_ok { $s->watch(a => 10, b => 20) }
        qr/must be a coderef/i, 'Throw exception when watch() is called without callback';
    throws_ok { $s->watch() }
        qr/must be a coderef/i, 'Throw exception when watch() is called without any argument';
    note('--- -- what if condition input is a coderef?');
    warning_like { $s->watch(a => sub {10}) }
        qr(odd number)i, 'Warning from warning pragma when watch() is called without callback but condition input is a coderef';
    my $fired = 0;
    $s->register(a => sub {
        my ($in) = @_;
        $fired = 1;
        ok(!defined($in), "condition input is undef.");
        return 1;
    });
    is(int($s->watchers), 1, "watcher is alive");
    $s->trigger('a');
    is($fired, 1, "watcher fired.");
    is(int($s->watchers), 1, "watcher is alive. cancel() is not called in the watcher callback.");
}

{
    note('--- unregister() non-existent resource');
    my $s = new_ok('Async::Selector');
    $s->register(res => sub { return 'FOOBAR'});
    is(int($s->resources), 1, "one resource registered");
    warning_is { $s->unregister(qw(this does not exist)) } undef, "non-existent resources are silently ignored in unregister().";
    warning_is { $s->unregister(qw(unknown res)) } undef, "you can unregister() existent resource as well as non-existent one.";
    is(int($s->resources), 0, "no resource registered");
}

{
    note('--- unregister() undef resource');
    my $s = new_ok('Async::Selector');
    $s->register('res' => sub { return 10 });
    is(int($s->resources), 1, "One resource.");
    warning_is { $s->unregister(undef) } undef, "unregister(undef) is silently ignored.";
    is(int($s->resources), 1, "Still one resource.");
    warning_is { $s->unregister(undef, 'res') } undef, "unregister(undef, 'res'). undef is ignored.";
    is(int($s->resources), 0, "Unresgistered.");
}

{
    note('--- unregister() a resource multiple times.');
    my $s = new_ok('Async::Selector');
    $s->register('res' => sub { return 10 });
    is(int($s->resources), 1, "One resource.");
    warning_is { $s->unregister('res', 'res', 'res') } undef, "unregister() the same resource multiple times at once is no problem.";
    is(int($s->resources), 0, "Unregistered.");
}

{
    note('--- unregister() while watcher is active.');
    my $s = new_ok('Async::Selector');
    my $res = 0;
    $s->register('res' => sub { my $in = shift; return $res >= $in ? $res : undef });
    my $result = "";
    my @watchers = ();
    push @watchers, $s->watch('res' => 5, catter(\$result, 0));
    push @watchers, $s->watch('res' => 10, catter(\$result, 1));
    checkWNum $s, 2;
    warning_is { $s->unregister('res') } undef, "unregister() does not warn even when the deleted resource is now selected.";
    checkWNum $s, 2;
    $res = 100;
    $s->trigger('res');
    is($result, "", "Resource 'res' is no longer registered, so triggering it does no effect.");
    checkWNum $s, 2;
}

{
    note('--- register() with no resource');
    my $s = new_ok('Async::Selector');
    checkRNum $s, 0;
    warning_is { $s->register() } undef, "It's OK to call register() with no argument. It does nothing.";
    checkRNum $s, 0;
}

{
    note('--- register() undef resource');



( run in 0.581 second using v1.01-cache-2.11-cpan-df04353d9ac )