diff options
Diffstat (limited to 't/worker.t')
-rw-r--r-- | t/worker.t | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/t/worker.t b/t/worker.t new file mode 100644 index 0000000..33db0f4 --- /dev/null +++ b/t/worker.t @@ -0,0 +1,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' ); |