GOSSIP-26 Gossip shared data
This commit is contained in:
@ -12,6 +12,7 @@ import java.util.concurrent.TimeUnit;
|
||||
import org.apache.gossip.event.GossipListener;
|
||||
import org.apache.gossip.event.GossipState;
|
||||
import org.apache.gossip.model.GossipDataMessage;
|
||||
import org.apache.gossip.model.SharedGossipDataMessage;
|
||||
import org.junit.Test;
|
||||
|
||||
import io.teknek.tunit.TUnit;
|
||||
@ -19,7 +20,7 @@ import io.teknek.tunit.TUnit;
|
||||
public class DataTest {
|
||||
|
||||
@Test
|
||||
public void abc() throws InterruptedException, UnknownHostException, URISyntaxException{
|
||||
public void dataTest() throws InterruptedException, UnknownHostException, URISyntaxException{
|
||||
GossipSettings settings = new GossipSettings();
|
||||
String cluster = UUID.randomUUID().toString();
|
||||
int seedNodes = 1;
|
||||
@ -51,20 +52,32 @@ public class DataTest {
|
||||
return total;
|
||||
}}).afterWaitingAtMost(20, TimeUnit.SECONDS).isEqualTo(2);
|
||||
clients.get(0).gossipPerNodeData(msg());
|
||||
clients.get(0).gossipSharedData(sharedMsg());
|
||||
Thread.sleep(10000);
|
||||
TUnit.assertThat(
|
||||
|
||||
new Callable<Object> (){
|
||||
new Callable<Object>() {
|
||||
public Object call() throws Exception {
|
||||
GossipDataMessage x = clients.get(1).findPerNodeData(1+"" , "a");
|
||||
if (x == null) return "";
|
||||
else return x.getPayload();
|
||||
}})
|
||||
|
||||
|
||||
//() -> clients.get(1).findGossipData(1+"" , "a").getPayload())
|
||||
.afterWaitingAtMost(20, TimeUnit.SECONDS)
|
||||
.isEqualTo("b");
|
||||
GossipDataMessage x = clients.get(1).findPerNodeData(1 + "", "a");
|
||||
if (x == null)
|
||||
return "";
|
||||
else
|
||||
return x.getPayload();
|
||||
}
|
||||
}).afterWaitingAtMost(20, TimeUnit.SECONDS).isEqualTo("b");
|
||||
|
||||
|
||||
TUnit.assertThat(
|
||||
new Callable<Object>() {
|
||||
public Object call() throws Exception {
|
||||
SharedGossipDataMessage x = clients.get(1).findSharedData("a");
|
||||
if (x == null)
|
||||
return "";
|
||||
else
|
||||
return x.getPayload();
|
||||
}
|
||||
}).afterWaitingAtMost(20, TimeUnit.SECONDS).isEqualTo("c");
|
||||
|
||||
|
||||
for (int i = 0; i < clusterMembers; ++i) {
|
||||
clients.get(i).shutdown();
|
||||
}
|
||||
@ -78,4 +91,14 @@ public class DataTest {
|
||||
g.setTimestamp(System.currentTimeMillis());
|
||||
return g;
|
||||
}
|
||||
|
||||
private SharedGossipDataMessage sharedMsg(){
|
||||
SharedGossipDataMessage g = new SharedGossipDataMessage();
|
||||
g.setExpireAt(Long.MAX_VALUE);
|
||||
g.setKey("a");
|
||||
g.setPayload("c");
|
||||
g.setTimestamp(System.currentTimeMillis());
|
||||
return g;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ import java.net.URI;
|
||||
import org.apache.gossip.GossipSettings;
|
||||
import org.apache.gossip.manager.random.RandomGossipManager;
|
||||
import org.apache.gossip.model.GossipDataMessage;
|
||||
import org.apache.gossip.model.SharedGossipDataMessage;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
@ -21,9 +22,13 @@ public class DataReaperTest {
|
||||
GossipManager gm = RandomGossipManager.newBuilder().cluster("abc").settings(settings)
|
||||
.withId(myId).uri(URI.create("udp://localhost:5000")).build();
|
||||
gm.gossipPerNodeData(perNodeDatum(key, value));
|
||||
Assert.assertEquals(value, gm.findGossipData(myId, key).getPayload());
|
||||
gm.getDataReaper().runOnce();
|
||||
TUnit.assertThat(() -> gm.findGossipData(myId, key)).equals(null);
|
||||
gm.gossipSharedData(sharedDatum(key, value));
|
||||
Assert.assertEquals(value, gm.findPerNodeGossipData(myId, key).getPayload());
|
||||
Assert.assertEquals(value, gm.findSharedGossipData(key).getPayload());
|
||||
gm.getDataReaper().runPerNodeOnce();
|
||||
gm.getDataReaper().runSharedOnce();
|
||||
TUnit.assertThat(() -> gm.findPerNodeGossipData(myId, key)).equals(null);
|
||||
TUnit.assertThat(() -> gm.findSharedGossipData(key)).equals(null);
|
||||
}
|
||||
|
||||
private GossipDataMessage perNodeDatum(String key, String value) {
|
||||
@ -34,6 +39,16 @@ public class DataReaperTest {
|
||||
m.setTimestamp(System.currentTimeMillis());
|
||||
return m;
|
||||
}
|
||||
|
||||
private SharedGossipDataMessage sharedDatum(String key, String value) {
|
||||
SharedGossipDataMessage m = new SharedGossipDataMessage();
|
||||
m.setExpireAt(System.currentTimeMillis() + 5L);
|
||||
m.setKey(key);
|
||||
m.setPayload(value);
|
||||
m.setTimestamp(System.currentTimeMillis());
|
||||
return m;
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testHigherTimestampWins() {
|
||||
@ -47,9 +62,9 @@ public class DataReaperTest {
|
||||
GossipDataMessage after = perNodeDatum(key, "b");
|
||||
after.setTimestamp(after.getTimestamp() - 1);
|
||||
gm.gossipPerNodeData(before);
|
||||
Assert.assertEquals(value, gm.findGossipData(myId, key).getPayload());
|
||||
Assert.assertEquals(value, gm.findPerNodeGossipData(myId, key).getPayload());
|
||||
gm.gossipPerNodeData(after);
|
||||
Assert.assertEquals(value, gm.findGossipData(myId, key).getPayload());
|
||||
Assert.assertEquals(value, gm.findPerNodeGossipData(myId, key).getPayload());
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user