diff options
author | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-03-13 14:05:38 +0100 |
---|---|---|
committer | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-03-13 14:05:38 +0100 |
commit | b256d125695da6b2a8eedd6f59ad07e94bd2b0bc (patch) | |
tree | 042074790f5d34775e528cc1ef164056bbf94849 /same/src | |
parent | af836cf8e577d2dabeb6450066bcc7ff55bf70e8 (diff) |
Clients can control their master.
Add failing test.
Diffstat (limited to 'same/src')
-rw-r--r-- | same/src/main/java/com/orbekk/same/Client.java | 12 | ||||
-rw-r--r-- | same/src/main/java/com/orbekk/same/SameController.java | 1 | ||||
-rw-r--r-- | same/src/test/java/com/orbekk/same/FunctionalTest.java | 24 |
3 files changed, 36 insertions, 1 deletions
diff --git a/same/src/main/java/com/orbekk/same/Client.java b/same/src/main/java/com/orbekk/same/Client.java index 4d5965b..d4ec19e 100644 --- a/same/src/main/java/com/orbekk/same/Client.java +++ b/same/src/main/java/com/orbekk/same/Client.java @@ -23,6 +23,7 @@ public class Client implements DiscoveryListener { private String myUrl; String masterUrl; private int masterId = -1; + private MasterController masterController = null; private List<StateChangedListener> stateListeners = new ArrayList<StateChangedListener>(); @@ -160,6 +161,10 @@ public class Client implements DiscoveryListener { return connectionState; } + public void setMasterController(MasterController masterController) { + this.masterController = masterController; + } + public void joinNetwork(String masterUrl) { logger.info("joinNetwork({})", masterUrl); connectionState = ConnectionState.UNSTABLE; @@ -233,10 +238,15 @@ public class Client implements DiscoveryListener { return paxosUrls; } - private void startMasterElection() { + public void startMasterElection() { + if (masterController == null) { + logger.warn("Could not become master: No master controller."); + return; + } List<String> paxosUrls = getPaxosUrls(); MasterProposer proposer = new MasterProposer(getUrl(), paxosUrls, connections); // TODO: Run election. + masterController.enableMaster(state); } } diff --git a/same/src/main/java/com/orbekk/same/SameController.java b/same/src/main/java/com/orbekk/same/SameController.java index c4bdfe7..a9b389c 100644 --- a/same/src/main/java/com/orbekk/same/SameController.java +++ b/same/src/main/java/com/orbekk/same/SameController.java @@ -130,6 +130,7 @@ public class SameController { public void start() throws Exception { server.start(); + client.setMasterController(masterController); client.start(); if (discoveryService != null) { discoveryService.start(); diff --git a/same/src/test/java/com/orbekk/same/FunctionalTest.java b/same/src/test/java/com/orbekk/same/FunctionalTest.java index 3707581..215efd9 100644 --- a/same/src/test/java/com/orbekk/same/FunctionalTest.java +++ b/same/src/test/java/com/orbekk/same/FunctionalTest.java @@ -1,6 +1,8 @@ package com.orbekk.same; import static org.junit.Assert.*; +import static org.mockito.Mockito.*; + import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; @@ -95,4 +97,26 @@ public class FunctionalTest { assertThat(x1.get(), is("TestValue1")); assertThat(x2.get(), is("TestValue1")); } + + @Test public void clientBecomesMaster() { + String newMasterUrl = "http://newMaster/MasterService.json"; + final Master newMaster = Master.create(connections, + broadcaster, newMasterUrl, "TestMaster"); + connections.masterMap.put(newMasterUrl, newMaster.getService()); + joinClients(); + MasterController controller = new MasterController() { + @Override + public void enableMaster(State lastKnownState) { + newMaster.resumeFrom(lastKnownState); + } + @Override + public void disableMaster() { + } + }; + client1.setMasterController(controller); + client1.startMasterElection(); + newMaster.performWork(); + assertThat(client1.masterUrl, is(newMasterUrl)); + assertThat(client2.masterUrl, is(newMasterUrl)); + } } |