move load_optional_class into Class::C3::Componentised
[dbsrgits/DBIx-Class.git] / t / 33storage_reconnect.t
1 use strict;
2 use warnings;  
3
4 use FindBin;
5 use File::Copy;
6 use Test::More;
7 use lib qw(t/lib);
8 use DBICTest;
9
10 # equivalent of $Module::Install::AUTHOR
11 my $author = (
12   ( not  -d './inc' )
13     or
14   ( -e ($^O eq 'VMS' ? './inc/_author' : './inc/.author') )
15 );
16
17 plan $author
18   ? (tests => 6)
19   : (skip_all => 'Test temporarily disabled due to a widespread buggy SQLite version')
20 ;
21
22 my $db_orig = "$FindBin::Bin/var/DBIxClass.db";
23 my $db_tmp  = "$db_orig.tmp";
24
25 # Set up the "usual" sqlite for DBICTest
26 my $schema = DBICTest->init_schema( sqlite_use_file => 1 );
27
28 # Make sure we're connected by doing something
29 my @art = $schema->resultset("Artist")->search({ }, { order_by => 'name DESC'});
30 cmp_ok(@art, '==', 3, "Three artists returned");
31
32 # Disconnect the dbh, and be sneaky about it
33 # Also test if DBD::SQLite finaly knows how to ->disconnect properly
34 TODO: {
35     local $TODO = 'SQLite is evil/braindead. Once this test starts passing, remove the related atrocity from DBIx::Class::Storage::DBI::SQLite';
36     my $w;
37     local $SIG{__WARN__} = sub { $w = shift };
38     $schema->storage->_dbh->disconnect;
39     ok ($w !~ /active statement handles/, 'SQLite can disconnect properly \o/');
40 }
41
42 # Try the operation again - What should happen here is:
43 #   1. S::DBI blindly attempts the SELECT, which throws an exception
44 #   2. It catches the exception, checks ->{Active}/->ping, sees the disconnected state...
45 #   3. Reconnects, and retries the operation
46 #   4. Success!
47 my @art_two = $schema->resultset("Artist")->search({ }, { order_by => 'name DESC'});
48 cmp_ok(@art_two, '==', 3, "Three artists returned");
49
50 ### Now, disconnect the dbh, and move the db file;
51 # create a new one and chmod 000 to prevent SQLite from connecting.
52 $schema->storage->_dbh->disconnect;
53 move( $db_orig, $db_tmp );
54 open DBFILE, '>', $db_orig;
55 print DBFILE 'THIS IS NOT A REAL DATABASE';
56 close DBFILE;
57 chmod 0000, $db_orig;
58
59 ### Try the operation again... it should fail, since there's no db
60 {
61     # Catch the DBI connection error
62     local $SIG{__WARN__} = sub {};
63     eval {
64         my @art_three = $schema->resultset("Artist")->search( {}, { order_by => 'name DESC' } );
65     };
66     ok( $@, 'The operation failed' );
67 }
68
69 ### Now, move the db file back to the correct name
70 unlink($db_orig);
71 move( $db_tmp, $db_orig );
72
73 SKIP: {
74     skip "Cannot reconnect if original connection didn't fail", 2
75         if ( $@ =~ /encrypted or is not a database/ );
76
77     ### Try the operation again... this time, it should succeed
78     my @art_four;
79     eval {
80         @art_four = $schema->resultset("Artist")->search( {}, { order_by => 'name DESC' } );
81     };
82     ok( !$@, 'The operation succeeded' );
83     cmp_ok( @art_four, '==', 3, "Three artists returned" );
84 }