Commit | Line | Data |
e4dc89b3 |
1 | use strict; |
2 | use warnings; |
68de9438 |
3 | |
e4dc89b3 |
4 | use Test::More; |
68de9438 |
5 | |
4bea1fe7 |
6 | use lib qw(t/lib); |
7 | use DBICTest; |
8 | |
68de9438 |
9 | BEGIN { |
10 | require DBIx::Class; |
11 | plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('test_replicated') |
12 | unless DBIx::Class::Optional::Dependencies->req_ok_for ('test_replicated'); |
13 | } |
14 | |
e4ee5861 |
15 | |
16 | if (DBICTest::RunMode->is_smoker) { |
17 | my $mver = Moose->VERSION; |
18 | plan skip_all => "A trial version $mver of Moose detected known to break replication - skipping test known to fail" |
19 | if ($mver >= 1.99 and $mver <= 1.9902); |
20 | } |
21 | |
68de9438 |
22 | use Test::Moose; |
c4d3fae2 |
23 | use Test::Exception; |
9901aad7 |
24 | use List::Util 'first'; |
b2e4d522 |
25 | use Scalar::Util 'reftype'; |
3da4f736 |
26 | use File::Spec; |
f404f53c |
27 | use IO::Handle; |
68de9438 |
28 | use Moose(); |
29 | use MooseX::Types(); |
30 | note "Using Moose version $Moose::VERSION and MooseX::Types version $MooseX::Types::VERSION"; |
8f7986d6 |
31 | |
7672675a |
32 | my $var_dir = quotemeta ( File::Spec->catdir(qw/t var/) ); |
33 | |
26ab719a |
34 | use_ok 'DBIx::Class::Storage::DBI::Replicated::Pool'; |
35 | use_ok 'DBIx::Class::Storage::DBI::Replicated::Balancer'; |
36 | use_ok 'DBIx::Class::Storage::DBI::Replicated::Replicant'; |
37 | use_ok 'DBIx::Class::Storage::DBI::Replicated'; |
0f83441a |
38 | |
6a151f58 |
39 | |
89cf6a70 |
40 | =head1 HOW TO USE |
41 | |
42 | This is a test of the replicated storage system. This will work in one of |
43 | two ways, either it was try to fake replication with a couple of SQLite DBs |
44 | and creative use of copy, or if you define a couple of %ENV vars correctly |
45 | will try to test those. If you do that, it will assume the setup is properly |
46 | replicating. Your results may vary, but I have demonstrated this to work with |
47 | mysql native replication. |
d59cf2f2 |
48 | |
89cf6a70 |
49 | =cut |
50 | |
51 | |
0f83441a |
52 | ## ---------------------------------------------------------------------------- |
53 | ## Build a class to hold all our required testing data and methods. |
54 | ## ---------------------------------------------------------------------------- |
55 | |
857d66ca |
56 | TESTSCHEMACLASSES: { |
2bf79155 |
57 | |
857d66ca |
58 | ## --------------------------------------------------------------------- ## |
59 | ## Create an object to contain your replicated stuff. |
60 | ## --------------------------------------------------------------------- ## |
d59cf2f2 |
61 | |
2bf79155 |
62 | package DBIx::Class::DBI::Replicated::TestReplication; |
d59cf2f2 |
63 | |
2bf79155 |
64 | use DBICTest; |
65 | use base qw/Class::Accessor::Fast/; |
d59cf2f2 |
66 | |
857d66ca |
67 | __PACKAGE__->mk_accessors( qw/schema/ ); |
2bf79155 |
68 | |
69 | ## Initialize the object |
d59cf2f2 |
70 | |
71 | sub new { |
72 | my ($class, $schema_method) = (shift, shift); |
73 | my $self = $class->SUPER::new(@_); |
74 | |
75 | $self->schema( $self->init_schema($schema_method) ); |
76 | return $self; |
77 | } |
78 | |
26ab719a |
79 | ## Get the Schema and set the replication storage type |
d59cf2f2 |
80 | |
2bf79155 |
81 | sub init_schema { |
7536c92b |
82 | #my ($class, $schema_getter) = @_; |
83 | shift->${\ ( 'get_schema_' . shift ) }; |
2bf79155 |
84 | } |
dcdf7b2c |
85 | |
86 | sub get_schema_by_storage_type { |
87 | DBICTest->init_schema( |
88 | sqlite_use_file => 1, |
89 | storage_type=>{ |
90 | '::DBI::Replicated' => { |
91 | balancer_type=>'::Random', |
92 | balancer_args=>{ |
93 | auto_validate_every=>100, |
64ae1667 |
94 | master_read_weight => 1 |
dcdf7b2c |
95 | }, |
96 | } |
97 | }, |
98 | deploy_args=>{ |
99 | add_drop_table => 1, |
100 | }, |
101 | ); |
102 | } |
103 | |
104 | sub get_schema_by_connect_info { |
105 | DBICTest->init_schema( |
106 | sqlite_use_file => 1, |
107 | storage_type=> '::DBI::Replicated', |
108 | balancer_type=>'::Random', |
109 | balancer_args=> { |
64ae1667 |
110 | auto_validate_every=>100, |
111 | master_read_weight => 1 |
112 | }, |
113 | pool_args=>{ |
114 | maximum_lag=>1, |
dcdf7b2c |
115 | }, |
116 | deploy_args=>{ |
117 | add_drop_table => 1, |
118 | }, |
119 | ); |
120 | } |
121 | |
857d66ca |
122 | sub generate_replicant_connect_info {} |
123 | sub replicate {} |
124 | sub cleanup {} |
125 | |
b2e4d522 |
126 | ## --------------------------------------------------------------------- ## |
127 | ## Add a connect_info option to test option merging. |
128 | ## --------------------------------------------------------------------- ## |
129 | { |
130 | package DBIx::Class::Storage::DBI::Replicated; |
131 | |
132 | use Moose; |
133 | |
134 | __PACKAGE__->meta->make_mutable; |
135 | |
136 | around connect_info => sub { |
137 | my ($next, $self, $info) = @_; |
138 | $info->[3]{master_option} = 1; |
139 | $self->$next($info); |
140 | }; |
141 | |
142 | __PACKAGE__->meta->make_immutable; |
143 | |
144 | no Moose; |
145 | } |
d59cf2f2 |
146 | |
857d66ca |
147 | ## --------------------------------------------------------------------- ## |
148 | ## Subclass for when you are using SQLite for testing, this provides a fake |
149 | ## replication support. |
150 | ## --------------------------------------------------------------------- ## |
d59cf2f2 |
151 | |
857d66ca |
152 | package DBIx::Class::DBI::Replicated::TestReplication::SQLite; |
153 | |
154 | use DBICTest; |
d59cf2f2 |
155 | use File::Copy; |
857d66ca |
156 | use base 'DBIx::Class::DBI::Replicated::TestReplication'; |
d59cf2f2 |
157 | |
3da4f736 |
158 | __PACKAGE__->mk_accessors(qw/master_path slave_paths/); |
d59cf2f2 |
159 | |
3da4f736 |
160 | ## Set the master path from DBICTest |
d59cf2f2 |
161 | |
162 | sub new { |
163 | my $class = shift @_; |
164 | my $self = $class->SUPER::new(@_); |
165 | |
166 | $self->master_path( DBICTest->_sqlite_dbfilename ); |
167 | $self->slave_paths([ |
168 | File::Spec->catfile(qw/t var DBIxClass_slave1.db/), |
169 | File::Spec->catfile(qw/t var DBIxClass_slave2.db/), |
170 | ]); |
171 | |
172 | return $self; |
173 | } |
174 | |
26ab719a |
175 | ## Return an Array of ArrayRefs where each ArrayRef is suitable to use for |
176 | ## $storage->connect_info to be used for connecting replicants. |
d59cf2f2 |
177 | |
26ab719a |
178 | sub generate_replicant_connect_info { |
857d66ca |
179 | my $self = shift @_; |
26ab719a |
180 | my @dsn = map { |
181 | "dbi:SQLite:${_}"; |
182 | } @{$self->slave_paths}; |
d59cf2f2 |
183 | |
9901aad7 |
184 | my @connect_infos = map { [$_,'','',{AutoCommit=>1}] } @dsn; |
185 | |
d59cf2f2 |
186 | ## Make sure nothing is left over from a failed test |
187 | $self->cleanup; |
3da4f736 |
188 | |
d59cf2f2 |
189 | ## try a hashref too |
9901aad7 |
190 | my $c = $connect_infos[0]; |
191 | $connect_infos[0] = { |
192 | dsn => $c->[0], |
193 | user => $c->[1], |
194 | password => $c->[2], |
195 | %{ $c->[3] } |
196 | }; |
197 | |
198 | @connect_infos |
26ab719a |
199 | } |
9901aad7 |
200 | |
26ab719a |
201 | ## Do a 'good enough' replication by copying the master dbfile over each of |
50336325 |
202 | ## the slave dbfiles. If the master is SQLite we do this, otherwise we |
203 | ## just do a one second pause to let the slaves catch up. |
d59cf2f2 |
204 | |
26ab719a |
205 | sub replicate { |
206 | my $self = shift @_; |
207 | foreach my $slave (@{$self->slave_paths}) { |
208 | copy($self->master_path, $slave); |
209 | } |
210 | } |
d59cf2f2 |
211 | |
26ab719a |
212 | ## Cleanup after ourselves. Unlink all gthe slave paths. |
d59cf2f2 |
213 | |
26ab719a |
214 | sub cleanup { |
215 | my $self = shift @_; |
216 | foreach my $slave (@{$self->slave_paths}) { |
d59cf2f2 |
217 | if(-e $slave) { |
218 | unlink $slave; |
219 | } |
220 | } |
26ab719a |
221 | } |
d59cf2f2 |
222 | |
857d66ca |
223 | ## --------------------------------------------------------------------- ## |
224 | ## Subclass for when you are setting the databases via custom export vars |
225 | ## This is for when you have a replicating database setup that you are |
226 | ## going to test against. You'll need to define the correct $ENV and have |
227 | ## two slave databases to test against, as well as a replication system |
228 | ## that will replicate in less than 1 second. |
229 | ## --------------------------------------------------------------------- ## |
d59cf2f2 |
230 | |
231 | package DBIx::Class::DBI::Replicated::TestReplication::Custom; |
857d66ca |
232 | use base 'DBIx::Class::DBI::Replicated::TestReplication'; |
d59cf2f2 |
233 | |
857d66ca |
234 | ## Return an Array of ArrayRefs where each ArrayRef is suitable to use for |
235 | ## $storage->connect_info to be used for connecting replicants. |
d59cf2f2 |
236 | |
237 | sub generate_replicant_connect_info { |
857d66ca |
238 | return ( |
239 | [$ENV{"DBICTEST_SLAVE0_DSN"}, $ENV{"DBICTEST_SLAVE0_DBUSER"}, $ENV{"DBICTEST_SLAVE0_DBPASS"}, {AutoCommit => 1}], |
d59cf2f2 |
240 | [$ENV{"DBICTEST_SLAVE1_DSN"}, $ENV{"DBICTEST_SLAVE1_DBUSER"}, $ENV{"DBICTEST_SLAVE1_DBPASS"}, {AutoCommit => 1}], |
857d66ca |
241 | ); |
242 | } |
d59cf2f2 |
243 | |
244 | ## pause a bit to let the replication catch up |
245 | |
857d66ca |
246 | sub replicate { |
d59cf2f2 |
247 | sleep 1; |
248 | } |
2bf79155 |
249 | } |
250 | |
251 | ## ---------------------------------------------------------------------------- |
252 | ## Create an object and run some tests |
253 | ## ---------------------------------------------------------------------------- |
254 | |
26ab719a |
255 | ## Thi first bunch of tests are basic, just make sure all the bits are behaving |
2bf79155 |
256 | |
857d66ca |
257 | my $replicated_class = DBICTest->has_custom_dsn ? |
258 | 'DBIx::Class::DBI::Replicated::TestReplication::Custom' : |
259 | 'DBIx::Class::DBI::Replicated::TestReplication::SQLite'; |
260 | |
dcdf7b2c |
261 | my $replicated; |
262 | |
263 | for my $method (qw/by_connect_info by_storage_type/) { |
a3df35f8 |
264 | undef $replicated; |
dcdf7b2c |
265 | ok $replicated = $replicated_class->new($method) |
266 | => "Created a replication object $method"; |
d59cf2f2 |
267 | |
dcdf7b2c |
268 | isa_ok $replicated->schema |
269 | => 'DBIx::Class::Schema'; |
d59cf2f2 |
270 | |
dcdf7b2c |
271 | isa_ok $replicated->schema->storage |
272 | => 'DBIx::Class::Storage::DBI::Replicated'; |
273 | |
274 | isa_ok $replicated->schema->storage->balancer |
275 | => 'DBIx::Class::Storage::DBI::Replicated::Balancer::Random' |
276 | => 'configured balancer_type'; |
277 | } |
26ab719a |
278 | |
f29315ed |
279 | ### check that all Storage::DBI methods are handled by ::Replicated |
280 | { |
b75235fe |
281 | my @storage_dbi_methods = Class::MOP::Class |
282 | ->initialize('DBIx::Class::Storage::DBI')->get_all_method_names; |
f29315ed |
283 | |
b75235fe |
284 | my @replicated_methods = DBIx::Class::Storage::DBI::Replicated->meta |
285 | ->get_all_method_names; |
f29315ed |
286 | |
b75235fe |
287 | # remove constants and OTHER_CRAP |
f29315ed |
288 | @storage_dbi_methods = grep !/^[A-Z_]+\z/, @storage_dbi_methods; |
289 | |
290 | # remove CAG accessors |
291 | @storage_dbi_methods = grep !/_accessor\z/, @storage_dbi_methods; |
292 | |
293 | # remove DBIx::Class (the root parent, with CAG and stuff) methods |
b75235fe |
294 | my @root_methods = Class::MOP::Class->initialize('DBIx::Class') |
f29315ed |
295 | ->get_all_method_names; |
296 | my %count; |
b75235fe |
297 | $count{$_}++ for (@storage_dbi_methods, @root_methods); |
f29315ed |
298 | |
299 | @storage_dbi_methods = grep $count{$_} != 2, @storage_dbi_methods; |
300 | |
301 | # make hashes |
302 | my %storage_dbi_methods; |
303 | @storage_dbi_methods{@storage_dbi_methods} = (); |
304 | my %replicated_methods; |
305 | @replicated_methods{@replicated_methods} = (); |
306 | |
307 | # remove ::Replicated-specific methods |
308 | for my $method (@replicated_methods) { |
309 | delete $replicated_methods{$method} |
310 | unless exists $storage_dbi_methods{$method}; |
311 | } |
e4c5abce |
312 | @replicated_methods = keys %replicated_methods; |
f29315ed |
313 | |
314 | # check that what's left is implemented |
315 | %count = (); |
316 | $count{$_}++ for (@storage_dbi_methods, @replicated_methods); |
317 | |
318 | if ((grep $count{$_} == 2, @storage_dbi_methods) == @storage_dbi_methods) { |
319 | pass 'all DBIx::Class::Storage::DBI methods implemented'; |
320 | } |
321 | else { |
322 | my @unimplemented = grep $count{$_} == 1, @storage_dbi_methods; |
323 | |
324 | fail 'the following DBIx::Class::Storage::DBI methods are unimplemented: ' |
325 | . "@unimplemented"; |
326 | } |
327 | } |
328 | |
26ab719a |
329 | ok $replicated->schema->storage->meta |
330 | => 'has a meta object'; |
d59cf2f2 |
331 | |
26ab719a |
332 | isa_ok $replicated->schema->storage->master |
333 | => 'DBIx::Class::Storage::DBI'; |
d59cf2f2 |
334 | |
26ab719a |
335 | isa_ok $replicated->schema->storage->pool |
336 | => 'DBIx::Class::Storage::DBI::Replicated::Pool'; |
d59cf2f2 |
337 | |
17b05c13 |
338 | does_ok $replicated->schema->storage->balancer |
d59cf2f2 |
339 | => 'DBIx::Class::Storage::DBI::Replicated::Balancer'; |
26ab719a |
340 | |
341 | ok my @replicant_connects = $replicated->generate_replicant_connect_info |
342 | => 'got replication connect information'; |
343 | |
955a6df6 |
344 | ok my @replicated_storages = $replicated->schema->storage->connect_replicants(@replicant_connects) |
26ab719a |
345 | => 'Created some storages suitable for replicants'; |
6412a592 |
346 | |
071bbccb |
347 | our %debug; |
348 | $replicated->schema->storage->debug(1); |
349 | $replicated->schema->storage->debugcb(sub { |
d59cf2f2 |
350 | my ($op, $info) = @_; |
351 | ##warn "\n$op, $info\n"; |
352 | %debug = ( |
353 | op => $op, |
354 | info => $info, |
355 | dsn => ($info=~m/\[(.+)\]/)[0], |
356 | storage_type => $info=~m/REPLICANT/ ? 'REPLICANT' : 'MASTER', |
357 | ); |
071bbccb |
358 | }); |
359 | |
6412a592 |
360 | ok my @all_storages = $replicated->schema->storage->all_storages |
361 | => '->all_storages'; |
362 | |
dcdf7b2c |
363 | is scalar @all_storages, |
364 | 3 |
6412a592 |
365 | => 'correct number of ->all_storages'; |
366 | |
dcdf7b2c |
367 | is ((grep $_->isa('DBIx::Class::Storage::DBI'), @all_storages), |
368 | 3 |
6412a592 |
369 | => '->all_storages are correct type'); |
b2e4d522 |
370 | |
dcdf7b2c |
371 | my @all_storage_opts = |
372 | grep { (reftype($_)||'') eq 'HASH' } |
373 | map @{ $_->_connect_info }, @all_storages; |
374 | |
375 | is ((grep $_->{master_option}, @all_storage_opts), |
376 | 3 |
b2e4d522 |
377 | => 'connect_info was merged from master to replicants'); |
d59cf2f2 |
378 | |
9901aad7 |
379 | my @replicant_names = keys %{ $replicated->schema->storage->replicants }; |
380 | |
bd5da369 |
381 | ok @replicant_names, "found replicant names @replicant_names"; |
382 | |
9901aad7 |
383 | ## Silence warning about not supporting the is_replicating method if using the |
384 | ## sqlite dbs. |
385 | $replicated->schema->storage->debugobj->silence(1) |
7672675a |
386 | if first { $_ =~ /$var_dir/ } @replicant_names; |
d59cf2f2 |
387 | |
cb6ec758 |
388 | isa_ok $replicated->schema->storage->balancer->current_replicant |
d59cf2f2 |
389 | => 'DBIx::Class::Storage::DBI'; |
9901aad7 |
390 | |
391 | $replicated->schema->storage->debugobj->silence(0); |
392 | |
26ab719a |
393 | ok $replicated->schema->storage->pool->has_replicants |
d59cf2f2 |
394 | => 'does have replicants'; |
26ab719a |
395 | |
17b05c13 |
396 | is $replicated->schema->storage->pool->num_replicants => 2 |
26ab719a |
397 | => 'has two replicants'; |
d59cf2f2 |
398 | |
de5dc9ef |
399 | does_ok $replicated_storages[0] |
26ab719a |
400 | => 'DBIx::Class::Storage::DBI::Replicated::Replicant'; |
401 | |
de5dc9ef |
402 | does_ok $replicated_storages[1] |
26ab719a |
403 | => 'DBIx::Class::Storage::DBI::Replicated::Replicant'; |
d59cf2f2 |
404 | |
de5dc9ef |
405 | does_ok $replicated->schema->storage->replicants->{$replicant_names[0]} |
26ab719a |
406 | => 'DBIx::Class::Storage::DBI::Replicated::Replicant'; |
407 | |
de5dc9ef |
408 | does_ok $replicated->schema->storage->replicants->{$replicant_names[1]} |
d59cf2f2 |
409 | => 'DBIx::Class::Storage::DBI::Replicated::Replicant'; |
26ab719a |
410 | |
411 | ## Add some info to the database |
412 | |
413 | $replicated |
414 | ->schema |
415 | ->populate('Artist', [ |
416 | [ qw/artistid name/ ], |
417 | [ 4, "Ozric Tentacles"], |
418 | ]); |
071bbccb |
419 | |
d59cf2f2 |
420 | is $debug{storage_type}, 'MASTER', |
421 | "got last query from a master: $debug{dsn}"; |
422 | |
423 | like $debug{info}, qr/INSERT/, 'Last was an insert'; |
424 | |
26ab719a |
425 | ## Make sure all the slaves have the table definitions |
426 | |
427 | $replicated->replicate; |
5c1d82d2 |
428 | $replicated->schema->storage->replicants->{$replicant_names[0]}->active(1); |
429 | $replicated->schema->storage->replicants->{$replicant_names[1]}->active(1); |
9901aad7 |
430 | |
431 | ## Silence warning about not supporting the is_replicating method if using the |
432 | ## sqlite dbs. |
433 | $replicated->schema->storage->debugobj->silence(1) |
7672675a |
434 | if first { $_ =~ /$var_dir/ } @replicant_names; |
d59cf2f2 |
435 | |
f15afa13 |
436 | $replicated->schema->storage->pool->validate_replicants; |
26ab719a |
437 | |
9901aad7 |
438 | $replicated->schema->storage->debugobj->silence(0); |
439 | |
26ab719a |
440 | ## Make sure we can read the data. |
441 | |
442 | ok my $artist1 = $replicated->schema->resultset('Artist')->find(4) |
443 | => 'Created Result'; |
444 | |
071bbccb |
445 | ## We removed testing here since master read weight is on, so we can't tell in |
446 | ## advance what storage to expect. We turn master read weight off a bit lower |
d59cf2f2 |
447 | ## is $debug{storage_type}, 'REPLICANT' |
448 | ## => "got last query from a replicant: $debug{dsn}, $debug{info}"; |
071bbccb |
449 | |
26ab719a |
450 | isa_ok $artist1 |
451 | => 'DBICTest::Artist'; |
d59cf2f2 |
452 | |
26ab719a |
453 | is $artist1->name, 'Ozric Tentacles' |
454 | => 'Found expected name for first result'; |
455 | |
ee356d00 |
456 | ## Check that master_read_weight is honored |
457 | { |
87974600 |
458 | no warnings qw/once redefine/; |
ee356d00 |
459 | |
460 | local |
f404f53c |
461 | *DBIx::Class::Storage::DBI::Replicated::Balancer::Random::_random_number = |
d59cf2f2 |
462 | sub { 999 }; |
ee356d00 |
463 | |
464 | $replicated->schema->storage->balancer->increment_storage; |
465 | |
466 | is $replicated->schema->storage->balancer->current_replicant, |
467 | $replicated->schema->storage->master |
468 | => 'master_read_weight is honored'; |
469 | |
470 | ## turn it off for the duration of the test |
471 | $replicated->schema->storage->balancer->master_read_weight(0); |
472 | $replicated->schema->storage->balancer->increment_storage; |
473 | } |
474 | |
26ab719a |
475 | ## Add some new rows that only the master will have This is because |
476 | ## we overload any type of write operation so that is must hit the master |
477 | ## database. |
478 | |
479 | $replicated |
480 | ->schema |
481 | ->populate('Artist', [ |
482 | [ qw/artistid name/ ], |
483 | [ 5, "Doom's Children"], |
484 | [ 6, "Dead On Arrival"], |
485 | [ 7, "Watergate"], |
486 | ]); |
487 | |
d59cf2f2 |
488 | is $debug{storage_type}, 'MASTER', |
489 | "got last query from a master: $debug{dsn}"; |
490 | |
491 | like $debug{info}, qr/INSERT/, 'Last was an insert'; |
071bbccb |
492 | |
26ab719a |
493 | ## Make sure all the slaves have the table definitions |
494 | $replicated->replicate; |
495 | |
496 | ## Should find some data now |
497 | |
498 | ok my $artist2 = $replicated->schema->resultset('Artist')->find(5) |
499 | => 'Sync succeed'; |
071bbccb |
500 | |
d59cf2f2 |
501 | is $debug{storage_type}, 'REPLICANT' |
502 | => "got last query from a replicant: $debug{dsn}"; |
503 | |
26ab719a |
504 | isa_ok $artist2 |
505 | => 'DBICTest::Artist'; |
d59cf2f2 |
506 | |
26ab719a |
507 | is $artist2->name, "Doom's Children" |
508 | => 'Found expected name for first result'; |
509 | |
510 | ## What happens when we disconnect all the replicants? |
511 | |
50336325 |
512 | is $replicated->schema->storage->pool->connected_replicants => 2 |
513 | => "both replicants are connected"; |
d59cf2f2 |
514 | |
89cf6a70 |
515 | $replicated->schema->storage->replicants->{$replicant_names[0]}->disconnect; |
516 | $replicated->schema->storage->replicants->{$replicant_names[1]}->disconnect; |
26ab719a |
517 | |
50336325 |
518 | is $replicated->schema->storage->pool->connected_replicants => 0 |
519 | => "both replicants are now disconnected"; |
520 | |
521 | ## All these should pass, since the database should automatically reconnect |
522 | |
26ab719a |
523 | ok my $artist3 = $replicated->schema->resultset('Artist')->find(6) |
524 | => 'Still finding stuff.'; |
071bbccb |
525 | |
d59cf2f2 |
526 | is $debug{storage_type}, 'REPLICANT' |
527 | => "got last query from a replicant: $debug{dsn}"; |
528 | |
26ab719a |
529 | isa_ok $artist3 |
530 | => 'DBICTest::Artist'; |
d59cf2f2 |
531 | |
26ab719a |
532 | is $artist3->name, "Dead On Arrival" |
533 | => 'Found expected name for first result'; |
2bf79155 |
534 | |
50336325 |
535 | is $replicated->schema->storage->pool->connected_replicants => 1 |
13b9e828 |
536 | => "At Least One replicant reconnected to handle the job"; |
d59cf2f2 |
537 | |
6f6fb437 |
538 | ## What happens when we try to select something that doesn't exist? |
539 | |
540 | ok ! $replicated->schema->resultset('Artist')->find(666) |
541 | => 'Correctly failed to find something.'; |
071bbccb |
542 | |
d59cf2f2 |
543 | is $debug{storage_type}, 'REPLICANT' |
544 | => "got last query from a replicant: $debug{dsn}"; |
545 | |
cb6ec758 |
546 | ## test the reliable option |
547 | |
548 | TESTRELIABLE: { |
d59cf2f2 |
549 | |
550 | $replicated->schema->storage->set_reliable_storage; |
551 | |
552 | ok $replicated->schema->resultset('Artist')->find(2) |
553 | => 'Read from master 1'; |
554 | |
555 | is $debug{storage_type}, 'MASTER', |
556 | "got last query from a master: $debug{dsn}"; |
557 | |
558 | ok $replicated->schema->resultset('Artist')->find(5) |
559 | => 'Read from master 2'; |
560 | |
561 | is $debug{storage_type}, 'MASTER', |
562 | "got last query from a master: $debug{dsn}"; |
563 | |
564 | $replicated->schema->storage->set_balanced_storage; |
565 | |
566 | ok $replicated->schema->resultset('Artist')->find(3) |
9c748388 |
567 | => 'Read from replicant'; |
071bbccb |
568 | |
d59cf2f2 |
569 | is $debug{storage_type}, 'REPLICANT', |
570 | "got last query from a replicant: $debug{dsn}"; |
cb6ec758 |
571 | } |
572 | |
9c748388 |
573 | ## Make sure when reliable goes out of scope, we are using replicants again |
cb6ec758 |
574 | |
575 | ok $replicated->schema->resultset('Artist')->find(1) |
576 | => 'back to replicant 1.'; |
071bbccb |
577 | |
d59cf2f2 |
578 | is $debug{storage_type}, 'REPLICANT', |
579 | "got last query from a replicant: $debug{dsn}"; |
580 | |
cb6ec758 |
581 | ok $replicated->schema->resultset('Artist')->find(2) |
582 | => 'back to replicant 2.'; |
2156bbdd |
583 | |
d59cf2f2 |
584 | is $debug{storage_type}, 'REPLICANT', |
585 | "got last query from a replicant: $debug{dsn}"; |
071bbccb |
586 | |
106d5f3b |
587 | ## set all the replicants to inactive, and make sure the balancer falls back to |
588 | ## the master. |
589 | |
89cf6a70 |
590 | $replicated->schema->storage->replicants->{$replicant_names[0]}->active(0); |
591 | $replicated->schema->storage->replicants->{$replicant_names[1]}->active(0); |
9901aad7 |
592 | |
f404f53c |
593 | { |
594 | ## catch the fallback to master warning |
595 | open my $debugfh, '>', \my $fallback_warning; |
596 | my $oldfh = $replicated->schema->storage->debugfh; |
597 | $replicated->schema->storage->debugfh($debugfh); |
de5dc9ef |
598 | |
f404f53c |
599 | ok $replicated->schema->resultset('Artist')->find(2) |
d59cf2f2 |
600 | => 'Fallback to master'; |
071bbccb |
601 | |
d59cf2f2 |
602 | is $debug{storage_type}, 'MASTER', |
603 | "got last query from a master: $debug{dsn}"; |
f404f53c |
604 | |
605 | like $fallback_warning, qr/falling back to master/ |
9450f312 |
606 | => 'emits falling back to master debug'; |
f404f53c |
607 | |
608 | $replicated->schema->storage->debugfh($oldfh); |
609 | } |
9901aad7 |
610 | |
de5dc9ef |
611 | $replicated->schema->storage->replicants->{$replicant_names[0]}->active(1); |
612 | $replicated->schema->storage->replicants->{$replicant_names[1]}->active(1); |
9901aad7 |
613 | |
614 | ## Silence warning about not supporting the is_replicating method if using the |
615 | ## sqlite dbs. |
616 | $replicated->schema->storage->debugobj->silence(1) |
7672675a |
617 | if first { $_ =~ /$var_dir/ } @replicant_names; |
d59cf2f2 |
618 | |
f15afa13 |
619 | $replicated->schema->storage->pool->validate_replicants; |
de5dc9ef |
620 | |
9901aad7 |
621 | $replicated->schema->storage->debugobj->silence(0); |
622 | |
9450f312 |
623 | { |
624 | ## catch the fallback to master warning |
625 | open my $debugfh, '>', \my $return_warning; |
626 | my $oldfh = $replicated->schema->storage->debugfh; |
627 | $replicated->schema->storage->debugfh($debugfh); |
628 | |
629 | ok $replicated->schema->resultset('Artist')->find(2) |
630 | => 'Return to replicants'; |
631 | |
632 | is $debug{storage_type}, 'REPLICANT', |
633 | "got last query from a replicant: $debug{dsn}"; |
071bbccb |
634 | |
9450f312 |
635 | like $return_warning, qr/Moved back to slave/ |
636 | => 'emits returning to slave debug'; |
637 | |
638 | $replicated->schema->storage->debugfh($oldfh); |
639 | } |
d59cf2f2 |
640 | |
de5dc9ef |
641 | ## Getting slave status tests |
642 | |
f797e89e |
643 | SKIP: { |
644 | ## We skip this tests unless you have a custom replicants, since the default |
645 | ## sqlite based replication tests don't support these functions. |
d59cf2f2 |
646 | |
647 | skip 'Cannot Test Replicant Status on Non Replicating Database', 10 |
f797e89e |
648 | unless DBICTest->has_custom_dsn && $ENV{"DBICTEST_SLAVE0_DSN"}; |
649 | |
650 | $replicated->replicate; ## Give the slaves a chance to catchup. |
651 | |
d59cf2f2 |
652 | ok $replicated->schema->storage->replicants->{$replicant_names[0]}->is_replicating |
653 | => 'Replicants are replicating'; |
654 | |
655 | is $replicated->schema->storage->replicants->{$replicant_names[0]}->lag_behind_master, 0 |
656 | => 'Replicant is zero seconds behind master'; |
657 | |
658 | ## Test the validate replicants |
659 | |
660 | $replicated->schema->storage->pool->validate_replicants; |
661 | |
662 | is $replicated->schema->storage->pool->active_replicants, 2 |
663 | => 'Still have 2 replicants after validation'; |
664 | |
665 | ## Force the replicants to fail the validate test by required their lag to |
666 | ## be negative (ie ahead of the master!) |
667 | |
7edf5f1c |
668 | $replicated->schema->storage->pool->maximum_lag(-10); |
669 | $replicated->schema->storage->pool->validate_replicants; |
d59cf2f2 |
670 | |
7edf5f1c |
671 | is $replicated->schema->storage->pool->active_replicants, 0 |
672 | => 'No way a replicant be be ahead of the master'; |
d59cf2f2 |
673 | |
7edf5f1c |
674 | ## Let's be fair to the replicants again. Let them lag up to 5 |
d59cf2f2 |
675 | |
7edf5f1c |
676 | $replicated->schema->storage->pool->maximum_lag(5); |
677 | $replicated->schema->storage->pool->validate_replicants; |
d59cf2f2 |
678 | |
7edf5f1c |
679 | is $replicated->schema->storage->pool->active_replicants, 2 |
d59cf2f2 |
680 | => 'Both replicants in good standing again'; |
681 | |
682 | ## Check auto validate |
683 | |
684 | is $replicated->schema->storage->balancer->auto_validate_every, 100 |
685 | => "Got the expected value for auto validate"; |
686 | |
687 | ## This will make sure we auto validatge everytime |
688 | $replicated->schema->storage->balancer->auto_validate_every(0); |
689 | |
690 | ## set all the replicants to inactive, and make sure the balancer falls back to |
691 | ## the master. |
692 | |
693 | $replicated->schema->storage->replicants->{$replicant_names[0]}->active(0); |
694 | $replicated->schema->storage->replicants->{$replicant_names[1]}->active(0); |
695 | |
696 | ## Ok, now when we go to run a query, autovalidate SHOULD reconnect |
697 | |
698 | is $replicated->schema->storage->pool->active_replicants => 0 |
699 | => "both replicants turned off"; |
700 | |
701 | ok $replicated->schema->resultset('Artist')->find(5) |
702 | => 'replicant reactivated'; |
703 | |
704 | is $debug{storage_type}, 'REPLICANT', |
705 | "got last query from a replicant: $debug{dsn}"; |
706 | |
707 | is $replicated->schema->storage->pool->active_replicants => 2 |
708 | => "both replicants reactivated"; |
f797e89e |
709 | } |
710 | |
c4d3fae2 |
711 | ## Test the reliably callback |
712 | |
713 | ok my $reliably = sub { |
d59cf2f2 |
714 | |
c4d3fae2 |
715 | ok $replicated->schema->resultset('Artist')->find(5) |
071bbccb |
716 | => 'replicant reactivated'; |
717 | |
d59cf2f2 |
718 | is $debug{storage_type}, 'MASTER', |
719 | "got last query from a master: $debug{dsn}"; |
720 | |
c4d3fae2 |
721 | } => 'created coderef properly'; |
722 | |
723 | $replicated->schema->storage->execute_reliably($reliably); |
724 | |
725 | ## Try something with an error |
726 | |
727 | ok my $unreliably = sub { |
d59cf2f2 |
728 | |
c4d3fae2 |
729 | ok $replicated->schema->resultset('ArtistXX')->find(5) |
d59cf2f2 |
730 | => 'replicant reactivated'; |
731 | |
c4d3fae2 |
732 | } => 'created coderef properly'; |
733 | |
d59cf2f2 |
734 | throws_ok {$replicated->schema->storage->execute_reliably($unreliably)} |
ed213e85 |
735 | qr/Can't find source for ArtistXX/ |
c4d3fae2 |
736 | => 'Bad coderef throws proper error'; |
d59cf2f2 |
737 | |
ed213e85 |
738 | ## Make sure replication came back |
739 | |
740 | ok $replicated->schema->resultset('Artist')->find(3) |
741 | => 'replicant reactivated'; |
071bbccb |
742 | |
743 | is $debug{storage_type}, 'REPLICANT', "got last query from a replicant: $debug{dsn}"; |
d59cf2f2 |
744 | |
c4d3fae2 |
745 | ## make sure transactions are set to execute_reliably |
746 | |
747 | ok my $transaction = sub { |
d59cf2f2 |
748 | |
749 | my $id = shift @_; |
750 | |
751 | $replicated |
752 | ->schema |
753 | ->populate('Artist', [ |
754 | [ qw/artistid name/ ], |
84f7e8a1 |
755 | [ $id, "Children of the Grave $id"], |
d59cf2f2 |
756 | ]); |
757 | |
64cdad22 |
758 | ok my $result = $replicated->schema->resultset('Artist')->find($id) |
bd5da369 |
759 | => "Found expected artist for $id"; |
071bbccb |
760 | |
d59cf2f2 |
761 | is $debug{storage_type}, 'MASTER', |
762 | "got last query from a master: $debug{dsn}"; |
763 | |
64cdad22 |
764 | ok my $more = $replicated->schema->resultset('Artist')->find(1) |
bd5da369 |
765 | => 'Found expected artist again for 1'; |
071bbccb |
766 | |
d59cf2f2 |
767 | is $debug{storage_type}, 'MASTER', |
768 | "got last query from a master: $debug{dsn}"; |
769 | |
ed213e85 |
770 | return ($result, $more); |
d59cf2f2 |
771 | |
64cdad22 |
772 | } => 'Created a coderef properly'; |
c4d3fae2 |
773 | |
ed213e85 |
774 | ## Test the transaction with multi return |
775 | { |
d59cf2f2 |
776 | ok my @return = $replicated->schema->txn_do($transaction, 666) |
777 | => 'did transaction'; |
778 | |
779 | is $return[0]->id, 666 |
780 | => 'first returned value is correct'; |
071bbccb |
781 | |
d59cf2f2 |
782 | is $debug{storage_type}, 'MASTER', |
783 | "got last query from a master: $debug{dsn}"; |
071bbccb |
784 | |
d59cf2f2 |
785 | is $return[1]->id, 1 |
786 | => 'second returned value is correct'; |
787 | |
788 | is $debug{storage_type}, 'MASTER', |
789 | "got last query from a master: $debug{dsn}"; |
071bbccb |
790 | |
ed213e85 |
791 | } |
792 | |
793 | ## Test that asking for single return works |
794 | { |
d59cf2f2 |
795 | ok my @return = $replicated->schema->txn_do($transaction, 777) |
796 | => 'did transaction'; |
797 | |
798 | is $return[0]->id, 777 |
799 | => 'first returned value is correct'; |
800 | |
801 | is $return[1]->id, 1 |
802 | => 'second returned value is correct'; |
ed213e85 |
803 | } |
804 | |
805 | ## Test transaction returning a single value |
806 | |
807 | { |
d59cf2f2 |
808 | ok my $result = $replicated->schema->txn_do(sub { |
809 | ok my $more = $replicated->schema->resultset('Artist')->find(1) |
810 | => 'found inside a transaction'; |
811 | is $debug{storage_type}, 'MASTER', "got last query from a master: $debug{dsn}"; |
812 | return $more; |
813 | }) => 'successfully processed transaction'; |
814 | |
815 | is $result->id, 1 |
816 | => 'Got expected single result from transaction'; |
ed213e85 |
817 | } |
c4d3fae2 |
818 | |
819 | ## Make sure replication came back |
820 | |
ed213e85 |
821 | ok $replicated->schema->resultset('Artist')->find(1) |
c4d3fae2 |
822 | => 'replicant reactivated'; |
071bbccb |
823 | |
824 | is $debug{storage_type}, 'REPLICANT', "got last query from a replicant: $debug{dsn}"; |
d59cf2f2 |
825 | |
ed213e85 |
826 | ## Test Discard changes |
827 | |
828 | { |
d59cf2f2 |
829 | ok my $artist = $replicated->schema->resultset('Artist')->find(2) |
830 | => 'got an artist to test discard changes'; |
071bbccb |
831 | |
d59cf2f2 |
832 | is $debug{storage_type}, 'REPLICANT', "got last query from a replicant: $debug{dsn}"; |
071bbccb |
833 | |
d59cf2f2 |
834 | ok $artist->get_from_storage({force_pool=>'master'}) |
835 | => 'properly discard changes'; |
071bbccb |
836 | |
d59cf2f2 |
837 | is $debug{storage_type}, 'MASTER', "got last query from a master: $debug{dsn}"; |
071bbccb |
838 | |
3a443bb0 |
839 | ok $artist->discard_changes({force_pool=>'master'}) |
840 | => 'properly called discard_changes against master (manual attrs)'; |
9c169362 |
841 | |
3a443bb0 |
842 | is $debug{storage_type}, 'MASTER', "got last query from a master: $debug{dsn}"; |
9c169362 |
843 | |
3a443bb0 |
844 | ok $artist->discard_changes() |
845 | => 'properly called discard_changes against master (default attrs)'; |
9c169362 |
846 | |
3a443bb0 |
847 | is $debug{storage_type}, 'MASTER', "got last query from a master: $debug{dsn}"; |
8287e9f8 |
848 | |
3a443bb0 |
849 | ok $artist->discard_changes({force_pool=>$replicant_names[0]}) |
850 | => 'properly able to override the default attributes'; |
8287e9f8 |
851 | |
3a443bb0 |
852 | is $debug{storage_type}, 'REPLICANT', "got last query from a replicant: $debug{dsn}" |
ed213e85 |
853 | } |
64cdad22 |
854 | |
855 | ## Test some edge cases, like trying to do a transaction inside a transaction, etc |
856 | |
857 | { |
858 | ok my $result = $replicated->schema->txn_do(sub { |
d59cf2f2 |
859 | return $replicated->schema->txn_do(sub { |
860 | ok my $more = $replicated->schema->resultset('Artist')->find(1) |
861 | => 'found inside a transaction inside a transaction'; |
862 | is $debug{storage_type}, 'MASTER', "got last query from a master: $debug{dsn}"; |
863 | return $more; |
864 | }); |
64cdad22 |
865 | }) => 'successfully processed transaction'; |
d59cf2f2 |
866 | |
64cdad22 |
867 | is $result->id, 1 |
d59cf2f2 |
868 | => 'Got expected single result from transaction'; |
64cdad22 |
869 | } |
870 | |
871 | { |
872 | ok my $result = $replicated->schema->txn_do(sub { |
d59cf2f2 |
873 | return $replicated->schema->storage->execute_reliably(sub { |
874 | return $replicated->schema->txn_do(sub { |
875 | return $replicated->schema->storage->execute_reliably(sub { |
876 | ok my $more = $replicated->schema->resultset('Artist')->find(1) |
877 | => 'found inside crazy deep transactions and execute_reliably'; |
878 | is $debug{storage_type}, 'MASTER', "got last query from a master: $debug{dsn}"; |
879 | return $more; |
880 | }); |
881 | }); |
882 | }); |
64cdad22 |
883 | }) => 'successfully processed transaction'; |
d59cf2f2 |
884 | |
64cdad22 |
885 | is $result->id, 1 |
d59cf2f2 |
886 | => 'Got expected single result from transaction'; |
887 | } |
2ce6e9a6 |
888 | |
7e38d850 |
889 | ## Test the force_pool resultset attribute. |
bbafcf26 |
890 | |
891 | { |
d59cf2f2 |
892 | ok my $artist_rs = $replicated->schema->resultset('Artist') |
bbafcf26 |
893 | => 'got artist resultset'; |
d59cf2f2 |
894 | |
895 | ## Turn on Forced Pool Storage |
896 | ok my $reliable_artist_rs = $artist_rs->search(undef, {force_pool=>'master'}) |
7e38d850 |
897 | => 'Created a resultset using force_pool storage'; |
d59cf2f2 |
898 | |
899 | ok my $artist = $reliable_artist_rs->find(2) |
7e38d850 |
900 | => 'got an artist result via force_pool storage'; |
071bbccb |
901 | |
d59cf2f2 |
902 | is $debug{storage_type}, 'MASTER', "got last query from a master: $debug{dsn}"; |
bbafcf26 |
903 | } |
904 | |
bd5da369 |
905 | ## Test the force_pool resultset attribute part two. |
906 | |
907 | { |
d59cf2f2 |
908 | ok my $artist_rs = $replicated->schema->resultset('Artist') |
bd5da369 |
909 | => 'got artist resultset'; |
d59cf2f2 |
910 | |
911 | ## Turn on Forced Pool Storage |
912 | ok my $reliable_artist_rs = $artist_rs->search(undef, {force_pool=>$replicant_names[0]}) |
bd5da369 |
913 | => 'Created a resultset using force_pool storage'; |
d59cf2f2 |
914 | |
915 | ok my $artist = $reliable_artist_rs->find(2) |
bd5da369 |
916 | => 'got an artist result via force_pool storage'; |
071bbccb |
917 | |
d59cf2f2 |
918 | is $debug{storage_type}, 'REPLICANT', "got last query from a replicant: $debug{dsn}"; |
bd5da369 |
919 | } |
0f83441a |
920 | ## Delete the old database files |
50336325 |
921 | $replicated->cleanup; |
ee356d00 |
922 | |
6988233d |
923 | done_testing; |
924 | |
ee356d00 |
925 | # vim: sw=4 sts=4 : |