Fix typo.
[dbsrgits/DBIx-Class.git] / t / 93storage_replication.t
1 use strict;
2 use warnings;
3 use lib qw(t/lib);
4
5 use File::Copy;
6
7 use DBICTest;
8
9 use Test::More;
10
11 BEGIN {
12     eval "use DBD::Multi";
13     plan $@
14         ? ( skip_all => 'needs DBD::Multi for testing' )
15         : ( tests => 3 );
16 }
17
18 my $schema = DBICTest->init_schema();
19
20 $schema->storage_type( '::DBI::Replication' );
21
22
23 my $db_file1 = "t/var/DBIxClass.db";
24 my $db_file2 = "t/var/DBIxClass_slave1.db";
25 my $db_file3 = "t/var/DBIxClass_slave2.db";
26 my $dsn1 = $ENV{"DBICTEST_DSN"} || "dbi:SQLite:${db_file1}";
27 my $dsn2 = $ENV{"DBICTEST_DSN2"} || "dbi:SQLite:${db_file2}";
28 my $dsn3 = $ENV{"DBICTEST_DSN3"} || "dbi:SQLite:${db_file3}";
29
30 $schema->connect( [
31                    [ $dsn1, '', '', { AutoCommit => 1 } ],
32                    [ $dsn2, '', '', { priority => 10 } ],
33                    [ $dsn3, '', '', { priority => 10 } ]
34                   ]
35                 );
36
37 $schema->populate('Artist', [
38                              [ qw/artistid name/ ],
39                              [ 4, 'Ozric Tentacles']
40                             ]);
41
42 my $new_artist1 = $schema->resultset('Artist')->find(4);
43
44 isa_ok ($new_artist1, 'DBICTest::Artist');
45
46 # reconnect
47 my $schema2 = $schema->connect( [
48                                  [ $dsn1, '', '', { AutoCommit => 1 } ],
49                                  [ $dsn2, '', '', { priority => 10 } ],
50                                  [ $dsn3, '', '', { priority => 10 } ]
51                                 ]
52                               );
53
54 # try and read (should fail)
55 eval { my $new_artist2 = $schema2->resultset('Artist')->find(4); };
56 ok($@, 'read after disconnect fails because it uses slave 1 which we have neglected to "replicate" yet');
57
58 # try and read (should succede after faked synchronisation)
59 copy($db_file1, $db_file2);
60 $schema2 = $schema->connect( [
61                               [ $dsn1, '', '', { AutoCommit => 1 } ],
62                               [ $dsn2, '', '', { priority => 10 } ],
63                               [ $dsn3, '', '', { priority => 10 } ]
64                              ]
65                            );
66 my $new_artist3 = $schema2->resultset('Artist')->find(4);
67 isa_ok ($new_artist3, 'DBICTest::Artist');
68
69 unlink $db_file2;