summaryrefslogtreecommitdiff
path: root/src/test/java/com/amazon/carbonado/repo/replicated/TestRepair.java
blob: 6e3a7eec5fa2643c584f71004871f883e1d88042 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
/*
 * Copyright 2006-2012 Amazon Technologies, Inc. or its affiliates.
 * Amazon, Amazon.com and Carbonado are trademarks or registered trademarks
 * of Amazon Technologies, Inc. or its affiliates.  All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.amazon.carbonado.repo.replicated;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamClass;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import junit.framework.TestCase;
import junit.framework.TestSuite;

import com.amazon.carbonado.CorruptEncodingException;
import com.amazon.carbonado.Cursor;
import com.amazon.carbonado.OptimisticLockException;
import com.amazon.carbonado.PersistException;
import com.amazon.carbonado.Repository;
import com.amazon.carbonado.RepositoryBuilder;
import com.amazon.carbonado.Storable;
import com.amazon.carbonado.Storage;
import com.amazon.carbonado.Trigger;
import com.amazon.carbonado.UniqueConstraintException;

import com.amazon.carbonado.capability.ResyncCapability;

import com.amazon.carbonado.layout.StoredLayout;
import com.amazon.carbonado.layout.StoredLayoutProperty;

import com.amazon.carbonado.repo.replicated.ReplicatedRepository;
import com.amazon.carbonado.repo.replicated.ReplicatedRepositoryBuilder;

import com.amazon.carbonado.repo.sleepycat.BDBRepositoryBuilder;

import com.amazon.carbonado.TestUtilities;
import com.amazon.carbonado.stored.StorableTestBasic;
import com.amazon.carbonado.stored.StorableTestMinimal;
import com.amazon.carbonado.stored.StorableVersioned;

import com.amazon.carbonado.layout.TestLayout;
import org.cojen.classfile.TypeDesc;

/**
 *
 *
 * @author Brian S O'Neill
 */
public class TestRepair extends TestCase {
    private static final String REPLICA_NAME = "rr-replica";
    private static final String MASTER_NAME = "rr-master";

    public static void main(String[] args) {
        junit.textui.TestRunner.run(suite());
    }

    public static TestSuite suite() {
        return new TestSuite(TestRepair.class);
    }

    private Repository mReplica;
    private Repository mMaster;
    private Repository mReplicated;

    public TestRepair(String name) {
        super(name);
    }

    protected void setUp() throws Exception {
        RepositoryBuilder replica = TestUtilities.newTempRepositoryBuilder(REPLICA_NAME);
        RepositoryBuilder master = TestUtilities.newTempRepositoryBuilder(MASTER_NAME);

        ReplicatedRepositoryBuilder builder = new ReplicatedRepositoryBuilder();
        builder.setName("rr");
        builder.setReplicaRepositoryBuilder(replica);
        builder.setMasterRepositoryBuilder(master);

        ReplicatedRepository rr = (ReplicatedRepository) builder.build();

        mReplica = rr.getReplicaRepository();
        mMaster = rr.getMasterRepository();
        mReplicated = rr;
    }

    protected void tearDown() throws Exception {
        if (mReplicated != null) {
            mReplicated.close();
        }
        if (mReplica != null) {
            mReplica.close();
        }
        if (mMaster != null) {
            mMaster.close();
        }
        mReplica = null;
        mMaster = null;
        mReplicated = null;
    }

    /**
     * @return repository locations
     */
    private String[] reOpenPersistent(String[] locations) throws Exception {
        tearDown();

        String replicaLocation, masterLocation;

        if (locations != null) {
            replicaLocation = locations[0];
            masterLocation = locations[1];
        } else {
            replicaLocation = TestUtilities.makeTestDirectoryString(REPLICA_NAME);
            masterLocation = TestUtilities.makeTestDirectoryString(MASTER_NAME);
        }

        {
            BDBRepositoryBuilder replica = new BDBRepositoryBuilder();
            replica.setName(REPLICA_NAME);
            replica.setTransactionNoSync(true);
            replica.setEnvironmentHome(replicaLocation);

            BDBRepositoryBuilder master = new BDBRepositoryBuilder();
            master.setName(MASTER_NAME);
            master.setTransactionNoSync(true);
            master.setEnvironmentHome(masterLocation);

            ReplicatedRepositoryBuilder builder = new ReplicatedRepositoryBuilder();
            builder.setName("rr");
            builder.setReplicaRepositoryBuilder(replica);
            builder.setMasterRepositoryBuilder(master);

            ReplicatedRepository rr = (ReplicatedRepository) builder.build();

            mReplica = rr.getReplicaRepository();
            mMaster = rr.getMasterRepository();
            mReplicated = rr;
        }

        return new String[] {replicaLocation, masterLocation};
    }

