summaryrefslogtreecommitdiff
path: root/t/worker.t
blob: 33db0f44fa0a305284b5d058850563690a65210f (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
#!perl 
use strict;
use warnings;
use Test::Most tests => 17, 'die';
use threads;
use Thread::Task::Worker;
use Thread::Task::Thread;
 
is( scalar( threads->list ), 0, 'One thread exists' );
SCOPE: {
 
        # Create the master thread 
        my $master = Thread::Task::Thread->new->spawn;
        isa_ok( $master'Thread::Task::Thread' );
        is( scalar( threads->list ), 1, 'Found 1 thread' );
        ok( $master->is_running, 'Master is_running' );
 
        # Create a single worker 
        my $worker = Thread::Task::Worker->new;
        isa_ok( $worker'Thread::Task::Worker' );
 
        # Start the worker inside the master 
        $master->start($worker);
        sleep 1;
        is( scalar( threads->list ), 2, 'Found 2 threads' );
        ok( $master->is_running,   'Master is_running' );
        ok( !$master->is_joinable, 'Master is not is_joinable' );
        ok( !$master->is_detached, 'Master is not is_detached' );
        ok( $worker->is_running,   'Worker is_running' );
        ok( !$worker->is_joinable, 'Worker is not is_joinable' );
        ok( !$worker->is_detached, 'Worker is not is_detached' );
 
        # Shut down the worker but leave the master running 
        $worker->stop;
        sleep 1;
        ok( $master->is_running,   'Master is_running' );
        ok( !$master->is_joinable, 'Master is not is_joinable' );
        ok( !$master->is_detached, 'Master is not is_detached' );
        ok( !$worker->thread,      'Worker thread has ended' );
}
 
is( scalar( threads->list ), 1, 'Thread is gone' );