Skip to content

Commit c521aa6

Browse files
tomekl007pivovarit
authored andcommitted
BAEL-1155 crdt (eugenp#2659)
* BAEL-1155 crdt * BAEL-1155 fix pom
1 parent 6818505 commit c521aa6

2 files changed

Lines changed: 163 additions & 4 deletions

File tree

‎libraries/pom.xml‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
44
<parent>
55
<artifactId>parent-modules</artifactId>
66
<groupId>com.baeldung</groupId>
@@ -487,7 +487,7 @@
487487
<artifactId>vavr</artifactId>
488488
<version>${vavr.version}</version>
489489
</dependency>
490-
490+
491491
<!-- Retrofit -->
492492
<dependency>
493493
<groupId>com.squareup.retrofit2</groupId>
@@ -503,7 +503,7 @@
503503
<groupId>com.squareup.retrofit2</groupId>
504504
<artifactId>adapter-rxjava</artifactId>
505505
<version>${retrofit.version}</version>
506-
</dependency>
506+
</dependency>
507507
<dependency>
508508
<groupId>com.squareup.okhttp3</groupId>
509509
<artifactId>logging-interceptor</artifactId>
@@ -596,6 +596,11 @@
596596
<artifactId>jgrapht-core</artifactId>
597597
<version>1.0.1</version>
598598
</dependency>
599+
<dependency>
600+
<groupId>com.netopyr.wurmloch</groupId>
601+
<artifactId>wurmloch-crdt</artifactId>
602+
<version>${crdt.version}</version>
603+
</dependency>
599604
</dependencies>
600605
<repositories>
601606
<repository>
@@ -617,6 +622,7 @@
617622
</repository>
618623
</repositories>
619624
<properties>
625+
<crdt.version>0.1.0</crdt.version>
620626
<multiverse.version>0.7.0</multiverse.version>
621627
<cglib.version>3.2.4</cglib.version>
622628
<commons-lang.version>3.6</commons-lang.version>
@@ -669,6 +675,6 @@
669675
<protonpack.version>1.14</protonpack.version>
670676
<unit-ri.version>1.0.3</unit-ri.version>
671677
<cache.version>1.0.0</cache.version>
672-
<hazelcast.version>3.8.4</hazelcast.version>
678+
<hazelcast.version>3.8.4</hazelcast.version>
673679
</properties>
674680
</project>
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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

Comments
 (0)