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