parent
6818505bea
commit
c521aa66af
|
@ -1,6 +1,6 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
|
@ -487,7 +487,7 @@
|
|||
<artifactId>vavr</artifactId>
|
||||
<version>${vavr.version}</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- Retrofit -->
|
||||
<dependency>
|
||||
<groupId>com.squareup.retrofit2</groupId>
|
||||
|
@ -503,7 +503,7 @@
|
|||
<groupId>com.squareup.retrofit2</groupId>
|
||||
<artifactId>adapter-rxjava</artifactId>
|
||||
<version>${retrofit.version}</version>
|
||||
</dependency>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.squareup.okhttp3</groupId>
|
||||
<artifactId>logging-interceptor</artifactId>
|
||||
|
@ -596,6 +596,11 @@
|
|||
<artifactId>jgrapht-core</artifactId>
|
||||
<version>1.0.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.netopyr.wurmloch</groupId>
|
||||
<artifactId>wurmloch-crdt</artifactId>
|
||||
<version>${crdt.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<repositories>
|
||||
<repository>
|
||||
|
@ -617,6 +622,7 @@
|
|||
</repository>
|
||||
</repositories>
|
||||
<properties>
|
||||
<crdt.version>0.1.0</crdt.version>
|
||||
<multiverse.version>0.7.0</multiverse.version>
|
||||
<cglib.version>3.2.4</cglib.version>
|
||||
<commons-lang.version>3.6</commons-lang.version>
|
||||
|
@ -669,6 +675,6 @@
|
|||
<protonpack.version>1.14</protonpack.version>
|
||||
<unit-ri.version>1.0.3</unit-ri.version>
|
||||
<cache.version>1.0.0</cache.version>
|
||||
<hazelcast.version>3.8.4</hazelcast.version>
|
||||
<hazelcast.version>3.8.4</hazelcast.version>
|
||||
</properties>
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,153 @@
|
|||
package com.baeldung.crdt;
|
||||
|
||||
import com.netopyr.wurmloch.crdt.GCounter;
|
||||
import com.netopyr.wurmloch.crdt.GSet;
|
||||
import com.netopyr.wurmloch.crdt.LWWRegister;
|
||||
import com.netopyr.wurmloch.crdt.PNCounter;
|
||||
import com.netopyr.wurmloch.store.LocalCrdtStore;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class CRDTTest {
|
||||
|
||||
@Test
|
||||
public void givenGrowOnlySet_whenTwoReplicasDiverge_thenShouldMergeItWithoutAConflict() {
|
||||
//given
|
||||
final LocalCrdtStore crdtStore1 = new LocalCrdtStore();
|
||||
final LocalCrdtStore crdtStore2 = new LocalCrdtStore();
|
||||
crdtStore1.connect(crdtStore2);
|
||||
|
||||
final GSet<String> replica1 = crdtStore1.createGSet("ID_1");
|
||||
final GSet<String> replica2 = crdtStore2.<String>findGSet("ID_1").get();
|
||||
|
||||
//when
|
||||
replica1.add("apple");
|
||||
replica2.add("banana");
|
||||
|
||||
//then
|
||||
assertThat(replica1).contains("apple", "banana");
|
||||
assertThat(replica2).contains("apple", "banana");
|
||||
|
||||
//when
|
||||
crdtStore1.disconnect(crdtStore2);
|
||||
|
||||
replica1.add("strawberry");
|
||||
replica2.add("pear");
|
||||
|
||||
|
||||
assertThat(replica1).contains("apple", "banana", "strawberry");
|
||||
assertThat(replica2).contains("apple", "banana", "pear");
|
||||
|
||||
crdtStore1.connect(crdtStore2);
|
||||
|
||||
//then
|
||||
assertThat(replica1).contains("apple", "banana", "strawberry", "pear");
|
||||
assertThat(replica2).contains("apple", "banana", "strawberry", "pear");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIncrementOnlyCounter_whenTwoReplicasDiverge_thenShouldMergeIt() {
|
||||
//given
|
||||
final LocalCrdtStore crdtStore1 = new LocalCrdtStore();
|
||||
final LocalCrdtStore crdtStore2 = new LocalCrdtStore();
|
||||
crdtStore1.connect(crdtStore2);
|
||||
|
||||
final GCounter replica1 = crdtStore1.createGCounter("ID_1");
|
||||
final GCounter replica2 = crdtStore2.findGCounter("ID_1").get();
|
||||
|
||||
//when
|
||||
replica1.increment();
|
||||
replica2.increment(2L);
|
||||
|
||||
//then
|
||||
assertThat(replica1.get()).isEqualTo(3L);
|
||||
assertThat(replica2.get()).isEqualTo(3L);
|
||||
|
||||
//when
|
||||
crdtStore1.disconnect(crdtStore2);
|
||||
|
||||
replica1.increment(3L);
|
||||
replica2.increment(5L);
|
||||
|
||||
|
||||
assertThat(replica1.get()).isEqualTo(6L);
|
||||
assertThat(replica2.get()).isEqualTo(8L);
|
||||
|
||||
crdtStore1.connect(crdtStore2);
|
||||
|
||||
// then
|
||||
assertThat(replica1.get()).isEqualTo(11L);
|
||||
assertThat(replica2.get()).isEqualTo(11L);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPNCounter_whenReplicasDiverge_thenShouldMergeWithoutAConflict() {
|
||||
// given
|
||||
final LocalCrdtStore crdtStore1 = new LocalCrdtStore();
|
||||
final LocalCrdtStore crdtStore2 = new LocalCrdtStore();
|
||||
crdtStore1.connect(crdtStore2);
|
||||
|
||||
final PNCounter replica1 = crdtStore1.createPNCounter("ID_1");
|
||||
final PNCounter replica2 = crdtStore2.findPNCounter("ID_1").get();
|
||||
|
||||
//when
|
||||
replica1.increment();
|
||||
replica2.decrement(2L);
|
||||
|
||||
//then
|
||||
assertThat(replica1.get()).isEqualTo(-1L);
|
||||
assertThat(replica2.get()).isEqualTo(-1L);
|
||||
|
||||
//when
|
||||
crdtStore1.disconnect(crdtStore2);
|
||||
|
||||
replica1.decrement(3L);
|
||||
replica2.increment(5L);
|
||||
|
||||
assertThat(replica1.get()).isEqualTo(-4L);
|
||||
assertThat(replica2.get()).isEqualTo(4L);
|
||||
|
||||
crdtStore1.connect(crdtStore2);
|
||||
|
||||
//then
|
||||
assertThat(replica1.get()).isEqualTo(1L);
|
||||
assertThat(replica2.get()).isEqualTo(1L);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLastWriteWinsStrategy_whenReplicasDiverge_thenAfterMergeShouldKeepOnlyLastValue() {
|
||||
//given
|
||||
final LocalCrdtStore crdtStore1 = new LocalCrdtStore("N_1");
|
||||
final LocalCrdtStore crdtStore2 = new LocalCrdtStore("N_2");
|
||||
crdtStore1.connect(crdtStore2);
|
||||
|
||||
final LWWRegister<String> replica1 = crdtStore1.createLWWRegister("ID_1");
|
||||
final LWWRegister<String> replica2 = crdtStore2.<String>findLWWRegister("ID_1").get();
|
||||
|
||||
//when
|
||||
replica1.set("apple");
|
||||
replica2.set("banana");
|
||||
|
||||
// then
|
||||
assertThat(replica1.get()).isEqualTo("banana");
|
||||
assertThat(replica2.get()).isEqualTo("banana");
|
||||
|
||||
|
||||
// when
|
||||
crdtStore1.disconnect(crdtStore2);
|
||||
|
||||
replica1.set("strawberry");
|
||||
replica2.set("pear");
|
||||
|
||||
|
||||
assertThat(replica1.get()).isEqualTo("strawberry");
|
||||
assertThat(replica2.get()).isEqualTo("pear");
|
||||
|
||||
crdtStore1.connect(crdtStore2);
|
||||
|
||||
//then
|
||||
assertThat(replica1.get()).isEqualTo("pear");
|
||||
assertThat(replica2.get()).isEqualTo("pear");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue