Search in sources :

Example 1 with CacheEntry

use of org.apache.hadoop.oncrpc.RpcCallCache.CacheEntry in project hadoop by apache.

the class TestRpcCallCache method testCacheFunctionality.

@Test
public void testCacheFunctionality() throws UnknownHostException {
    RpcCallCache cache = new RpcCallCache("Test", 10);
    // Add 20 entries to the cache and only last 10 should be retained
    int size = 0;
    for (int clientId = 0; clientId < 20; clientId++) {
        InetAddress clientIp = InetAddress.getByName("1.1.1." + clientId);
        System.out.println("Adding " + clientIp);
        cache.checkOrAddToCache(clientIp, 0);
        size = Math.min(++size, 10);
        System.out.println("Cache size " + cache.size());
        // Ensure the cache size is correct
        assertEquals(size, cache.size());
        // Ensure the cache entries are correct
        int startEntry = Math.max(clientId - 10 + 1, 0);
        Iterator<Entry<ClientRequest, CacheEntry>> iterator = cache.iterator();
        for (int i = 0; i < size; i++) {
            ClientRequest key = iterator.next().getKey();
            System.out.println("Entry " + key.getClientId());
            assertEquals(InetAddress.getByName("1.1.1." + (startEntry + i)), key.getClientId());
        }
        // Ensure cache entries are returned as in progress.
        for (int i = 0; i < size; i++) {
            CacheEntry e = cache.checkOrAddToCache(InetAddress.getByName("1.1.1." + (startEntry + i)), 0);
            assertNotNull(e);
            assertTrue(e.isInProgress());
            assertFalse(e.isCompleted());
        }
    }
}
Also used : Entry(java.util.Map.Entry) CacheEntry(org.apache.hadoop.oncrpc.RpcCallCache.CacheEntry) CacheEntry(org.apache.hadoop.oncrpc.RpcCallCache.CacheEntry) InetAddress(java.net.InetAddress) ClientRequest(org.apache.hadoop.oncrpc.RpcCallCache.ClientRequest) Test(org.junit.Test)

Example 2 with CacheEntry

use of org.apache.hadoop.oncrpc.RpcCallCache.CacheEntry in project hadoop by apache.

the class TestRpcCallCache method testCacheEntry.

@Test
public void testCacheEntry() {
    CacheEntry c = new CacheEntry();
    validateInprogressCacheEntry(c);
    assertTrue(c.isInProgress());
    assertFalse(c.isCompleted());
    assertNull(c.getResponse());
    RpcResponse response = mock(RpcResponse.class);
    c.setResponse(response);
    validateCompletedCacheEntry(c, response);
}
Also used : CacheEntry(org.apache.hadoop.oncrpc.RpcCallCache.CacheEntry) Test(org.junit.Test)

Example 3 with CacheEntry

use of org.apache.hadoop.oncrpc.RpcCallCache.CacheEntry in project hadoop by apache.

the class TestRpcCallCache method testAddRemoveEntries.

@Test
public void testAddRemoveEntries() throws UnknownHostException {
    RpcCallCache cache = new RpcCallCache("test", 100);
    InetAddress clientIp = InetAddress.getByName("1.1.1.1");
    int xid = 100;
    // Ensure null is returned when there is no entry in the cache
    // An entry is added to indicate the request is in progress
    CacheEntry e = cache.checkOrAddToCache(clientIp, xid);
    assertNull(e);
    e = cache.checkOrAddToCache(clientIp, xid);
    validateInprogressCacheEntry(e);
    // Set call as completed
    RpcResponse response = mock(RpcResponse.class);
    cache.callCompleted(clientIp, xid, response);
    e = cache.checkOrAddToCache(clientIp, xid);
    validateCompletedCacheEntry(e, response);
}
Also used : CacheEntry(org.apache.hadoop.oncrpc.RpcCallCache.CacheEntry) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Aggregations

CacheEntry (org.apache.hadoop.oncrpc.RpcCallCache.CacheEntry)3 Test (org.junit.Test)3 InetAddress (java.net.InetAddress)2 Entry (java.util.Map.Entry)1 ClientRequest (org.apache.hadoop.oncrpc.RpcCallCache.ClientRequest)1