diff options
Diffstat (limited to 't/handle.t')
-rw-r--r-- | t/handle.t | 80 |
1 files changed, 80 insertions, 0 deletions
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' ); +} |