    public void testMissingEntry() throws Exception {
        // Insert an entry into master.
        {
            Storage<StorableTestBasic> storage = mMaster.storageFor(StorableTestBasic.class);
            StorableTestBasic stb = storage.prepare();
            stb.setId(5);
            stb.setStringProp("hello");
            stb.setIntProp(1);
            stb.setLongProp(1L);
            stb.setDoubleProp(1.0);
            stb.insert();
        }

        // Verify not available from rr.
        {
            Storage<StorableTestBasic> storage = mReplicated.storageFor(StorableTestBasic.class);
            StorableTestBasic stb = storage.prepare();
            stb.setId(5);
            assertFalse(stb.tryLoad());
        }

        // Insert into rr.
        {
            Storage<StorableTestBasic> storage = mReplicated.storageFor(StorableTestBasic.class);
            StorableTestBasic stb = storage.prepare();
            stb.setId(5);
            stb.setStringProp("world");
            stb.setIntProp(1);
            stb.setLongProp(1L);
            stb.setDoubleProp(1.0);
            try {
                stb.insert();
                fail();
            } catch (UniqueConstraintException e) {
            }
        }

        // Wait a moment for repair thread to finish.
        Thread.sleep(1000);

        // Verify available from rr.
        {
            Storage<StorableTestBasic> storage = mReplicated.storageFor(StorableTestBasic.class);
            StorableTestBasic stb = storage.prepare();
            stb.setId(5);
            assertTrue(stb.tryLoad());
            assertEquals("hello", stb.getStringProp());
        }
    }

    public void testMissingVersionedEntry() throws Exception {
        // Insert an entry into master.
        {
            Storage<StorableVersioned> storage = mMaster.storageFor(StorableVersioned.class);
            StorableVersioned sv = storage.prepare();
            sv.setID(5);
            sv.setValue("hello");
            sv.insert();
        }

        // Verify not available from rr.
        {
            Storage<StorableVersioned> storage = mReplicated.storageFor(StorableVersioned.class);
            StorableVersioned sv = storage.prepare();
            sv.setID(5);
            assertFalse(sv.tryLoad());
        }

        // Insert into rr.
        {
            Storage<StorableVersioned> storage = mReplicated.storageFor(StorableVersioned.class);
            StorableVersioned sv = storage.prepare();
            sv.setID(5);
            sv.setValue("world");
            try {
                sv.insert();
                fail();
            } catch (UniqueConstraintException e) {
            }
        }

        // Wait a moment for repair thread to finish.
        Thread.sleep(1000);

        // Verify available from rr.
        {
            Storage<StorableVersioned> storage = mReplicated.storageFor(StorableVersioned.class);
            StorableVersioned sv = storage.prepare();
            sv.setID(5);
            assertTrue(sv.tryLoad());
            assertEquals("hello", sv.getValue());
            assertEquals(1, sv.getVersion());
        }
    }

