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