use of com.hazelcast.multimap.MultiMap in project hazelcast by hazelcast.
the class ClientMultiMapTest method testGet.
@Test
public void testGet() {
final Object key = "key";
final int maxItemsPerKey = 33;
final MultiMap mm = client.getMultiMap(randomString());
Set expected = new TreeSet();
for (int i = 0; i < maxItemsPerKey; i++) {
mm.put(key, i);
expected.add(i);
}
Collection resultSet = new TreeSet(mm.get(key));
assertEquals(expected, resultSet);
}
use of com.hazelcast.multimap.MultiMap in project hazelcast by hazelcast.
the class ClientMultiMapTest method testPut_withNullKey.
@Test(expected = NullPointerException.class)
public void testPut_withNullKey() {
Object value = "value";
final MultiMap mm = client.getMultiMap(randomString());
mm.put(null, value);
}
use of com.hazelcast.multimap.MultiMap in project hazelcast by hazelcast.
the class ClientMultiMapTest method testEntrySet_whenEmpty.
@Test
public void testEntrySet_whenEmpty() {
final MultiMap mm = client.getMultiMap(randomString());
assertEquals(Collections.EMPTY_SET, mm.entrySet());
}
use of com.hazelcast.multimap.MultiMap in project hazelcast by hazelcast.
the class ClientMultiMapTest method testMultiMapPutAllTemplate.
public void testMultiMapPutAllTemplate(Map<String, Collection<? extends Integer>> expectedMultiMap, Consumer<MultiMap<String, Integer>> putAllOperation) throws InterruptedException {
MultiMap<String, Integer> mmap1 = client.getMultiMap("testMultiMapList");
MultiMap<String, Integer> mmap2 = client.getMultiMap("testMultiMapSet");
Map<String, Collection<Integer>> resultMap1 = new ConcurrentHashMap<>();
Map<String, Collection<Integer>> resultMap2 = new ConcurrentHashMap<>();
int totalItems = 0;
Set<String> ks = expectedMultiMap.keySet();
for (String s : ks) {
Collection expectedCollection = expectedMultiMap.get(s);
totalItems += expectedCollection.size() + ((Long) expectedCollection.stream().distinct().count()).intValue();
}
final CountDownLatch latchAdded = new CountDownLatch(totalItems);
mmap1.addEntryListener(putAllEntryListenerBuilder((event) -> {
String key = (String) event.getKey();
Integer value = (Integer) event.getValue();
Collection<Integer> c;
if (!resultMap1.containsKey(key)) {
c = new ArrayList<>();
} else {
c = resultMap1.get(key);
}
c.add(value);
resultMap1.put(key, c);
latchAdded.countDown();
}), true);
mmap2.addEntryListener(putAllEntryListenerBuilder((event) -> {
String key = (String) event.getKey();
Integer value = (Integer) event.getValue();
Collection<Integer> c;
if (!resultMap2.containsKey(key)) {
c = new ArrayList<>();
} else {
c = resultMap2.get(key);
}
c.add(value);
resultMap2.put(key, c);
latchAdded.countDown();
}), true);
putAllOperation.accept(mmap1);
putAllOperation.accept(mmap2);
assertOpenEventually(latchAdded);
for (String s : ks) {
Collection c1 = resultMap1.get(s);
Collection c2 = resultMap2.get(s);
Collection expectedCollection = expectedMultiMap.get(s);
assertEquals(expectedCollection.size(), c1.size());
assertEquals(expectedCollection.stream().distinct().count(), c2.size());
}
}
use of com.hazelcast.multimap.MultiMap in project hazelcast by hazelcast.
the class ClientTxnMultiMapTest method testConcrruentTxnPut.
@Test
public void testConcrruentTxnPut() throws Exception {
final String mapName = randomString();
final MultiMap multiMap = client.getMultiMap(mapName);
final int threads = 10;
final ExecutorService ex = Executors.newFixedThreadPool(threads);
final CountDownLatch latch = new CountDownLatch(threads);
final AtomicReference<Throwable> error = new AtomicReference<Throwable>(null);
for (int i = 0; i < threads; i++) {
final int key = i;
ex.execute(new Runnable() {
public void run() {
multiMap.put(key, "value");
final TransactionContext context = client.newTransactionContext();
try {
context.beginTransaction();
final TransactionalMultiMap txnMultiMap = context.getMultiMap(mapName);
txnMultiMap.put(key, "value");
txnMultiMap.put(key, "value1");
txnMultiMap.put(key, "value2");
assertEquals(3, txnMultiMap.get(key).size());
context.commitTransaction();
assertEquals(3, multiMap.get(key).size());
} catch (TransactionException e) {
error.compareAndSet(null, e);
} finally {
latch.countDown();
}
}
});
}
try {
latch.await(1, TimeUnit.MINUTES);
assertNull(error.get());
} finally {
ex.shutdownNow();
}
}
Aggregations