b28734b3c93b4cf245339bb32858af3b6a38b7d5
[dbsrgits/DBIx-Class.git] / t / storage / reconnect.t
1 use strict;
2 use warnings;
3
4 use FindBin;
5 use File::Copy 'move';
6 use Test::More;
7 use Test::Exception;
8 use lib qw(t/lib);
9 use DBICTest;
10
11 my $db_orig = DBICTest->_sqlite_dbfilename;
12 my $db_tmp  = "$db_orig.tmp";
13
14 # Set up the "usual" sqlite for DBICTest
15 my $schema = DBICTest->init_schema( sqlite_use_file => 1 );
16
17 # Make sure we're connected by doing something
18 my @art = $schema->resultset("Artist")->search({ }, { order_by => 'name DESC'});
19 cmp_ok(@art, '==', 3, "Three artists returned");
20
21 # Disconnect the dbh, and be sneaky about it
22 # Also test if DBD::SQLite finaly knows how to ->disconnect properly
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');
28 }
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!
35 my @art_two = $schema->resultset("Artist")->search({ }, { order_by => 'name DESC'});
36 cmp_ok(@art_two, '==', 3, "Three artists returned");
37
38 ### Now, disconnect the dbh, and move the db file;
39 # create a new one and chmod 000 to prevent SQLite from connecting.
40 $schema->storage->_dbh->disconnect;
41 move( $db_orig, $db_tmp )
42   or die "failed to move $db_orig to $db_tmp: $!";
43 open DBFILE, '>', $db_orig;
44 print DBFILE 'THIS IS NOT A REAL DATABASE';
45 close DBFILE;
46 chmod 0000, $db_orig;
47
48 ### Try the operation again... it should fail, since there's no db
49 {
50     # Catch the DBI connection error
51     local $SIG{__WARN__} = sub {};
52     dies_ok {
53         my @art_three = $schema->resultset("Artist")->search( {}, { order_by => 'name DESC' } );
54     } 'The operation failed';
55 }
56
57 # otherwise can't unlink the fake db file
58 $schema->storage->_dbh->disconnect if $^O eq 'MSWin32';
59
60 ### Now, move the db file back to the correct name
61 unlink($db_orig) or die "could not delete $db_orig: $!";
62 move( $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
66 my @art_four;
67 lives_ok {
68     @art_four = $schema->resultset("Artist")->search( {}, { order_by => 'name DESC' } );
69 } 'The operation succeeded';
70 cmp_ok( @art_four, '==', 3, "Three artists returned" );
71
72 # check that reconnection contexts are preserved in txn_do / dbh_do
73
74 my $args = [1, 2, 3];
75
76 my $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
91 for my $ctx (keys %$ctx_map) {
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
111 done_testing;