EV-Gearman
view release on metacpan or search on metacpan
t/07_edge_cases.t view on Meta::CPAN
}
# 5) grab_job direct callback (no register_function) â JOB_ASSIGN path
# Use a server-side echo to confirm the worker's can_do has been
# processed before we submit the job: that synchronizes the test
# without depending on a fixed timer.
{
my $cli = EV::Gearman->new(host => $host, port => $port);
my $wkr = EV::Gearman->new(host => $host, port => $port);
my $func = "edge_grab_$$";
$wkr->can_do($func);
# echo round-trip flushes all preceding writes (including CAN_DO)
# â by the time the echo callback fires, gearmand has registered
# this worker's ability for $func.
$wkr->echo("sync", sub { EV::break });
my $g = EV::timer 5, 0, sub { fail "echo sync timeout"; EV::break };
EV::run;
# Submit and grab live on different connections, so the server
# could see GRAB before SUBMIT if both are racing on the wire.
# Defer the grab until $cli has seen JOB_CREATED â then the job
# is definitely queued server-side before the worker asks for it.
my ($got_job);
$cli->submit_job_bg($func, "WORK", sub {
my ($handle) = @_;
$wkr->grab_job(sub {
my ($j, $err) = @_;
$got_job = $j;
$j->complete(scalar reverse $j->workload) if $j;
EV::break;
});
});
$g = EV::timer 5, 0, sub { fail "grab_job timeout"; EV::break };
EV::run;
ok $got_job, 'grab_job delivered a job';
is $got_job && $got_job->workload, 'WORK', 'job workload correct';
}
# 6) grab_job direct callback (no register_function) â NO_JOB path
{
my $wkr = EV::Gearman->new(host => $host, port => $port);
my $func = "edge_nojob_$$"; # never submitted to
$wkr->can_do($func);
my ($job, $err);
$wkr->on_connect(sub {
$wkr->grab_job(sub { ($job, $err) = @_; EV::break });
});
my $g = EV::timer 3, 0, sub { EV::break };
EV::run;
ok !$job, 'grab_job got no job';
is $err, 'no job', '"no job" error string';
}
# 7) accessor round-trip
{
my $g = EV::Gearman->new;
$g->priority(2);
is $g->priority, 2, 'priority round-trip';
$g->keepalive(120);
is $g->keepalive, 120, 'keepalive round-trip';
}
# 8) is_connected progresses through the connecting â connected phases
{
my $g = EV::Gearman->new(host => $host, port => $port);
ok $g->is_connected, 'is_connected true while connecting';
$g->on_connect(sub { EV::break });
my $w = EV::timer 3, 0, sub { EV::break };
EV::run;
ok $g->is_connected, 'is_connected true after on_connect';
$g->disconnect;
ok !$g->is_connected, 'is_connected false after disconnect';
}
done_testing;
( run in 0.801 second using v1.01-cache-2.11-cpan-a49fcb8fa48 )