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);
}
Aggregations