@Test
 public void valuesToArray() {
   HazelcastClient hClient = getHazelcastClient();
   IMap map = hClient.getMap("valuesToArray");
   assertEquals(0, map.size());
   map.put("a", "1");
   map.put("b", "2");
   map.put("c", "3");
   assertEquals(3, map.size());
   {
     final Object[] values = map.values().toArray();
     Arrays.sort(values);
     assertArrayEquals(new Object[] {"1", "2", "3"}, values);
   }
   {
     final String[] values = (String[]) map.values().toArray(new String[3]);
     Arrays.sort(values);
     assertArrayEquals(new String[] {"1", "2", "3"}, values);
   }
   {
     final String[] values = (String[]) map.values().toArray(new String[2]);
     Arrays.sort(values);
     assertArrayEquals(new String[] {"1", "2", "3"}, values);
   }
   {
     final String[] values = (String[]) map.values().toArray(new String[5]);
     Arrays.sort(values, 0, 3);
     assertArrayEquals(new String[] {"1", "2", "3", null, null}, values);
   }
 }
 @Test
 public void addIndex() {
   HazelcastClient hClient = getHazelcastClient();
   IMap map = hClient.getMap("addIndex");
   int size = 1000;
   for (int i = 0; i < size; i++) {
     map.put(String.valueOf(i), new Employee("name" + i, i, true, 0));
   }
   EntryObject e = new PredicateBuilder().getEntryObject();
   Predicate predicate = e.get("age").equal(23);
   long begin = Clock.currentTimeMillis();
   Set<Entry<Object, Object>> set = map.entrySet(predicate);
   long timeWithoutIndex = Clock.currentTimeMillis() - begin;
   assertEquals(1, set.size());
   assertEquals(size, map.size());
   map.destroy();
   map = hClient.getMap("addIndex");
   map.addIndex("age", true);
   for (int i = 0; i < size; i++) {
     map.put(String.valueOf(i), new Employee("name" + i, i, true, 0));
   }
   begin = Clock.currentTimeMillis();
   set = map.entrySet(predicate);
   long timeWithIndex = Clock.currentTimeMillis() - begin;
   assertEquals(1, set.size());
   assertEquals(size, map.size());
   assertTrue(timeWithoutIndex > 2 * timeWithIndex);
   //    	map.addIndex("age", true);
 }
 @Test
 public void tryPut() throws InterruptedException {
   HazelcastClient hClient = getHazelcastClient();
   IMap<String, String> map = hClient.getMap("tryPut");
   assertEquals(0, map.size());
   Boolean result = map.tryPut("1", "CBDEF", 1, TimeUnit.SECONDS);
   assertTrue(result);
   assertEquals(1, map.size());
 }
 @Test
 public void putWithTTL() throws InterruptedException {
   HazelcastClient hClient = getHazelcastClient();
   IMap<String, String> map = hClient.getMap("putWithTTL");
   assertEquals(0, map.size());
   map.put("1", "CBDEF", 100, TimeUnit.MILLISECONDS);
   assertEquals(1, map.size());
   Thread.sleep(200);
   assertEquals(0, map.size());
 }
 @Test
 public void putIfAbsentWithTtl() throws InterruptedException {
   HazelcastClient hClient = getHazelcastClient();
   IMap<String, String> map = hClient.getMap("putIfAbsentWithTtl");
   String result = map.put("1", "CBDEF");
   assertNull(result);
   assertNull(map.putIfAbsent("2", "C", 100, TimeUnit.MILLISECONDS));
   assertEquals(2, map.size());
   assertEquals("C", map.putIfAbsent("2", "D", 100, TimeUnit.MILLISECONDS));
   Thread.sleep(100);
   assertEquals(1, map.size());
 }