    public void testStaleEntry() throws Exception {
        // Insert an entry into rr.
        {
            Storage<StorableVersioned> storage = mReplicated.storageFor(StorableVersioned.class);
            StorableVersioned sv = storage.prepare();
            sv.setID(5);
            sv.setValue("hello");
            sv.insert();
        }

        // Update master entry.
        {
            Storage<StorableVersioned> storage = mMaster.storageFor(StorableVersioned.class);
            StorableVersioned sv = storage.prepare();
            sv.setID(5);
            sv.load();
            sv.setValue("world");
            sv.update();
        }

        // Verify old version in replica.
        {
            Storage<StorableVersioned> storage = mReplica.storageFor(StorableVersioned.class);
            StorableVersioned sv = storage.prepare();
            sv.setID(5);
            sv.load();
            assertEquals(1, sv.getVersion());
            assertEquals("hello", sv.getValue());
        }

        // Attempt to update rr entry.
        {
            Storage<StorableVersioned> storage = mReplicated.storageFor(StorableVersioned.class);
            StorableVersioned sv = storage.prepare();
            sv.setID(5);
            sv.load();
            assertEquals(1, sv.getVersion());
            assertEquals("hello", sv.getValue());

            sv.setValue("ciao");
            try {
                sv.update();
                fail();
            } catch (OptimisticLockException e) {
            }
        }

        // Wait a moment for repair thread to finish.
        Thread.sleep(1000);

        // Verify new version in rr and update it.
        {
            Storage<StorableVersioned> storage = mReplicated.storageFor(StorableVersioned.class);
            StorableVersioned sv = storage.prepare();
            sv.setID(5);
            sv.load();
            assertEquals(2, sv.getVersion());
            assertEquals("world", sv.getValue());

            sv.setValue("ciao");
            sv.update();
        }
    }

    public void testStaleEntryAndBackoff() throws Exception {
        // Insert an entry into rr.
        {
            Storage<StorableVersioned> storage = mReplicated.storageFor(StorableVersioned.class);
            StorableVersioned sv = storage.prepare();
            sv.setID(5);
            sv.setValue("hello");
            sv.insert();
        }

        // Update master entry.
        {
            Storage<StorableVersioned> storage = mMaster.storageFor(StorableVersioned.class);
            StorableVersioned sv = storage.prepare();
            sv.setID(5);
            sv.load();
            sv.setValue("world");
            sv.update();
        }

        // Attempt to update rr entry.
        {
            Storage<StorableVersioned> storage = mReplicated.storageFor(StorableVersioned.class);
            StorableVersioned sv = storage.prepare();
            sv.setID(5);

            int failCount = 0;

            for (int retryCount = 3;;) {
                try {
                    sv.load();
                    sv.setValue("ciao");
                    sv.update();
                    break;
                } catch (OptimisticLockException e) {
                    failCount++;
                    retryCount = e.backoff(e, retryCount, 1000);
                }
            }

            assertTrue(failCount > 0);
        }

        // Verify new version in rr.
        {
            Storage<StorableVersioned> storage = mReplicated.storageFor(StorableVersioned.class);
            StorableVersioned sv = storage.prepare();
            sv.setID(5);
            sv.load();
            assertEquals(3, sv.getVersion());
            assertEquals("ciao", sv.getValue());
        }
    }

    public void testDeletedEntry() throws Exception {
        // Insert some entries into rr.
        for (int i=0; i<10; i++) {
            Storage<StorableTestBasic> storage = mReplicated.storageFor(StorableTestBasic.class);
            StorableTestBasic stb = storage.prepare();
            stb.setId(i);
            stb.setStringProp("hello");
            stb.setIntProp(1);
            stb.setLongProp(1L);
            stb.setDoubleProp(1.0);
            stb.insert();
        }

        // Delete on record from from master.
        {
            Storage<StorableTestBasic> storage = mMaster.storageFor(StorableTestBasic.class);
            StorableTestBasic stb = storage.prepare();
            stb.setId(5);
            stb.delete();
        }

        // Verify record is still available from rr.
        {
            Storage<StorableTestBasic> storage = mReplicated.storageFor(StorableTestBasic.class);
            StorableTestBasic stb = storage.prepare();
            stb.setId(5);
            assertTrue(stb.tryLoad());
            assertEquals("hello", stb.getStringProp());
        }

        ResyncCapability cap = mReplicated.getCapability(ResyncCapability.class);

        cap.resync(StorableTestBasic.class, 1.0, "id=?", 5);

        // Verify record is not available from rr anymore.
        {
            Storage<StorableTestBasic> storage = mReplicated.storageFor(StorableTestBasic.class);
            StorableTestBasic stb = storage.prepare();
            stb.setId(5);
            assertFalse(stb.tryLoad());
        }
    }

    public void testCorruptEntry() throws Exception {
        testCorruptEntry(false, false);
    }

