File: purge_failures.t

package info (click to toggle)
libdbd-multi-perl 1.02-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 196 kB
  • sloc: perl: 342; makefile: 2
file content (66 lines) | stat: -rw-r--r-- 1,716 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# vim: ft=perl
use Test::More 'no_plan';
use strict;
$^W = 1;

# Test that failing dbs are eventually re-tried.

use DBI;
use DBD::SQLite;
use DBD::Multi;

my %SOURCE = ( 1 => "dbi:SQLite:one.db",
               2 => "dbi:SQLite:two.db" );
my $WORKING = 0;

init();
my $c = db_connect();
my @count = (undef,0,0);

for (1..100) {
    my $val = $c->selectrow_array("SELECT id FROM multi");
    $count[$val]++;
}

is($count[1], 0, "first db returned no values because it had crashed." );
is($count[2], 100, "second db used for every query." );

@count = (undef,0,0);

$WORKING = 1;
sleep( 3 );

for (1..100) {
    my $val = $c->selectrow_array("SELECT id FROM multi");
    $count[$val]++;
}

is($count[1], 100, "first db used every time due to higher priority." );
is($count[2], 0, "second db unused because first db was available." );

unlink "$_.db" for qw[one two];

sub init {
    unlink "$_.db" for qw[one two];
    # Set up the first DB with a value of 1
     my $dbh_1 = DBI->connect($SOURCE{1});
     is $dbh_1->do("CREATE TABLE multi(id int)"), '0E0', 'do create successful';
     is($dbh_1->do("INSERT INTO multi VALUES(1)"), 1, 'insert via do works');

    # And the second DB with the value of 2
     $dbh_1 = DBI->connect($SOURCE{2});
     is $dbh_1->do("CREATE TABLE multi(id int)"), '0E0', 'do create successful';
     is($dbh_1->do("INSERT INTO multi VALUES(2)"), 1, 'insert via do works');
}

sub db_connect {
    my $first = sub { $WORKING ? DBI->connect($SOURCE{1}) : undef };
    my $second = sub { DBI->connect($SOURCE{2}) };
    return DBI->connect('DBI:Multi:', undef, undef, {
        dsns => [
            1 => $first,
            2 => $second,
        ],
        failed_expire => 2,
    });
}