summaryrefslogtreecommitdiff
path: root/same/src
diff options
context:
space:
mode:
authorKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-01-15 18:56:40 +0100
committerKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-01-15 18:56:40 +0100
commit171341543525b950a1db1692bab10d251a2245dc (patch)
tree1fee5d834b752ba026a7120aadaa0aab9c5bda2f /same/src
parent6f58c3a3ce15289e559360330896cfb0cc22ae4d (diff)
Make Broadcaster testable.
Diffstat (limited to 'same/src')
-rw-r--r--same/src/main/java/com/orbekk/same/Broadcaster.java11
-rw-r--r--same/src/main/java/com/orbekk/same/BroadcasterImpl.java (renamed from same/src/main/java/com/orbekk/same/BroadcastRunner.java)13
-rw-r--r--same/src/main/java/com/orbekk/same/MasterServiceImpl.java6
-rw-r--r--same/src/test/java/com/orbekk/same/MasterServiceImplTest.java3
4 files changed, 24 insertions, 9 deletions
diff --git a/same/src/main/java/com/orbekk/same/Broadcaster.java b/same/src/main/java/com/orbekk/same/Broadcaster.java
new file mode 100644
index 0000000..ce1ad4f
--- /dev/null
+++ b/same/src/main/java/com/orbekk/same/Broadcaster.java
@@ -0,0 +1,11 @@
+package com.orbekk.same;
+
+import java.util.List;
+
+/**
+ * An interface for broadcasting a message to all clients.
+ */
+public interface Broadcaster {
+ public void broadcast(final List<String> targets,
+ final ServiceOperation operation);
+}
diff --git a/same/src/main/java/com/orbekk/same/BroadcastRunner.java b/same/src/main/java/com/orbekk/same/BroadcasterImpl.java
index 45935b6..688a388 100644
--- a/same/src/main/java/com/orbekk/same/BroadcastRunner.java
+++ b/same/src/main/java/com/orbekk/same/BroadcasterImpl.java
@@ -4,25 +4,28 @@ import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
-public class BroadcastRunner {
+public class BroadcasterImpl {
private ConnectionManager connections;
private Executor executor;
/**
* Get a BroadcastRunner for ClientService using a thread pool of size 20.
*/
- public static BroadcastRunner getDefaultBroadcastRunner() {
- return new BroadcastRunner(Executors.newFixedThreadPool(20));
+ public static BroadcasterImpl getDefaultBroadcastRunner() {
+ return new BroadcasterImpl(Executors.newFixedThreadPool(20),
+ new ConnectionManagerImpl());
}
- public BroadcastRunner(Executor executor, ConnectionManager connections) {
+ public BroadcasterImpl(Executor executor,
+ ConnectionManager connections) {
+ this.connections = connections;
this.executor = executor;
}
public synchronized void broadcast(final List<String> targets,
final ServiceOperation operation) {
for (String t : targets) {
- final ClientService client = connections.getConnection(t);
+ final ClientService client = connections.getClient(t);
executor.execute(new Runnable() {
@Override public void run() {
operation.run(client);
diff --git a/same/src/main/java/com/orbekk/same/MasterServiceImpl.java b/same/src/main/java/com/orbekk/same/MasterServiceImpl.java
index dcebb80..6093fa4 100644
--- a/same/src/main/java/com/orbekk/same/MasterServiceImpl.java
+++ b/same/src/main/java/com/orbekk/same/MasterServiceImpl.java
@@ -10,10 +10,10 @@ public class MasterServiceImpl implements MasterService, UrlReceiver, Runnable {
private ConnectionManager connections;
private State state;
private boolean stopped = false;
- private BroadcastRunner broadcaster;
+ private Broadcaster broadcaster;
public MasterServiceImpl(State initialState, ConnectionManager connections,
- BroadcastRunner broadcaster) {
+ Broadcaster broadcaster) {
state = initialState;
this.broadcaster = broadcaster;
}
@@ -45,7 +45,7 @@ public class MasterServiceImpl implements MasterService, UrlReceiver, Runnable {
logger.info("Broadcasting new component {}", state.show(component));
broadcaster.broadcast(participants(), new ServiceOperation() {
- @Override void run(ClientService client) {
+ @Override public void run(ClientService client) {
client.setState(component, state.getDataOf(component),
state.getRevision(component));
}
diff --git a/same/src/test/java/com/orbekk/same/MasterServiceImplTest.java b/same/src/test/java/com/orbekk/same/MasterServiceImplTest.java
index cbfbf12..78efb99 100644
--- a/same/src/test/java/com/orbekk/same/MasterServiceImplTest.java
+++ b/same/src/test/java/com/orbekk/same/MasterServiceImplTest.java
@@ -9,7 +9,8 @@ import org.junit.Test;
public class MasterServiceImplTest {
private State state = new State("TestNetwork");
- private MasterServiceImpl master = new MasterServiceImpl(state);
+ private MasterServiceImpl master = new MasterServiceImpl(state, null,
+ null);
@Test
public void setsMasterUrl() {