diff options
author | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-03-12 20:51:12 +0100 |
---|---|---|
committer | Kjetil Ørbekk <kjetil.orbekk@gmail.com> | 2012-03-12 20:51:12 +0100 |
commit | 68145bc0805364fe3e01e4388de5bb70baa56fe0 (patch) | |
tree | 2333cfa0c0b26b25124cad5c22bda144261cbf32 /same | |
parent | 0c1fe5d3169b069bc88c1de7375658c1c885487c (diff) |
Add failing functional test.
Test fails because of the new ConnectionState that is not properly
implemented.
Diffstat (limited to 'same')
-rw-r--r-- | same/pom.xml | 6 | ||||
-rw-r--r-- | same/src/main/java/com/orbekk/same/Client.java | 6 | ||||
-rw-r--r-- | same/src/main/java/com/orbekk/same/Master.java | 2 | ||||
-rw-r--r-- | same/src/test/java/com/orbekk/same/FunctionalTest.java | 99 |
4 files changed, 111 insertions, 2 deletions
diff --git a/same/pom.xml b/same/pom.xml index c1f7946..2081a1e 100644 --- a/same/pom.xml +++ b/same/pom.xml @@ -81,6 +81,12 @@ <artifactId>httpclient</artifactId> <version>4.1.2</version> </dependency> + <dependency> + <groupId>org.hamcrest</groupId> + <artifactId>hamcrest-all</artifactId> + <version>1.1</version> + <scope>test</scope> + </dependency> </dependencies> <build> <plugins> diff --git a/same/src/main/java/com/orbekk/same/Client.java b/same/src/main/java/com/orbekk/same/Client.java index 35e3f0e..2bfee27 100644 --- a/same/src/main/java/com/orbekk/same/Client.java +++ b/same/src/main/java/com/orbekk/same/Client.java @@ -19,7 +19,7 @@ public class Client implements DiscoveryListener { /** TODO: Not really useful yet. Remove? */ private ConnectionState connectionState = ConnectionState.DISCONNECTED; private ConnectionManager connections; - private State state; + State state; private String myUrl; private String masterUrl; private int masterId = -1; @@ -148,6 +148,10 @@ public class Client implements DiscoveryListener { discoveryThread.interrupt(); } + void performWork() { + discoveryThread.performWork(); + } + public String getUrl() { return myUrl; } diff --git a/same/src/main/java/com/orbekk/same/Master.java b/same/src/main/java/com/orbekk/same/Master.java index b491313..ba87c12 100644 --- a/same/src/main/java/com/orbekk/same/Master.java +++ b/same/src/main/java/com/orbekk/same/Master.java @@ -10,7 +10,7 @@ import org.slf4j.LoggerFactory; public class Master { private Logger logger = LoggerFactory.getLogger(getClass()); private final ConnectionManager connections; - private State state; + State state; private Broadcaster broadcaster; public static Master create(ConnectionManager connections, diff --git a/same/src/test/java/com/orbekk/same/FunctionalTest.java b/same/src/test/java/com/orbekk/same/FunctionalTest.java new file mode 100644 index 0000000..dfb7d3b --- /dev/null +++ b/same/src/test/java/com/orbekk/same/FunctionalTest.java @@ -0,0 +1,99 @@ +package com.orbekk.same; + +import static org.junit.Assert.*; +import static org.mockito.Matchers.*; +import static org.mockito.Mockito.*; +import static org.hamcrest.Matcher.*; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Before; +import org.junit.Test; + +/** A functional test that runs with a master and several clients. */ +public class FunctionalTest { + Master master; + String masterUrl = "http://master/MasterService.json"; + Client client1; + Client client2; + Client client3; + VariableFactory vf1; + VariableFactory vf2; + VariableFactory vf3; + List<Client> clients = new ArrayList<Client>(); + TestConnectionManager connections = new TestConnectionManager(); + TestBroadcaster broadcaster = new TestBroadcaster(); + + @Before public void setUp() { + master = new Master(new State("TestMaster"), connections, + broadcaster); + connections.masterMap.put(masterUrl, + master.getService()); + client1 = newClient("TestClient1", "http://client1/ClientService.json"); + vf1 = new VariableFactory(client1.getInterface()); + client2 = newClient("TestClient2", "http://client2/ClientService.json"); + vf2 = new VariableFactory(client2.getInterface()); + client3 = newClient("TestClient3", "http://client3/ClientService.json"); + vf3 = new VariableFactory(client3.getInterface()); + } + + Client newClient(String clientName, String clientUrl) { + Client client = new Client(new State(clientName), connections, + clientUrl); + connections.clientMap.put(clientUrl, client.getService()); + clients.add(client); + return client; + } + + void performWork() { + for (int i = 0; i < 2; i++) { + master.performWork(); + for (Client c : clients) { + c.performWork(); + } + } + } + + void joinClients() { + for (Client c : clients) { + c.joinNetwork(masterUrl); + } + performWork(); + } + + List<State> getStates() { + List<State> states = new ArrayList<State>(); + states.add(master.state); + for (Client c : clients) { + states.add(c.state); + } + return states; + } + + @Test public void testJoin() { + joinClients(); + for (State s : getStates()) { + List<String> participants = s.getList(".participants"); + assertThat(participants, hasItem("http://client1/ClientService.json")); + assertThat(participants, hasItem("http://client2/ClientService.json")); + assertThat(participants, hasItem("http://client3/ClientService.json")); + } + for (Client c : clients) { + assertThat(c.getConnectionState(), is(ConnectionState.STABLE)); + } + } + + @Test public void setState() { + joinClients(); + Variable<String> x1 = vf1.createString("x"); + Variable<String> x2 = vf2.createString("x"); + x1.set("TestValue1"); + performWork(); + x2.update(); + assertThat(x1.get(), is("TestValue1")); + assertThat(x2.get(), is("TestValue1")); + } +} |