Commit | Line | Data |
4ffbc1d6 |
1 | use strict; |
f54428ab |
2 | use warnings; |
4ffbc1d6 |
3 | |
e7827df0 |
4 | use FindBin; |
5 | use File::Copy; |
4ffbc1d6 |
6 | use Test::More; |
7 | use lib qw(t/lib); |
8 | use DBICTest; |
9 | |
f9dc732b |
10 | plan tests => 6; |
e7827df0 |
11 | |
41271080 |
12 | my $db_orig = "$FindBin::Bin/../var/DBIxClass.db"; |
e7827df0 |
13 | my $db_tmp = "$db_orig.tmp"; |
4ffbc1d6 |
14 | |
15 | # Set up the "usual" sqlite for DBICTest |
fcf741b1 |
16 | my $schema = DBICTest->init_schema( sqlite_use_file => 1 ); |
4ffbc1d6 |
17 | |
18 | # Make sure we're connected by doing something |
19 | my @art = $schema->resultset("Artist")->search({ }, { order_by => 'name DESC'}); |
20 | cmp_ok(@art, '==', 3, "Three artists returned"); |
21 | |
22 | # Disconnect the dbh, and be sneaky about it |
b5bf138f |
23 | # Also test if DBD::SQLite finaly knows how to ->disconnect properly |
f9dc732b |
24 | { |
25 | my $w; |
26 | local $SIG{__WARN__} = sub { $w = shift }; |
27 | $schema->storage->_dbh->disconnect; |
28 | ok ($w !~ /active statement handles/, 'SQLite can disconnect properly'); |
b5bf138f |
29 | } |
4ffbc1d6 |
30 | |
31 | # Try the operation again - What should happen here is: |
32 | # 1. S::DBI blindly attempts the SELECT, which throws an exception |
33 | # 2. It catches the exception, checks ->{Active}/->ping, sees the disconnected state... |
34 | # 3. Reconnects, and retries the operation |
35 | # 4. Success! |
36 | my @art_two = $schema->resultset("Artist")->search({ }, { order_by => 'name DESC'}); |
37 | cmp_ok(@art_two, '==', 3, "Three artists returned"); |
e7827df0 |
38 | |
39 | ### Now, disconnect the dbh, and move the db file; |
40 | # create a new one and chmod 000 to prevent SQLite from connecting. |
41 | $schema->storage->_dbh->disconnect; |
42 | move( $db_orig, $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 |
b5bf138f |
49 | { |
50 | # Catch the DBI connection error |
51 | local $SIG{__WARN__} = sub {}; |
52 | eval { |
53 | my @art_three = $schema->resultset("Artist")->search( {}, { order_by => 'name DESC' } ); |
54 | }; |
55 | ok( $@, 'The operation failed' ); |
56 | } |
e7827df0 |
57 | |
58 | ### Now, move the db file back to the correct name |
59 | unlink($db_orig); |
60 | move( $db_tmp, $db_orig ); |
61 | |
413abe68 |
62 | SKIP: { |
63 | skip "Cannot reconnect if original connection didn't fail", 2 |
64 | if ( $@ =~ /encrypted or is not a database/ ); |
65 | |
66 | ### Try the operation again... this time, it should succeed |
67 | my @art_four; |
68 | eval { |
69 | @art_four = $schema->resultset("Artist")->search( {}, { order_by => 'name DESC' } ); |
70 | }; |
71 | ok( !$@, 'The operation succeeded' ); |
72 | cmp_ok( @art_four, '==', 3, "Three artists returned" ); |
73 | } |