package com.orbekk.paxos;

import com.googlecode.jsonrpc4j.JsonRpcServer;
import com.orbekk.same.ConnectionManagerImpl;
import com.orbekk.same.RpcHandler;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.junit.Before;
import org.junit.Test;

public class PaxosServiceFunctionalTest {
    ConnectionManagerImpl connections = new ConnectionManagerImpl(500, 500);
    List<String> paxosUrls = new ArrayList<String>();
    RpcHandler handler = new RpcHandler(null);
    TestServer server;
    String myUrl;

    @Before
    public void setUp() throws Exception {
        server = TestServer.create(handler);
        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);
            String serviceId = "/PaxosService" + i + ".json";
            handler.addRpcServer(serviceId, jsonServer);
        }
    }

    @Test
    public void nullTest() {
    }

    public static class TestServer {
        public Server server;
        public int port;

        public static TestServer create(Handler handler) throws Exception {
            Server server = new Server(0);
            server.setHandler(handler);
            server.start();
            int port = server.getConnectors()[0].getLocalPort();
            return new TestServer(server, port);
        }

        private TestServer(Server server, int port) {
            this.server = server;
            this.port = port;
        }
    }
}

@@ -20,16 +20,51 @@
 paxos;%0A%0A
+import static org.junit.Assert.*;%0A%0A
 import c
@@ -230,24 +230,49 @@
 .util.List;%0A
+import java.util.Random;%0A
 import org.e
@@ -804,14 +804,8 @@
 port
- + %22/%22
 ;%0A  
@@ -1027,21 +1027,29 @@
 ceImpl(%22
+P
 %22 + i
+ + %22: %22
 ), Paxos
@@ -1195,55 +1195,1092 @@
     
-%7D%0A    %7D%0A%0A    @Test%0A    public void nullTest() %7B
+    paxosUrls.add(myUrl + serviceId);%0A        %7D%0A    %7D%0A%0A    @Test%0A    public void testMasterElection() %7B%0A        MasterProposer m1 = new MasterProposer(%22http://client1%22, paxosUrls,%0A                connections);%0A        assertTrue(m1.propose(1, 1));%0A    %7D%0A%0A    @Test%0A    public void testWithCompetition() %7B%0A        int proposers = 5;%0A        List%3CThread%3E masterProposers = new ArrayList%3CThread%3E(); %0A        for (int i = 1; i %3C= proposers; i++) %7B%0A            final int j = i;%0A            masterProposers.add(new Thread() %7B%0A                @Override public void run() %7B%0A                    MasterProposer client =%0A                            new MasterProposer(%22http:/client%22 + j, paxosUrls,%0A                                    connections);%0A                    client.propose(1, 1);%0A                %7D%0A            %7D);%0A        %7D%0A        for (Thread t : masterProposers) %7B%0A            t.start();%0A        %7D%0A        for (Thread t : masterProposers) %7B%0A            try %7B%0A                t.join();%0A            %7D catch (InterruptedException e) %7B%0A                // Ignore.%0A            %7D%0A        %7D
 %0A   

Add multi-threaded Paxos test code.