Introduce GOVERNANCE document and empty RESOLUTIONS file.
[dbsrgits/DBIx-Class.git] / t / storage / reconnect.t
CommitLineData
c0329273 1BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
4ffbc1d6 3use strict;
f54428ab 4use warnings;
4ffbc1d6 5
729656c5 6use B::Deparse;
729656c5 7use Scalar::Util 'weaken';
4ffbc1d6 8use Test::More;
463c56d2 9use Test::Exception;
c0329273 10
4ffbc1d6 11use DBICTest;
12
8d6b1478 13my $db_orig = DBICTest->_sqlite_dbfilename;
e7827df0 14my $db_tmp = "$db_orig.tmp";
4ffbc1d6 15
16# Set up the "usual" sqlite for DBICTest
fcf741b1 17my $schema = DBICTest->init_schema( sqlite_use_file => 1 );
4ffbc1d6 18
7704dbc9 19my $exception_callback_count;
20my $ea = $schema->exception_action(sub {
21 $exception_callback_count++;
ddcc02d1 22 die @_;
23});
24
7704dbc9 25
26# No, this is not a great idea.
27# Yes, people do it anyway.
28# Might as well test that we have fixed it for good, by never invoking
29# a potential __DIE__ handler in internal_try() stacks. In cases of regular
30# exceptions we expect *both* the exception action *AND* the __DIE__ to
31# fire once
32$SIG{__DIE__} = sub { &$ea };
33
34
4ffbc1d6 35# Make sure we're connected by doing something
9fca7eda 36my @art = $schema->resultset("Artist")->search({ }, { order_by => { -desc => 'name' }});
4ffbc1d6 37cmp_ok(@art, '==', 3, "Three artists returned");
38
39# Disconnect the dbh, and be sneaky about it
b5bf138f 40# Also test if DBD::SQLite finaly knows how to ->disconnect properly
f9dc732b 41{
42 my $w;
43 local $SIG{__WARN__} = sub { $w = shift };
44 $schema->storage->_dbh->disconnect;
45 ok ($w !~ /active statement handles/, 'SQLite can disconnect properly');
b5bf138f 46}
4ffbc1d6 47
48# Try the operation again - What should happen here is:
49# 1. S::DBI blindly attempts the SELECT, which throws an exception
50# 2. It catches the exception, checks ->{Active}/->ping, sees the disconnected state...
51# 3. Reconnects, and retries the operation
52# 4. Success!
9fca7eda 53my @art_two = $schema->resultset("Artist")->search({ }, { order_by => { -desc => 'name' }});
4ffbc1d6 54cmp_ok(@art_two, '==', 3, "Three artists returned");
e7827df0 55
56### Now, disconnect the dbh, and move the db file;
8d1fb3e2 57# create a new one full of garbage, prevent SQLite from connecting.
e7827df0 58$schema->storage->_dbh->disconnect;
aff5e9c1 59rename( $db_orig, $db_tmp )
463c56d2 60 or die "failed to move $db_orig to $db_tmp: $!";
8d1fb3e2 61open my $db_file, '>', $db_orig;
62print $db_file 'THIS IS NOT A REAL DATABASE';
63close $db_file;
e7827df0 64
8d1fb3e2 65### Try the operation again... it should fail, since there's no valid db
b5bf138f 66{
8d1fb3e2 67 # Catch the DBI connection error
68 local $SIG{__WARN__} = sub {};
69 throws_ok {
b74b15b0 70 $schema->resultset("Artist")->create({ name => 'not gonna happen' });
8d1fb3e2 71 } qr/not a database/, 'The operation failed';
b5bf138f 72}
e7827df0 73
8d1fb3e2 74ok (! $schema->storage->connected, 'We are not connected' );
463c56d2 75
e7827df0 76### Now, move the db file back to the correct name
463c56d2 77unlink($db_orig) or die "could not delete $db_orig: $!";
aff5e9c1 78rename( $db_tmp, $db_orig )
463c56d2 79 or die "could not move $db_tmp to $db_orig: $!";
80
81### Try the operation again... this time, it should succeed
82my @art_four;
83lives_ok {
9fca7eda 84 @art_four = $schema->resultset("Artist")->search( {}, { order_by => { -desc => 'name' } } );
463c56d2 85} 'The operation succeeded';
86cmp_ok( @art_four, '==', 3, "Three artists returned" );
9345b14c 87
88# check that reconnection contexts are preserved in txn_do / dbh_do
89
90my $args = [1, 2, 3];
91
92my $ctx_map = {
93 VOID => {
94 invoke => sub { shift->(); 1 },
95 wa => undef,
96 },
97 SCALAR => {
98 invoke => sub { my $foo = shift->() },
99 wa => '',
100 },
101 LIST => {
102 invoke => sub { my @foo = shift->() },
103 wa => 1,
104 },
105};
106
65d35121 107for my $ctx (keys %$ctx_map) {
9345b14c 108
109 # start disconnected and then connected
110 $schema->storage->disconnect;
7704dbc9 111 $exception_callback_count = 0;
ddcc02d1 112
9345b14c 113 for (1, 2) {
114 my $disarmed;
115
116 $ctx_map->{$ctx}{invoke}->(sub { $schema->txn_do(sub {
117 is_deeply (\@_, $args, 'Args propagated correctly' );
118
119 is (wantarray(), $ctx_map->{$ctx}{wa}, "Correct $ctx context");
120
121 # this will cause a retry
122 $schema->storage->_dbh->disconnect unless $disarmed++;
123
124 isa_ok ($schema->resultset('Artist')->next, 'DBICTest::Artist');
125 }, @$args) });
126 }
ddcc02d1 127
7704dbc9 128 is( $exception_callback_count, 0, 'neither exception_action nor $SIG{__DIE__} ever called' );
9345b14c 129};
130
729656c5 131# make sure RT#110429 does not recur on manual DBI-side disconnect
132for my $cref (
133 sub {
134 my $schema = shift;
135
136 my $g = $schema->txn_scope_guard;
137
138 is( $schema->storage->transaction_depth, 1, "Expected txn depth" );
139
140 $schema->storage->_dbh->disconnect;
141
142 $schema->storage->dbh_do(sub { $_[1]->do('SELECT 1') } );
143 },
144 sub {
145 my $schema = shift;
146 $schema->txn_do(sub {
147 $schema->storage->_dbh->disconnect
148 } );
149 },
150 sub {
151 my $schema = shift;
152 $schema->txn_do(sub {
153 $schema->storage->disconnect;
154 die "VIOLENCE";
155 } );
156 },
157) {
158
159 note( "Testing with " . B::Deparse->new->coderef2text($cref) );
160
161 $schema->storage->disconnect;
7704dbc9 162 $exception_callback_count = 0;
729656c5 163
164 ok( !$schema->storage->connected, 'Not connected' );
165
166 is( $schema->storage->transaction_depth, undef, "Start with unknown txn depth" );
167
168 # messages vary depending on version and whether txn or do, whatever
169 dies_ok {
170 $cref->($schema)
171 } 'Threw *something*';
172
173 ok( !$schema->storage->connected, 'Not connected as a result of failed rollback' );
174
175 is( $schema->storage->transaction_depth, undef, "Depth expectedly unknown after failed rollbacks" );
ddcc02d1 176
7704dbc9 177 is( $exception_callback_count, 2, 'exception_action and $SIG{__DIE__} called only once each' );
ddcc02d1 178}
179
180# check exception_action under tenacious disconnect
181{
182 $schema->storage->disconnect;
7704dbc9 183 $exception_callback_count = 0;
ddcc02d1 184
185 throws_ok { $schema->txn_do(sub {
186 $schema->storage->_dbh->disconnect;
187
188 $schema->resultset('Artist')->next;
189 })} qr/prepare on inactive database handle/;
190
7704dbc9 191 is( $exception_callback_count, 2, 'exception_action and $SIG{__DIE__} called only once each' );
729656c5 192}
193
194# check that things aren't crazy with a non-violent disconnect
195{
196 my $schema = DBICTest->init_schema( sqlite_use_file => 0, no_deploy => 1 );
197 weaken( my $ws = $schema );
198
199 $schema->is_executed_sql_bind( sub {
200 $ws->txn_do(sub { $ws->storage->disconnect } );
201 }, [ [ 'BEGIN' ] ], 'Only one BEGIN statement' );
202
203 $schema->is_executed_sql_bind( sub {
204 my $g = $ws->txn_scope_guard;
205 $ws->storage->disconnect;
206 }, [ [ 'BEGIN' ] ], 'Only one BEGIN statement' );
207}
208
9345b14c 209done_testing;