From 0581dbd91216c18ded2812ec9f3527c0a889da76 Mon Sep 17 00:00:00 2001 From: dakkar Date: Mon, 12 Jul 2010 21:50:31 +0100 Subject: test for TT::Manager & signaling --- lib/Thread/Task.pm | 11 ++++++--- lib/Thread/Task/Handle.pm | 2 +- lib/Thread/Task/Manager.pm | 11 ++++----- lib/Thread/Task/Role/Conduit.pm | 30 +++++++++++++++++++++++++ t/lib/Test/Conduit.pm | 30 +++++++++++++++++++++++++ t/signal.t | 49 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 122 insertions(+), 11 deletions(-) create mode 100644 lib/Thread/Task/Role/Conduit.pm create mode 100644 t/lib/Test/Conduit.pm create mode 100644 t/signal.t diff --git a/lib/Thread/Task.pm b/lib/Thread/Task.pm index 86a1387..5567805 100644 --- a/lib/Thread/Task.pm +++ b/lib/Thread/Task.pm @@ -102,9 +102,14 @@ class Thread::Task { die "Can't update between different task classes"; } for my $attr ($self->meta->get_all_attributes) { - $attr->set_value($self, - $attr->get_value($new_task) - ); + if ($attr->has_value($new_task)) { + $attr->set_value($self, + $attr->get_value($new_task) + ); + } + else { + $attr->clear_value($self); + } } return; } diff --git a/lib/Thread/Task/Handle.pm b/lib/Thread/Task/Handle.pm index 5cb2eed..e3f0eea 100644 --- a/lib/Thread/Task/Handle.pm +++ b/lib/Thread/Task/Handle.pm @@ -119,7 +119,7 @@ class Thread::Task::Handle { } method stopped() { - $self->message('__STOPPED__'); + $self->message('__STOPPED__',$self->task); } method on_stopped(Task_T $new_task, @args) { diff --git a/lib/Thread/Task/Manager.pm b/lib/Thread/Task/Manager.pm index 1410799..14f6963 100644 --- a/lib/Thread/Task/Manager.pm +++ b/lib/Thread/Task/Manager.pm @@ -49,7 +49,6 @@ class Thread::Task::Manager { _workers_count => 'count', _set_worker => 'set', _get_worker => 'get', - _delete_worker => 'delete', _workers => 'elements', }, ); @@ -64,7 +63,6 @@ class Thread::Task::Manager { _set_handle => 'set', _get_handle => 'get', _delete_handle => 'delete', - _handles => 'elements', }, ); @@ -74,11 +72,9 @@ class Thread::Task::Manager { default => sub { {} }, init_arg => undef, handles => { - _running_count => 'count', _set_running => 'set', _get_running => 'get', _delete_running => 'delete', - _running => 'elements', }, ); @@ -99,7 +95,7 @@ class Thread::Task::Manager { does => Conduit_T, ); - method BUILD() { + method BUILD(HashRef $params) { $self->conduit->conduit_init($self); } @@ -131,7 +127,8 @@ class Thread::Task::Manager { } method stop_thread(Int $worker_id) { - $self->_delete_worker($worker_id)->stop; + my $worker=$self->_get_worker($worker_id); + $worker->stop; } method next_thread() { @@ -173,7 +170,7 @@ class Thread::Task::Manager { my $worker = $self->next_thread or return; $worker->hid($hid); - $handle->wid($worker->wid); + #$handle->wid($worker->wid); $worker->send(task => $handle->as_array); diff --git a/lib/Thread/Task/Role/Conduit.pm b/lib/Thread/Task/Role/Conduit.pm new file mode 100644 index 0000000..1ffbd04 --- /dev/null +++ b/lib/Thread/Task/Role/Conduit.pm @@ -0,0 +1,30 @@ +use 5.008003; +use MooseX::Declare; + +role Thread::Task::Role::Conduit { + use MooseX::Types::Moose qw(ClassName); + use Thread::Task::Types qw(Manager_T); + use Carp; + + requires 'signal'; + + has manager => ( + isa => Manager_T, + is => 'ro', + required => 0, + writer => '_set_manager', + predicate => '_has_manager', + ); + + method conduit_init(Manager_T $manager) { + $self->_set_manager($manager); + } + + before signal { + if (!$self->_has_manager) { + croak "Can't signal without a manager"; + } + } +} + +1; diff --git a/t/lib/Test/Conduit.pm b/t/lib/Test/Conduit.pm new file mode 100644 index 0000000..02454e7 --- /dev/null +++ b/t/lib/Test/Conduit.pm @@ -0,0 +1,30 @@ +use 5.008003; +use MooseX::Declare; + +class Test::Conduit with Thread::Task::Role::Conduit { + use Thread::Queue; + use Thread::Task::Types qw(Queue_T); + + has queue => ( + isa => Queue_T, + is => 'ro', + init_arg => undef, + default => sub { Thread::Queue->new }, + handles => { + signal => 'enqueue', + pop => 'dequeue', + }, + ); + + method runloop() { + while (my $msg=$self->pop) { + $self->manager->on_signal($msg); + } + } + + method runonce() { + $self->manager->on_signal($self->pop); + } +} + +1; diff --git a/t/signal.t b/t/signal.t new file mode 100644 index 0000000..81e984e --- /dev/null +++ b/t/signal.t @@ -0,0 +1,49 @@ +#!perl +use strict; +use warnings; +use Test::Most tests => 10, 'die'; +use threads; +use lib 't/lib'; +use Thread::Task::Manager; +use Test::Addition; +use Test::Conduit; + +is( scalar( threads->list ), 0, 'No threads' ); +SCOPE: { + my $conduit = Test::Conduit->new(); + + my $manager = Thread::Task::Manager->new( conduit => $conduit ); + isa_ok( $manager, 'Thread::Task::Manager' ); + is( scalar( threads->list ), 0, 'No threads' ); + + # Run the startup process + $manager->start; + sleep(1); + is( scalar( threads->list ), $manager->minimum+1, 'Three threads exists' ); + + # Create the sample task + my $addition = Test::Addition->new( + x => 2, + y => 3, + ); + isa_ok( $addition, 'Test::Addition' ); + + # Schedule the task (which should trigger its execution) + $manager->schedule($addition); + + # Only the prepare phase should run (for now) + is( $addition->prepare_cnt, 1, '->prepare_cnt is 1' ); + is( $addition->run_cnt, 0, '->run_cnt is 0' ); + is( $addition->finish_cnt, 0, '->finish_cnt is 0' ); + + $conduit->runonce(); + $conduit->runonce(); + + # Run the shutdown process + $manager->stop; + sleep(1); + is( scalar( threads->list ), 0, 'No threads' ); +} + +# Do we start with no threads as expected +is( scalar( threads->list ), 0, 'No threads' ); -- cgit v1.2.3