AE-AdHoc

 view release on metacpan or  search on metacpan

examples/port-probe-multi.pl  view on Meta::CPAN

	/^(.*):(\d+)$/ or die "Expecting host:port. See $0 --help\n"; [$1, $2, $_];
} @ARGV;
usage() unless @probe;

# Real work
eval {
	ae_recv {
		tcp_connect $_->[0], $_->[1], ae_goal("$_->[0]:$_->[1]") for @probe;
	} $timeout;
};
die $@ if $@ and $@ !~ /^Timeout/;

my @offline = sort keys %{ AE::AdHoc->goals };
my (@alive, @reject);

my $results = AE::AdHoc->results;
foreach (keys %$results) {
	# tcp_connect will not feed any args if connect failed
	ref $results->{$_}->[0]
		? push @alive, $_
		: push @reject, $_;

lib/AE/AdHoc.pm  view on Meta::CPAN

Instead, it will call C<$subref-E<gt>( $results )> when stuff is done.

Now we need to test do_stuff, so we set up an event loop. We also need a timer,
because a test that runs forever is annoying. So the script goes like this:

    use AnyEvent;

    # set up event loop
    my $cv = AnyEvent->condvar;
    my $timer = AnyEvent->timer(
        after => 10, cb => sub { $cv->croak("Timeout"); }
    );

    do_stuff( @args, sub{ $cv->send(shift); } );

    # run event loop, get rid of timer
    my $result = $cv->recv();
    undef $timer;

    # finally
    analyze_results( $result );

t/11-begin-end.t  view on Meta::CPAN

	} 1;
} "A simple begin/end example works";

throws_ok {
	my $timer;
	ae_recv {
		ae_begin;
		ae_begin;
		$timer = AnyEvent->timer( after => 0.01, cb => ae_end );
	} 0.02;
} qr(Timeout), "A simple example with extra begin dies";

my @trace;
my $val;

lives_ok {
	ae_recv {
		my $tm;
		my $iter;
		my $attimer;
		$attimer = sub {

t/13-null-timeout.t  view on Meta::CPAN

throws_ok {
	ae_recv{ };
} qr(timeout.*non-?zero), "No timeout = no go";

throws_ok {
	ae_recv{ } "foo";
} qr(timeout.*non-?zero), "Non-numeric timeout = no go";

throws_ok {
	ae_recv{ } 0.01;
} qr(^Timeout after), "Timeout with empty body";

is (scalar @warn, 0, "no warnings");
note "warning: $_" for @warn;

t/16-goals2.t  view on Meta::CPAN

use Test::More tests => 8;
use Test::Exception;

use AE::AdHoc;

throws_ok {
	ae_recv {
		ae_goal("never");
		ae_goal("always")->();
	} 0.1;
} qr(^Timeout), "Goals not done, sorry";
is_deeply( AE::AdHoc->results, { always => [] }, "1 goal done");
is_deeply( AE::AdHoc->goals, { never => 1 }, "1 goal left");

ae_recv { ae_send->(137) } 0.1;

is_deeply( AE::AdHoc->results, { }, "results cleared");
is_deeply( AE::AdHoc->goals, { }, "goals cleared");

throws_ok {
	ae_recv {
		ae_goal("never") for 1..3;
		ae_goal("always")->($_) for 1..3;
	} 0.1;
} qr(^Timeout), "Goals not done, sorry";
is_deeply( AE::AdHoc->results, { always => [1] }, "only first goal callback counts");
is_deeply( AE::AdHoc->goals, { never => 3 }, "1 goal left, but 3 times");

t/19-soft-timeout.t  view on Meta::CPAN

use Test::Exception;

use AE::AdHoc;

lives_ok {
	ae_recv { } soft_timeout => 0.1
} "soft timeout";

throws_ok {
	ae_recv { } timeout => 0.1
} qr(Timeout.*seconds), "hard timeout in options";

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 1.342 second using v1.00-cache-2.02-grep-82fe00e-cpan-1925d2aa809 )