Skip to content

Commit

Permalink
Merge pull request #3 from kappa/master
Browse files Browse the repository at this point in the history
Really unregister functions in worker
  • Loading branch information
melo committed Jun 2, 2015
2 parents a1276c3 + 091b81c commit 1c0eaf7
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/AnyEvent/Gearman/Worker.pm
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ sub unregister_function {
$js->context($self) unless $js->context;
$js->unregister_function( $func_name );
}

delete $self->functions->{ $func_name };
}

__PACKAGE__->meta->make_immutable;
Expand Down
64 changes: 64 additions & 0 deletions t/07_unreg.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!perl

use strict;
use warnings;
use Test::Base;
use Test::TCP;

use AnyEvent::Gearman::Worker;

eval q{
use Gearman::Worker;
use Gearman::Server;
};
if ($@) {
plan skip_all
=> "Gearman::Worker and Gearman::Server are required to run this test";
}

plan 'no_plan';

my $port = empty_port;

sub run_tests {
my $server_hostspec = '127.0.0.1:' . $port;

my $worker = AnyEvent::Gearman::Worker->new(
job_servers => [$server_hostspec],
);
$worker->register_function( reverse => sub {
my $job = shift;
my $res = reverse $job->workload;
$job->status(99, 100);
$job->complete($res);
});

eval {
$worker->register_function( reverse => sub {} );
};
like $@, qr/"reverse" already registered/, 'already registered';

$worker->unregister_function( 'reverse' );
eval {
$worker->register_function( reverse => sub {} );
};
is $@, '', 'register after unregister successful';
}

my $child = fork;
if (!defined $child) {
die "fork failed: $!";
}
elsif ($child == 0) {
my $server = Gearman::Server->new( port => $port );
Danga::Socket->EventLoop;
exit;
}
else {
END { kill 9, $child if $child }
}

sleep 1;

run_tests;

0 comments on commit 1c0eaf7

Please sign in to comment.