    public void testCorruptEntryPreventDelete() throws Exception {
        testCorruptEntry(false, true);
    }

    public void testIndividualCorruptEntry() throws Exception {
        testCorruptEntry(true, false);
    }

    private void testCorruptEntry(boolean individualRepair, boolean preventDelete)
        throws Exception
    {
        // Close and open repository again, this time on disk. We need to close
        // and re-open the repository as part of the test.
        String[] locations = reOpenPersistent(null);

        // Insert different versions of the same record...

        final String recordName = "test.TheTestRecord";

        final Class<? extends StorableTestMinimal> type0 = 
            TestLayout.defineStorable(recordName, 1, TypeDesc.INT);

        Class<? extends StorableTestMinimal> type1 = 
            TestLayout.defineStorable(recordName, 2, TypeDesc.INT);

        Storage<? extends StorableTestMinimal> storage0 = mReplicated.storageFor(type0);
        Storage<? extends StorableTestMinimal> storage1 = mReplicated.storageFor(type1);

        final int seed = 5469232;
        final int count = 20;

        {
            Random rnd = new Random(seed);

            Method prop_0_of_0 = type0.getMethod("setProp0", int.class);

            Method prop_0_of_1 = type1.getMethod("setProp0", int.class);
            Method prop_1_of_1 = type1.getMethod("setProp1", int.class);

            boolean anyType0 = false;
            boolean anyType1 = false;

            for (int i=0; i<count; i++) {
                StorableTestMinimal stm;

                if (rnd.nextBoolean()) {
                    stm = storage0.prepare();
                    prop_0_of_0.invoke(stm, i + 1000);
                    anyType0 = true;
                } else {
                    stm = storage1.prepare();
                    prop_0_of_1.invoke(stm, i + 2000);
                    prop_1_of_1.invoke(stm, i + 4000);
                    anyType1 = true;
                }

                stm.setId(i);
                stm.insert();
            }

            // Assert mix of types.
            assertTrue(anyType0);
            assertTrue(anyType1);
        }

        // Verify records can be read via storage0, which will ignore the new property.
        {
            Cursor<? extends StorableTestMinimal> cursor = storage0.query().fetch();
            while (cursor.hasNext()) {
                StorableTestMinimal stm = cursor.next();
                //System.out.println(stm);
            }
        }
        
        // Verify records can be read via storage1, which may have zero for the new property.
        {
            Cursor<? extends StorableTestMinimal> cursor = storage1.query().fetch();
            while (cursor.hasNext()) {
                StorableTestMinimal stm = cursor.next();
                //System.out.println(stm);
            }
        }

        // Close and open only replica repository and create corruption by
        // deleting all information regarding generation 1 in the replica.
        locations = reOpenPersistent(locations);

        storage0 = mReplicated.storageFor(type0);

        // Replace all the masters with only type 0 records.
        {
            mMaster.storageFor(type0).query().deleteAll();
            Method prop_0_of_0 = type0.getMethod("setProp0", int.class);

            for (int i=0; i<count; i++) {
                StorableTestMinimal stm = mMaster.storageFor(type0).prepare();
                prop_0_of_0.invoke(stm, i + 1000);
                stm.setId(i);
                stm.insert();
            }
        }

        // Delete all knowledge of type 1.
        StoredLayout layout = mReplicated.storageFor(StoredLayout.class).prepare();
        layout.setStorableTypeName(recordName);
        layout.setGeneration(1);
        layout.load();
        layout.delete();

        mReplicated.storageFor(StoredLayoutProperty.class)
            .query("layoutID = ?").with(layout.getLayoutID()).deleteAll();

        // Close and open to rebuild replicated repository.
        locations = reOpenPersistent(locations);

        storage0 = mReplicated.storageFor(type0);

        // Verify corruption. (Replica is unable to figure out what layout generation 1 is)
        try {
            Cursor<? extends StorableTestMinimal> cursor = storage0.query().fetch();
            while (cursor.hasNext()) {
                StorableTestMinimal stm = cursor.next();
            }
            fail();
        } catch (CorruptEncodingException e) {
            // Verify serialization of primary key storable.
            assertNotNull(e.getStorableWithPrimaryKey());
            ByteArrayOutputStream bout = new ByteArrayOutputStream();
            ObjectOutputStream out = new ObjectOutputStream(bout);
            out.writeObject(e);
            out.close();
            byte[] bytes = bout.toByteArray();

            ByteArrayInputStream bin = new ByteArrayInputStream(bytes);

            ObjectInputStream in = new ObjectInputStream(bin) {
                // Special handling to load generated class.
                @Override
                protected Class<?> resolveClass(ObjectStreamClass desc)
                    throws IOException, ClassNotFoundException
                {
                    if (desc.getName().equals(recordName)) {
                        return type0;
                    }
                    return super.resolveClass(desc);
                }
            };

            CorruptEncodingException e2 = (CorruptEncodingException) in.readObject();

            Storable s1 = e.getStorableWithPrimaryKey();
            Storable s2 = e2.getStorableWithPrimaryKey();

            assertFalse(s1 == s2);
            assertTrue(s1.equalPrimaryKeys(s2));
            assertTrue(s2.equalPrimaryKeys(s1));
        }

        // Resync to repair.

        class Prevent extends Trigger {
            volatile boolean didRun;

            @Override
            public Object beforeDelete(Object s) throws PersistException {
                didRun = true;
                throw new PersistException("Cannot delete me!");
            }
        }

        Prevent prevent = null;
        if (preventDelete) {
            // This is a partially vestigial test. It used to be the case that
            // triggers would run during a resync or replication repair. This
            // is no longer the case. Instead, use this as an opportunity to
            // ensure the trigger does not run.
            prevent = new Prevent();
            storage0.addTrigger(prevent);
        }

        ResyncCapability cap = mReplicated.getCapability(ResyncCapability.class);

        if (individualRepair) {
            for (int i=0; i<count; i++) {
                cap.resync(type0, 1.0, "id=?", i);
            }
        } else {
            cap.resync(type0, 1.0, null);
        }

        if (preventDelete) {
            // Again, this is a partially vestigial test. The trigger should
            // not have run at all during the resync.
            assertFalse(prevent.didRun);
            storage0.removeTrigger(prevent);
            cap.resync(type0, 1.0, null);
        }

        {
            // Verify records can be read out now.
            Cursor<? extends StorableTestMinimal> cursor = storage0.query().fetch();
            int actual = 0;
            while (cursor.hasNext()) {
                StorableTestMinimal stm = cursor.next();
                //System.out.println(stm);
                actual++;
            }
            assertEquals(count, actual);
        }

        storage1 = mReplicated.storageFor(type1);

        {
            Cursor<? extends StorableTestMinimal> cursor = storage1.query().fetch();
            int actual = 0;
            while (cursor.hasNext()) {
                StorableTestMinimal stm = cursor.next();
                //System.out.println(stm);
                actual++;
            }
            assertEquals(count, actual);
        }
    }

