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