summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--same/src/main/java/com/orbekk/same/NewMaster.java64
-rw-r--r--same/src/test/java/com/orbekk/same/NewMasterTest.java79
2 files changed, 143 insertions, 0 deletions
diff --git a/same/src/main/java/com/orbekk/same/NewMaster.java b/same/src/main/java/com/orbekk/same/NewMaster.java
new file mode 100644
index 0000000..161586f
--- /dev/null
+++ b/same/src/main/java/com/orbekk/same/NewMaster.java
@@ -0,0 +1,64 @@
+package com.orbekk.same;
+
+import java.util.List;
+import com.orbekk.util.WorkQueue;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class NewMaster {
+ private Logger logger = LoggerFactory.getLogger(getClass());
+ private final ConnectionManager connections;
+ private State state;
+ private Broadcaster broadcaster;
+
+ public static NewMaster create(ConnectionManager connections,
+ Broadcaster broadcaster, String myUrl, String networkName) {
+ State state = new State(networkName);
+ state.update(".masterUrl", myUrl, 1);
+ return new NewMaster(state, connections, broadcaster);
+ }
+
+ NewMaster(State initialState, ConnectionManager connections,
+ Broadcaster broadcaster) {
+ this.state = initialState;
+ this.connections = connections;
+ this.broadcaster = broadcaster;
+ }
+
+ private MasterService serviceImpl = new MasterService() {
+ @Override
+ public boolean updateStateRequest(String component,
+ String newData, long revision) {
+ return false;
+ }
+
+ @Override
+ public void joinNetworkRequest(String clientUrl) {
+ }
+ };
+
+ WorkQueue<String> updateStateRequestThread = new WorkQueue<String>() {
+ @Override protected void onChange() {
+ List<String> pending = getAndClear();
+ for (String componentName : pending) {
+ logger.info("Component updated: {}", componentName);
+ }
+ }
+ };
+
+ void performWork() {
+ updateStateRequestThread.performWork();
+ }
+
+ public void start() {
+ updateStateRequestThread.start();
+ }
+
+ public void interrupt() {
+ updateStateRequestThread.interrupt();
+ }
+
+ public MasterService getService() {
+ return serviceImpl;
+ }
+}
diff --git a/same/src/test/java/com/orbekk/same/NewMasterTest.java b/same/src/test/java/com/orbekk/same/NewMasterTest.java
new file mode 100644
index 0000000..098b141
--- /dev/null
+++ b/same/src/test/java/com/orbekk/same/NewMasterTest.java
@@ -0,0 +1,79 @@
+package com.orbekk.same;
+
+import static org.junit.Assert.*;
+
+import java.util.List;
+import org.codehaus.jackson.type.TypeReference;
+import org.junit.Before;
+import org.junit.Test;
+
+public class NewMasterTest {
+ private State state = new State("TestNetwork");
+ private TestConnectionManager connections = new TestConnectionManager();
+ private TestBroadcaster broadcaster = new TestBroadcaster();
+ private NewMaster master;
+ private MasterService masterS;
+
+ public static class UnreachableClient implements ClientService {
+ @Override
+ public void notifyNetwork(String networkName, String masterUrl)
+ throws Exception {
+ throw new Exception("Unreachable client");
+ }
+
+ @Override
+ public void setState(String component, String data, long revision)
+ throws Exception {
+ throw new Exception("Unreachable client");
+ }
+
+ @Override
+ public void discoveryRequest(String remoteUrl) throws Exception {
+ throw new Exception("Unreachable client");
+ }
+ }
+
+ @Before
+ public void setUp() {
+ state.update(".masterUrl", "http://master/MasterService.json", 1);
+ master = new NewMaster(state, connections, broadcaster);
+ masterS = master.getService();
+ connections.masterMap.put("http://master/MasterService.json",
+ masterS);
+ }
+
+ @Test
+ public void updateStateRequest() throws Exception {
+ Client client1 = new Client(
+ new State("ClientNetwork"), connections,
+ "http://client/ClientService.json");
+ ClientService client1S = client1.getService();
+ connections.clientMap.put("http://client/ClientService.json", client1S);
+ Client client2 = new Client(
+ new State("ClientNetwork"), connections,
+ "http://client2/ClientService.json");
+ ClientService client2S = client2.getService();
+ connections.clientMap.put("http://client2/ClientService.json", client2S);
+
+ client1.joinNetwork("http://master/MasterService.json");
+ client2.joinNetwork("http://master/MasterService.json");
+
+ master.performWork();
+ assertTrue(state.getList(".participants").contains("http://client/ClientService.json"));
+ assertTrue(state.getList(".participants").contains("http://client2/ClientService.json"));
+ assertEquals(state, client1.testGetState());
+
+ assertTrue(masterS.updateStateRequest("A", "1", 0));
+ master.performWork();
+
+ assertEquals(state, client1.testGetState());
+ assertEquals(state, client2.testGetState());
+
+ assertFalse(masterS.updateStateRequest("A", "2", 0));
+ assertTrue(masterS.updateStateRequest("A", "3", 1));
+ master.performWork();
+
+ assertEquals(state, client1.testGetState());
+ assertEquals(state, client2.testGetState());
+ }
+}