    public void testResyncListener() throws Exception {
        prepareOutOfSyncEntries();

        final List<Storable> inserted = new ArrayList<Storable>();
        final List<Storable[]> updated = new ArrayList<Storable[]>();
        final List<Storable> deleted = new ArrayList<Storable>();

        ResyncCapability.Listener<Storable> listener = new ResyncCapability.Listener<Storable>() {
            @Override
            public void afterInsert(Storable newStorable, Object state) {
                inserted.add(newStorable);
            }

            @Override
            public Object beforeUpdate(Storable oldStorable, Storable newStorable) {
                updated.add(new Storable[] {oldStorable, newStorable});
                return null;
            }

            @Override
            public void afterDelete(Storable oldStorable, Object state) {
                deleted.add(oldStorable);
            }
        };

        ResyncCapability cap = mReplicated.getCapability(ResyncCapability.class);
        cap.resync(StorableTestBasic.class, listener, 1.0, null);
        
        assertEquals(1, inserted.size());
        assertEquals(1, ((StorableTestBasic) inserted.get(0)).getId());
        assertEquals("hello", ((StorableTestBasic) inserted.get(0)).getStringProp());

        assertEquals(1, updated.size());
        assertEquals(3, ((StorableTestBasic) (updated.get(0)[0])).getId());
        assertEquals("bar", ((StorableTestBasic) (updated.get(0)[0])).getStringProp());
        assertEquals(3, ((StorableTestBasic) (updated.get(0)[1])).getId());
        assertEquals("foo", ((StorableTestBasic) (updated.get(0)[1])).getStringProp());

        assertEquals(1, deleted.size());
        assertEquals(2, ((StorableTestBasic) deleted.get(0)).getId());
        assertEquals("world", ((StorableTestBasic) deleted.get(0)).getStringProp());
    }

