@Test
 public void testIssue321() throws Exception {
   HazelcastClient hClient = getHazelcastClient();
   final IMap<Integer, Integer> imap = hClient.getMap("testIssue321_1");
   final BlockingQueue<EntryEvent<Integer, Integer>> events1 =
       new LinkedBlockingQueue<EntryEvent<Integer, Integer>>();
   final BlockingQueue<EntryEvent<Integer, Integer>> events2 =
       new LinkedBlockingQueue<EntryEvent<Integer, Integer>>();
   imap.addEntryListener(
       new EntryAdapter<Integer, Integer>() {
         @Override
         public void entryAdded(EntryEvent event) {
           events2.add(event);
         }
       },
       false);
   imap.addEntryListener(
       new EntryAdapter<Integer, Integer>() {
         @Override
         public void entryAdded(EntryEvent event) {
           events1.add(event);
         }
       },
       true);
   imap.put(1, 1);
   final EntryEvent<Integer, Integer> event1 = events1.poll(10, TimeUnit.MILLISECONDS);
   final EntryEvent<Integer, Integer> event2 = events2.poll(10, TimeUnit.MILLISECONDS);
   assertNotNull(event1);
   assertNotNull(event2);
   assertNotNull(event1.getValue());
   assertNull(event2.getValue());
 }
  /* github issue #183 */
  @Test
  public void testKeyBasedListeners() throws InterruptedException {
    try {
      Config config = new Config();
      HazelcastInstance instance = Hazelcast.newHazelcastInstance(config);
      IMap<String, String> map = instance.getMap("map");
      map.put("key1", "value1");
      map.put("key2", "value2");
      map.put("key3", "value3");

      ClientConfig clientConfig = new ClientConfig();
      HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig);

      final AtomicInteger count = new AtomicInteger(0);
      IMap<String, String> clientMap = client.getMap("map");

      clientMap.addEntryListener(
          new EntryListener<String, String>() {
            public void entryAdded(EntryEvent<String, String> entryEvent) {
              count.incrementAndGet();
            }

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

            public void entryUpdated(EntryEvent<String, String> entryEvent) {
              count.incrementAndGet();
            }

            public void entryEvicted(EntryEvent<String, String> entryEvent) {}
          },
          "key1",
          true);

      clientMap.addEntryListener(
          new EntryListener<String, String>() {
            public void entryAdded(EntryEvent<String, String> entryEvent) {
              count.incrementAndGet();
            }

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

            public void entryUpdated(EntryEvent<String, String> entryEvent) {
              System.out.println("event map");
              count.incrementAndGet();
            }

            public void entryEvicted(EntryEvent<String, String> entryEvent) {}
          },
          "key2",
          true);

      map.put("key1", "new-value1");
      Thread.sleep(100);
      Assert.assertEquals(count.get(), 1);
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
Пример #3
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 removeListener() throws InterruptedException, IOException {
   HazelcastClient hClient = getHazelcastClient();
   final IMap<String, String> map = hClient.getMap("removeListener");
   final CountDownLatch entryAddLatch = new CountDownLatch(5);
   final CountDownLatch entryUpdatedLatch = new CountDownLatch(5);
   final CountDownLatch entryRemovedLatch = new CountDownLatch(5);
   CountDownLatchEntryListener<String, String> listener1 =
       new CountDownLatchEntryListener<String, String>(
           entryAddLatch, entryUpdatedLatch, entryRemovedLatch);
   CountDownLatchEntryListener<String, String> listener2 =
       new CountDownLatchEntryListener<String, String>(
           entryAddLatch, entryUpdatedLatch, entryRemovedLatch);
   map.addEntryListener(listener1, true);
   map.put("hello", "world");
   map.put("hello", "new world");
   map.remove("hello");
   Thread.sleep(100);
   assertEquals(4, entryAddLatch.getCount());
   assertEquals(4, entryRemovedLatch.getCount());
   assertEquals(4, entryUpdatedLatch.getCount());
   map.removeEntryListener(listener1);
   map.put("hello", "world");
   map.put("hello", "new world");
   map.remove("hello");
   Thread.sleep(100);
   assertEquals(4, entryAddLatch.getCount());
   assertEquals(4, entryRemovedLatch.getCount());
   assertEquals(4, entryUpdatedLatch.getCount());
 }
Пример #5
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);
  }
Пример #6
0
  @Test
  public void testMapPutTtlWithListener() throws InterruptedException {
    Config cfg = new Config();
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    final HazelcastInstance[] instances = factory.newInstances(cfg);
    warmUpPartitions(instances);

    final int k = 10;
    final int putCount = 10000;
    final CountDownLatch latch = new CountDownLatch(k * putCount);
    final IMap map = instances[0].getMap("testMapEvictionTtlWithListener");

    final AtomicBoolean error = new AtomicBoolean(false);
    final Set<Long> times = Collections.newSetFromMap(new ConcurrentHashMap<Long, Boolean>());

    map.addEntryListener(
        new EntryAdapter() {
          public void entryEvicted(final EntryEvent event) {
            final Long expectedEvictionTime = (Long) (event.getOldValue());
            long timeDifference = System.currentTimeMillis() - expectedEvictionTime;
            if (timeDifference > 5000) {
              error.set(true);
              times.add(timeDifference);
            }
            latch.countDown();
          }
        },
        true);

    for (int i = 0; i < k; i++) {
      final int threadId = i;
      int ttl = (int) (Math.random() * 5000 + 3000);
      for (int j = 0; j < putCount; j++) {
        final long expectedEvictionTime = ttl + System.currentTimeMillis();
        map.put(j + putCount * threadId, expectedEvictionTime, ttl, TimeUnit.MILLISECONDS);
      }
    }

    assertTrue(latch.await(1, TimeUnit.MINUTES));
    assertFalse("Some evictions took more than 3 seconds! -> " + times, error.get());
  }
