1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
7 use Scalar::Util 'weaken';
13 my $db_orig = DBICTest->_sqlite_dbfilename;
14 my $db_tmp = "$db_orig.tmp";
16 # Set up the "usual" sqlite for DBICTest
17 my $schema = DBICTest->init_schema( sqlite_use_file => 1 );
19 my $exception_callback_count;
20 my $ea = $schema->exception_action(sub {
21 $exception_callback_count++;
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
32 $SIG{__DIE__} = sub { &$ea };
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");
39 # Disconnect the dbh, and be sneaky about it
40 # Also test if DBD::SQLite finaly knows how to ->disconnect properly
43 local $SIG{__WARN__} = sub { $w = shift };
44 $schema->storage->_dbh->disconnect;
45 ok ($w !~ /active statement handles/, 'SQLite can disconnect properly');
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
53 my @art_two = $schema->resultset("Artist")->search({ }, { order_by => { -desc => 'name' }});
54 cmp_ok(@art_two, '==', 3, "Three artists returned");
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';
65 ### Try the operation again... it should fail, since there's no valid db
67 # Catch the DBI connection error
68 local $SIG{__WARN__} = sub {};
70 $schema->resultset("Artist")->create({ name => 'not gonna happen' });
71 } qr/not a database/, 'The operation failed';
74 ok (! $schema->storage->connected, 'We are not connected' );
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: $!";
81 ### Try the operation again... this time, it should succeed
84 @art_four = $schema->resultset("Artist")->search( {}, { order_by => { -desc => 'name' } } );
85 } 'The operation succeeded';
86 cmp_ok( @art_four, '==', 3, "Three artists returned" );
88 # check that reconnection contexts are preserved in txn_do / dbh_do
94 invoke => sub { shift->(); 1 },
98 invoke => sub { my $foo = shift->() },
102 invoke => sub { my @foo = shift->() },
107 for my $ctx (keys %$ctx_map) {
109 # start disconnected and then connected
110 $schema->storage->disconnect;
111 $exception_callback_count = 0;
116 $ctx_map->{$ctx}{invoke}->(sub { $schema->txn_do(sub {
117 is_deeply (\@_, $args, 'Args propagated correctly' );
119 is (wantarray(), $ctx_map->{$ctx}{wa}, "Correct $ctx context");
121 # this will cause a retry
122 $schema->storage->_dbh->disconnect unless $disarmed++;
124 isa_ok ($schema->resultset('Artist')->next, 'DBICTest::Artist');
128 is( $exception_callback_count, 0, 'neither exception_action nor $SIG{__DIE__} ever called' );
131 # make sure RT#110429 does not recur on manual DBI-side disconnect
136 my $g = $schema->txn_scope_guard;
138 is( $schema->storage->transaction_depth, 1, "Expected txn depth" );
140 $schema->storage->_dbh->disconnect;
142 $schema->storage->dbh_do(sub { $_[1]->do('SELECT 1') } );
146 $schema->txn_do(sub {
147 $schema->storage->_dbh->disconnect
152 $schema->txn_do(sub {
153 $schema->storage->disconnect;
159 note( "Testing with " . B::Deparse->new->coderef2text($cref) );
161 $schema->storage->disconnect;
162 $exception_callback_count = 0;
164 ok( !$schema->storage->connected, 'Not connected' );
166 is( $schema->storage->transaction_depth, undef, "Start with unknown txn depth" );
168 # messages vary depending on version and whether txn or do, whatever
171 } 'Threw *something*';
173 ok( !$schema->storage->connected, 'Not connected as a result of failed rollback' );
175 is( $schema->storage->transaction_depth, undef, "Depth expectedly unknown after failed rollbacks" );
177 is( $exception_callback_count, 2, 'exception_action and $SIG{__DIE__} called only once each' );
180 # check exception_action under tenacious disconnect
182 $schema->storage->disconnect;
183 $exception_callback_count = 0;
185 throws_ok { $schema->txn_do(sub {
186 $schema->storage->_dbh->disconnect;
188 $schema->resultset('Artist')->next;
189 })} qr/prepare on inactive database handle/;
191 is( $exception_callback_count, 2, 'exception_action and $SIG{__DIE__} called only once each' );
194 # check that things aren't crazy with a non-violent disconnect
196 my $schema = DBICTest->init_schema( sqlite_use_file => 0, no_deploy => 1 );
197 weaken( my $ws = $schema );
199 $schema->is_executed_sql_bind( sub {
200 $ws->txn_do(sub { $ws->storage->disconnect } );
201 }, [ [ 'BEGIN' ] ], 'Only one BEGIN statement' );
203 $schema->is_executed_sql_bind( sub {
204 my $g = $ws->txn_scope_guard;
205 $ws->storage->disconnect;
206 }, [ [ 'BEGIN' ] ], 'Only one BEGIN statement' );