summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordakkar <dakkar@thenautilus.net>2010-07-11 22:44:16 +0100
committerdakkar <dakkar@thenautilus.net>2010-07-11 22:44:16 +0100
commit2a1c8d80336a93bdf5b89008917947b4ca912f27 (patch)
tree773f54cf2066fcbd0684b1872709d35d02f2fda8
parenttest for TT::Thread->master (diff)
downloadThread-Task-2a1c8d80336a93bdf5b89008917947b4ca912f27.tar.gz
Thread-Task-2a1c8d80336a93bdf5b89008917947b4ca912f27.tar.bz2
Thread-Task-2a1c8d80336a93bdf5b89008917947b4ca912f27.zip
test for TT::Handle
-rw-r--r--lib/Thread/Task.pm28
-rw-r--r--lib/Thread/Task/Handle.pm8
-rw-r--r--lib/Thread/Task/Manager.pm2
-rw-r--r--lib/Thread/Task/Types.pm6
-rw-r--r--t/handle.t80
-rw-r--r--t/lib/Test/Addition.pm56
6 files changed, 162 insertions, 18 deletions
diff --git a/lib/Thread/Task.pm b/lib/Thread/Task.pm
index 35976c6..86a1387 100644
--- a/lib/Thread/Task.pm
+++ b/lib/Thread/Task.pm
@@ -2,8 +2,9 @@ use 5.008003;
use MooseX::Declare;
class Thread::Task {
- use MooseX::Types::Moose qw(ClassName Str Int);
- use Thread::Task::Types qw(Handle_T Task_T);
+ use MooseX::Types::Moose qw(ClassName Str Int HashRef);
+ use Thread::Task::Types qw(Handle_T Task_T TaskRev_T);
+ use Moose::Util::TypeConstraints;
require Storable;
@@ -18,9 +19,15 @@ class Thread::Task {
is => 'ro',
weak_ref => 1,
required => 0,
+ init_arg => 'owner',
predicate => '_has_owner_taskrev',
);
+ coerce TaskRev_T, from Task_T,
+ via {
+ $_->task_revision;
+ };
+
has callback => (
isa => Str,
is => 'ro',
@@ -36,16 +43,7 @@ class Thread::Task {
clearer => 'clear_handle',
);
- around BUILDARGS(ClassName $class: @rest) {
- my $params = $class->$orig(@rest);
-
- if (exists $params->{owner}) {
- $params->{_owner_taskrev} =
- delete($params->{owner})->task_revision;
- }
- }
-
- method BUILD() {
+ method BUILD(HashRef $params) {
if ($self->_has_owner_taskrev) {
my $owner = $self->owner;
my $callback = $self->callback;
@@ -91,12 +89,12 @@ class Thread::Task {
}
method from_string(ClassName $class: Str $serialization) {
- my $self=Storable::nthaw($serialization);
- my $self_class=$self->meta->name;
+ my $new_self=Storable::thaw($serialization);
+ my $self_class=$new_self->meta->name;
unless ($self_class eq $class) {
die "Deserialized as $self_class instead of $class";
}
- return $self;
+ return $new_self;
}
method _update(Task_T $new_task) {
diff --git a/lib/Thread/Task/Handle.pm b/lib/Thread/Task/Handle.pm
index 5dfcd40..5cb2eed 100644
--- a/lib/Thread/Task/Handle.pm
+++ b/lib/Thread/Task/Handle.pm
@@ -7,6 +7,7 @@ class Thread::Task::Handle {
use MooseX::Types::Moose qw(ClassName Int ArrayRef);
use Thread::Task::Types qw(Handle_T Worker_T Task_T Finished_ET);
use Thread::Task::Exception::Finished;
+ use MooseX::MultiMethods;
require Scalar::Util;
@@ -16,7 +17,6 @@ class Thread::Task::Handle {
isa => Int,
is => 'ro',
default => sub { ++$SEQUENCE },
- init_arg => undef,
);
has task => (
@@ -32,6 +32,10 @@ class Thread::Task::Handle {
init_arg => undef,
);
+ multi method BUILDARGS(ClassName $class: Task_T $task) {
+ return $class->next::method({task=>$task});
+ }
+
method prepare() {
try {
$self->task->prepare;
@@ -86,7 +90,7 @@ class Thread::Task::Handle {
return $class->new(
hid=>$hid,
- task=>Thread::Task->from_string($task_serialized),
+ task=>$task_class->from_string($task_serialized),
);
}
diff --git a/lib/Thread/Task/Manager.pm b/lib/Thread/Task/Manager.pm
index f94aba6..1410799 100644
--- a/lib/Thread/Task/Manager.pm
+++ b/lib/Thread/Task/Manager.pm
@@ -183,7 +183,7 @@ class Thread::Task::Manager {
method on_signal(Str $frozen) {
my $message;
try {
- $message = Storable::nthaw($frozen);
+ $message = Storable::thaw($frozen);
}
catch {
return;
diff --git a/lib/Thread/Task/Types.pm b/lib/Thread/Task/Types.pm
index 8a4d044..9ee87ed 100644
--- a/lib/Thread/Task/Types.pm
+++ b/lib/Thread/Task/Types.pm
@@ -5,15 +5,21 @@ use MooseX::Types
Handle_T
Worker_T
Task_T
+ TaskRev_T
+ Manager_T
Conduit_T
Finished_ET
Queue_T
Queue_Ev_T
)];
+use MooseX::Types::Moose qw(Int);
class_type Handle_T, { class => 'Thread::Task::Handle' };
class_type Worker_T, { class => 'Thread::Task::Worker' };
class_type Task_T, { class => 'Thread::Task' };
+class_type Manager_T, { class => 'Thread::Task::Manager' };
+
+subtype TaskRev_T, as Int;
role_type Conduit_T, { role => 'Thread::Task::Role::Conduit' };
diff --git a/t/handle.t b/t/handle.t
new file mode 100644
index 0000000..1000cd2
--- /dev/null
+++ b/t/handle.t
@@ -0,0 +1,80 @@
+#!perl
+use strict;
+use warnings;
+use Test::Most tests => 34, 'die';
+use threads;
+use lib 't/lib';
+use Thread::Task::Handle;
+use Test::Addition;
+
+SCOPE: {
+ my $addition = Test::Addition->new({
+ x => 2,
+ y => 3,
+ });
+ isa_ok( $addition, 'Test::Addition' );
+ is( $addition->x, 2, '->x matches expected' );
+ is( $addition->y, 3, '->y matches expected' );
+ is( $addition->z, undef, '->z matches expected' );
+
+ # Run the task
+ is( $addition->prepare_cnt, 0, '->prepare_cnt is 0' );
+ $addition->prepare;
+ is( $addition->prepare_cnt, 1, '->prepare_cnt is 1' );
+
+ is( $addition->run_cnt, 0, '->run_cnt is 0' );
+ $addition->run;
+ is( $addition->run_cnt, 1, '->run_cnt is 1' );
+
+ is( $addition->finish_cnt, 0, '->finish_cnt is 0' );
+ $addition->finish;
+ is( $addition->finish_cnt, 1, '->finish_cnt is 1' );
+
+ is( $addition->x, 2, '->x matches expected' );
+ is( $addition->y, 3, '->y matches expected' );
+ is( $addition->z, 5, '->z matches expected' );
+
+ # Check task round-trip serialization
+ my $string = $addition->as_string;
+ ok( ( defined $string and !ref $string and length $string ),
+ '->as_string ok',
+ );
+ my $round = Test::Addition->from_string($string);
+ isa_ok( $round, 'Test::Addition' );
+ is_deeply( $round, $addition, 'Task round-trips ok' );
+}
+
+SCOPE: {
+ my $task = Test::Addition->new( x => 2, y => 3 );
+ my $handle = Thread::Task::Handle->new($task);
+ isa_ok( $handle, 'Thread::Task::Handle' );
+ isa_ok( $handle->task, 'Test::Addition' );
+ is( $handle->hid, 1, '->hid ok' );
+ is( $handle->task->x, 2, '->x matches expected' );
+ is( $handle->task->y, 3, '->y matches expected' );
+ is( $handle->task->z, undef, '->z matches expected' );
+
+ # Run the task
+ is( $task->prepare_cnt, 0, '->prepare_cnt is 0' );
+ $handle->prepare;
+ is( $task->prepare_cnt, 1, '->prepare_cnt is 1' );
+
+ is( $task->run_cnt, 0, '->run_cnt is 0' );
+ $handle->run;
+ is( $task->run_cnt, 1, '->run_cnt is 1' );
+
+ is( $task->finish_cnt, 0, '->finish_cnt is false' );
+ $handle->finish;
+ is( $task->finish_cnt, 1, '->finish_cnt is true' );
+
+ is( $handle->task->x, 2, '->x matches expected' );
+ is( $handle->task->y, 3, '->y matches expected' );
+ is( $handle->task->z, 5, '->z matches expected' );
+
+ # Check handle round-trip serialisation
+ my $array = $handle->as_array;
+ is( ref($array), 'ARRAY', '->as_array ok' );
+ my $round = Thread::Task::Handle->from_array($array);
+ isa_ok( $round, 'Thread::Task::Handle' );
+ is_deeply( $round, $handle, 'Round trip serialisation ok' );
+}
diff --git a/t/lib/Test/Addition.pm b/t/lib/Test/Addition.pm
new file mode 100644
index 0000000..888bb78
--- /dev/null
+++ b/t/lib/Test/Addition.pm
@@ -0,0 +1,56 @@
+use 5.008003;
+use MooseX::Declare;
+
+class Test::Addition extends Thread::Task {
+ use MooseX::Types::Moose qw(Int);
+
+ has prepare_cnt => (
+ traits => ['Counter'],
+ isa => Int,
+ is => 'ro',
+ default => 0,
+ init_arg => undef,
+ handles => {
+ prepare => 'inc',
+ },
+ );
+
+ has finish_cnt => (
+ traits => ['Counter'],
+ isa => Int,
+ is => 'ro',
+ default => 0,
+ init_arg => undef,
+ handles => {
+ finish => 'inc',
+ },
+ );
+
+ has run_cnt => (
+ traits => ['Counter'],
+ isa => Int,
+ is => 'ro',
+ default => 0,
+ init_arg => undef,
+ handles => {
+ mark_run => 'inc',
+ },
+ );
+
+ has 'x' => (isa=>Int,is=>'ro',required=>1);
+ has 'y' => (isa=>Int,is=>'ro',required=>1);
+ has 'z' => (isa=>Int,is=>'rw',required=>0);
+
+ method run() {
+ $self->mark_run;
+ $self->z($self->x + $self->y);
+ return;
+ }
+
+ before from_string(ClassName $class: Str $serialization) {
+ warn "$class->from_string\n";
+ }
+
+}
+
+1;