summaryrefslogtreecommitdiff
path: root/t/02connect.t
blob: 999e946413088bd9b736b4b217b769ebf5ca40b3 (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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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 {
    pass '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 {
    pass '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 {
    pass '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();