Commit | Line | Data |
e4dc89b3 |
1 | use strict; |
2 | use warnings; |
3 | use lib qw(t/lib); |
e4dc89b3 |
4 | use Test::More; |
c4d3fae2 |
5 | use Test::Exception; |
857d66ca |
6 | use DBICTest; |
8f7986d6 |
7 | |
86583fa7 |
8 | BEGIN { |
de5dc9ef |
9 | eval "use Moose; use Test::Moose"; |
86583fa7 |
10 | plan $@ |
2bf79155 |
11 | ? ( skip_all => 'needs Moose for testing' ) |
ed213e85 |
12 | : ( tests => 71 ); |
26ab719a |
13 | } |
14 | |
15 | use_ok 'DBIx::Class::Storage::DBI::Replicated::Pool'; |
16 | use_ok 'DBIx::Class::Storage::DBI::Replicated::Balancer'; |
17 | use_ok 'DBIx::Class::Storage::DBI::Replicated::Replicant'; |
18 | use_ok 'DBIx::Class::Storage::DBI::Replicated'; |
0f83441a |
19 | |
89cf6a70 |
20 | =head1 HOW TO USE |
21 | |
22 | This is a test of the replicated storage system. This will work in one of |
23 | two ways, either it was try to fake replication with a couple of SQLite DBs |
24 | and creative use of copy, or if you define a couple of %ENV vars correctly |
25 | will try to test those. If you do that, it will assume the setup is properly |
26 | replicating. Your results may vary, but I have demonstrated this to work with |
27 | mysql native replication. |
28 | |
29 | =cut |
30 | |
31 | |
0f83441a |
32 | ## ---------------------------------------------------------------------------- |
33 | ## Build a class to hold all our required testing data and methods. |
34 | ## ---------------------------------------------------------------------------- |
35 | |
857d66ca |
36 | TESTSCHEMACLASSES: { |
2bf79155 |
37 | |
857d66ca |
38 | ## --------------------------------------------------------------------- ## |
39 | ## Create an object to contain your replicated stuff. |
40 | ## --------------------------------------------------------------------- ## |
41 | |
2bf79155 |
42 | package DBIx::Class::DBI::Replicated::TestReplication; |
43 | |
44 | use DBICTest; |
45 | use base qw/Class::Accessor::Fast/; |
46 | |
857d66ca |
47 | __PACKAGE__->mk_accessors( qw/schema/ ); |
2bf79155 |
48 | |
49 | ## Initialize the object |
50 | |
51 | sub new { |
26ab719a |
52 | my $class = shift @_; |
53 | my $self = $class->SUPER::new(@_); |
2bf79155 |
54 | |
55 | $self->schema( $self->init_schema ); |
2bf79155 |
56 | return $self; |
57 | } |
58 | |
26ab719a |
59 | ## Get the Schema and set the replication storage type |
2bf79155 |
60 | |
61 | sub init_schema { |
62 | my $class = shift @_; |
89cf6a70 |
63 | |
cb6ec758 |
64 | my $schema = DBICTest->init_schema( |
161fb223 |
65 | storage_type=>{ |
106d5f3b |
66 | '::DBI::Replicated' => { |
67 | balancer_type=>'::Random', |
17b05c13 |
68 | balancer_args=>{ |
69 | auto_validate_every=>100, |
70 | }, |
89cf6a70 |
71 | } |
161fb223 |
72 | }, |
89cf6a70 |
73 | deploy_args=>{ |
74 | add_drop_table => 1, |
75 | }, |
76 | ); |
cb6ec758 |
77 | |
2bf79155 |
78 | return $schema; |
79 | } |
26ab719a |
80 | |
857d66ca |
81 | sub generate_replicant_connect_info {} |
82 | sub replicate {} |
83 | sub cleanup {} |
84 | |
85 | |
86 | ## --------------------------------------------------------------------- ## |
87 | ## Subclass for when you are using SQLite for testing, this provides a fake |
88 | ## replication support. |
89 | ## --------------------------------------------------------------------- ## |
90 | |
91 | package DBIx::Class::DBI::Replicated::TestReplication::SQLite; |
92 | |
93 | use DBICTest; |
94 | use File::Copy; |
95 | use base 'DBIx::Class::DBI::Replicated::TestReplication'; |
96 | |
97 | __PACKAGE__->mk_accessors( qw/master_path slave_paths/ ); |
98 | |
99 | ## Set the mastep path from DBICTest |
100 | |
101 | sub new { |
102 | my $class = shift @_; |
103 | my $self = $class->SUPER::new(@_); |
104 | |
105 | $self->master_path( DBICTest->_sqlite_dbfilename ); |
106 | $self->slave_paths([ |
107 | "t/var/DBIxClass_slave1.db", |
108 | "t/var/DBIxClass_slave2.db", |
109 | ]); |
110 | |
111 | return $self; |
112 | } |
113 | |
26ab719a |
114 | ## Return an Array of ArrayRefs where each ArrayRef is suitable to use for |
115 | ## $storage->connect_info to be used for connecting replicants. |
116 | |
117 | sub generate_replicant_connect_info { |
857d66ca |
118 | my $self = shift @_; |
26ab719a |
119 | my @dsn = map { |
120 | "dbi:SQLite:${_}"; |
121 | } @{$self->slave_paths}; |
122 | |
857d66ca |
123 | return map { [$_,'','',{AutoCommit=>1}] } @dsn; |
26ab719a |
124 | } |
125 | |
126 | ## Do a 'good enough' replication by copying the master dbfile over each of |
50336325 |
127 | ## the slave dbfiles. If the master is SQLite we do this, otherwise we |
128 | ## just do a one second pause to let the slaves catch up. |
26ab719a |
129 | |
130 | sub replicate { |
131 | my $self = shift @_; |
132 | foreach my $slave (@{$self->slave_paths}) { |
133 | copy($self->master_path, $slave); |
134 | } |
135 | } |
136 | |
137 | ## Cleanup after ourselves. Unlink all gthe slave paths. |
138 | |
139 | sub cleanup { |
140 | my $self = shift @_; |
141 | foreach my $slave (@{$self->slave_paths}) { |
142 | unlink $slave; |
143 | } |
144 | } |
857d66ca |
145 | |
146 | ## --------------------------------------------------------------------- ## |
147 | ## Subclass for when you are setting the databases via custom export vars |
148 | ## This is for when you have a replicating database setup that you are |
149 | ## going to test against. You'll need to define the correct $ENV and have |
150 | ## two slave databases to test against, as well as a replication system |
151 | ## that will replicate in less than 1 second. |
152 | ## --------------------------------------------------------------------- ## |
153 | |
154 | package DBIx::Class::DBI::Replicated::TestReplication::Custom; |
155 | use base 'DBIx::Class::DBI::Replicated::TestReplication'; |
156 | |
157 | ## Return an Array of ArrayRefs where each ArrayRef is suitable to use for |
158 | ## $storage->connect_info to be used for connecting replicants. |
159 | |
160 | sub generate_replicant_connect_info { |
161 | return ( |
162 | [$ENV{"DBICTEST_SLAVE0_DSN"}, $ENV{"DBICTEST_SLAVE0_DBUSER"}, $ENV{"DBICTEST_SLAVE0_DBPASS"}, {AutoCommit => 1}], |
163 | [$ENV{"DBICTEST_SLAVE1_DSN"}, $ENV{"DBICTEST_SLAVE1_DBUSER"}, $ENV{"DBICTEST_SLAVE1_DBPASS"}, {AutoCommit => 1}], |
164 | ); |
165 | } |
166 | |
167 | ## pause a bit to let the replication catch up |
168 | |
169 | sub replicate { |
170 | sleep 1; |
171 | } |
2bf79155 |
172 | } |
173 | |
174 | ## ---------------------------------------------------------------------------- |
175 | ## Create an object and run some tests |
176 | ## ---------------------------------------------------------------------------- |
177 | |
26ab719a |
178 | ## Thi first bunch of tests are basic, just make sure all the bits are behaving |
2bf79155 |
179 | |
857d66ca |
180 | my $replicated_class = DBICTest->has_custom_dsn ? |
181 | 'DBIx::Class::DBI::Replicated::TestReplication::Custom' : |
182 | 'DBIx::Class::DBI::Replicated::TestReplication::SQLite'; |
183 | |
184 | ok my $replicated = $replicated_class->new |
185 | => 'Created a replication object'; |
2bf79155 |
186 | |
26ab719a |
187 | isa_ok $replicated->schema |
2bf79155 |
188 | => 'DBIx::Class::Schema'; |
189 | |
26ab719a |
190 | isa_ok $replicated->schema->storage |
191 | => 'DBIx::Class::Storage::DBI::Replicated'; |
192 | |
193 | ok $replicated->schema->storage->meta |
194 | => 'has a meta object'; |
195 | |
196 | isa_ok $replicated->schema->storage->master |
197 | => 'DBIx::Class::Storage::DBI'; |
198 | |
199 | isa_ok $replicated->schema->storage->pool |
200 | => 'DBIx::Class::Storage::DBI::Replicated::Pool'; |
201 | |
17b05c13 |
202 | does_ok $replicated->schema->storage->balancer |
26ab719a |
203 | => 'DBIx::Class::Storage::DBI::Replicated::Balancer'; |
204 | |
205 | ok my @replicant_connects = $replicated->generate_replicant_connect_info |
206 | => 'got replication connect information'; |
207 | |
955a6df6 |
208 | ok my @replicated_storages = $replicated->schema->storage->connect_replicants(@replicant_connects) |
26ab719a |
209 | => 'Created some storages suitable for replicants'; |
210 | |
cb6ec758 |
211 | isa_ok $replicated->schema->storage->balancer->current_replicant |
26ab719a |
212 | => 'DBIx::Class::Storage::DBI'; |
213 | |
214 | ok $replicated->schema->storage->pool->has_replicants |
215 | => 'does have replicants'; |
216 | |
17b05c13 |
217 | is $replicated->schema->storage->pool->num_replicants => 2 |
26ab719a |
218 | => 'has two replicants'; |
219 | |
de5dc9ef |
220 | does_ok $replicated_storages[0] |
26ab719a |
221 | => 'DBIx::Class::Storage::DBI::Replicated::Replicant'; |
222 | |
de5dc9ef |
223 | does_ok $replicated_storages[1] |
26ab719a |
224 | => 'DBIx::Class::Storage::DBI::Replicated::Replicant'; |
225 | |
89cf6a70 |
226 | my @replicant_names = keys %{$replicated->schema->storage->replicants}; |
de5dc9ef |
227 | |
228 | does_ok $replicated->schema->storage->replicants->{$replicant_names[0]} |
26ab719a |
229 | => 'DBIx::Class::Storage::DBI::Replicated::Replicant'; |
230 | |
de5dc9ef |
231 | does_ok $replicated->schema->storage->replicants->{$replicant_names[1]} |
26ab719a |
232 | => 'DBIx::Class::Storage::DBI::Replicated::Replicant'; |
233 | |
234 | ## Add some info to the database |
235 | |
236 | $replicated |
237 | ->schema |
238 | ->populate('Artist', [ |
239 | [ qw/artistid name/ ], |
240 | [ 4, "Ozric Tentacles"], |
241 | ]); |
242 | |
243 | ## Make sure all the slaves have the table definitions |
244 | |
245 | $replicated->replicate; |
5c1d82d2 |
246 | $replicated->schema->storage->replicants->{$replicant_names[0]}->active(1); |
247 | $replicated->schema->storage->replicants->{$replicant_names[1]}->active(1); |
26ab719a |
248 | |
249 | ## Make sure we can read the data. |
250 | |
251 | ok my $artist1 = $replicated->schema->resultset('Artist')->find(4) |
252 | => 'Created Result'; |
253 | |
254 | isa_ok $artist1 |
255 | => 'DBICTest::Artist'; |
256 | |
257 | is $artist1->name, 'Ozric Tentacles' |
258 | => 'Found expected name for first result'; |
259 | |
260 | ## Add some new rows that only the master will have This is because |
261 | ## we overload any type of write operation so that is must hit the master |
262 | ## database. |
263 | |
264 | $replicated |
265 | ->schema |
266 | ->populate('Artist', [ |
267 | [ qw/artistid name/ ], |
268 | [ 5, "Doom's Children"], |
269 | [ 6, "Dead On Arrival"], |
270 | [ 7, "Watergate"], |
271 | ]); |
272 | |
89cf6a70 |
273 | SKIP: { |
274 | ## We can't do this test if we have a custom replicants, since we assume |
275 | ## if there are custom one that you are trying to test a real replicating |
276 | ## system. See docs above for more. |
277 | |
278 | skip 'Cannot test inconsistent replication since you have a real replication system', 1 |
279 | if DBICTest->has_custom_dsn && $ENV{"DBICTEST_SLAVE0_DSN"}; |
280 | |
281 | ## Alright, the database 'cluster' is not in a consistent state. When we do |
282 | ## a read now we expect bad news |
283 | is $replicated->schema->resultset('Artist')->find(5), undef |
284 | => 'read after disconnect fails because it uses a replicant which we have neglected to "replicate" yet'; |
285 | } |
26ab719a |
286 | |
287 | ## Make sure all the slaves have the table definitions |
288 | $replicated->replicate; |
289 | |
290 | ## Should find some data now |
291 | |
292 | ok my $artist2 = $replicated->schema->resultset('Artist')->find(5) |
293 | => 'Sync succeed'; |
294 | |
295 | isa_ok $artist2 |
296 | => 'DBICTest::Artist'; |
297 | |
298 | is $artist2->name, "Doom's Children" |
299 | => 'Found expected name for first result'; |
300 | |
301 | ## What happens when we disconnect all the replicants? |
302 | |
50336325 |
303 | is $replicated->schema->storage->pool->connected_replicants => 2 |
304 | => "both replicants are connected"; |
305 | |
89cf6a70 |
306 | $replicated->schema->storage->replicants->{$replicant_names[0]}->disconnect; |
307 | $replicated->schema->storage->replicants->{$replicant_names[1]}->disconnect; |
26ab719a |
308 | |
50336325 |
309 | is $replicated->schema->storage->pool->connected_replicants => 0 |
310 | => "both replicants are now disconnected"; |
311 | |
312 | ## All these should pass, since the database should automatically reconnect |
313 | |
26ab719a |
314 | ok my $artist3 = $replicated->schema->resultset('Artist')->find(6) |
315 | => 'Still finding stuff.'; |
2bf79155 |
316 | |
26ab719a |
317 | isa_ok $artist3 |
318 | => 'DBICTest::Artist'; |
2bf79155 |
319 | |
26ab719a |
320 | is $artist3->name, "Dead On Arrival" |
321 | => 'Found expected name for first result'; |
2bf79155 |
322 | |
50336325 |
323 | is $replicated->schema->storage->pool->connected_replicants => 1 |
324 | => "One replicant reconnected to handle the job"; |
6f6fb437 |
325 | |
326 | ## What happens when we try to select something that doesn't exist? |
327 | |
328 | ok ! $replicated->schema->resultset('Artist')->find(666) |
329 | => 'Correctly failed to find something.'; |
cb6ec758 |
330 | |
331 | ## test the reliable option |
332 | |
333 | TESTRELIABLE: { |
334 | |
335 | $replicated->schema->storage->set_reliable_storage; |
336 | |
337 | ok $replicated->schema->resultset('Artist')->find(2) |
338 | => 'Read from master 1'; |
339 | |
340 | ok $replicated->schema->resultset('Artist')->find(5) |
341 | => 'Read from master 2'; |
342 | |
343 | $replicated->schema->storage->set_balanced_storage; |
344 | |
345 | ok $replicated->schema->resultset('Artist')->find(3) |
9c748388 |
346 | => 'Read from replicant'; |
cb6ec758 |
347 | } |
348 | |
9c748388 |
349 | ## Make sure when reliable goes out of scope, we are using replicants again |
cb6ec758 |
350 | |
351 | ok $replicated->schema->resultset('Artist')->find(1) |
352 | => 'back to replicant 1.'; |
353 | |
354 | ok $replicated->schema->resultset('Artist')->find(2) |
355 | => 'back to replicant 2.'; |
2156bbdd |
356 | |
106d5f3b |
357 | ## set all the replicants to inactive, and make sure the balancer falls back to |
358 | ## the master. |
359 | |
89cf6a70 |
360 | $replicated->schema->storage->replicants->{$replicant_names[0]}->active(0); |
361 | $replicated->schema->storage->replicants->{$replicant_names[1]}->active(0); |
106d5f3b |
362 | |
363 | ok $replicated->schema->resultset('Artist')->find(2) |
de5dc9ef |
364 | => 'Fallback to master'; |
365 | |
366 | $replicated->schema->storage->replicants->{$replicant_names[0]}->active(1); |
367 | $replicated->schema->storage->replicants->{$replicant_names[1]}->active(1); |
368 | |
369 | ok $replicated->schema->resultset('Artist')->find(2) |
370 | => 'Returned to replicates'; |
371 | |
372 | ## Getting slave status tests |
373 | |
f797e89e |
374 | SKIP: { |
375 | ## We skip this tests unless you have a custom replicants, since the default |
376 | ## sqlite based replication tests don't support these functions. |
377 | |
17b05c13 |
378 | skip 'Cannot Test Replicant Status on Non Replicating Database', 9 |
f797e89e |
379 | unless DBICTest->has_custom_dsn && $ENV{"DBICTEST_SLAVE0_DSN"}; |
380 | |
381 | $replicated->replicate; ## Give the slaves a chance to catchup. |
382 | |
383 | ok $replicated->schema->storage->replicants->{$replicant_names[0]}->is_replicating |
384 | => 'Replicants are replicating'; |
385 | |
386 | is $replicated->schema->storage->replicants->{$replicant_names[0]}->lag_behind_master, 0 |
387 | => 'Replicant is zero seconds behind master'; |
7edf5f1c |
388 | |
389 | ## Test the validate replicants |
390 | |
391 | $replicated->schema->storage->pool->validate_replicants; |
392 | |
393 | is $replicated->schema->storage->pool->active_replicants, 2 |
394 | => 'Still have 2 replicants after validation'; |
395 | |
396 | ## Force the replicants to fail the validate test by required their lag to |
397 | ## be negative (ie ahead of the master!) |
398 | |
399 | $replicated->schema->storage->pool->maximum_lag(-10); |
400 | $replicated->schema->storage->pool->validate_replicants; |
401 | |
402 | is $replicated->schema->storage->pool->active_replicants, 0 |
403 | => 'No way a replicant be be ahead of the master'; |
404 | |
405 | ## Let's be fair to the replicants again. Let them lag up to 5 |
406 | |
407 | $replicated->schema->storage->pool->maximum_lag(5); |
408 | $replicated->schema->storage->pool->validate_replicants; |
409 | |
410 | is $replicated->schema->storage->pool->active_replicants, 2 |
411 | => 'Both replicants in good standing again'; |
17b05c13 |
412 | |
413 | ## Check auto validate |
414 | |
415 | is $replicated->schema->storage->balancer->auto_validate_every, 100 |
416 | => "Got the expected value for auto validate"; |
417 | |
418 | ## This will make sure we auto validatge everytime |
419 | $replicated->schema->storage->balancer->auto_validate_every(0); |
420 | |
421 | ## set all the replicants to inactive, and make sure the balancer falls back to |
422 | ## the master. |
423 | |
424 | $replicated->schema->storage->replicants->{$replicant_names[0]}->active(0); |
425 | $replicated->schema->storage->replicants->{$replicant_names[1]}->active(0); |
426 | |
427 | ## Ok, now when we go to run a query, autovalidate SHOULD reconnect |
428 | |
429 | is $replicated->schema->storage->pool->active_replicants => 0 |
430 | => "both replicants turned off"; |
431 | |
432 | ok $replicated->schema->resultset('Artist')->find(5) |
433 | => 'replicant reactivated'; |
434 | |
435 | is $replicated->schema->storage->pool->active_replicants => 2 |
436 | => "both replicants reactivated"; |
f797e89e |
437 | } |
438 | |
c4d3fae2 |
439 | ## Test the reliably callback |
440 | |
441 | ok my $reliably = sub { |
442 | |
443 | ok $replicated->schema->resultset('Artist')->find(5) |
444 | => 'replicant reactivated'; |
445 | |
446 | } => 'created coderef properly'; |
447 | |
448 | $replicated->schema->storage->execute_reliably($reliably); |
449 | |
450 | ## Try something with an error |
451 | |
452 | ok my $unreliably = sub { |
453 | |
454 | ok $replicated->schema->resultset('ArtistXX')->find(5) |
455 | => 'replicant reactivated'; |
456 | |
457 | } => 'created coderef properly'; |
458 | |
459 | throws_ok {$replicated->schema->storage->execute_reliably($unreliably)} |
ed213e85 |
460 | qr/Can't find source for ArtistXX/ |
c4d3fae2 |
461 | => 'Bad coderef throws proper error'; |
462 | |
ed213e85 |
463 | ## Make sure replication came back |
464 | |
465 | ok $replicated->schema->resultset('Artist')->find(3) |
466 | => 'replicant reactivated'; |
467 | |
c4d3fae2 |
468 | ## make sure transactions are set to execute_reliably |
469 | |
470 | ok my $transaction = sub { |
471 | |
ed213e85 |
472 | my $id = shift @_; |
473 | |
c4d3fae2 |
474 | $replicated |
475 | ->schema |
476 | ->populate('Artist', [ |
477 | [ qw/artistid name/ ], |
ed213e85 |
478 | [ $id, "Children of the Grave"], |
c4d3fae2 |
479 | ]); |
480 | |
ed213e85 |
481 | ok my $result = $replicated->schema->resultset('Artist')->find($id); |
482 | ok my $more = $replicated->schema->resultset('Artist')->find(1); |
483 | |
484 | return ($result, $more); |
c4d3fae2 |
485 | |
486 | }; |
487 | |
ed213e85 |
488 | ## Test the transaction with multi return |
489 | { |
490 | ok my @return = $replicated->schema->txn_do($transaction, 666) |
491 | => 'did transaction'; |
492 | |
493 | is $return[0]->id, 666 |
494 | => 'first returned value is correct'; |
495 | |
496 | is $return[1]->id, 1 |
497 | => 'second returned value is correct'; |
498 | } |
499 | |
500 | ## Test that asking for single return works |
501 | { |
502 | ok my $return = $replicated->schema->txn_do($transaction, 777) |
503 | => 'did transaction'; |
504 | |
505 | is $return->id, 777 |
506 | => 'first returned value is correct'; |
507 | } |
508 | |
509 | ## Test transaction returning a single value |
510 | |
511 | { |
512 | ok my $result = $replicated->schema->txn_do(sub { |
513 | ok my $more = $replicated->schema->resultset('Artist')->find(1); |
514 | return $more; |
515 | }) => 'successfully processed transaction'; |
516 | |
517 | is $result->id, 1 |
518 | => 'Got expected single result from transaction'; |
519 | } |
c4d3fae2 |
520 | |
521 | ## Make sure replication came back |
522 | |
ed213e85 |
523 | ok $replicated->schema->resultset('Artist')->find(1) |
c4d3fae2 |
524 | => 'replicant reactivated'; |
ed213e85 |
525 | |
526 | ## Test Discard changes |
527 | |
528 | { |
529 | ok my $artist = $replicated->schema->resultset('Artist')->find(2) |
530 | => 'got an artist to test discard changes'; |
531 | |
532 | ok $artist->discard_changes |
533 | => 'properly discard changes'; |
534 | } |
c4d3fae2 |
535 | |
0f83441a |
536 | ## Delete the old database files |
50336325 |
537 | $replicated->cleanup; |
0f83441a |
538 | |
539 | |
540 | |
541 | |
542 | |
543 | |