summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2009-05-17 16:53:19 +0200
committerYorhel <git@yorhel.nl>2009-05-17 17:13:36 +0200
commitd1e3b2294aff9d2ff9e718b9719bfcb981b25907 (patch)
treec7a5895320da48ceb777e1b1df59ed606a4f7565
parent15a510c4c393336df9563df2bad0214ed8909818 (diff)
Wrote another test file to test the connection process
-rw-r--r--t/02connect.t90
1 files changed, 90 insertions, 0 deletions
diff --git a/t/02connect.t b/t/02connect.t
new file mode 100644
index 0000000..b5caece
--- /dev/null
+++ b/t/02connect.t
@@ -0,0 +1,90 @@
+#!/usr/bin/perl
+
+# - Tests connecting to the database
+# - Tests that the connect event is dispatched on connection
+# - Tests whether shutdown() works while connected
+# - Tests that the arguments of connect() overwrite the ones given to spawn()
+# - Tests that the arguments given to spawn() are used when connecting
+
+use strict;
+use warnings;
+use Test::More;
+use POE::Component::Pg;
+use DBI;
+use POE;
+
+my $dbh;
+eval {
+ $dbh = DBI->connect();
+};
+if($@) {
+ BAIL_OUT("Couldn't connect to PostgreSQL, please set the DBI_DSN, DBI_USER and DBI_PASS environment variables.");
+} else {
+ plan tests => 7;
+}
+
+POE::Component::Pg->spawn(alias => 'pg');
+
+
+POE::Session->create(inline_states => {
+ _start => sub {
+ $_[KERNEL]->post(pg => register => connect => 'pg_connect', error => 'pg_error');
+ $_[KERNEL]->post(pg => 'connect');
+ $_[KERNEL]->delay('connect_timeout', 10);
+ },
+
+ pg_connect => sub {
+ ok 'Connect';
+ $_[KERNEL]->post(pg => 'shutdown');
+ $_[KERNEL]->delay('connect_timeout');
+ $_[KERNEL]->yield('disconnect');
+ },
+ pg_error => sub { fail 'Connect (error)' },
+ connect_timeout => sub { fail 'Connect (timeout)' },
+
+ disconnect => sub {
+ ok !defined $_[KERNEL]->alias_resolve('pg'), 'Connection close';
+ POE::Component::Pg->spawn(alias => 'pg', dsn => 'dbi:doesntexists', user => 'wont-ever-work', password => 'some-never-used-password');
+ $_[KERNEL]->yield('overwrite');
+ },
+
+ overwrite => sub {
+ ok defined $_[KERNEL]->alias_resolve('pg'), 'Re-open session';
+ $_[KERNEL]->post(pg => register => connect => 'overwrite_connect', error => 'overwrite_error');
+ $_[KERNEL]->post(pg => connect => { dsn => $ENV{DBI_DSN}, user => $ENV{DBI_USER}, password => $ENV{DBI_PASS} });
+ $_[KERNEL]->delay('overwrite_timeout', 10);
+ },
+ overwrite_connect => sub {
+ ok 'Overwrite';
+ $_[KERNEL]->delay('overwrite_timeout');
+ $_[KERNEL]->post(pg => 'shutdown');
+ $_[KERNEL]->yield('disconnect2');
+ },
+ overwrite_error => sub { fail 'Overwrite (error)' },
+ overwrite_timeout => sub { fail 'Overwrite (timeout)' },
+
+ disconnect2 => sub {
+ ok !defined $_[KERNEL]->alias_resolve('pg'), 'Connection close again';
+ POE::Component::Pg->spawn(alias => 'pg', dsn => $ENV{DBI_DSN}, user => $ENV{DBI_USER}, password => $ENV{DBI_PASS});
+ $ENV{DBI_DSN} = $ENV{DBI_USER} = $ENV{DBI_PASS} = 'this-really-isnt-goint-to-work';
+ $_[KERNEL]->yield('spawn');
+ },
+ spawn => sub {
+ ok defined $_[KERNEL]->alias_resolve('pg'), 'Re-open session again';
+ $_[KERNEL]->post(pg => register => connect => 'spawn_connect', error => 'spawn_error');
+ $_[KERNEL]->post(pg => 'connect');
+ $_[KERNEL]->delay('spawn_timeout', 10);
+ },
+ spawn_connect => sub {
+ ok 'Spawn';
+ $_[KERNEL]->delay('spawn_timeout');
+ $_[KERNEL]->post(pg => 'shutdown');
+ },
+ spawn_error => sub { fail 'Spawn (error)' },
+ spawn_timeout => sub { fail 'Spawn (timeout)' },
+});
+
+
+$poe_kernel->run();
+$dbh->disconnect();
+