Avoid ResultSourceProxy calls whenever possible
[dbsrgits/DBIx-Class-Historic.git] / t / storage / reconnect.t
CommitLineData
4ffbc1d6 1use strict;
f54428ab 2use warnings;
4ffbc1d6 3
e7827df0 4use FindBin;
463c56d2 5use File::Copy 'move';
4ffbc1d6 6use Test::More;
463c56d2 7use Test::Exception;
4ffbc1d6 8use lib qw(t/lib);
9use DBICTest;
10
8d6b1478 11my $db_orig = DBICTest->_sqlite_dbfilename;
e7827df0 12my $db_tmp = "$db_orig.tmp";
4ffbc1d6 13
14# Set up the "usual" sqlite for DBICTest
fcf741b1 15my $schema = DBICTest->init_schema( sqlite_use_file => 1 );
4ffbc1d6 16
17# Make sure we're connected by doing something
9fca7eda 18my @art = $schema->resultset("Artist")->search({ }, { order_by => { -desc => 'name' }});
4ffbc1d6 19cmp_ok(@art, '==', 3, "Three artists returned");
20
21# Disconnect the dbh, and be sneaky about it
b5bf138f 22# Also test if DBD::SQLite finaly knows how to ->disconnect properly
f9dc732b 23{
24 my $w;
25 local $SIG{__WARN__} = sub { $w = shift };
26 $schema->storage->_dbh->disconnect;
27 ok ($w !~ /active statement handles/, 'SQLite can disconnect properly');
b5bf138f 28}
4ffbc1d6 29
30# Try the operation again - What should happen here is:
31# 1. S::DBI blindly attempts the SELECT, which throws an exception
32# 2. It catches the exception, checks ->{Active}/->ping, sees the disconnected state...
33# 3. Reconnects, and retries the operation
34# 4. Success!
9fca7eda 35my @art_two = $schema->resultset("Artist")->search({ }, { order_by => { -desc => 'name' }});
4ffbc1d6 36cmp_ok(@art_two, '==', 3, "Three artists returned");
e7827df0 37
38### Now, disconnect the dbh, and move the db file;
8d1fb3e2 39# create a new one full of garbage, prevent SQLite from connecting.
e7827df0 40$schema->storage->_dbh->disconnect;
463c56d2 41move( $db_orig, $db_tmp )
42 or die "failed to move $db_orig to $db_tmp: $!";
8d1fb3e2 43open my $db_file, '>', $db_orig;
44print $db_file 'THIS IS NOT A REAL DATABASE';
45close $db_file;
e7827df0 46
8d1fb3e2 47### Try the operation again... it should fail, since there's no valid db
b5bf138f 48{
8d1fb3e2 49 # Catch the DBI connection error
50 local $SIG{__WARN__} = sub {};
51 throws_ok {
9fca7eda 52 my @art_three = $schema->resultset("Artist")->search( {}, { order_by => { -desc => 'name' } } );
8d1fb3e2 53 } qr/not a database/, 'The operation failed';
b5bf138f 54}
e7827df0 55
8d1fb3e2 56ok (! $schema->storage->connected, 'We are not connected' );
463c56d2 57
e7827df0 58### Now, move the db file back to the correct name
463c56d2 59unlink($db_orig) or die "could not delete $db_orig: $!";
60move( $db_tmp, $db_orig )
61 or die "could not move $db_tmp to $db_orig: $!";
62
63### Try the operation again... this time, it should succeed
64my @art_four;
65lives_ok {
9fca7eda 66 @art_four = $schema->resultset("Artist")->search( {}, { order_by => { -desc => 'name' } } );
463c56d2 67} 'The operation succeeded';
68cmp_ok( @art_four, '==', 3, "Three artists returned" );
9345b14c 69
70# check that reconnection contexts are preserved in txn_do / dbh_do
71
72my $args = [1, 2, 3];
73
74my $ctx_map = {
75 VOID => {
76 invoke => sub { shift->(); 1 },
77 wa => undef,
78 },
79 SCALAR => {
80 invoke => sub { my $foo = shift->() },
81 wa => '',
82 },
83 LIST => {
84 invoke => sub { my @foo = shift->() },
85 wa => 1,
86 },
87};
88
65d35121 89for my $ctx (keys %$ctx_map) {
9345b14c 90
91 # start disconnected and then connected
92 $schema->storage->disconnect;
93 for (1, 2) {
94 my $disarmed;
95
96 $ctx_map->{$ctx}{invoke}->(sub { $schema->txn_do(sub {
97 is_deeply (\@_, $args, 'Args propagated correctly' );
98
99 is (wantarray(), $ctx_map->{$ctx}{wa}, "Correct $ctx context");
100
101 # this will cause a retry
102 $schema->storage->_dbh->disconnect unless $disarmed++;
103
104 isa_ok ($schema->resultset('Artist')->next, 'DBICTest::Artist');
105 }, @$args) });
106 }
107};
108
109done_testing;