From 2a1c8d80336a93bdf5b89008917947b4ca912f27 Mon Sep 17 00:00:00 2001 From: dakkar Date: Sun, 11 Jul 2010 22:44:16 +0100 Subject: test for TT::Handle --- t/handle.t | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++ t/lib/Test/Addition.pm | 56 +++++++++++++++++++++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 t/handle.t create mode 100644 t/lib/Test/Addition.pm (limited to 't') 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; -- cgit v1.2.3