Search in sources :

Example 81 with HazelcastInstance

use of com.hazelcast.core.HazelcastInstance in project hazelcast by hazelcast.

the class EvictionTest method testContainsKeyShouldDelayEviction.

/**
     * Test for issue 614
     */
@Test
public void testContainsKeyShouldDelayEviction() throws InterruptedException {
    String mapName = randomMapName();
    final int waitSeconds = 2;
    Config config = getConfig();
    config.getMapConfig(mapName).setMaxIdleSeconds(30);
    HazelcastInstance instance = createHazelcastInstance(config);
    final IMap<Object, Object> map = instance.getMap(mapName);
    map.put(1, 1);
    sleepSeconds(waitSeconds);
    EntryView<Object, Object> entryView = map.getEntryView(1);
    final long lastAccessTime = entryView.getLastAccessTime();
    //1. Shift lastAccessTime.
    map.containsKey(1);
    entryView = map.getEntryView(1);
    final long lastAccessTimeAfterContainsOperation = entryView.getLastAccessTime();
    //2. Expecting lastAccessTime to be shifted by containsKey operation.
    final long diffSecs = TimeUnit.MILLISECONDS.toSeconds(lastAccessTimeAfterContainsOperation - lastAccessTime);
    //3. So there should be a diff at least waitSeconds.
    final String failureMessage = format("Diff seconds %d, wait seconds %d", diffSecs, waitSeconds);
    assertTrue(failureMessage, diffSecs >= waitSeconds);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) MaxSizeConfig(com.hazelcast.config.MaxSizeConfig) MapConfig(com.hazelcast.config.MapConfig) EntryListenerConfig(com.hazelcast.config.EntryListenerConfig) Config(com.hazelcast.config.Config) NearCacheConfig(com.hazelcast.config.NearCacheConfig) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 82 with HazelcastInstance

use of com.hazelcast.core.HazelcastInstance in project hazelcast by hazelcast.

the class EvictionTest method testIssue585ZeroTTLShouldPreventEvictionWithSet.

@Test
public void testIssue585ZeroTTLShouldPreventEvictionWithSet() throws InterruptedException {
    Config config = getConfig();
    config.getGroupConfig().setName("testIssue585ZeroTTLShouldPreventEvictionWithSet");
    NearCacheConfig nearCacheConfig = new NearCacheConfig();
    config.getMapConfig("default").setNearCacheConfig(nearCacheConfig);
    int n = 1;
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(n);
    HazelcastInstance h = factory.newHazelcastInstance(config);
    IMap<String, String> map = h.getMap("testIssue585ZeroTTLShouldPreventEvictionWithSet");
    map.set("key", "value", 1, TimeUnit.SECONDS);
    map.set("key", "value2", 0, TimeUnit.SECONDS);
    sleepSeconds(2);
    assertEquals("value2", map.get("key"));
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) MaxSizeConfig(com.hazelcast.config.MaxSizeConfig) MapConfig(com.hazelcast.config.MapConfig) EntryListenerConfig(com.hazelcast.config.EntryListenerConfig) Config(com.hazelcast.config.Config) NearCacheConfig(com.hazelcast.config.NearCacheConfig) NearCacheConfig(com.hazelcast.config.NearCacheConfig) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 83 with HazelcastInstance

use of com.hazelcast.core.HazelcastInstance in project hazelcast by hazelcast.

the class EvictionTest method testIssue304EvictionDespitePut.

