Search in sources :

Example 16 with ReadWriteUpdateTable

use of org.apache.samza.table.ReadWriteUpdateTable in project samza by apache.

the class TestCachingTable method getMockCache.

private static Pair<ReadWriteUpdateTable<String, String, String>, Map<String, String>> getMockCache() {
    // To allow concurrent writes for disjoint keys by testConcurrentAccess, we must use CHM here.
    // This is okay because the atomic section in CachingTable covers both cache and table so using
    // CHM for each does not serialize such two-step operation so the atomicity is still tested.
    // Regular HashMap is not thread-safe even for disjoint keys.
    final Map<String, String> cacheStore = new ConcurrentHashMap<>();
    final ReadWriteUpdateTable cacheTable = mock(ReadWriteUpdateTable.class);
    doAnswer(invocation -> {
        String key = invocation.getArgumentAt(0, String.class);
        String value = invocation.getArgumentAt(1, String.class);
        cacheStore.put(key, value);
        return null;
    }).when(cacheTable).put(any(), any());
    doAnswer(invocation -> {
        String key = invocation.getArgumentAt(0, String.class);
        return cacheStore.get(key);
    }).when(cacheTable).get(any());
    doAnswer(invocation -> {
        String key = invocation.getArgumentAt(0, String.class);
        return cacheStore.remove(key);
    }).when(cacheTable).delete(any());
    return Pair.of(cacheTable, cacheStore);
}
Also used : ReadWriteUpdateTable(org.apache.samza.table.ReadWriteUpdateTable) Matchers.anyString(org.mockito.Matchers.anyString) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

ReadWriteUpdateTable (org.apache.samza.table.ReadWriteUpdateTable)16 Test (org.junit.Test)11 Context (org.apache.samza.context.Context)3 MockContext (org.apache.samza.context.MockContext)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 JavaTableConfig (org.apache.samza.config.JavaTableConfig)2 KV (org.apache.samza.operators.KV)2 StreamTableJoinOperatorSpec (org.apache.samza.operators.spec.StreamTableJoinOperatorSpec)2 MessageCollector (org.apache.samza.task.MessageCollector)2 TaskCoordinator (org.apache.samza.task.TaskCoordinator)2 Matchers.anyString (org.mockito.Matchers.anyString)2 Preconditions (com.google.common.base.Preconditions)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutorService (java.util.concurrent.ExecutorService)1 Executors (java.util.concurrent.Executors)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 SamzaException (org.apache.samza.SamzaException)1