|
@@ -38,55 +38,55 @@ import static org.mockito.Mockito.*;
|
|
* Unit tests for {@link RpcCallCache}
|
|
* Unit tests for {@link RpcCallCache}
|
|
*/
|
|
*/
|
|
public class TestRpcCallCache {
|
|
public class TestRpcCallCache {
|
|
-
|
|
|
|
|
|
+
|
|
@Test(expected=IllegalArgumentException.class)
|
|
@Test(expected=IllegalArgumentException.class)
|
|
public void testRpcCallCacheConstructorIllegalArgument0(){
|
|
public void testRpcCallCacheConstructorIllegalArgument0(){
|
|
new RpcCallCache("test", 0);
|
|
new RpcCallCache("test", 0);
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
@Test(expected=IllegalArgumentException.class)
|
|
@Test(expected=IllegalArgumentException.class)
|
|
public void testRpcCallCacheConstructorIllegalArgumentNegative(){
|
|
public void testRpcCallCacheConstructorIllegalArgumentNegative(){
|
|
new RpcCallCache("test", -1);
|
|
new RpcCallCache("test", -1);
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
@Test
|
|
@Test
|
|
public void testRpcCallCacheConstructor(){
|
|
public void testRpcCallCacheConstructor(){
|
|
RpcCallCache cache = new RpcCallCache("test", 100);
|
|
RpcCallCache cache = new RpcCallCache("test", 100);
|
|
assertEquals("test", cache.getProgram());
|
|
assertEquals("test", cache.getProgram());
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
@Test
|
|
@Test
|
|
public void testAddRemoveEntries() throws UnknownHostException {
|
|
public void testAddRemoveEntries() throws UnknownHostException {
|
|
RpcCallCache cache = new RpcCallCache("test", 100);
|
|
RpcCallCache cache = new RpcCallCache("test", 100);
|
|
InetAddress clientIp = InetAddress.getByName("1.1.1.1");
|
|
InetAddress clientIp = InetAddress.getByName("1.1.1.1");
|
|
int xid = 100;
|
|
int xid = 100;
|
|
-
|
|
|
|
|
|
+
|
|
// Ensure null is returned when there is no entry in the cache
|
|
// Ensure null is returned when there is no entry in the cache
|
|
// An entry is added to indicate the request is in progress
|
|
// An entry is added to indicate the request is in progress
|
|
CacheEntry e = cache.checkOrAddToCache(clientIp, xid);
|
|
CacheEntry e = cache.checkOrAddToCache(clientIp, xid);
|
|
assertNull(e);
|
|
assertNull(e);
|
|
e = cache.checkOrAddToCache(clientIp, xid);
|
|
e = cache.checkOrAddToCache(clientIp, xid);
|
|
validateInprogressCacheEntry(e);
|
|
validateInprogressCacheEntry(e);
|
|
-
|
|
|
|
|
|
+
|
|
// Set call as completed
|
|
// Set call as completed
|
|
RpcResponse response = mock(RpcResponse.class);
|
|
RpcResponse response = mock(RpcResponse.class);
|
|
cache.callCompleted(clientIp, xid, response);
|
|
cache.callCompleted(clientIp, xid, response);
|
|
e = cache.checkOrAddToCache(clientIp, xid);
|
|
e = cache.checkOrAddToCache(clientIp, xid);
|
|
validateCompletedCacheEntry(e, response);
|
|
validateCompletedCacheEntry(e, response);
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
private void validateInprogressCacheEntry(CacheEntry c) {
|
|
private void validateInprogressCacheEntry(CacheEntry c) {
|
|
assertTrue(c.isInProgress());
|
|
assertTrue(c.isInProgress());
|
|
assertFalse(c.isCompleted());
|
|
assertFalse(c.isCompleted());
|
|
assertNull(c.getResponse());
|
|
assertNull(c.getResponse());
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
private void validateCompletedCacheEntry(CacheEntry c, RpcResponse response) {
|
|
private void validateCompletedCacheEntry(CacheEntry c, RpcResponse response) {
|
|
assertFalse(c.isInProgress());
|
|
assertFalse(c.isInProgress());
|
|
assertTrue(c.isCompleted());
|
|
assertTrue(c.isCompleted());
|
|
assertEquals(response, c.getResponse());
|
|
assertEquals(response, c.getResponse());
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
@Test
|
|
@Test
|
|
public void testCacheEntry() {
|
|
public void testCacheEntry() {
|
|
CacheEntry c = new CacheEntry();
|
|
CacheEntry c = new CacheEntry();
|
|
@@ -94,16 +94,16 @@ public class TestRpcCallCache {
|
|
assertTrue(c.isInProgress());
|
|
assertTrue(c.isInProgress());
|
|
assertFalse(c.isCompleted());
|
|
assertFalse(c.isCompleted());
|
|
assertNull(c.getResponse());
|
|
assertNull(c.getResponse());
|
|
-
|
|
|
|
|
|
+
|
|
RpcResponse response = mock(RpcResponse.class);
|
|
RpcResponse response = mock(RpcResponse.class);
|
|
c.setResponse(response);
|
|
c.setResponse(response);
|
|
validateCompletedCacheEntry(c, response);
|
|
validateCompletedCacheEntry(c, response);
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
@Test
|
|
@Test
|
|
public void testCacheFunctionality() throws UnknownHostException {
|
|
public void testCacheFunctionality() throws UnknownHostException {
|
|
RpcCallCache cache = new RpcCallCache("Test", 10);
|
|
RpcCallCache cache = new RpcCallCache("Test", 10);
|
|
-
|
|
|
|
|
|
+
|
|
// Add 20 entries to the cache and only last 10 should be retained
|
|
// Add 20 entries to the cache and only last 10 should be retained
|
|
int size = 0;
|
|
int size = 0;
|
|
for (int clientId = 0; clientId < 20; clientId++) {
|
|
for (int clientId = 0; clientId < 20; clientId++) {
|
|
@@ -113,7 +113,7 @@ public class TestRpcCallCache {
|
|
size = Math.min(++size, 10);
|
|
size = Math.min(++size, 10);
|
|
System.out.println("Cache size " + cache.size());
|
|
System.out.println("Cache size " + cache.size());
|
|
assertEquals(size, cache.size()); // Ensure the cache size is correct
|
|
assertEquals(size, cache.size()); // Ensure the cache size is correct
|
|
-
|
|
|
|
|
|
+
|
|
// Ensure the cache entries are correct
|
|
// Ensure the cache entries are correct
|
|
int startEntry = Math.max(clientId - 10 + 1, 0);
|
|
int startEntry = Math.max(clientId - 10 + 1, 0);
|
|
Iterator<Entry<ClientRequest, CacheEntry>> iterator = cache.iterator();
|
|
Iterator<Entry<ClientRequest, CacheEntry>> iterator = cache.iterator();
|
|
@@ -123,7 +123,7 @@ public class TestRpcCallCache {
|
|
assertEquals(InetAddress.getByName("1.1.1." + (startEntry + i)),
|
|
assertEquals(InetAddress.getByName("1.1.1." + (startEntry + i)),
|
|
key.getClientId());
|
|
key.getClientId());
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
// Ensure cache entries are returned as in progress.
|
|
// Ensure cache entries are returned as in progress.
|
|
for (int i = 0; i < size; i++) {
|
|
for (int i = 0; i < size; i++) {
|
|
CacheEntry e = cache.checkOrAddToCache(
|
|
CacheEntry e = cache.checkOrAddToCache(
|