Esempio n. 1
0
  @Test
  public void testZeroResetsTTL() throws InterruptedException {
    Config cfg = new Config();
    MapConfig mc = cfg.getMapConfig("testZeroResetsTTL");
    int ttl = 5;
    mc.setTimeToLiveSeconds(ttl);
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(1);
    HazelcastInstance instance = factory.newHazelcastInstance(cfg);
    IMap<Object, Object> map = instance.getMap("testZeroResetsTTL");
    final CountDownLatch latch = new CountDownLatch(1);
    map.addEntryListener(
        new EntryAdapter<Object, Object>() {
          public void entryEvicted(EntryEvent event) {
            latch.countDown();
          }
        },
        false);

    map.put(1, 1);
    map.put(2, 2);
    map.put(1, 2, 0, TimeUnit.SECONDS);
    latch.await(10, TimeUnit.SECONDS);
    assertNull(map.get(2));
    assertEquals(2, map.get(1));
  }
 @Test
 public void evictFromMap() {
   HazelcastClient hClient = getHazelcastClient();
   IMap map = hClient.getMap("evictFromMap");
   assertNull(map.put("a", "b"));
   assertEquals("b", map.get("a"));
   assertTrue(map.evict("a"));
   assertNull(map.get("a"));
 }
 @Test
 public void clear() {
   HazelcastClient hClient = getHazelcastClient();
   IMap map = hClient.getMap("clear");
   for (int i = 0; i < 100; i++) {
     assertNull(map.put(i, i));
     assertEquals(i, map.get(i));
   }
   map.clear();
   for (int i = 0; i < 100; i++) {
     assertNull(map.get(i));
   }
 }
 @Test
 public void iterateOverMapEntries() {
   HazelcastClient hClient = getHazelcastClient();
   IMap<String, String> map = hClient.getMap("iterateOverMapEntries");
   map.put("1", "A");
   map.put("2", "B");
   map.put("3", "C");
   Set<Entry<String, String>> entrySet = map.entrySet();
   assertEquals(3, entrySet.size());
   Set<String> keySet = map.keySet();
   for (Entry<String, String> entry : entrySet) {
     assertTrue(keySet.contains(entry.getKey()));
     assertEquals(entry.getValue(), map.get(entry.getKey()));
   }
   Iterator<Entry<String, String>> it = entrySet.iterator();
   for (String key : keySet) {
     MapEntry mapEntry = map.getMapEntry(key);
     assertEquals(1, mapEntry.getHits());
   }
   while (it.hasNext()) {
     it.next();
     it.remove();
   }
   assertTrue(map.isEmpty());
 }
 @Test
 public void getMapEntry() {
   HazelcastClient hClient = getHazelcastClient();
   IMap map = hClient.getMap("getMapEntry");
   assertNull(map.put("a", "b"));
   map.get("a");
   map.get("a");
   MapEntry<String, String> entry = map.getMapEntry("a");
   assertEquals("a", entry.getKey());
   assertEquals("b", entry.getValue());
   assertEquals(2, entry.getHits());
   assertEquals("b", entry.getValue());
   assertEquals("b", entry.setValue("c"));
   assertEquals("c", map.get("a"));
   assertEquals("c", entry.getValue());
 }
