Search in sources :

Example 86 with MultiMap

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);
}
Also used : MultiMap(com.hazelcast.multimap.MultiMap) TreeSet(java.util.TreeSet) Set(java.util.Set) TreeSet(java.util.TreeSet) Collection(java.util.Collection) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 87 with MultiMap

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);
}
Also used : MultiMap(com.hazelcast.multimap.MultiMap) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 88 with MultiMap

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());
}
Also used : MultiMap(com.hazelcast.multimap.MultiMap) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 89 with MultiMap

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());
    }
}
Also used : ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) Arrays(java.util.Arrays) QuickTest(com.hazelcast.test.annotation.QuickTest) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) MultiMapConfig(com.hazelcast.config.MultiMapConfig) MultiMap(com.hazelcast.multimap.MultiMap) After(org.junit.After) Map(java.util.Map) HazelcastTestSupport.assertOpenEventually(com.hazelcast.test.HazelcastTestSupport.assertOpenEventually) TestHazelcastFactory(com.hazelcast.client.test.TestHazelcastFactory) Before(org.junit.Before) EntryEvent(com.hazelcast.core.EntryEvent) HazelcastInstance(com.hazelcast.core.HazelcastInstance) HazelcastTestSupport.randomString(com.hazelcast.test.HazelcastTestSupport.randomString) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) EntryListener(com.hazelcast.core.EntryListener) Test(org.junit.Test) Category(org.junit.experimental.categories.Category) Consumer(java.util.function.Consumer) CountDownLatch(java.util.concurrent.CountDownLatch) Assert.assertFalse(org.junit.Assert.assertFalse) HazelcastParallelClassRunner(com.hazelcast.test.HazelcastParallelClassRunner) EntryAdapter(com.hazelcast.core.EntryAdapter) MapEvent(com.hazelcast.map.MapEvent) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) ArrayList(java.util.ArrayList) Collection(java.util.Collection) HazelcastTestSupport.randomString(com.hazelcast.test.HazelcastTestSupport.randomString) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 90 with MultiMap

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();
    }
}
Also used : MultiMap(com.hazelcast.multimap.MultiMap) TransactionalMultiMap(com.hazelcast.transaction.TransactionalMultiMap) TransactionException(com.hazelcast.transaction.TransactionException) TransactionContext(com.hazelcast.transaction.TransactionContext) TransactionalMultiMap(com.hazelcast.transaction.TransactionalMultiMap) ExecutorService(java.util.concurrent.ExecutorService) AtomicReference(java.util.concurrent.atomic.AtomicReference) HazelcastTestSupport.randomString(com.hazelcast.test.HazelcastTestSupport.randomString) CountDownLatch(java.util.concurrent.CountDownLatch) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

MultiMap (com.hazelcast.multimap.MultiMap)93 Test (org.junit.Test)93 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)92 QuickTest (com.hazelcast.test.annotation.QuickTest)92 HazelcastTestSupport.randomString (com.hazelcast.test.HazelcastTestSupport.randomString)11 TransactionContext (com.hazelcast.transaction.TransactionContext)8 TransactionalMultiMap (com.hazelcast.transaction.TransactionalMultiMap)8 CountDownLatch (java.util.concurrent.CountDownLatch)7 Collection (java.util.Collection)6 Set (java.util.Set)5 TreeSet (java.util.TreeSet)5 AssertTask (com.hazelcast.test.AssertTask)4 UUID (java.util.UUID)4 HazelcastInstance (com.hazelcast.core.HazelcastInstance)3 EntryAdapter (com.hazelcast.core.EntryAdapter)2 EntryEvent (com.hazelcast.core.EntryEvent)2 EntryListener (com.hazelcast.core.EntryListener)2 ArrayList (java.util.ArrayList)2 ClientConnectionRegistration (com.hazelcast.client.impl.spi.impl.listener.ClientConnectionRegistration)1 TestHazelcastFactory (com.hazelcast.client.test.TestHazelcastFactory)1