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