Search in sources :

Example 1 with ClientRequest

use of org.apache.hadoop.oncrpc.RpcCallCache.ClientRequest 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)

Aggregations

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