Fix #1384, possibly #1174 too

This commit is contained in:
Winston Li 2016-04-05 20:39:36 +01:00
parent bb619bfaf1
commit a409a15f07
3 changed files with 41 additions and 11 deletions

View file

@ -123,6 +123,12 @@
<artifactId>slf4j-simple</artifactId>
<version>1.7.13</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>
<dependency>
<groupId>org.mock-server</groupId>
<artifactId>mockserver-netty</artifactId>

View file

@ -1,10 +1,13 @@
package uk.ac.ic.wlgitbridge.snapshot.push;
import uk.ac.ic.wlgitbridge.snapshot.push.exception.*;
import uk.ac.ic.wlgitbridge.util.Log;
import com.google.common.base.Preconditions;
import uk.ac.ic.wlgitbridge.snapshot.push.exception.InvalidPostbackKeyException;
import uk.ac.ic.wlgitbridge.snapshot.push.exception.SnapshotPostException;
import uk.ac.ic.wlgitbridge.snapshot.push.exception.UnexpectedPostbackException;
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@ -14,11 +17,17 @@ import java.util.Map;
public class PostbackManager {
private final SecureRandom random;
private final Map<String, PostbackPromise> postbackContentsTable;
final Map<String, PostbackPromise> postbackContentsTable;
PostbackManager(SecureRandom random) {
this.random = random;
postbackContentsTable = Collections.synchronizedMap(
new HashMap<String, PostbackPromise>()
);
}
public PostbackManager() {
random = new SecureRandom();
postbackContentsTable = new HashMap<String, PostbackPromise>();
this(new SecureRandom());
}
public int waitForVersionIdOrThrow(String projectName)
@ -26,10 +35,7 @@ public class PostbackManager {
try {
PostbackPromise postbackPromise =
postbackContentsTable.get(projectName);
if (postbackPromise == null) {
Log.warn("[{}] PostbackPromise was null.", projectName);
throw new InternalErrorException();
}
Preconditions.checkNotNull(postbackPromise);
return postbackPromise.waitForPostback();
} finally {
postbackContentsTable.remove(projectName);
@ -56,7 +62,7 @@ public class PostbackManager {
private PostbackPromise getPostbackForProject(String projectName)
throws UnexpectedPostbackException {
PostbackPromise contents = postbackContentsTable.remove(projectName);
PostbackPromise contents = postbackContentsTable.get(projectName);
if (contents == null) {
throw new UnexpectedPostbackException();
}

View file

@ -6,6 +6,8 @@ import uk.ac.ic.wlgitbridge.snapshot.push.exception.InternalErrorException;
import uk.ac.ic.wlgitbridge.snapshot.push.exception.SnapshotPostException;
import uk.ac.ic.wlgitbridge.snapshot.push.exception.UnexpectedPostbackException;
import static org.junit.Assert.*;
/**
* Created by winston on 05/04/2016.
*/
@ -20,7 +22,7 @@ public class PostbackManagerTest {
String key = postbackManager.makeKeyForProject("proj");
postbackManager.postVersionIDForProject("proj", 1, key);
int versionId = postbackManager.waitForVersionIdOrThrow("proj");
Assert.assertEquals("Version id didn't match posted", 1, versionId);
assertEquals("Version id didn't match posted", 1, versionId);
}
@Test
@ -38,4 +40,20 @@ public class PostbackManagerTest {
Assert.fail("Exception wasn't thrown as required");
}
@Test
public void testTableConsistency() throws UnexpectedPostbackException,
SnapshotPostException {
String key1 = postbackManager.makeKeyForProject("proj1");
assertEquals(1, postbackManager.postbackContentsTable.size());
String key2 = postbackManager.makeKeyForProject("proj2");
assertEquals(2, postbackManager.postbackContentsTable.size());
postbackManager.postVersionIDForProject("proj1", 1, key1);
postbackManager.postVersionIDForProject("proj2", 1, key2);
assertEquals(2, postbackManager.postbackContentsTable.size());
postbackManager.waitForVersionIdOrThrow("proj1");
assertEquals(1, postbackManager.postbackContentsTable.size());
postbackManager.waitForVersionIdOrThrow("proj2");
Assert.assertTrue(postbackManager.postbackContentsTable.isEmpty());
}
}