Introduce GOVERNANCE document and empty RESOLUTIONS file.
[dbsrgits/DBIx-Class.git] / t / storage / reconnect.t
1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
2
3 use strict;
4 use warnings;
5
6 use B::Deparse;
7 use Scalar::Util 'weaken';
8 use Test::More;
9 use Test::Exception;
10
11 use DBICTest;
12
13 my $db_orig = DBICTest->_sqlite_dbfilename;
14 my $db_tmp  = "$db_orig.tmp";
15
16 # Set up the "usual" sqlite for DBICTest
17 my $schema = DBICTest->init_schema( sqlite_use_file => 1 );
18
19 my $exception_callback_count;
20 my $ea = $schema->exception_action(sub {
21   $exception_callback_count++;
22   die @_;
23 });
24
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
35 # Make sure we're connected by doing something
36 my @art = $schema->resultset("Artist")->search({ }, { order_by => { -desc => 'name' }});
37 cmp_ok(@art, '==', 3, "Three artists returned");
38
39 # Disconnect the dbh, and be sneaky about it
40 # Also test if DBD::SQLite finaly knows how to ->disconnect properly
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');
46 }
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!
53 my @art_two = $schema->resultset("Artist")->search({ }, { order_by => { -desc => 'name' }});
54 cmp_ok(@art_two, '==', 3, "Three artists returned");
55
56 ### Now, disconnect the dbh, and move the db file;
57 # create a new one full of garbage, prevent SQLite from connecting.
58 $schema->storage->_dbh->disconnect;
59 rename( $db_orig, $db_tmp )
60   or die "failed to move $db_orig to $db_tmp: $!";
61 open my $db_file, '>', $db_orig;
62 print $db_file 'THIS IS NOT A REAL DATABASE';
63 close $db_file;
64
65 ### Try the operation again... it should fail, since there's no valid db
66 {
67   # Catch the DBI connection error
68   local $SIG{__WARN__} = sub {};
69   throws_ok {
70     $schema->resultset("Artist")->create({ name => 'not gonna happen' });
71   }  qr/not a database/, 'The operation failed';
72 }
73
74 ok (! $schema->storage->connected, 'We are not connected' );
75
76 ### Now, move the db file back to the correct name
77 unlink($db_orig) or die "could not delete $db_orig: $!";
78 rename( $db_tmp, $db_orig )
79   or die "could not move $db_tmp to $db_orig: $!";
80
81 ### Try the operation again... this time, it should succeed
82 my @art_four;
83 lives_ok {
84     @art_four = $schema->resultset("Artist")->search( {}, { order_by => { -desc => 'name' } } );
85 } 'The operation succeeded';
86 cmp_ok( @art_four, '==', 3, "Three artists returned" );
87
88 # check that reconnection contexts are preserved in txn_do / dbh_do
89
90 my $args = [1, 2, 3];
91
92 my $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
107 for my $ctx (keys %$ctx_map) {
108
109   # start disconnected and then connected
110   $schema->storage->disconnect;
111   $exception_callback_count = 0;
112
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   }
127
128   is( $exception_callback_count, 0, 'neither exception_action nor $SIG{__DIE__} ever called' );
129 };
130
131 # make sure RT#110429 does not recur on manual DBI-side disconnect
132 for 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;
162   $exception_callback_count = 0;
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" );
176
177   is( $exception_callback_count, 2, 'exception_action and $SIG{__DIE__} called only once each' );
178 }
179
180 # check exception_action under tenacious disconnect
181 {
182   $schema->storage->disconnect;
183   $exception_callback_count = 0;
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
191   is( $exception_callback_count, 2, 'exception_action and $SIG{__DIE__} called only once each' );
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
209 done_testing;