Esempio n. 6
0
  @Test
  public void testEvictionLFU() {
    try {
      final int k = 1;
      final int size = 10000;

      final String mapName = "testEvictionLFU";
      Config cfg = new Config();
      MapConfig mc = cfg.getMapConfig(mapName);
      mc.setEvictionPolicy(MapConfig.EvictionPolicy.LFU);
      mc.setEvictionPercentage(20);
      MaxSizeConfig msc = new MaxSizeConfig();
      msc.setMaxSizePolicy(MaxSizeConfig.MaxSizePolicy.PER_NODE);
      msc.setSize(size);
      mc.setMaxSizeConfig(msc);

      TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(k);
      final HazelcastInstance[] instances = factory.newInstances(cfg);
      IMap<Object, Object> map = instances[0].getMap(mapName);

      for (int i = 0; i < size / 2; i++) {
        map.put(i, i);
        map.get(i);
      }
      Thread.sleep(1000);
      for (int i = size / 2; i < size; i++) {
        map.put(i, i);
      }

      Thread.sleep(3000);

      Assert.assertFalse("No eviction!?!?!?", map.size() == size);
      boolean isFrequentlyUsedEvicted = false;
      for (int i = 0; i < size / 2; i++) {
        if (map.get(i) == null) {
          isFrequentlyUsedEvicted = true;
          break;
        }
      }
      Assert.assertFalse(isFrequentlyUsedEvicted);
      instances[0].getLifecycleService().shutdown();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
 @Test
 public void removeIfSame() {
   HazelcastClient hClient = getHazelcastClient();
   IMap<String, String> map = hClient.getMap("remove");
   String result = map.put("1", "CBDEF");
   assertNull(result);
   assertFalse(map.remove("1", "CBD"));
   assertEquals("CBDEF", map.get("1"));
   assertTrue(map.remove("1", "CBDEF"));
 }
 @Test
 public void isEmpty() {
   HazelcastClient hClient = getHazelcastClient();
   IMap map = hClient.getMap("isEmpty");
   int counter = 100;
   assertTrue(map.isEmpty());
   for (int i = 0; i < counter; i++) {
     assertNull(map.put(i, i));
     assertEquals(i, map.get(i));
   }
   assertFalse(map.isEmpty());
 }
 @Test
 public void addListenerForKey() throws InterruptedException, IOException {
   HazelcastClient hClient = getHazelcastClient();
   final IMap<String, String> map = hClient.getMap("addListenerForKey");
   map.clear();
   assertEquals(0, map.size());
   final CountDownLatch entryAddLatch = new CountDownLatch(1);
   final CountDownLatch entryUpdatedLatch = new CountDownLatch(1);
   final CountDownLatch entryRemovedLatch = new CountDownLatch(1);
   CountDownLatchEntryListener<String, String> listener =
       new CountDownLatchEntryListener<String, String>(
           entryAddLatch, entryUpdatedLatch, entryRemovedLatch);
   map.addEntryListener(listener, "hello", true);
   assertNull(map.get("hello"));
   map.put("hello", "world");
   map.put("hello", "new world");
   assertEquals("new world", map.get("hello"));
   map.remove("hello");
   assertTrue(entryAddLatch.await(10, TimeUnit.SECONDS));
   assertTrue(entryUpdatedLatch.await(10, TimeUnit.SECONDS));
   assertTrue(entryRemovedLatch.await(10, TimeUnit.SECONDS));
 }
 @Test
 public void containsValue() {
   HazelcastClient hClient = getHazelcastClient();
   IMap map = hClient.getMap("containsValue");
   int counter = 100;
   for (int i = 0; i < counter; i++) {
     assertNull(map.put(i, i));
     assertEquals(i, map.get(i));
   }
   for (int i = 0; i < counter; i++) {
     assertTrue(map.containsValue(i));
   }
 }
 @Test
 public void testIssue508And513() throws Exception {
   HazelcastClient client = getHazelcastClient();
   IMap<String, HashSet<byte[]>> callEventsMap = client.getMap("CALL_EVENTS");
   IMap<String, Long> metaDataMap = client.getMap("CALL_META_DATA");
   IMap<String, byte[]> callStartMap = client.getMap("CALL_START_EVENTS");
   MultiMap<String, String> calls = client.getMultiMap("CALLS");
   calls.lock("1");
   calls.unlock("1");
   byte[] bytes = new byte[10];
   HashSet<byte[]> hashSet = new HashSet<byte[]>();
   hashSet.add(bytes);
   String callId = "1";
   callEventsMap.put(callId, hashSet);
   callStartMap.put(callId, bytes);
   metaDataMap.put(callId, 10L);
   Transaction txn = client.getTransaction();
   txn.begin();
   try {
     // remove the data
     callEventsMap.remove(callId);
     // remove meta data
     metaDataMap.remove(callId);
     // remove call start
     callStartMap.remove(callId);
     calls.put(callId, callId);
     txn.commit();
   } catch (Exception e) {
     fail();
   }
   assertNull(callEventsMap.get(callId));
   assertNull(metaDataMap.get(callId));
   assertNull(callStartMap.get(callId));
   assertEquals(0, callEventsMap.size());
   assertEquals(0, metaDataMap.size());
   assertEquals(0, callStartMap.size());
 }
Esempio n. 12
0
  @Test
  public void testEvictionLFU2() {
    try {
      final int k = 2;
      final int size = 10000;
      final String mapName = "testEvictionLFU2";
      Config cfg = new Config();
      MapConfig mc = cfg.getMapConfig(mapName);
      mc.setEvictionPolicy(MapConfig.EvictionPolicy.LFU);
      mc.setEvictionPercentage(90);
      MaxSizeConfig msc = new MaxSizeConfig();
      msc.setMaxSizePolicy(MaxSizeConfig.MaxSizePolicy.PER_NODE);
      msc.setSize(size);
      mc.setMaxSizeConfig(msc);

      TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(k);
      final HazelcastInstance[] instances = factory.newInstances(cfg);
      IMap<Object, Object> map = instances[0].getMap(mapName);

      for (int i = 0; i < size; i++) {
        map.put(i, i);
      }

      for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 100; j++) {
          assertNotNull(map.get(j));
        }
        for (int j = size - 100; j < size; j++) {
          assertNotNull(map.get(j));
        }
        Thread.sleep(1000);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
 @Test
 public void putAllMany() {
   HazelcastClient hClient = getHazelcastClient();
   IMap map = hClient.getMap("putAllMany");
   int counter = 100;
   for (int j = 0; j < 4; j++, counter *= 10) {
     Map tempMap = new HashMap();
     for (int i = 0; i < counter; i++) {
       tempMap.put(i, i);
     }
     map.putAll(tempMap);
     assertEquals(1, map.get(1));
   }
   map.destroy();
 }
Esempio n. 14
0
 /*
    github issue 585
 */
 @Test
 public void testIssue585ZeroTTLShouldPreventEvictionWithSet() throws InterruptedException {
   Config config = new Config();
   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);
   Thread.sleep(2000);
   assertEquals("value2", map.get("key"));
   h.getLifecycleService().shutdown();
 }
 @Test
 public void destroyMap() throws InterruptedException {
   HazelcastClient hClient = getHazelcastClient();
   IMap map = hClient.getMap("destroy");
   for (int i = 0; i < 100; i++) {
     assertNull(map.put(i, i));
     assertEquals(i, map.get(i));
   }
   IMap<Integer, Integer> map2 = hClient.getMap("destroy");
   assertTrue(map == map2);
   assertTrue(map.getId().equals(map2.getId()));
   map.destroy();
   //        map2 = hClient.getMap("destroy");
   //        assertFalse(map == map2);
   for (int i = 0; i < 100; i++) {
     //            assertNull(map2.get(i));
   }
 }
Esempio n. 16
0
  @Test
  public void testEvictionLRU() {
    final int k = 2;
    final int size = 10000;

    try {
      final String mapName = "testEvictionLRU";
      Config cfg = new Config();
      MapConfig mc = cfg.getMapConfig(mapName);
      mc.setEvictionPolicy(MapConfig.EvictionPolicy.LRU);
      mc.setEvictionPercentage(10);
      MaxSizeConfig msc = new MaxSizeConfig();
      msc.setMaxSizePolicy(MaxSizeConfig.MaxSizePolicy.PER_NODE);
      msc.setSize(size);
      mc.setMaxSizeConfig(msc);

      TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(k);
      final HazelcastInstance[] instances = factory.newInstances(cfg);
      IMap<Object, Object> map = instances[0].getMap(mapName);
      Thread.sleep(1000);

      for (int i = size / 2; i < size; i++) {
        map.put(i, i);
      }
      Thread.sleep(2000);
      for (int i = 0; i < size / 2; i++) {
        map.put(i, i);
      }
      Thread.sleep(1000);

      int recentlyUsedEvicted = 0;
      for (int i = 0; i < size / 2; i++) {
        if (map.get(i) == null) {
          recentlyUsedEvicted++;
        }
      }
      Assert.assertTrue(recentlyUsedEvicted == 0);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
Esempio n. 17
0
  /*
     github issue 304
  */
  @Test
  public void testIssue304EvictionDespitePut() throws InterruptedException {
    Config c = new Config();
    c.getGroupConfig().setName("testIssue304EvictionDespitePut");
    final HashMap<String, MapConfig> mapConfigs = new HashMap<String, MapConfig>();
    final MapConfig value = new MapConfig();
    value.setMaxIdleSeconds(3);
    mapConfigs.put("default", value);
    c.setMapConfigs(mapConfigs);
    final Properties properties = new Properties();
    properties.setProperty("hazelcast.map.cleanup.delay.seconds", "1"); // we need faster cleanups
    c.setProperties(properties);
    int n = 1;
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(n);
    final HazelcastInstance hazelcastInstance = factory.newHazelcastInstance(c);

    IMap<String, Long> map = hazelcastInstance.getMap("testIssue304EvictionDespitePutMap");
    final AtomicInteger evictCount = new AtomicInteger(0);
    map.addEntryListener(
        new EntryListener<String, Long>() {
          public void entryAdded(EntryEvent<String, Long> event) {}

          public void entryRemoved(EntryEvent<String, Long> event) {}

          public void entryUpdated(EntryEvent<String, Long> event) {}

          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());
      Thread.sleep(1000);
    }
    assertEquals(evictCount.get(), 0);
    assertNotNull(map.get(key));
    hazelcastInstance.getLifecycleService().shutdown();
  }
 @Test
 public void putAll() {
   HazelcastClient hClient = getHazelcastClient();
   IMap map = hClient.getMap("putAll");
   int counter = 100;
   Set keys = new HashSet(counter);
   for (int i = 0; i < counter; i++) {
     keys.add(i);
   }
   Map all = map.getAll(keys);
   assertEquals(0, all.size());
   Map tempMap = new HashMap();
   for (int i = 0; i < counter; i++) {
     tempMap.put(i, i);
   }
   map.putAll(tempMap);
   for (int i = 0; i < counter; i++) {
     assertEquals(i, map.get(i));
   }
   all = map.getAll(keys);
   assertEquals(counter, all.size());
 }
 @Test
 public void addListenerAndMultiPut() throws InterruptedException, IOException {
   HazelcastClient hClient = getHazelcastClient();
   final IMap<String, byte[]> map = hClient.getMap("addListenerAndMultiPut");
   map.clear();
   int counter = 100;
   assertEquals(0, map.size());
   final CountDownLatch entryAddLatch = new CountDownLatch(counter);
   final CountDownLatch entryUpdatedLatch = new CountDownLatch(counter);
   final CountDownLatch entryRemovedLatch = new CountDownLatch(counter);
   CountDownLatchEntryListener<String, byte[]> listener =
       new CountDownLatchEntryListener<String, byte[]>(
           entryAddLatch, entryUpdatedLatch, entryRemovedLatch);
   map.addEntryListener(listener, true);
   assertNull(map.get("hello"));
   Map<String, byte[]> many = new HashMap<String, byte[]>();
   for (int i = 0; i < counter; i++) {
     many.put("" + i, new byte[i]);
   }
   map.putAll(many);
   assertTrue(entryAddLatch.await(10, TimeUnit.SECONDS));
   //        assertTrue(entryUpdatedLatch.await(10, TimeUnit.MILLISECONDS));
   //        assertTrue(entryRemovedLatch.await(10, TimeUnit.MILLISECONDS));
 }
 /**
  * Returns the status of whether the worker is enabled or not
  *
  * @param id the id of the worker to test
  * @return true if the worker is enabled, false otherwise
  */
 @Override
 public boolean workerEnabled(String id) {
   return workerEnabled.containsKey(id) && workerEnabled.get(id);
 }