Commit | Line | Data |
4ffbc1d6 |
1 | use strict; |
f54428ab |
2 | use warnings; |
4ffbc1d6 |
3 | |
e7827df0 |
4 | use FindBin; |
463c56d2 |
5 | use File::Copy 'move'; |
4ffbc1d6 |
6 | use Test::More; |
463c56d2 |
7 | use Test::Exception; |
4ffbc1d6 |
8 | use lib qw(t/lib); |
9 | use DBICTest; |
10 | |
8d6b1478 |
11 | my $db_orig = DBICTest->_sqlite_dbfilename; |
e7827df0 |
12 | my $db_tmp = "$db_orig.tmp"; |
4ffbc1d6 |
13 | |
14 | # Set up the "usual" sqlite for DBICTest |
fcf741b1 |
15 | my $schema = DBICTest->init_schema( sqlite_use_file => 1 ); |
4ffbc1d6 |
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 |
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! |
35 | my @art_two = $schema->resultset("Artist")->search({ }, { order_by => 'name DESC'}); |
36 | cmp_ok(@art_two, '==', 3, "Three artists returned"); |
e7827df0 |
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; |
463c56d2 |
41 | move( $db_orig, $db_tmp ) |
42 | or die "failed to move $db_orig to $db_tmp: $!"; |
e7827df0 |
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 {}; |
463c56d2 |
52 | dies_ok { |
b5bf138f |
53 | my @art_three = $schema->resultset("Artist")->search( {}, { order_by => 'name DESC' } ); |
463c56d2 |
54 | } 'The operation failed'; |
b5bf138f |
55 | } |
e7827df0 |
56 | |
64819b0e |
57 | # otherwise win32 can't unlink the fake db file |
58 | # as root sqlite will have the file open and won't reconnect |
59 | $schema->storage->_dbh->disconnect if $^O eq 'MSWin32' or $> == 0; |
463c56d2 |
60 | |
e7827df0 |
61 | ### Now, move the db file back to the correct name |
463c56d2 |
62 | unlink($db_orig) or die "could not delete $db_orig: $!"; |
63 | move( $db_tmp, $db_orig ) |
64 | or die "could not move $db_tmp to $db_orig: $!"; |
65 | |
66 | ### Try the operation again... this time, it should succeed |
67 | my @art_four; |
68 | lives_ok { |
69 | @art_four = $schema->resultset("Artist")->search( {}, { order_by => 'name DESC' } ); |
70 | } 'The operation succeeded'; |
71 | cmp_ok( @art_four, '==', 3, "Three artists returned" ); |
9345b14c |
72 | |
73 | # check that reconnection contexts are preserved in txn_do / dbh_do |
74 | |
75 | my $args = [1, 2, 3]; |
76 | |
77 | my $ctx_map = { |
78 | VOID => { |
79 | invoke => sub { shift->(); 1 }, |
80 | wa => undef, |
81 | }, |
82 | SCALAR => { |
83 | invoke => sub { my $foo = shift->() }, |
84 | wa => '', |
85 | }, |
86 | LIST => { |
87 | invoke => sub { my @foo = shift->() }, |
88 | wa => 1, |
89 | }, |
90 | }; |
91 | |
65d35121 |
92 | for my $ctx (keys %$ctx_map) { |
9345b14c |
93 | |
94 | # start disconnected and then connected |
95 | $schema->storage->disconnect; |
96 | for (1, 2) { |
97 | my $disarmed; |
98 | |
99 | $ctx_map->{$ctx}{invoke}->(sub { $schema->txn_do(sub { |
100 | is_deeply (\@_, $args, 'Args propagated correctly' ); |
101 | |
102 | is (wantarray(), $ctx_map->{$ctx}{wa}, "Correct $ctx context"); |
103 | |
104 | # this will cause a retry |
105 | $schema->storage->_dbh->disconnect unless $disarmed++; |
106 | |
107 | isa_ok ($schema->resultset('Artist')->next, 'DBICTest::Artist'); |
108 | }, @$args) }); |
109 | } |
110 | }; |
111 | |
112 | done_testing; |