5 use Data::Dump qw/dump/;
10 ? ( skip_all => 'needs Moose for testing' )
14 use_ok 'DBIx::Class::Storage::DBI::Replicated::Pool';
15 use_ok 'DBIx::Class::Storage::DBI::Replicated::Balancer';
16 use_ok 'DBIx::Class::Storage::DBI::Replicated::Replicant';
17 use_ok 'DBIx::Class::Storage::DBI::Replicated';
19 ## ----------------------------------------------------------------------------
20 ## Build a class to hold all our required testing data and methods.
21 ## ----------------------------------------------------------------------------
25 package DBIx::Class::DBI::Replicated::TestReplication;
29 use Data::Dump qw/dump/;
31 use base qw/Class::Accessor::Fast/;
33 __PACKAGE__->mk_accessors( qw/schema master_path slave_paths/ );
35 ## Initialize the object
39 my $self = $class->SUPER::new(@_);
41 $self->schema( $self->init_schema );
42 $self->master_path("t/var/DBIxClass.db");
47 ## Get the Schema and set the replication storage type
51 my $schema = DBICTest->init_schema(storage_type=>'::DBI::Replicated');
55 ## Return an Array of ArrayRefs where each ArrayRef is suitable to use for
56 ## $storage->connect_info to be used for connecting replicants.
58 sub generate_replicant_connect_info {
62 } @{$self->slave_paths};
64 return map { [$_,'','',{}] } @dsn;
67 ## Do a 'good enough' replication by copying the master dbfile over each of
68 ## the slave dbfiles. If the master is SQLite we do this, otherwise we
69 ## just do a one second pause to let the slaves catch up.
73 foreach my $slave (@{$self->slave_paths}) {
74 copy($self->master_path, $slave);
78 ## Cleanup after ourselves. Unlink all gthe slave paths.
82 foreach my $slave (@{$self->slave_paths}) {
88 ## ----------------------------------------------------------------------------
89 ## Create an object and run some tests
90 ## ----------------------------------------------------------------------------
92 ## Thi first bunch of tests are basic, just make sure all the bits are behaving
94 ok my $replicated = DBIx::Class::DBI::Replicated::TestReplication
97 "t/var/DBIxClass_slave1.db",
98 "t/var/DBIxClass_slave2.db",
100 }) => 'Created a replication object';
102 isa_ok $replicated->schema
103 => 'DBIx::Class::Schema';
105 isa_ok $replicated->schema->storage
106 => 'DBIx::Class::Storage::DBI::Replicated';
108 ok $replicated->schema->storage->meta
109 => 'has a meta object';
111 isa_ok $replicated->schema->storage->master
112 => 'DBIx::Class::Storage::DBI';
114 isa_ok $replicated->schema->storage->pool
115 => 'DBIx::Class::Storage::DBI::Replicated::Pool';
117 isa_ok $replicated->schema->storage->balancer
118 => 'DBIx::Class::Storage::DBI::Replicated::Balancer';
120 ok my @replicant_connects = $replicated->generate_replicant_connect_info
121 => 'got replication connect information';
123 ok my @replicated_storages = $replicated->schema->storage->connect_replicants(@replicant_connects)
124 => 'Created some storages suitable for replicants';
126 isa_ok $replicated->schema->storage->current_replicant
127 => 'DBIx::Class::Storage::DBI';
129 ok $replicated->schema->storage->pool->has_replicants
130 => 'does have replicants';
132 is $replicated->schema->storage->num_replicants => 2
133 => 'has two replicants';
135 isa_ok $replicated_storages[0]
136 => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
138 isa_ok $replicated_storages[1]
139 => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
141 isa_ok $replicated->schema->storage->replicants->{"t/var/DBIxClass_slave1.db"}
142 => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
144 isa_ok $replicated->schema->storage->replicants->{"t/var/DBIxClass_slave2.db"}
145 => 'DBIx::Class::Storage::DBI::Replicated::Replicant';
147 ## Add some info to the database
151 ->populate('Artist', [
152 [ qw/artistid name/ ],
153 [ 4, "Ozric Tentacles"],
156 ## Make sure all the slaves have the table definitions
158 $replicated->replicate;
160 ## Make sure we can read the data.
162 ok my $artist1 = $replicated->schema->resultset('Artist')->find(4)
166 => 'DBICTest::Artist';
168 is $artist1->name, 'Ozric Tentacles'
169 => 'Found expected name for first result';
171 ## Add some new rows that only the master will have This is because
172 ## we overload any type of write operation so that is must hit the master
177 ->populate('Artist', [
178 [ qw/artistid name/ ],
179 [ 5, "Doom's Children"],
180 [ 6, "Dead On Arrival"],
184 ## Alright, the database 'cluster' is not in a consistent state. When we do
185 ## a read now we expect bad news
187 is $replicated->schema->resultset('Artist')->find(5), undef
188 => 'read after disconnect fails because it uses a replicant which we have neglected to "replicate" yet';
190 ## Make sure all the slaves have the table definitions
191 $replicated->replicate;
193 ## Should find some data now
195 ok my $artist2 = $replicated->schema->resultset('Artist')->find(5)
199 => 'DBICTest::Artist';
201 is $artist2->name, "Doom's Children"
202 => 'Found expected name for first result';
204 ## What happens when we disconnect all the replicants?
206 is $replicated->schema->storage->pool->connected_replicants => 2
207 => "both replicants are connected";
209 $replicated->schema->storage->replicants->{"t/var/DBIxClass_slave1.db"}->disconnect;
210 $replicated->schema->storage->replicants->{"t/var/DBIxClass_slave2.db"}->disconnect;
212 is $replicated->schema->storage->pool->connected_replicants => 0
213 => "both replicants are now disconnected";
215 ## All these should pass, since the database should automatically reconnect
217 ok my $artist3 = $replicated->schema->resultset('Artist')->find(6)
218 => 'Still finding stuff.';
221 => 'DBICTest::Artist';
223 is $artist3->name, "Dead On Arrival"
224 => 'Found expected name for first result';
226 is $replicated->schema->storage->pool->connected_replicants => 1
227 => "One replicant reconnected to handle the job";
229 ## Delete the old database files
230 $replicated->cleanup;