Esempio n. 6
0
  @Test
  public void testMapRecordEviction() throws InterruptedException {
    int size = 100000;
    Config cfg = new Config();
    MapConfig mc = cfg.getMapConfig("testMapRecordEviction");
    mc.setTimeToLiveSeconds(1);
    final CountDownLatch latch = new CountDownLatch(size);
    mc.addEntryListenerConfig(
        new EntryListenerConfig()
            .setImplementation(
                new EntryAdapter() {
                  public void entryEvicted(EntryEvent event) {
                    latch.countDown();
                  }
                })
            .setLocal(true));

    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    final HazelcastInstance[] instances = factory.newInstances(cfg);

    IMap map = instances[0].getMap("testMapRecordEviction");
    for (int i = 0; i < size; i++) {
      map.put(i, i);
    }
    assertTrue(latch.await(5, TimeUnit.MINUTES));
    assertEquals(0, map.size());
  }
Esempio n. 7
0
  @Test
  public void testMapRecordIdleEvictionOnMigration() throws InterruptedException {
    Config cfg = new Config();
    final String name = "testMapRecordIdleEvictionOnMigration";
    MapConfig mc = cfg.getMapConfig(name);
    int maxIdleSeconds = 10;
    int size = 100;
    final int nsize = size / 5;
    mc.setMaxIdleSeconds(maxIdleSeconds);
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(3);

    HazelcastInstance instance1 = factory.newHazelcastInstance(cfg);
    final IMap map = instance1.getMap(name);
    final CountDownLatch latch = new CountDownLatch(size - nsize);
    map.addEntryListener(
        new EntryAdapter() {
          public void entryEvicted(EntryEvent event) {
            latch.countDown();
          }
        },
        false);

    for (int i = 0; i < size; i++) {
      map.put(i, i);
    }
    final Thread thread =
        new Thread(
            new Runnable() {
              public void run() {
                while (!Thread.currentThread().isInterrupted()) {
                  try {
                    for (int i = 0; i < nsize; i++) {
                      map.get(i);
                    }
                    Thread.sleep(1000);
                  } catch (HazelcastInstanceNotActiveException e) {
                    return;
                  } catch (InterruptedException e) {
                    return;
                  }
                }
              }
            });
    thread.start();
    HazelcastInstance instance2 = factory.newHazelcastInstance(cfg);
    HazelcastInstance instance3 = factory.newHazelcastInstance(cfg);

    assertTrue(latch.await(1, TimeUnit.MINUTES));
    Assert.assertEquals(nsize, map.size());

    thread.interrupt();
    thread.join(5000);
  }
 @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. 9
0
 /*
    github issue 585
 */
 @Test
 public void testIssue585SetWithoutTTL() 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");
   Thread.sleep(2000);
   assertEquals(0, map.size());
   h.getLifecycleService().shutdown();
 }
Esempio n. 10
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 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));
 }
Esempio n. 12
0
  @Test
  public void testMapWideEviction() throws InterruptedException {
    int size = 10000;

    Config cfg = new Config();
    MapConfig mc = cfg.getMapConfig("testMapWideEviction");
    mc.setEvictionPolicy(MapConfig.EvictionPolicy.LRU);
    mc.setEvictionPercentage(25);
    MaxSizeConfig msc = new MaxSizeConfig();
    msc.setMaxSizePolicy(MaxSizeConfig.MaxSizePolicy.PER_NODE);
    msc.setSize(size);
    mc.setMaxSizeConfig(msc);
    int n = 3;
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(n);
    final HazelcastInstance[] instances = factory.newInstances(cfg);

    IMap map = instances[0].getMap("testMapWideEviction");
    for (int i = 0; i < size; i++) {
      map.put(i, i);
    }
    Thread.sleep(1200);

    assertTrue(map.size() <= (size * n * (100 - mc.getEvictionPercentage()) / 100));
  }
 @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));
 }
 @Test
 public void getSize() {
   HazelcastClient hClient = getHazelcastClient();
   IMap map = hClient.getMap("getSize");
   assertEquals(0, map.size());
   map.put("a", "b");
   assertEquals(1, map.size());
   for (int i = 0; i < 100; i++) {
     map.put(String.valueOf(i), String.valueOf(i));
   }
   assertEquals(101, map.size());
   map.remove("a");
   assertEquals(100, map.size());
   for (int i = 0; i < 50; i++) {
     map.remove(String.valueOf(i));
   }
   assertEquals(50, map.size());
   for (int i = 50; i < 100; i++) {
     map.remove(String.valueOf(i));
   }
   assertEquals(0, map.size());
 }