Ensure the $storage state reflects the current connection state closely
[dbsrgits/DBIx-Class.git] / t / storage / reconnect.t
CommitLineData
4ffbc1d6 1use strict;
f54428ab 2use warnings;
4ffbc1d6 3
e7827df0 4use FindBin;
729656c5 5use B::Deparse;
463c56d2 6use File::Copy 'move';
729656c5 7use Scalar::Util 'weaken';
4ffbc1d6 8use Test::More;
463c56d2 9use Test::Exception;
4ffbc1d6 10use lib qw(t/lib);
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
19# Make sure we're connected by doing something
9fca7eda 20my @art = $schema->resultset("Artist")->search({ }, { order_by => { -desc => 'name' }});
4ffbc1d6 21cmp_ok(@art, '==', 3, "Three artists returned");
22
23# Disconnect the dbh, and be sneaky about it
b5bf138f 24# Also test if DBD::SQLite finaly knows how to ->disconnect properly
f9dc732b 25{
26 my $w;
27 local $SIG{__WARN__} = sub { $w = shift };
28 $schema->storage->_dbh->disconnect;
29 ok ($w !~ /active statement handles/, 'SQLite can disconnect properly');
b5bf138f 30}
4ffbc1d6 31
32# Try the operation again - What should happen here is:
33# 1. S::DBI blindly attempts the SELECT, which throws an exception
34# 2. It catches the exception, checks ->{Active}/->ping, sees the disconnected state...
35# 3. Reconnects, and retries the operation
36# 4. Success!
9fca7eda 37my @art_two = $schema->resultset("Artist")->search({ }, { order_by => { -desc => 'name' }});
4ffbc1d6 38cmp_ok(@art_two, '==', 3, "Three artists returned");
e7827df0 39
40### Now, disconnect the dbh, and move the db file;
8d1fb3e2 41# create a new one full of garbage, prevent SQLite from connecting.
e7827df0 42$schema->storage->_dbh->disconnect;
463c56d2 43move( $db_orig, $db_tmp )
44 or die "failed to move $db_orig to $db_tmp: $!";
8d1fb3e2 45open my $db_file, '>', $db_orig;
46print $db_file 'THIS IS NOT A REAL DATABASE';
47close $db_file;
e7827df0 48
8d1fb3e2 49### Try the operation again... it should fail, since there's no valid db
b5bf138f 50{
8d1fb3e2 51 # Catch the DBI connection error
52 local $SIG{__WARN__} = sub {};
53 throws_ok {
b74b15b0 54 $schema->resultset("Artist")->create({ name => 'not gonna happen' });
8d1fb3e2 55 } qr/not a database/, 'The operation failed';
b5bf138f 56}
e7827df0 57
8d1fb3e2 58ok (! $schema->storage->connected, 'We are not connected' );
463c56d2 59
e7827df0 60### Now, move the db file back to the correct name
463c56d2 61unlink($db_orig) or die "could not delete $db_orig: $!";
62move( $db_tmp, $db_orig )
63 or die "could not move $db_tmp to $db_orig: $!";
64
65### Try the operation again... this time, it should succeed
66my @art_four;
67lives_ok {
9fca7eda 68 @art_four = $schema->resultset("Artist")->search( {}, { order_by => { -desc => 'name' } } );
463c56d2 69} 'The operation succeeded';
70cmp_ok( @art_four, '==', 3, "Three artists returned" );
9345b14c 71
72# check that reconnection contexts are preserved in txn_do / dbh_do
73
74my $args = [1, 2, 3];
75
76my $ctx_map = {
77 VOID => {
78 invoke => sub { shift->(); 1 },
79 wa => undef,
80 },
81 SCALAR => {
82 invoke => sub { my $foo = shift->() },
83 wa => '',
84 },
85 LIST => {
86 invoke => sub { my @foo = shift->() },
87 wa => 1,
88 },
89};
90
65d35121 91for my $ctx (keys %$ctx_map) {
9345b14c 92
93 # start disconnected and then connected
94 $schema->storage->disconnect;
95 for (1, 2) {
96 my $disarmed;
97
98 $ctx_map->{$ctx}{invoke}->(sub { $schema->txn_do(sub {
99 is_deeply (\@_, $args, 'Args propagated correctly' );
100
101 is (wantarray(), $ctx_map->{$ctx}{wa}, "Correct $ctx context");
102
103 # this will cause a retry
104 $schema->storage->_dbh->disconnect unless $disarmed++;
105
106 isa_ok ($schema->resultset('Artist')->next, 'DBICTest::Artist');
107 }, @$args) });
108 }
109};
110
729656c5 111# make sure RT#110429 does not recur on manual DBI-side disconnect
112for my $cref (
113 sub {
114 my $schema = shift;
115
116 my $g = $schema->txn_scope_guard;
117
118 is( $schema->storage->transaction_depth, 1, "Expected txn depth" );
119
120 $schema->storage->_dbh->disconnect;
121
122 $schema->storage->dbh_do(sub { $_[1]->do('SELECT 1') } );
123 },
124 sub {
125 my $schema = shift;
126 $schema->txn_do(sub {
127 $schema->storage->_dbh->disconnect
128 } );
129 },
130 sub {
131 my $schema = shift;
132 $schema->txn_do(sub {
133 $schema->storage->disconnect;
134 die "VIOLENCE";
135 } );
136 },
137) {
138
139 note( "Testing with " . B::Deparse->new->coderef2text($cref) );
140
141 $schema->storage->disconnect;
142
143 ok( !$schema->storage->connected, 'Not connected' );
144
145 is( $schema->storage->transaction_depth, undef, "Start with unknown txn depth" );
146
147 # messages vary depending on version and whether txn or do, whatever
148 dies_ok {
149 $cref->($schema)
150 } 'Threw *something*';
151
152 ok( !$schema->storage->connected, 'Not connected as a result of failed rollback' );
153
154 is( $schema->storage->transaction_depth, undef, "Depth expectedly unknown after failed rollbacks" );
155}
156
157# check that things aren't crazy with a non-violent disconnect
158{
159 my $schema = DBICTest->init_schema( sqlite_use_file => 0, no_deploy => 1 );
160 weaken( my $ws = $schema );
161
162 $schema->is_executed_sql_bind( sub {
163 $ws->txn_do(sub { $ws->storage->disconnect } );
164 }, [ [ 'BEGIN' ] ], 'Only one BEGIN statement' );
165
166 $schema->is_executed_sql_bind( sub {
167 my $g = $ws->txn_scope_guard;
168 $ws->storage->disconnect;
169 }, [ [ 'BEGIN' ] ], 'Only one BEGIN statement' );
170}
171
9345b14c 172done_testing;