diff options
author | Yorhel <git@yorhel.nl> | 2009-05-17 16:03:07 +0200 |
---|---|---|
committer | Yorhel <git@yorhel.nl> | 2009-05-17 16:03:07 +0200 |
commit | 15a510c4c393336df9563df2bad0214ed8909818 (patch) | |
tree | 155adc60a1f2fc8de809d15878e38f87831ed54e /t | |
parent | 1c21b1039ceff4a301fb6858f6551e146e7b2055 (diff) |
Added two basic tests
Diffstat (limited to 't')
-rw-r--r-- | t/00load.t | 12 | ||||
-rw-r--r-- | t/01register.t | 89 |
2 files changed, 101 insertions, 0 deletions
diff --git a/t/00load.t b/t/00load.t new file mode 100644 index 0000000..306081c --- /dev/null +++ b/t/00load.t @@ -0,0 +1,12 @@ +#!/usr/bin/perl + +# - Make sure we can load the module +# - Module version is available + +use Test::More tests => 3; + +use_ok 'POE::Component::Pg'; + +ok defined($POE::Component::Pg::VERSION), 'Module version exists'; +ok length($POE::Component::Pg::VERSION) > 0, 'Module version is non-empty'; + diff --git a/t/01register.t b/t/01register.t new file mode 100644 index 0000000..d3ba6b4 --- /dev/null +++ b/t/01register.t @@ -0,0 +1,89 @@ +#!/usr/bin/perl + + +# - Checks whether spawn() actually creates a session +# - Checks whether the alias is correctly set +# - Tests register() with the error event +# - Tests error event dispatch on failed connect() +# - Tests shutdown() in an idle situation +# - Tests unregister() with the error event +# - Tests whether the calling session terminates after a shutdown +# (i.e. refcount is correctly decremented) + + +use Test::More tests => 8; + +use POE; +use POE::Component::Pg; + +my $ret = POE::Component::Pg->spawn( + alias => 'pg', +); + +# NOTE: we don't check for what it actually returns, as this isn't defined yet. +ok $ret, 'Spawn returns a true value'; + + +my $state = 0; + +my $tester = POE::Session->create(inline_states => { + _start => sub { + $_[KERNEL]->yield('check_alias'); + }, + + check_alias => sub { + ok defined $_[KERNEL]->alias_resolve('pg'), 'Session exists and alias is set'; + $_[KERNEL]->post(pg => 'shutdown'); + $_[KERNEL]->yield('check_shutdown'); + }, + + check_shutdown => sub { + ok !defined $_[KERNEL]->alias_resolve('pg'), 'Session has stopped'; + POE::Component::Pg->spawn(alias => 'pg2', dsn => 'dbi:incorrect-name'); + $_[KERNEL]->yield('do_misconnect'); + }, + + do_misconnect => sub { + ok defined $_[KERNEL]->alias_resolve('pg2'), 'Session restart'; + $_[KERNEL]->post(pg2 => register => error => 'misconnect_error', connect => 'misconnect_connect'); + $_[KERNEL]->post(pg2 => 'connect'); + $_[KERNEL]->delay('misconnect_timeout', 5); + }, + + misconnect_error => sub { + if(!$state++) { + ok 'Incorrect connection fires error'; + $_[KERNEL]->delay('misconnect_timeout'); + $_[KERNEL]->post(pg2 => unregister => 'error'); + $_[KERNEL]->post(pg2 => 'connect'); + $_[KERNEL]->delay('misconnect_2nd', 1); + } else { + fail 'unregister'; + } + }, + misconnect_connect => sub { fail 'Incorrect connection fires error (connect)' }, + misconnect_timeout => sub { fail 'Incorrect connection fires error (timeout)' }, + misconnect_2nd => sub { + ok 'unregister'; + $_[KERNEL]->post(pg2 => 'shutdown'); + $_[KERNEL]->yield('check_lastshutdown'); + }, + + check_lastshutdown => sub { + ok !defined $_[KERNEL]->alias_resolve('pg2'), 'Second shutdown'; + $_[KERNEL]->post(watcher => 'shutdown'); + }, +}); + + +POE::Session->create(inline_states => { + _start => sub { $_[KERNEL]->alias_set('watcher') }, + shutdown => sub { $_[KERNEL]->yield('shutdown_done') }, + shutdown_done => sub { + ok !defined $_[KERNEL]->alias_resolve($tester), 'Refcount reaches zero'; + }, +}); + + +$poe_kernel->run(); + |