From 36a5b60d59f1f4e5e179f9800f4dbf08771c1e5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kjetil=20=C3=98rbekk?= Date: Mon, 23 Jan 2012 22:01:28 +0100 Subject: Add multi-threaded Paxos test code. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (Doesn't actually make assertions – it just burns CPU cycles :P) --- .../orbekk/paxos/PaxosServiceFunctionalTest.java | 40 ++++++++++++++++++++-- 1 file changed, 37 insertions(+), 3 deletions(-) (limited to 'same') diff --git a/same/src/test/java/com/orbekk/paxos/PaxosServiceFunctionalTest.java b/same/src/test/java/com/orbekk/paxos/PaxosServiceFunctionalTest.java index c537d18..8f20273 100644 --- a/same/src/test/java/com/orbekk/paxos/PaxosServiceFunctionalTest.java +++ b/same/src/test/java/com/orbekk/paxos/PaxosServiceFunctionalTest.java @@ -1,10 +1,13 @@ package com.orbekk.paxos; +import static org.junit.Assert.*; + import com.googlecode.jsonrpc4j.JsonRpcServer; import com.orbekk.same.ConnectionManagerImpl; import com.orbekk.same.RpcHandler; import java.util.ArrayList; import java.util.List; +import java.util.Random; import org.eclipse.jetty.server.Handler; import org.eclipse.jetty.server.Server; import org.junit.Before; @@ -20,21 +23,52 @@ public class PaxosServiceFunctionalTest { @Before public void setUp() throws Exception { server = TestServer.create(handler); - myUrl = "http://localhost:" + server.port + "/"; + myUrl = "http://localhost:" + server.port; setupPaxos(5); } public void setupPaxos(int instances) { for (int i = 1; i <= instances; i++) { JsonRpcServer jsonServer = new JsonRpcServer( - new PaxosServiceImpl("" + i), PaxosService.class); + new PaxosServiceImpl("P" + i + ": "), PaxosService.class); String serviceId = "/PaxosService" + i + ".json"; handler.addRpcServer(serviceId, jsonServer); + paxosUrls.add(myUrl + serviceId); } } @Test - public void nullTest() { + public void testMasterElection() { + MasterProposer m1 = new MasterProposer("http://client1", paxosUrls, + connections); + assertTrue(m1.propose(1, 1)); + } + + @Test + public void testWithCompetition() { + int proposers = 5; + List masterProposers = new ArrayList(); + for (int i = 1; i <= proposers; i++) { + final int j = i; + masterProposers.add(new Thread() { + @Override public void run() { + MasterProposer client = + new MasterProposer("http:/client" + j, paxosUrls, + connections); + client.propose(1, 1); + } + }); + } + for (Thread t : masterProposers) { + t.start(); + } + for (Thread t : masterProposers) { + try { + t.join(); + } catch (InterruptedException e) { + // Ignore. + } + } } public static class TestServer { -- cgit v1.2.3