Commit | Line | Data |
e4dc89b3 |
1 | use strict; |
2 | use warnings; |
68de9438 |
3 | |
e4dc89b3 |
4 | use Test::More; |
68de9438 |
5 | |
6 | BEGIN { |
7 | require DBIx::Class; |
8 | plan skip_all => 'Test needs ' . DBIx::Class::Optional::Dependencies->req_missing_for ('test_replicated') |
9 | unless DBIx::Class::Optional::Dependencies->req_ok_for ('test_replicated'); |
10 | } |
11 | |
12 | use Test::Moose; |
c4d3fae2 |
13 | use Test::Exception; |
9901aad7 |
14 | use List::Util 'first'; |
b2e4d522 |
15 | use Scalar::Util 'reftype'; |
3da4f736 |
16 | use File::Spec; |
f404f53c |
17 | use IO::Handle; |
68de9438 |
18 | use Moose(); |
19 | use MooseX::Types(); |
20 | note "Using Moose version $Moose::VERSION and MooseX::Types version $MooseX::Types::VERSION"; |
8f7986d6 |
21 | |
68de9438 |
22 | use lib qw(t/lib); |
23 | use DBICTest; |
26ab719a |
24 | |
7672675a |
25 | my $var_dir = quotemeta ( File::Spec->catdir(qw/t var/) ); |
26 | |
26ab719a |
27 | use_ok 'DBIx::Class::Storage::DBI::Replicated::Pool'; |
28 | use_ok 'DBIx::Class::Storage::DBI::Replicated::Balancer'; |
29 | use_ok 'DBIx::Class::Storage::DBI::Replicated::Replicant'; |
30 | use_ok 'DBIx::Class::Storage::DBI::Replicated'; |
0f83441a |
31 | |
6a151f58 |
32 | |
89cf6a70 |
33 | =head1 HOW TO USE |
34 | |
35 | This is a test of the replicated storage system. This will work in one of |
36 | two ways, either it was try to fake replication with a couple of SQLite DBs |
37 | and creative use of copy, or if you define a couple of %ENV vars correctly |
38 | will try to test those. If you do that, it will assume the setup is properly |
39 | replicating. Your results may vary, but I have demonstrated this to work with |
40 | mysql native replication. |
d59cf2f2 |
41 | |
89cf6a70 |
42 | =cut |
43 | |
44 | |
0f83441a |
45 | ## ---------------------------------------------------------------------------- |
46 | ## Build a class to hold all our required testing data and methods. |
47 | ## ---------------------------------------------------------------------------- |
48 | |
857d66ca |
49 | TESTSCHEMACLASSES: { |
2bf79155 |
50 | |
857d66ca |
51 | ## --------------------------------------------------------------------- ## |
52 | ## Create an object to contain your replicated stuff. |
53 | ## --------------------------------------------------------------------- ## |
d59cf2f2 |
54 | |
2bf79155 |
55 | package DBIx::Class::DBI::Replicated::TestReplication; |
d59cf2f2 |
56 | |
2bf79155 |
57 | use DBICTest; |
58 | use base qw/Class::Accessor::Fast/; |
d59cf2f2 |
59 | |
857d66ca |
60 | __PACKAGE__->mk_accessors( qw/schema/ ); |
2bf79155 |
61 | |
62 | ## Initialize the object |
d59cf2f2 |
63 | |
64 | sub new { |
65 | my ($class, $schema_method) = (shift, shift); |
66 | my $self = $class->SUPER::new(@_); |
67 | |
68 | $self->schema( $self->init_schema($schema_method) ); |
69 | return $self; |
70 | } |
71 | |
26ab719a |
72 | ## Get the Schema and set the replication storage type |
d59cf2f2 |
73 | |
2bf79155 |
74 | sub init_schema { |
f6ace689 |
75 | # current SQLT SQLite producer does not handle DROP TABLE IF EXISTS, trap warnings here |
2361982d |
76 | local $SIG{__WARN__} = sub { warn @_ unless $_[0] =~ /no such table.+DROP TABLE/s }; |
f6ace689 |
77 | |
dcdf7b2c |
78 | my ($class, $schema_method) = @_; |
79 | |
80 | my $method = "get_schema_$schema_method"; |
81 | my $schema = $class->$method; |
cb6ec758 |
82 | |
2bf79155 |
83 | return $schema; |
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/ |
d59cf2f2 |
606 | => 'emits falling back to master warning'; |
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 | |
de5dc9ef |
623 | ok $replicated->schema->resultset('Artist')->find(2) |
624 | => 'Returned to replicates'; |
071bbccb |
625 | |
d59cf2f2 |
626 | is $debug{storage_type}, 'REPLICANT', |
627 | "got last query from a replicant: $debug{dsn}"; |
628 | |
de5dc9ef |
629 | ## Getting slave status tests |
630 | |
f797e89e |
631 | SKIP: { |
632 | ## We skip this tests unless you have a custom replicants, since the default |
633 | ## sqlite based replication tests don't support these functions. |
d59cf2f2 |
634 | |
635 | skip 'Cannot Test Replicant Status on Non Replicating Database', 10 |
f797e89e |
636 | unless DBICTest->has_custom_dsn && $ENV{"DBICTEST_SLAVE0_DSN"}; |
637 | |
638 | $replicated->replicate; ## Give the slaves a chance to catchup. |
639 | |
d59cf2f2 |
640 | ok $replicated->schema->storage->replicants->{$replicant_names[0]}->is_replicating |
641 | => 'Replicants are replicating'; |
642 | |
643 | is $replicated->schema->storage->replicants->{$replicant_names[0]}->lag_behind_master, 0 |
644 | => 'Replicant is zero seconds behind master'; |
645 | |
646 | ## Test the validate replicants |
647 | |
648 | $replicated->schema->storage->pool->validate_replicants; |
649 | |
650 | is $replicated->schema->storage->pool->active_replicants, 2 |
651 | => 'Still have 2 replicants after validation'; |
652 | |
653 | ## Force the replicants to fail the validate test by required their lag to |
654 | ## be negative (ie ahead of the master!) |
655 | |
7edf5f1c |
656 | $replicated->schema->storage->pool->maximum_lag(-10); |
657 | $replicated->schema->storage->pool->validate_replicants; |
d59cf2f2 |
658 | |
7edf5f1c |
659 | is $replicated->schema->storage->pool->active_replicants, 0 |
660 | => 'No way a replicant be be ahead of the master'; |
d59cf2f2 |
661 | |
7edf5f1c |
662 | ## Let's be fair to the replicants again. Let them lag up to 5 |
d59cf2f2 |
663 | |
7edf5f1c |
664 | $replicated->schema->storage->pool->maximum_lag(5); |
665 | $replicated->schema->storage->pool->validate_replicants; |
d59cf2f2 |
666 | |
7edf5f1c |
667 | is $replicated->schema->storage->pool->active_replicants, 2 |
d59cf2f2 |
668 | => 'Both replicants in good standing again'; |
669 | |
670 | ## Check auto validate |
671 | |
672 | is $replicated->schema->storage->balancer->auto_validate_every, 100 |
673 | => "Got the expected value for auto validate"; |
674 | |
675 | ## This will make sure we auto validatge everytime |
676 | $replicated->schema->storage->balancer->auto_validate_every(0); |
677 | |
678 | ## set all the replicants to inactive, and make sure the balancer falls back to |
679 | ## the master. |
680 | |
681 | $replicated->schema->storage->replicants->{$replicant_names[0]}->active(0); |
682 | $replicated->schema->storage->replicants->{$replicant_names[1]}->active(0); |
683 | |
684 | ## Ok, now when we go to run a query, autovalidate SHOULD reconnect |
685 | |
686 | is $replicated->schema->storage->pool->active_replicants => 0 |
687 | => "both replicants turned off"; |
688 | |
689 | ok $replicated->schema->resultset('Artist')->find(5) |
690 | => 'replicant reactivated'; |
691 | |
692 | is $debug{storage_type}, 'REPLICANT', |
693 | "got last query from a replicant: $debug{dsn}"; |
694 | |
695 | is $replicated->schema->storage->pool->active_replicants => 2 |
696 | => "both replicants reactivated"; |
f797e89e |
697 | } |
698 | |
c4d3fae2 |
699 | ## Test the reliably callback |
700 | |
701 | ok my $reliably = sub { |
d59cf2f2 |
702 | |
c4d3fae2 |
703 | ok $replicated->schema->resultset('Artist')->find(5) |
071bbccb |
704 | => 'replicant reactivated'; |
705 | |
d59cf2f2 |
706 | is $debug{storage_type}, 'MASTER', |
707 | "got last query from a master: $debug{dsn}"; |
708 | |
c4d3fae2 |
709 | } => 'created coderef properly'; |
710 | |
711 | $replicated->schema->storage->execute_reliably($reliably); |
712 | |
713 | ## Try something with an error |
714 | |
715 | ok my $unreliably = sub { |
d59cf2f2 |
716 | |
c4d3fae2 |
717 | ok $replicated->schema->resultset('ArtistXX')->find(5) |
d59cf2f2 |
718 | => 'replicant reactivated'; |
719 | |
c4d3fae2 |
720 | } => 'created coderef properly'; |
721 | |
d59cf2f2 |
722 | throws_ok {$replicated->schema->storage->execute_reliably($unreliably)} |
ed213e85 |
723 | qr/Can't find source for ArtistXX/ |
c4d3fae2 |
724 | => 'Bad coderef throws proper error'; |
d59cf2f2 |
725 | |
ed213e85 |
726 | ## Make sure replication came back |
727 | |
728 | ok $replicated->schema->resultset('Artist')->find(3) |
729 | => 'replicant reactivated'; |
071bbccb |
730 | |
731 | is $debug{storage_type}, 'REPLICANT', "got last query from a replicant: $debug{dsn}"; |
d59cf2f2 |
732 | |
c4d3fae2 |
733 | ## make sure transactions are set to execute_reliably |
734 | |
735 | ok my $transaction = sub { |
d59cf2f2 |
736 | |
737 | my $id = shift @_; |
738 | |
739 | $replicated |
740 | ->schema |
741 | ->populate('Artist', [ |
742 | [ qw/artistid name/ ], |
84f7e8a1 |
743 | [ $id, "Children of the Grave $id"], |
d59cf2f2 |
744 | ]); |
745 | |
64cdad22 |
746 | ok my $result = $replicated->schema->resultset('Artist')->find($id) |
bd5da369 |
747 | => "Found expected artist for $id"; |
071bbccb |
748 | |
d59cf2f2 |
749 | is $debug{storage_type}, 'MASTER', |
750 | "got last query from a master: $debug{dsn}"; |
751 | |
64cdad22 |
752 | ok my $more = $replicated->schema->resultset('Artist')->find(1) |
bd5da369 |
753 | => 'Found expected artist again for 1'; |
071bbccb |
754 | |
d59cf2f2 |
755 | is $debug{storage_type}, 'MASTER', |
756 | "got last query from a master: $debug{dsn}"; |
757 | |
ed213e85 |
758 | return ($result, $more); |
d59cf2f2 |
759 | |
64cdad22 |
760 | } => 'Created a coderef properly'; |
c4d3fae2 |
761 | |
ed213e85 |
762 | ## Test the transaction with multi return |
763 | { |
d59cf2f2 |
764 | ok my @return = $replicated->schema->txn_do($transaction, 666) |
765 | => 'did transaction'; |
766 | |
767 | is $return[0]->id, 666 |
768 | => 'first returned value is correct'; |
071bbccb |
769 | |
d59cf2f2 |
770 | is $debug{storage_type}, 'MASTER', |
771 | "got last query from a master: $debug{dsn}"; |
071bbccb |
772 | |
d59cf2f2 |
773 | is $return[1]->id, 1 |
774 | => 'second returned value is correct'; |
775 | |
776 | is $debug{storage_type}, 'MASTER', |
777 | "got last query from a master: $debug{dsn}"; |
071bbccb |
778 | |
ed213e85 |
779 | } |
780 | |
781 | ## Test that asking for single return works |
782 | { |
d59cf2f2 |
783 | ok my @return = $replicated->schema->txn_do($transaction, 777) |
784 | => 'did transaction'; |
785 | |
786 | is $return[0]->id, 777 |
787 | => 'first returned value is correct'; |
788 | |
789 | is $return[1]->id, 1 |
790 | => 'second returned value is correct'; |
ed213e85 |
791 | } |
792 | |
793 | ## Test transaction returning a single value |
794 | |
795 | { |
d59cf2f2 |
796 | ok my $result = $replicated->schema->txn_do(sub { |
797 | ok my $more = $replicated->schema->resultset('Artist')->find(1) |
798 | => 'found inside a transaction'; |
799 | is $debug{storage_type}, 'MASTER', "got last query from a master: $debug{dsn}"; |
800 | return $more; |
801 | }) => 'successfully processed transaction'; |
802 | |
803 | is $result->id, 1 |
804 | => 'Got expected single result from transaction'; |
ed213e85 |
805 | } |
c4d3fae2 |
806 | |
807 | ## Make sure replication came back |
808 | |
ed213e85 |
809 | ok $replicated->schema->resultset('Artist')->find(1) |
c4d3fae2 |
810 | => 'replicant reactivated'; |
071bbccb |
811 | |
812 | is $debug{storage_type}, 'REPLICANT', "got last query from a replicant: $debug{dsn}"; |
d59cf2f2 |
813 | |
ed213e85 |
814 | ## Test Discard changes |
815 | |
816 | { |
d59cf2f2 |
817 | ok my $artist = $replicated->schema->resultset('Artist')->find(2) |
818 | => 'got an artist to test discard changes'; |
071bbccb |
819 | |
d59cf2f2 |
820 | is $debug{storage_type}, 'REPLICANT', "got last query from a replicant: $debug{dsn}"; |
071bbccb |
821 | |
d59cf2f2 |
822 | ok $artist->get_from_storage({force_pool=>'master'}) |
823 | => 'properly discard changes'; |
071bbccb |
824 | |
d59cf2f2 |
825 | is $debug{storage_type}, 'MASTER', "got last query from a master: $debug{dsn}"; |
071bbccb |
826 | |
3a443bb0 |
827 | ok $artist->discard_changes({force_pool=>'master'}) |
828 | => 'properly called discard_changes against master (manual attrs)'; |
9c169362 |
829 | |
3a443bb0 |
830 | is $debug{storage_type}, 'MASTER', "got last query from a master: $debug{dsn}"; |
9c169362 |
831 | |
3a443bb0 |
832 | ok $artist->discard_changes() |
833 | => 'properly called discard_changes against master (default attrs)'; |
9c169362 |
834 | |
3a443bb0 |
835 | is $debug{storage_type}, 'MASTER', "got last query from a master: $debug{dsn}"; |
8287e9f8 |
836 | |
3a443bb0 |
837 | ok $artist->discard_changes({force_pool=>$replicant_names[0]}) |
838 | => 'properly able to override the default attributes'; |
8287e9f8 |
839 | |
3a443bb0 |
840 | is $debug{storage_type}, 'REPLICANT', "got last query from a replicant: $debug{dsn}" |
ed213e85 |
841 | } |
64cdad22 |
842 | |
843 | ## Test some edge cases, like trying to do a transaction inside a transaction, etc |
844 | |
845 | { |
846 | ok my $result = $replicated->schema->txn_do(sub { |
d59cf2f2 |
847 | return $replicated->schema->txn_do(sub { |
848 | ok my $more = $replicated->schema->resultset('Artist')->find(1) |
849 | => 'found inside a transaction inside a transaction'; |
850 | is $debug{storage_type}, 'MASTER', "got last query from a master: $debug{dsn}"; |
851 | return $more; |
852 | }); |
64cdad22 |
853 | }) => 'successfully processed transaction'; |
d59cf2f2 |
854 | |
64cdad22 |
855 | is $result->id, 1 |
d59cf2f2 |
856 | => 'Got expected single result from transaction'; |
64cdad22 |
857 | } |
858 | |
859 | { |
860 | ok my $result = $replicated->schema->txn_do(sub { |
d59cf2f2 |
861 | return $replicated->schema->storage->execute_reliably(sub { |
862 | return $replicated->schema->txn_do(sub { |
863 | return $replicated->schema->storage->execute_reliably(sub { |
864 | ok my $more = $replicated->schema->resultset('Artist')->find(1) |
865 | => 'found inside crazy deep transactions and execute_reliably'; |
866 | is $debug{storage_type}, 'MASTER', "got last query from a master: $debug{dsn}"; |
867 | return $more; |
868 | }); |
869 | }); |
870 | }); |
64cdad22 |
871 | }) => 'successfully processed transaction'; |
d59cf2f2 |
872 | |
64cdad22 |
873 | is $result->id, 1 |
d59cf2f2 |
874 | => 'Got expected single result from transaction'; |
875 | } |
2ce6e9a6 |
876 | |
7e38d850 |
877 | ## Test the force_pool resultset attribute. |
bbafcf26 |
878 | |
879 | { |
d59cf2f2 |
880 | ok my $artist_rs = $replicated->schema->resultset('Artist') |
bbafcf26 |
881 | => 'got artist resultset'; |
d59cf2f2 |
882 | |
883 | ## Turn on Forced Pool Storage |
884 | ok my $reliable_artist_rs = $artist_rs->search(undef, {force_pool=>'master'}) |
7e38d850 |
885 | => 'Created a resultset using force_pool storage'; |
d59cf2f2 |
886 | |
887 | ok my $artist = $reliable_artist_rs->find(2) |
7e38d850 |
888 | => 'got an artist result via force_pool storage'; |
071bbccb |
889 | |
d59cf2f2 |
890 | is $debug{storage_type}, 'MASTER', "got last query from a master: $debug{dsn}"; |
bbafcf26 |
891 | } |
892 | |
bd5da369 |
893 | ## Test the force_pool resultset attribute part two. |
894 | |
895 | { |
d59cf2f2 |
896 | ok my $artist_rs = $replicated->schema->resultset('Artist') |
bd5da369 |
897 | => 'got artist resultset'; |
d59cf2f2 |
898 | |
899 | ## Turn on Forced Pool Storage |
900 | ok my $reliable_artist_rs = $artist_rs->search(undef, {force_pool=>$replicant_names[0]}) |
bd5da369 |
901 | => 'Created a resultset using force_pool storage'; |
d59cf2f2 |
902 | |
903 | ok my $artist = $reliable_artist_rs->find(2) |
bd5da369 |
904 | => 'got an artist result via force_pool storage'; |
071bbccb |
905 | |
d59cf2f2 |
906 | is $debug{storage_type}, 'REPLICANT', "got last query from a replicant: $debug{dsn}"; |
bd5da369 |
907 | } |
0f83441a |
908 | ## Delete the old database files |
50336325 |
909 | $replicated->cleanup; |
ee356d00 |
910 | |
6988233d |
911 | done_testing; |
912 | |
ee356d00 |
913 | # vim: sw=4 sts=4 : |