move load_optional_class into Class::C3::Componentised
[dbsrgits/DBIx-Class.git] / t / 33storage_reconnect.t
CommitLineData
4ffbc1d6 1use strict;
2use warnings;
3
e7827df0 4use FindBin;
5use File::Copy;
4ffbc1d6 6use Test::More;
7use lib qw(t/lib);
8use DBICTest;
9
d02304e1 10# equivalent of $Module::Install::AUTHOR
79fefe05 11my $author = (
12 ( not -d './inc' )
d02304e1 13 or
79fefe05 14 ( -e ($^O eq 'VMS' ? './inc/_author' : './inc/.author') )
15);
d02304e1 16
17plan $author
18 ? (tests => 6)
19 : (skip_all => 'Test temporarily disabled due to a widespread buggy SQLite version')
20;
e7827df0 21
22my $db_orig = "$FindBin::Bin/var/DBIxClass.db";
23my $db_tmp = "$db_orig.tmp";
4ffbc1d6 24
25# Set up the "usual" sqlite for DBICTest
fcf741b1 26my $schema = DBICTest->init_schema( sqlite_use_file => 1 );
4ffbc1d6 27
28# Make sure we're connected by doing something
29my @art = $schema->resultset("Artist")->search({ }, { order_by => 'name DESC'});
30cmp_ok(@art, '==', 3, "Three artists returned");
31
32# Disconnect the dbh, and be sneaky about it
b5bf138f 33# Also test if DBD::SQLite finaly knows how to ->disconnect properly
34TODO: {
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}
4ffbc1d6 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!
47my @art_two = $schema->resultset("Artist")->search({ }, { order_by => 'name DESC'});
48cmp_ok(@art_two, '==', 3, "Three artists returned");
e7827df0 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;
53move( $db_orig, $db_tmp );
54open DBFILE, '>', $db_orig;
55print DBFILE 'THIS IS NOT A REAL DATABASE';
56close DBFILE;
57chmod 0000, $db_orig;
58
59### Try the operation again... it should fail, since there's no db
b5bf138f 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}
e7827df0 68
69### Now, move the db file back to the correct name
70unlink($db_orig);
71move( $db_tmp, $db_orig );
72
413abe68 73SKIP: {
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}