summaryrefslogtreecommitdiff
path: root/t/handle.t
blob: 1000cd28990b9db36a268facb8c18fff40480528 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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({
                => 2,
                => 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( => 2, => 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' );
}