@Test
public void testIssue304EvictionDespitePut() throws InterruptedException {
    Config config = getConfig();
    config.getGroupConfig().setName("testIssue304EvictionDespitePut");
    MapConfig mapConfig = config.getMapConfig("testIssue304EvictionDespitePut");
    mapConfig.setMaxIdleSeconds(5);
    HazelcastInstance hazelcastInstance = createHazelcastInstance(config);
    IMap<String, Long> map = hazelcastInstance.getMap("testIssue304EvictionDespitePut");
    final AtomicInteger evictCount = new AtomicInteger(0);
    map.addEntryListener(new EntryAdapter<String, Long>() {

        public void entryEvicted(EntryEvent<String, Long> event) {
            evictCount.incrementAndGet();
        }
    }, true);
    String key = "key";
    for (int i = 0; i < 5; i++) {
        map.put(key, System.currentTimeMillis());
        sleepMillis(500);
    }
    assertEquals(evictCount.get(), 0);
    assertNotNull(map.get(key));
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MaxSizeConfig(com.hazelcast.config.MaxSizeConfig) MapConfig(com.hazelcast.config.MapConfig) EntryListenerConfig(com.hazelcast.config.EntryListenerConfig) Config(com.hazelcast.config.Config) NearCacheConfig(com.hazelcast.config.NearCacheConfig) MapConfig(com.hazelcast.config.MapConfig) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 84 with HazelcastInstance

use of com.hazelcast.core.HazelcastInstance in project hazelcast by hazelcast.

the class EvictionTest method testEvictionSpeedTest.

// current eviction check period is 1 second.
// about 30000 records can be put in one second, so the size should be adapted
@Test
public void testEvictionSpeedTest() throws InterruptedException {
    final int k = 3;
    final int size = 10000;
    final CountDownLatch latch = new CountDownLatch(k);
    final String mapName = "testEvictionSpeedTest";
    Config config = getConfig();
    final MapConfig mapConfig = config.getMapConfig(mapName);
    mapConfig.setEvictionPolicy(EvictionPolicy.LRU);
    mapConfig.setEvictionPercentage(25);
    final MaxSizeConfig maxSizeConfig = new MaxSizeConfig();
    maxSizeConfig.setMaxSizePolicy(MaxSizeConfig.MaxSizePolicy.PER_NODE);
    maxSizeConfig.setSize(size);
    mapConfig.setMaxSizeConfig(maxSizeConfig);
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(k);
    final HazelcastInstance[] instances = factory.newInstances(config);
    final AtomicBoolean success = new AtomicBoolean(true);
    new Thread() {

        final IMap map = instances[0].getMap(mapName);

        public void run() {
            try {
                Thread.sleep(1000);
                while (latch.getCount() != 0) {
                    try {
                        int mapSize = map.size();
                        if (mapSize > (size * k + size * k * 10 / 100)) {
                            success.set(false);
                            break;
                        }
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }.start();
    for (int i = 0; i < k; i++) {
        final IMap<String, Integer> map = instances[i].getMap(mapName);
        new Thread() {

            public void run() {
                for (int j = 0; j < size; j++) {
                    map.put(k + "-" + j, j);
                }
                latch.countDown();
            }
        }.start();
    }
    assertTrue(latch.await(10, TimeUnit.MINUTES));
    assertTrue(success.get());
}
Also used : MaxSizeConfig(com.hazelcast.config.MaxSizeConfig) MapConfig(com.hazelcast.config.MapConfig) EntryListenerConfig(com.hazelcast.config.EntryListenerConfig) Config(com.hazelcast.config.Config) NearCacheConfig(com.hazelcast.config.NearCacheConfig) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MaxSizeConfig(com.hazelcast.config.MaxSizeConfig) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IMap(com.hazelcast.core.IMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) MapConfig(com.hazelcast.config.MapConfig) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 85 with HazelcastInstance

use of com.hazelcast.core.HazelcastInstance in project hazelcast by hazelcast.

the class EvictionTest method createMapWithReadBackupDataEnabled.

private IMap<Integer, Integer> createMapWithReadBackupDataEnabled(int maxIdleSeconds) {
    final String mapName = randomMapName();
    Config config = getConfig();
    config.getMapConfig(mapName).setMaxIdleSeconds(maxIdleSeconds).setReadBackupData(true);
    TestHazelcastInstanceFactory hazelcastInstanceFactory = createHazelcastInstanceFactory(2);
    HazelcastInstance[] hazelcastInstances = hazelcastInstanceFactory.newInstances(config);
    return hazelcastInstances[0].getMap(mapName);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) MaxSizeConfig(com.hazelcast.config.MaxSizeConfig) MapConfig(com.hazelcast.config.MapConfig) EntryListenerConfig(com.hazelcast.config.EntryListenerConfig) Config(com.hazelcast.config.Config) NearCacheConfig(com.hazelcast.config.NearCacheConfig) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory)

Aggregations

HazelcastInstance (com.hazelcast.core.HazelcastInstance)2084 Test (org.junit.Test)1684 QuickTest (com.hazelcast.test.annotation.QuickTest)1466 ParallelTest (com.hazelcast.test.annotation.ParallelTest)1389 Config (com.hazelcast.config.Config)815 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)665 AssertTask (com.hazelcast.test.AssertTask)263 MapConfig (com.hazelcast.config.MapConfig)254 CountDownLatch (java.util.concurrent.CountDownLatch)251 NightlyTest (com.hazelcast.test.annotation.NightlyTest)230 MapStoreConfig (com.hazelcast.config.MapStoreConfig)169 IMap (com.hazelcast.core.IMap)149 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)144 ClientConfig (com.hazelcast.client.config.ClientConfig)137 Before (org.junit.Before)111 NearCacheConfig (com.hazelcast.config.NearCacheConfig)106 Member (com.hazelcast.core.Member)99 Map (java.util.Map)96 SlowTest (com.hazelcast.test.annotation.SlowTest)94 SqlPredicate (com.hazelcast.query.SqlPredicate)83