    public void testResyncListenerUnrepair() throws Exception {
        prepareOutOfSyncEntries();

        ResyncCapability.Listener<StorableTestBasic> listener =
            new ResyncCapability.Listener<StorableTestBasic>()
        {
            @Override
            public Object beforeInsert(StorableTestBasic storable) {
                // Force to be partially out of sync.
                storable.setDoubleProp(123.456);
                return null;
            }

            @Override
            public Object beforeUpdate(StorableTestBasic storable) {
                // Force to be partially out of sync.
                storable.setDoubleProp(654.321);
                return null;
            }
        };

        ResyncCapability cap = mReplicated.getCapability(ResyncCapability.class);
        cap.resync(StorableTestBasic.class, listener, 1.0, null);

        boolean insertUnrepair = false;
        boolean updateUnrepair = false;

        Storage<StorableTestBasic> storage = mReplicated.storageFor(StorableTestBasic.class);
        Cursor<StorableTestBasic> cursor = storage.query().fetch();
        while (cursor.hasNext()) {
            StorableTestBasic storable = cursor.next();
            insertUnrepair |= storable.getDoubleProp() == 123.456;
            updateUnrepair |= storable.getDoubleProp() == 654.321;
        }

        assertTrue(insertUnrepair);
        assertTrue(updateUnrepair);
    }

    private void prepareOutOfSyncEntries() throws Exception {
        // Insert an entry into master.
        {
            Storage<StorableTestBasic> storage = mMaster.storageFor(StorableTestBasic.class);
            StorableTestBasic stb = storage.prepare();
            stb.setId(1);
            stb.setStringProp("hello");
            stb.setIntProp(1);
            stb.setLongProp(1L);
            stb.setDoubleProp(1.0);
            stb.insert();
        }

        // Insert an entry into replica.
        {
            Storage<StorableTestBasic> storage = mReplica.storageFor(StorableTestBasic.class);
            StorableTestBasic stb = storage.prepare();
            stb.setId(2);
            stb.setStringProp("world");
            stb.setIntProp(1);
            stb.setLongProp(1L);
            stb.setDoubleProp(1.0);
            stb.insert();
        }

        // Insert conflicting entries into master and replica.
        {
            Storage<StorableTestBasic> storage = mMaster.storageFor(StorableTestBasic.class);
            StorableTestBasic stb = storage.prepare();
            stb.setId(3);
            stb.setStringProp("foo");
            stb.setIntProp(1);
            stb.setLongProp(1L);
            stb.setDoubleProp(1.0);
            stb.insert();
        }
        {
            Storage<StorableTestBasic> storage = mReplica.storageFor(StorableTestBasic.class);
            StorableTestBasic stb = storage.prepare();
            stb.setId(3);
            stb.setStringProp("bar");
            stb.setIntProp(1);
            stb.setLongProp(1L);
            stb.setDoubleProp(1.0);
            stb.insert();
        }

        // Insert matching entries into master and replica.
        {
            Storage<StorableTestBasic> storage = mMaster.storageFor(StorableTestBasic.class);
            StorableTestBasic stb = storage.prepare();
            stb.setId(4);
            stb.setStringProp("good");
            stb.setIntProp(1);
            stb.setLongProp(1L);
            stb.setDoubleProp(1.0);
            stb.insert();
        }
        {
            Storage<StorableTestBasic> storage = mReplica.storageFor(StorableTestBasic.class);
            StorableTestBasic stb = storage.prepare();
            stb.setId(4);
            stb.setStringProp("good");
            stb.setIntProp(1);
            stb.setLongProp(1L);
            stb.setDoubleProp(1.0);
            stb.insert();
        }
    }
}