summaryrefslogtreecommitdiff
path: root/same/src
diff options
context:
space:
mode:
authorKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-01-22 19:15:24 +0100
committerKjetil Ørbekk <kjetil.orbekk@gmail.com>2012-01-22 19:15:24 +0100
commit37aa311231b4abd2f9d0a1d2ad6dd3da698862df (patch)
treeaa793acd594392aca4f7a1202a0f0ef7b2f963da /same/src
parent39a99670d96d2d8fca324103d9c950dfea3c7214 (diff)
Unit test of Paxos Service.
Diffstat (limited to 'same/src')
-rw-r--r--same/src/main/java/com/orbekk/paxos/PaxosService.java2
-rw-r--r--same/src/main/java/com/orbekk/paxos/PaxosServiceImpl.java18
-rw-r--r--same/src/test/java/com/orbekk/paxos/PaxosServiceTest.java62
3 files changed, 77 insertions, 5 deletions
diff --git a/same/src/main/java/com/orbekk/paxos/PaxosService.java b/same/src/main/java/com/orbekk/paxos/PaxosService.java
index 3398e81..b650ed5 100644
--- a/same/src/main/java/com/orbekk/paxos/PaxosService.java
+++ b/same/src/main/java/com/orbekk/paxos/PaxosService.java
@@ -1,4 +1,4 @@
-pakage com.orbekk.paxos;
+package com.orbekk.paxos;
public interface PaxosService {
boolean propose(String clientUrl, int roundId, int proposalNumber);
diff --git a/same/src/main/java/com/orbekk/paxos/PaxosServiceImpl.java b/same/src/main/java/com/orbekk/paxos/PaxosServiceImpl.java
index a39abbe..50bd88b 100644
--- a/same/src/main/java/com/orbekk/paxos/PaxosServiceImpl.java
+++ b/same/src/main/java/com/orbekk/paxos/PaxosServiceImpl.java
@@ -9,8 +9,13 @@ public class PaxosServiceImpl implements PaxosService {
private int highestPromise = 0;
private String tag = "";
+ public PaxosServiceImpl(String tag) {
+ this.tag = tag;
+ }
+
@Override
- public boolean propose(String clientUrl, int roundId, int proposalNumber) {
+ public synchronized boolean propose(String clientUrl, int roundId,
+ int proposalNumber) {
if (roundId > this.roundId) {
newRound(roundId);
}
@@ -25,7 +30,7 @@ public class PaxosServiceImpl implements PaxosService {
if (proposalNumber > highestPromise) {
highestPromise = proposalNumber;
logger.info(tag + "propose({}, {}, {}) = accepted",
- new Object[]{clientUrl, roundId, proposalNumber);
+ new Object[]{clientUrl, roundId, proposalNumber});
return true;
} else {
logger.info(tag + "propose({}, {}, {}) = rejected " +
@@ -37,11 +42,12 @@ public class PaxosServiceImpl implements PaxosService {
}
@Override
- public boolean acceptRequest(String clientUrl, int roundId,
+ public synchronized boolean acceptRequest(String clientUrl, int roundId,
int proposalNumber) {
if (roundId == this.roundId && proposalNumber == highestPromise) {
logger.info(tag + "acceptRequest({}, {}, {}) = accepted",
new Object[]{clientUrl, roundId, proposalNumber});
+ finishRound();
return true;
} else {
logger.info(tag + "acceptRequest({}, {}, {}) = rejected " +
@@ -52,7 +58,11 @@ public class PaxosServiceImpl implements PaxosService {
}
}
- private void newRound(int roundId) {
+ private synchronized void finishRound() {
+ newRound(roundId + 1);
+ }
+
+ private synchronized void newRound(int roundId) {
logger.info(tag + "new round: {}", roundId);
this.roundId = roundId;
highestPromise = 0;
diff --git a/same/src/test/java/com/orbekk/paxos/PaxosServiceTest.java b/same/src/test/java/com/orbekk/paxos/PaxosServiceTest.java
new file mode 100644
index 0000000..d09ff70
--- /dev/null
+++ b/same/src/test/java/com/orbekk/paxos/PaxosServiceTest.java
@@ -0,0 +1,62 @@
+package com.orbekk.paxos;
+
+import static org.junit.Assert.*;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import org.junit.Before;
+import org.junit.Test;
+
+public class PaxosServiceTest {
+ private PaxosServiceImpl p1 = new PaxosServiceImpl("P1: ");
+ private PaxosServiceImpl p2 = new PaxosServiceImpl("P2: ");
+ private PaxosServiceImpl p3 = new PaxosServiceImpl("P3: ");
+ private PaxosServiceImpl p4 = new PaxosServiceImpl("P4: ");
+ private PaxosServiceImpl p5 = new PaxosServiceImpl("P5: ");
+ private String client = "client";
+ private String client1 = "client1";
+ private String client2 = "client2";
+ private String client3 = "client3";
+ private String client4 = "client4";
+ private String client5 = "client5";
+ private List<PaxosServiceImpl> servers = new ArrayList<PaxosServiceImpl>();
+
+ @Before
+ public void setUp() {
+ Collections.addAll(servers, p1, p2, p3, p4, p5);
+ }
+
+ @Test
+ public void simpleCase() {
+ assertTrue(p1.propose(client, 1, 1));
+ assertTrue(p1.acceptRequest(client, 1, 1));
+ }
+
+ @Test
+ public void lowerProposalFails() {
+ assertTrue(p1.propose(client1, 5, 10));
+ assertFalse(p1.propose(client2, 3, 9));
+ assertFalse(p1.propose(client2, 4, 100));
+ assertFalse(p1.propose(client2, 5, 9));
+ assertFalse(p1.propose(client2, 5, 10));
+ assertTrue(p1.propose(client2, 5, 11));
+ }
+
+ @Test
+ public void testAccept() {
+ assertTrue(p1.propose(client1, 2, 3));
+ assertTrue(p1.propose(client2, 2, 4));
+ assertFalse(p1.acceptRequest(client1, 2, 3));
+ assertTrue(p1.acceptRequest(client2, 2, 4));
+ }
+
+ @Test
+ public void testRoundFinished() {
+ assertTrue(p1.propose(client1, 4, 5));
+ assertTrue(p1.acceptRequest(client1, 4, 5));
+ assertFalse(p1.propose(client2, 4, 5));
+ assertFalse(p1.acceptRequest(client2, 4, 5));
+ assertTrue(p1.propose(client1, 5, 1));
+ }
+}