Пример #7
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();
  }
Пример #8
0
 /**
  * Test for the issue 537. Eviction event is fired for an object already removed
  *
  * @throws Exception
  */
 @Test
 public void testEvictionAfterRemove() throws InterruptedException {
   Config cfg = new Config();
   TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(1);
   HazelcastInstance instance = factory.newHazelcastInstance(cfg);
   IMap<Object, Object> map = instance.getMap("map");
   final AtomicInteger count = new AtomicInteger(0);
   map.addEntryListener(
       new EntryAdapter<Object, Object>() {
         @Override
         public void entryEvicted(EntryEvent<Object, Object> event) {
           count.incrementAndGet();
         }
       },
       true);
   map.put(1, 1, 1, TimeUnit.SECONDS);
   map.put(2, 2, 1, TimeUnit.SECONDS);
   map.remove(1);
   Thread.sleep(2000);
   assertEquals(1, count.get());
 }
 @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 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));
 }
Пример #11
0
 @Test
 public void testMigrationOfTTLAndLock() throws Exception {
   Config config = new Config();
   final HazelcastInstance h1 = Hazelcast.newHazelcastInstance(config);
   final HazelcastInstance h2 = Hazelcast.newHazelcastInstance(config);
   ConcurrentMapManager cmm1 = getConcurrentMapManager(h1);
   ConcurrentMapManager cmm2 = getConcurrentMapManager(h2);
   final IMap imap1 = h1.getMap("default");
   final IMap imap2 = h2.getMap("default");
   final Data dKey = toData("1");
   assertTrue(migrateKey("1", h1, h1, 0));
   assertTrue(migrateKey("1", h1, h2, 1));
   imap1.put("1", "value1", 60, TimeUnit.SECONDS);
   imap1.lock("1");
   Future put2 = imap2.putAsync("1", "value2");
   imap2.addEntryListener(
       new EntryAdapter() {
         @Override
         public void entryUpdated(EntryEvent entryEvent) {
           System.out.println(entryEvent);
         }
       },
       "1",
       true);
   if (put2 == null) fail();
   Thread.sleep(1000);
   CMap cmap1 = getCMap(h1, "default");
   CMap cmap2 = getCMap(h2, "default");
   Record record1 = cmap1.getRecord(dKey);
   assertEquals(1, record1.getScheduledActionCount());
   for (ScheduledAction scheduledAction : record1.getScheduledActions()) {
     assertTrue(scheduledAction.isValid());
   }
   assertNotNull(record1.getListeners());
   assertEquals(1, record1.getListeners().size());
   DistributedLock lock = cmap1.getRecord(dKey).getLock();
   assertTrue(cmm1.thisAddress.equals(lock.getLockAddress()));
   assertTrue(lock.getLockThreadId() != -1);
   assertEquals(1, lock.getLockCount());
   assertEquals(1, cmap1.mapRecords.size());
   assertEquals(1, cmap2.mapRecords.size());
   assertEquals(0, cmap2.getMapIndexService().getOwnedRecords().size());
   assertEquals(1, cmap1.getMapIndexService().getOwnedRecords().size());
   assertTrue(migrateKey("1", h1, h2, 0));
   assertTrue(migrateKey("1", h1, h1, 1));
   assertEquals(1, cmap1.mapRecords.size());
   assertEquals(1, cmap2.mapRecords.size());
   assertEquals(0, cmap1.getMapIndexService().getOwnedRecords().size());
   assertEquals(1, cmap2.getMapIndexService().getOwnedRecords().size());
   assertTrue(cmap1.getRecord(dKey).getRemainingTTL() < 60000);
   assertTrue(cmap2.getRecord(dKey).getRemainingTTL() < 60000);
   Record record2 = cmap2.getRecord(dKey);
   lock = record2.getLock();
   assertTrue(cmm1.thisAddress.equals(lock.getLockAddress()));
   assertTrue(lock.getLockThreadId() != -1);
   assertEquals(1, lock.getLockCount());
   lock = cmap1.getRecord(dKey).getLock();
   assertTrue(cmm1.thisAddress.equals(lock.getLockAddress()));
   assertTrue(lock.getLockThreadId() != -1);
   assertEquals(1, lock.getLockCount());
   imap1.unlock("1");
   put2.get(10, TimeUnit.SECONDS);
   assertEquals("value2", imap1.get("1"));
   assertEquals("value2", imap2.get("1"));
   assertNotNull(record2.getListeners());
   assertEquals(1, record2.getListeners().size());
 }