Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add cascade

  • Loading branch information...
commit dda142078acd52aa908e22955167dbd36dd1b6e7 1 parent 0d6888f
@nekokak authored
Showing with 63 additions and 1 deletion.
  1. +12 −1 lib/Clutch/Worker.pm
  2. +36 −0 t/005_cascade.t
  3. +15 −0 t/lib/Worker.pm
View
13 lib/Clutch/Worker.pm
@@ -17,9 +17,11 @@ our @EXPORT = qw(
dispatch
do_request
do_request_background
+ cascade
);
my $FUNCTIONS = +{};
+my $CONTEXT;
sub new {
my $class = shift;
@@ -37,7 +39,7 @@ sub new {
);
my $self = bless \%args, $class;
-
+ $CONTEXT = $self;
$self;
}
@@ -177,6 +179,15 @@ sub do_request_background {
return;
}
+sub cascade {
+ my ($function, $args) = @_;
+
+ my $code = $CONTEXT->{functions}->{$function};
+ my $res = $code ? ($code->($args) || $NULL)
+ : "ERROR: unknow function";
+ $res;
+}
+
sub register_function ($$) { ## no critic
my ($function, $code) = @_;
$FUNCTIONS->{$function} = $code;
View
36 t/005_cascade.t
@@ -0,0 +1,36 @@
+use strict;
+use warnings;
+use lib './t/lib';
+use Test::More;
+use Test::TCP;
+use Clutch::Client;
+use Worker;
+
+test_tcp(
+ client => sub {
+ my ($port, $server_pid) = @_;
+ my $client = Clutch::Client->new(
+ servers => ['127.0.0.1:'.$port]
+ );
+
+ my $res;
+ {
+ $res = $client->request('cascade_root');
+ note $res;
+ is $res, 'cascaded';
+ }
+
+ kill 'TERM', $server_pid;
+ },
+ server => sub {
+ my $port = shift;
+ Worker->new(
+ {
+ address => '127.0.0.1:'.$port,
+ }
+ )->run();
+ },
+);
+
+done_testing;
+
View
15 t/lib/Worker.pm
@@ -34,5 +34,20 @@ register_function(
}
);
+register_function(
+ 'cascade_root' => sub {
+ my $args = shift;
+ my $res = cascade('cascade_sub', $args);
+ return $res;
+ }
+);
+
+register_function(
+ 'cascade_sub' => sub {
+ my $args = shift;
+ return 'cascaded';
+ }
+);
+
1;
Please sign in to comment.
Something went wrong with that request. Please try again.