|
| 1 | +package com.baeldung.crdt; |
| 2 | + |
| 3 | +import com.netopyr.wurmloch.crdt.GCounter; |
| 4 | +import com.netopyr.wurmloch.crdt.GSet; |
| 5 | +import com.netopyr.wurmloch.crdt.LWWRegister; |
| 6 | +import com.netopyr.wurmloch.crdt.PNCounter; |
| 7 | +import com.netopyr.wurmloch.store.LocalCrdtStore; |
| 8 | +import org.junit.Test; |
| 9 | + |
| 10 | +import static org.assertj.core.api.Assertions.assertThat; |
| 11 | + |
| 12 | +public class CRDTTest { |
| 13 | + |
| 14 | + @Test |
| 15 | + public void givenGrowOnlySet_whenTwoReplicasDiverge_thenShouldMergeItWithoutAConflict() { |
| 16 | + //given |
| 17 | + final LocalCrdtStore crdtStore1 = new LocalCrdtStore(); |
| 18 | + final LocalCrdtStore crdtStore2 = new LocalCrdtStore(); |
| 19 | + crdtStore1.connect(crdtStore2); |
| 20 | + |
| 21 | + final GSet<String> replica1 = crdtStore1.createGSet("ID_1"); |
| 22 | + final GSet<String> replica2 = crdtStore2.<String>findGSet("ID_1").get(); |
| 23 | + |
| 24 | + //when |
| 25 | + replica1.add("apple"); |
| 26 | + replica2.add("banana"); |
| 27 | + |
| 28 | + //then |
| 29 | + assertThat(replica1).contains("apple", "banana"); |
| 30 | + assertThat(replica2).contains("apple", "banana"); |
| 31 | + |
| 32 | + //when |
| 33 | + crdtStore1.disconnect(crdtStore2); |
| 34 | + |
| 35 | + replica1.add("strawberry"); |
| 36 | + replica2.add("pear"); |
| 37 | + |
| 38 | + |
| 39 | + assertThat(replica1).contains("apple", "banana", "strawberry"); |
| 40 | + assertThat(replica2).contains("apple", "banana", "pear"); |
| 41 | + |
| 42 | + crdtStore1.connect(crdtStore2); |
| 43 | + |
| 44 | + //then |
| 45 | + assertThat(replica1).contains("apple", "banana", "strawberry", "pear"); |
| 46 | + assertThat(replica2).contains("apple", "banana", "strawberry", "pear"); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void givenIncrementOnlyCounter_whenTwoReplicasDiverge_thenShouldMergeIt() { |
| 51 | + //given |
| 52 | + final LocalCrdtStore crdtStore1 = new LocalCrdtStore(); |
| 53 | + final LocalCrdtStore crdtStore2 = new LocalCrdtStore(); |
| 54 | + crdtStore1.connect(crdtStore2); |
| 55 | + |
| 56 | + final GCounter replica1 = crdtStore1.createGCounter("ID_1"); |
| 57 | + final GCounter replica2 = crdtStore2.findGCounter("ID_1").get(); |
| 58 | + |
| 59 | + //when |
| 60 | + replica1.increment(); |
| 61 | + replica2.increment(2L); |
| 62 | + |
| 63 | + //then |
| 64 | + assertThat(replica1.get()).isEqualTo(3L); |
| 65 | + assertThat(replica2.get()).isEqualTo(3L); |
| 66 | + |
| 67 | + //when |
| 68 | + crdtStore1.disconnect(crdtStore2); |
| 69 | + |
| 70 | + replica1.increment(3L); |
| 71 | + replica2.increment(5L); |
| 72 | + |
| 73 | + |
| 74 | + assertThat(replica1.get()).isEqualTo(6L); |
| 75 | + assertThat(replica2.get()).isEqualTo(8L); |
| 76 | + |
| 77 | + crdtStore1.connect(crdtStore2); |
| 78 | + |
| 79 | + // then |
| 80 | + assertThat(replica1.get()).isEqualTo(11L); |
| 81 | + assertThat(replica2.get()).isEqualTo(11L); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void givenPNCounter_whenReplicasDiverge_thenShouldMergeWithoutAConflict() { |
| 86 | + // given |
| 87 | + final LocalCrdtStore crdtStore1 = new LocalCrdtStore(); |
| 88 | + final LocalCrdtStore crdtStore2 = new LocalCrdtStore(); |
| 89 | + crdtStore1.connect(crdtStore2); |
| 90 | + |
| 91 | + final PNCounter replica1 = crdtStore1.createPNCounter("ID_1"); |
| 92 | + final PNCounter replica2 = crdtStore2.findPNCounter("ID_1").get(); |
| 93 | + |
| 94 | + //when |
| 95 | + replica1.increment(); |
| 96 | + replica2.decrement(2L); |
| 97 | + |
| 98 | + //then |
| 99 | + assertThat(replica1.get()).isEqualTo(-1L); |
| 100 | + assertThat(replica2.get()).isEqualTo(-1L); |
| 101 | + |
| 102 | + //when |
| 103 | + crdtStore1.disconnect(crdtStore2); |
| 104 | + |
| 105 | + replica1.decrement(3L); |
| 106 | + replica2.increment(5L); |
| 107 | + |
| 108 | + assertThat(replica1.get()).isEqualTo(-4L); |
| 109 | + assertThat(replica2.get()).isEqualTo(4L); |
| 110 | + |
| 111 | + crdtStore1.connect(crdtStore2); |
| 112 | + |
| 113 | + //then |
| 114 | + assertThat(replica1.get()).isEqualTo(1L); |
| 115 | + assertThat(replica2.get()).isEqualTo(1L); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public void givenLastWriteWinsStrategy_whenReplicasDiverge_thenAfterMergeShouldKeepOnlyLastValue() { |
| 120 | + //given |
| 121 | + final LocalCrdtStore crdtStore1 = new LocalCrdtStore("N_1"); |
| 122 | + final LocalCrdtStore crdtStore2 = new LocalCrdtStore("N_2"); |
| 123 | + crdtStore1.connect(crdtStore2); |
| 124 | + |
| 125 | + final LWWRegister<String> replica1 = crdtStore1.createLWWRegister("ID_1"); |
| 126 | + final LWWRegister<String> replica2 = crdtStore2.<String>findLWWRegister("ID_1").get(); |
| 127 | + |
| 128 | + //when |
| 129 | + replica1.set("apple"); |
| 130 | + replica2.set("banana"); |
| 131 | + |
| 132 | + // then |
| 133 | + assertThat(replica1.get()).isEqualTo("banana"); |
| 134 | + assertThat(replica2.get()).isEqualTo("banana"); |
| 135 | + |
| 136 | + |
| 137 | + // when |
| 138 | + crdtStore1.disconnect(crdtStore2); |
| 139 | + |
| 140 | + replica1.set("strawberry"); |
| 141 | + replica2.set("pear"); |
| 142 | + |
| 143 | + |
| 144 | + assertThat(replica1.get()).isEqualTo("strawberry"); |
| 145 | + assertThat(replica2.get()).isEqualTo("pear"); |
| 146 | + |
| 147 | + crdtStore1.connect(crdtStore2); |
| 148 | + |
| 149 | + //then |
| 150 | + assertThat(replica1.get()).isEqualTo("pear"); |
| 151 | + assertThat(replica2.get()).isEqualTo("pear"); |
| 152 | + } |
| 153 | +} |
0 commit comments