1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
9 use Scalar::Util 'weaken';
15 my $db_orig = DBICTest->_sqlite_dbfilename;
16 my $db_tmp = "$db_orig.tmp";
18 # Set up the "usual" sqlite for DBICTest
19 my $schema = DBICTest->init_schema( sqlite_use_file => 1 );
21 my $exception_action_count;
22 $schema->exception_action(sub {
23 $exception_action_count++;
27 # Make sure we're connected by doing something
28 my @art = $schema->resultset("Artist")->search({ }, { order_by => { -desc => 'name' }});
29 cmp_ok(@art, '==', 3, "Three artists returned");
31 # Disconnect the dbh, and be sneaky about it
32 # Also test if DBD::SQLite finaly knows how to ->disconnect properly
35 local $SIG{__WARN__} = sub { $w = shift };
36 $schema->storage->_dbh->disconnect;
37 ok ($w !~ /active statement handles/, 'SQLite can disconnect properly');
40 # Try the operation again - What should happen here is:
41 # 1. S::DBI blindly attempts the SELECT, which throws an exception
42 # 2. It catches the exception, checks ->{Active}/->ping, sees the disconnected state...
43 # 3. Reconnects, and retries the operation
45 my @art_two = $schema->resultset("Artist")->search({ }, { order_by => { -desc => 'name' }});
46 cmp_ok(@art_two, '==', 3, "Three artists returned");
48 ### Now, disconnect the dbh, and move the db file;
49 # create a new one full of garbage, prevent SQLite from connecting.
50 $schema->storage->_dbh->disconnect;
51 move( $db_orig, $db_tmp )
52 or die "failed to move $db_orig to $db_tmp: $!";
53 open my $db_file, '>', $db_orig;
54 print $db_file 'THIS IS NOT A REAL DATABASE';
57 ### Try the operation again... it should fail, since there's no valid db
59 # Catch the DBI connection error
60 local $SIG{__WARN__} = sub {};
62 $schema->resultset("Artist")->create({ name => 'not gonna happen' });
63 } qr/not a database/, 'The operation failed';
66 ok (! $schema->storage->connected, 'We are not connected' );
68 ### Now, move the db file back to the correct name
69 unlink($db_orig) or die "could not delete $db_orig: $!";
70 move( $db_tmp, $db_orig )
71 or die "could not move $db_tmp to $db_orig: $!";
73 ### Try the operation again... this time, it should succeed
76 @art_four = $schema->resultset("Artist")->search( {}, { order_by => { -desc => 'name' } } );
77 } 'The operation succeeded';
78 cmp_ok( @art_four, '==', 3, "Three artists returned" );
80 # check that reconnection contexts are preserved in txn_do / dbh_do
86 invoke => sub { shift->(); 1 },
90 invoke => sub { my $foo = shift->() },
94 invoke => sub { my @foo = shift->() },
99 for my $ctx (keys %$ctx_map) {
101 # start disconnected and then connected
102 $schema->storage->disconnect;
103 $exception_action_count = 0;
108 $ctx_map->{$ctx}{invoke}->(sub { $schema->txn_do(sub {
109 is_deeply (\@_, $args, 'Args propagated correctly' );
111 is (wantarray(), $ctx_map->{$ctx}{wa}, "Correct $ctx context");
113 # this will cause a retry
114 $schema->storage->_dbh->disconnect unless $disarmed++;
116 isa_ok ($schema->resultset('Artist')->next, 'DBICTest::Artist');
120 is( $exception_action_count, 0, 'exception_action never called' );
123 # make sure RT#110429 does not recur on manual DBI-side disconnect
128 my $g = $schema->txn_scope_guard;
130 is( $schema->storage->transaction_depth, 1, "Expected txn depth" );
132 $schema->storage->_dbh->disconnect;
134 $schema->storage->dbh_do(sub { $_[1]->do('SELECT 1') } );
138 $schema->txn_do(sub {
139 $schema->storage->_dbh->disconnect
144 $schema->txn_do(sub {
145 $schema->storage->disconnect;
151 note( "Testing with " . B::Deparse->new->coderef2text($cref) );
153 $schema->storage->disconnect;
154 $exception_action_count = 0;
156 ok( !$schema->storage->connected, 'Not connected' );
158 is( $schema->storage->transaction_depth, undef, "Start with unknown txn depth" );
160 # messages vary depending on version and whether txn or do, whatever
163 } 'Threw *something*';
165 ok( !$schema->storage->connected, 'Not connected as a result of failed rollback' );
167 is( $schema->storage->transaction_depth, undef, "Depth expectedly unknown after failed rollbacks" );
169 is( $exception_action_count, 1, "exception_action called only once" );
172 # check exception_action under tenacious disconnect
174 $schema->storage->disconnect;
175 $exception_action_count = 0;
177 throws_ok { $schema->txn_do(sub {
178 $schema->storage->_dbh->disconnect;
180 $schema->resultset('Artist')->next;
181 })} qr/prepare on inactive database handle/;
183 is( $exception_action_count, 1, "exception_action called only once" );
186 # check that things aren't crazy with a non-violent disconnect
188 my $schema = DBICTest->init_schema( sqlite_use_file => 0, no_deploy => 1 );
189 weaken( my $ws = $schema );
191 $schema->is_executed_sql_bind( sub {
192 $ws->txn_do(sub { $ws->storage->disconnect } );
193 }, [ [ 'BEGIN' ] ], 'Only one BEGIN statement' );
195 $schema->is_executed_sql_bind( sub {
196 my $g = $ws->txn_scope_guard;
197 $ws->storage->disconnect;
198 }, [ [ 'BEGIN' ] ], 'Only one BEGIN statement' );