@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());
 }
 @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 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());
 }
Exemple #4
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 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(timeout = 120000)
  public void testSlowStore() throws Exception {
    final TestMapStore store = new WaitingOnFirstTestMapStore();
    Config config = getConfig();
    MapStoreConfig mapStoreConfig = new MapStoreConfig();
    mapStoreConfig.setEnabled(true);
    mapStoreConfig.setWriteDelaySeconds(1);
    mapStoreConfig.setImplementation(store);
    config.getMapConfig("default").setMapStoreConfig(mapStoreConfig);
    HazelcastInstance h1 = createHazelcastInstance(config);
    final IMap<Integer, Integer> map = h1.getMap("testSlowStore");
    int count = 1000;
    for (int i = 0; i < count; i++) {
      map.put(i, 1);
    }
    Thread.sleep(2000); // sleep for scheduling following puts to a different second
    for (int i = 0; i < count; i++) {
      map.put(i, 2);
    }
    for (int i = 0; i < count; i++) {
      final int index = i;
      assertTrueEventually(
          new AssertTask() {
            @Override
            public void run() throws Exception {
              final Integer valueInMap = map.get(index);
              final Integer valueInStore = (Integer) store.getStore().get(index);

              assertEquals(valueInMap, valueInStore);
            }
          });
    }
  }
  @Test
  public void testClientListenerDisconnected() throws InterruptedException {
    Config config = new Config();
    config.setProperty(GroupProperties.PROP_IO_THREAD_COUNT, "1");

    final HazelcastInstance hz = Hazelcast.newHazelcastInstance(config);
    final HazelcastInstance hz2 = Hazelcast.newHazelcastInstance(config);

    int clientCount = 10;
    ClientDisconnectedListenerLatch listenerLatch =
        new ClientDisconnectedListenerLatch(2 * clientCount);
    hz.getClientService().addClientListener(listenerLatch);
    hz2.getClientService().addClientListener(listenerLatch);

    Collection<HazelcastInstance> clients = new LinkedList<HazelcastInstance>();
    for (int i = 0; i < clientCount; i++) {
      HazelcastInstance client = HazelcastClient.newHazelcastClient();
      IMap<Object, Object> map = client.getMap(randomMapName());

      map.addEntryListener(new EntryAdapter<Object, Object>(), true);
      map.put(generateKeyOwnedBy(hz), "value");
      map.put(generateKeyOwnedBy(hz2), "value");

      clients.add(client);
    }

    ExecutorService ex = Executors.newFixedThreadPool(4);
    try {
      for (final HazelcastInstance client : clients) {
        ex.execute(
            new Runnable() {
              @Override
              public void run() {
                client.shutdown();
              }
            });
      }

      assertOpenEventually(listenerLatch, 30);

      assertTrueEventually(
          new AssertTask() {
            @Override
            public void run() throws Exception {
              assertEquals(0, hz.getClientService().getConnectedClients().size());
            }
          },
          10);
      assertTrueEventually(
          new AssertTask() {
            @Override
            public void run() throws Exception {
              assertEquals(0, hz2.getClientService().getConnectedClients().size());
            }
          },
          10);
    } finally {
      ex.shutdown();
    }
  }
  @Test
  public void testHigherBatchSize_shouldNotCauseAnyInvalidation_onClient() throws Exception {
    Config config = getConfig();
    configureBatching(config, true, Integer.MAX_VALUE, Integer.MAX_VALUE);
    HazelcastInstance server = factory.newHazelcastInstance(config);

    factory.newHazelcastInstance(config);

    ClientConfig clientConfig = newClientConfig(mapName);

    HazelcastInstance client = factory.newHazelcastClient(clientConfig);

    IMap<Integer, Integer> serverMap = server.getMap(mapName);
    IMap<Integer, Integer> clientMap = client.getMap(mapName);

    int size = 1000;

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

    // fill near-cache on client
    for (int i = 0; i < size; i++) {
      clientMap.get(i);
    }

    // generate invalidation data.
    for (int i = 0; i < size; i++) {
      serverMap.put(i, i);
    }

    assertNearCacheSizeEventually(clientMap, size);
  }
Exemple #9
0
  @Test
  public void testMapContainsKey_withNearCache() {
    int n = 3;
    String mapName = "test";

    Config config = new Config();
    config
        .getMapConfig(mapName)
        .setNearCacheConfig(new NearCacheConfig().setInvalidateOnChange(true));
    HazelcastInstance instance = createHazelcastInstanceFactory(n).newInstances(config)[0];

    IMap<String, String> map = instance.getMap("mapName");
    map.put("key1", "value1");
    map.put("key2", "value2");
    map.put("key3", "value3");

    map.get("key1");
    map.get("key2");
    map.get("key3");
    assertEquals(map.containsKey("key1"), true);
    assertEquals(map.containsKey("key5"), false);
    map.remove("key1");
    assertEquals(map.containsKey("key1"), false);
    assertEquals(map.containsKey("key2"), true);
    assertEquals(map.containsKey("key5"), false);
  }
  @Test
  public void testBatchInvalidationRemovesEntries() throws Exception {
    Config config = getConfig();
    configureBatching(config, true, 10, 1);
    HazelcastInstance server = factory.newHazelcastInstance(config);
    factory.newHazelcastInstance(config);

    ClientConfig clientConfig = newClientConfig(mapName);

    HazelcastInstance client = factory.newHazelcastClient(clientConfig);

    IMap<Integer, Integer> serverMap = server.getMap(mapName);
    IMap<Integer, Integer> clientMap = client.getMap(mapName);

    int size = 1000;

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

    // fill near-cache on client
    for (int i = 0; i < size; i++) {
      clientMap.get(i);
    }

    // generate invalidation data.
    for (int i = 0; i < size; i++) {
      serverMap.put(i, i);
    }

    assertNearCacheSizeEventually(clientMap, 0);
  }
  @Test
  public void testOperationNotBlockingAfterClusterShutdown() throws InterruptedException {
    final HazelcastInstance instance1 = Hazelcast.newHazelcastInstance();
    final HazelcastInstance instance2 = Hazelcast.newHazelcastInstance();

    final ClientConfig clientConfig = new ClientConfig();
    clientConfig.getNetworkConfig().setConnectionAttemptLimit(Integer.MAX_VALUE);
    final HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig);
    final IMap<String, String> m = client.getMap("m");

    m.put("elif", "Elif");
    m.put("ali", "Ali");
    m.put("alev", "Alev");

    instance1.getLifecycleService().terminate();
    instance2.getLifecycleService().terminate();

    final CountDownLatch latch = new CountDownLatch(1);
    new Thread() {
      public void run() {
        try {
          m.get("ali");
        } catch (Exception ignored) {
          latch.countDown();
        }
      }
    }.start();

    assertOpenEventually(latch);
  }
 public void doFunctionalQueryTest(IMap imap) {
   Employee em = new Employee("joe", 33, false, 14.56);
   imap.put("1", new Employee("joe", 33, false, 14.56));
   imap.put("2", new Employee("ali", 23, true, 15.00));
   for (int i = 3; i < 103; i++) {
     imap.put(
         String.valueOf(i), new Employee("name" + i, i % 60, ((i % 2) == 1), Double.valueOf(i)));
   }
   Set<Map.Entry> entries = imap.entrySet();
   assertEquals(102, entries.size());
   int itCount = 0;
   for (Map.Entry entry : entries) {
     Employee c = (Employee) entry.getValue();
     itCount++;
   }
   assertEquals(102, itCount);
   EntryObject e = new PredicateBuilder().getEntryObject();
   Predicate predicate = e.is("active").and(e.get("age").equal(23));
   entries = imap.entrySet(predicate);
   assertEquals(3, entries.size());
   for (Map.Entry entry : entries) {
     Employee c = (Employee) entry.getValue();
     assertEquals(c.getAge(), 23);
     assertTrue(c.isActive());
   }
   imap.remove("2");
   entries = imap.entrySet(predicate);
   assertEquals(2, entries.size());
   for (Map.Entry entry : entries) {
     Employee c = (Employee) entry.getValue();
     assertEquals(c.getAge(), 23);
     assertTrue(c.isActive());
   }
 }
  @Test(timeout = 120000)
  public void issue587CallMapLoaderDuringRemoval() {
    final AtomicInteger loadCount = new AtomicInteger(0);
    final AtomicInteger storeCount = new AtomicInteger(0);
    final AtomicInteger deleteCount = new AtomicInteger(0);
    class SimpleMapStore2 extends SimpleMapStore<String, Long> {

      SimpleMapStore2(ConcurrentMap<String, Long> store) {
        super(store);
      }

      public Long load(String key) {
        loadCount.incrementAndGet();
        return super.load(key);
      }

      public void store(String key, Long value) {
        storeCount.incrementAndGet();
        super.store(key, value);
      }

      public void delete(String key) {
        deleteCount.incrementAndGet();
        super.delete(key);
      }
    }
    final ConcurrentMap<String, Long> store = new ConcurrentHashMap<String, Long>();
    final MapStore<String, Long> myMapStore = new SimpleMapStore2(store);
    Config config = getConfig();
    config
        .getMapConfig("myMap")
        .setMapStoreConfig(new MapStoreConfig().setImplementation(myMapStore));
    TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(3);
    HazelcastInstance hc = nodeFactory.newHazelcastInstance(config);
    store.put("one", 1l);
    store.put("two", 2l);
    assertEquals(0, loadCount.get());
    assertEquals(0, storeCount.get());
    assertEquals(0, deleteCount.get());
    IMap<String, Long> myMap = hc.getMap("myMap");
    assertEquals(1l, myMap.get("one").longValue());
    assertEquals(2l, myMap.get("two").longValue());
    //        assertEquals(2, loadCount.get());
    assertEquals(0, storeCount.get());
    assertEquals(0, deleteCount.get());
    assertNull(myMap.remove("ten"));
    //        assertEquals(3, loadCount.get());
    assertEquals(0, storeCount.get());
    assertEquals(0, deleteCount.get());
    myMap.put("three", 3L);
    myMap.put("four", 4L);
    //        assertEquals(5, loadCount.get());
    assertEquals(2, storeCount.get());
    assertEquals(0, deleteCount.get());
    myMap.remove("one");
    assertEquals(2, storeCount.get());
    assertEquals(1, deleteCount.get());
    //        assertEquals(5, loadCount.get());
  }
  @Test(timeout = 120000)
  public void testOneMemberWriteThrough() throws Exception {
    TestMapStore testMapStore = new TestMapStore(1, 1, 1);
    testMapStore.setLoadAllKeys(false);
    Config config = newConfig(testMapStore, 0);
    HazelcastInstance h1 = createHazelcastInstance(config);
    Employee employee = new Employee("joe", 25, true, 100.00);
    Employee newEmployee = new Employee("ali", 26, true, 1000);
    testMapStore.insert("1", employee);
    testMapStore.insert("2", employee);
    testMapStore.insert("3", employee);
    testMapStore.insert("4", employee);
    testMapStore.insert("5", employee);
    testMapStore.insert("6", employee);
    testMapStore.insert("7", employee);

    IMap map = h1.getMap("default");
    map.addIndex("name", false);
    assertEquals(0, map.size());
    assertEquals(employee, map.get("1"));
    assertEquals(employee, testMapStore.getStore().get("1"));
    assertEquals(1, map.size());
    assertEquals(employee, map.put("2", newEmployee));
    assertEquals(newEmployee, testMapStore.getStore().get("2"));
    assertEquals(2, map.size());

    map.remove("1");
    map.put("1", employee, 1, TimeUnit.SECONDS);
    map.put("1", employee);
    Thread.sleep(2000);
    assertEquals(employee, testMapStore.getStore().get("1"));
    assertEquals(employee, map.get("1"));

    map.evict("2");
    assertEquals(newEmployee, map.get("2"));

    assertEquals(employee, map.get("3"));
    assertEquals(employee, map.put("3", newEmployee));
    assertEquals(newEmployee, map.get("3"));

    assertEquals(employee, map.remove("4"));

    assertEquals(employee, map.get("5"));
    assertEquals(employee, map.remove("5"));

    assertEquals(employee, map.putIfAbsent("6", newEmployee));
    assertEquals(employee, map.get("6"));
    assertEquals(employee, testMapStore.getStore().get("6"));

    assertTrue(map.containsKey("7"));
    assertEquals(employee, map.get("7"));

    assertNull(map.get("8"));
    assertFalse(map.containsKey("8"));
    assertNull(map.putIfAbsent("8", employee));
    assertEquals(employee, map.get("8"));
    assertEquals(employee, testMapStore.getStore().get("8"));
  }
  /* 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();
    }
  }
  @Test
  public void whenSelectingSomeEntries() {
    map.put("1", "good1");
    map.put("2", "bad");
    map.put("3", "good2");

    Collection<String> result = map.values(new GoodPredicate());

    assertEquals(2, result.size());
    assertContains(result, "good1");
    assertContains(result, "good2");
  }
  @Test
  public void whenSelecting_withoutPredicate() {
    map.put("1", "a");
    map.put("2", "b");
    map.put("3", "c");

    Collection<String> result = map.values();

    assertEquals(3, result.size());
    assertContains(result, "a");
    assertContains(result, "b");
    assertContains(result, "c");
  }
  @Test
  public void whenSelectingAllEntries() {
    map.put("1", "a");
    map.put("2", "b");
    map.put("3", "c");

    Collection<String> result = map.values(TruePredicate.INSTANCE);

    assertEquals(3, result.size());
    assertContains(result, "a");
    assertContains(result, "b");
    assertContains(result, "c");
  }
Exemple #19
0
  @Test
  public void testBasicUsage() throws Exception {
    int n = 3;
    String mapName = "test";

    Config config = new Config();
    config
        .getMapConfig(mapName)
        .setNearCacheConfig(new NearCacheConfig().setInvalidateOnChange(true));
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(n);

    HazelcastInstance[] instances = factory.newInstances(config);
    IMap<Object, Object> map = instances[0].getMap(mapName);

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

    for (HazelcastInstance instance : instances) {
      IMap<Object, Object> m = instance.getMap(mapName);
      for (int i = 0; i < count; i++) {
        Assert.assertNotNull(m.get(i));
      }
    }

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

    for (HazelcastInstance instance : instances) {
      IMap<Object, Object> m = instance.getMap(mapName);
      for (int i = 0; i < count; i++) {
        Assert.assertNotNull(m.get(i));
      }
    }

    for (HazelcastInstance instance : instances) {
      NearCache nearCache = getNearCache(mapName, instance);
      int size = nearCache.size();
      assertTrue("NearCache Size: " + size, size > 0);
    }

    map.clear();
    for (HazelcastInstance instance : instances) {
      NearCache nearCache = getNearCache(mapName, instance);
      int size = nearCache.size();
      assertEquals(0, size);
    }
  }
  public static void main(String[] args) throws ParseException {
    HazelcastInstance hz = Hazelcast.newHazelcastInstance();
    IMap<String, Car> map = hz.getMap("cars");

    map.put("1", new Car("Audi Q7", 250, 22000));
    map.put("2", new Car("BMW X5", 312, 34000));
    map.put("3", new Car("Porsche Cayenne", 408, 57000));

    // We're using a custom attribute called 'attribute' which is provided by the
    // 'CarAttributeExtractor'
    // We are also passing an argument 'mileage' to the extractor
    Set<Car> employees = (Set<Car>) map.values(new SqlPredicate("attribute[mileage] < 30000"));
    System.out.println("Cars:" + employees);
  }
  @Test
  public void testOneQuorumsFailsOneQuorumSuccessForDifferentMaps() throws Exception {
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(3);
    String fourNodeQuorum = randomString();
    QuorumConfig fourNodeQuorumConfig = new QuorumConfig(fourNodeQuorum, true);
    fourNodeQuorumConfig.setQuorumFunctionImplementation(
        new QuorumFunction() {
          @Override
          public boolean apply(Collection<Member> members) {
            return members.size() == 4;
          }
        });

    String threeNodeQuorum = randomString();
    QuorumConfig threeNodeQuorumConfig = new QuorumConfig(threeNodeQuorum, true);
    threeNodeQuorumConfig.setQuorumFunctionImplementation(
        new QuorumFunction() {
          @Override
          public boolean apply(Collection<Member> members) {
            return members.size() == 3;
          }
        });

    MapConfig fourNodeMapConfig = new MapConfig("fourNode");
    fourNodeMapConfig.setQuorumName(fourNodeQuorum);

    MapConfig threeNodeMapConfig = new MapConfig("threeNode");
    threeNodeMapConfig.setQuorumName(threeNodeQuorum);

    Config config = new Config();
    config.addQuorumConfig(threeNodeQuorumConfig);
    config.addQuorumConfig(fourNodeQuorumConfig);
    config.addMapConfig(fourNodeMapConfig);
    config.addMapConfig(threeNodeMapConfig);

    HazelcastInstance h1 = factory.newHazelcastInstance(config);
    HazelcastInstance h2 = factory.newHazelcastInstance(config);
    HazelcastInstance h3 = factory.newHazelcastInstance(config);

    IMap<Object, Object> fourNode = h1.getMap("fourNode");
    IMap<Object, Object> threeNode = h1.getMap("threeNode");
    threeNode.put(generateKeyOwnedBy(h1), "bar");
    try {
      fourNode.put(generateKeyOwnedBy(h1), "bar");
      fail();
    } catch (Exception e) {
    }
  }
  @Test(timeout = 120000)
  public void testMapStoreWriteRemoveOrder() {
    final String mapName = randomMapName("testMapStoreWriteDeleteOrder");
    final int numIterations = 10;
    final int writeDelaySeconds = 3;
    // create map store implementation
    final RecordingMapStore store = new RecordingMapStore(0, 1);
    // create hazelcast config
    final Config config = newConfig(mapName, store, writeDelaySeconds);
    // start hazelcast instance
    final HazelcastInstance hzInstance = createHazelcastInstance(config);
    // loop over num iterations
    final IMap<String, String> map = hzInstance.getMap(mapName);
    for (int k = 0; k < numIterations; k++) {
      String key = String.valueOf(k + 10); // 2 digits for sorting in output
      String value = "v:" + key;
      // add entry
      map.put(key, value);
      // sleep 300ms
      sleepMillis(1);
      // remove entry
      map.remove(key);
    }
    // wait for store to finish
    store.awaitStores();
    // wait for remove to finish
    store.awaitRemoves();

    assertEquals(0, store.getStore().size());
  }
  @Test(timeout = 120000)
  public void testIssue1142ExceptionWhenLoadAllReturnsNull() {
    Config config = getConfig();
    String mapname = "testIssue1142ExceptionWhenLoadAllReturnsNull";
    MapStoreConfig mapStoreConfig = new MapStoreConfig();
    mapStoreConfig.setImplementation(
        new MapStoreAdapter<String, String>() {
          @Override
          public Set<String> loadAllKeys() {
            Set keys = new HashSet();
            keys.add("key");
            return keys;
          }

          public Map loadAll(Collection keys) {
            return null;
          }
        });
    config.getMapConfig(mapname).setMapStoreConfig(mapStoreConfig);
    TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);
    HazelcastInstance instance = nodeFactory.newHazelcastInstance(config);
    HazelcastInstance instance2 = nodeFactory.newHazelcastInstance(config);
    final IMap map = instance.getMap(mapname);
    for (int i = 0; i < 300; i++) {
      map.put(i, i);
    }
    assertEquals(300, map.size());
  }
  @Test(timeout = 120000)
  public void testIssue991EvictedNullIssue() throws InterruptedException {
    MapStoreConfig mapStoreConfig = new MapStoreConfig();
    mapStoreConfig.setEnabled(true);
    mapStoreConfig.setImplementation(
        new MapLoader<String, String>() {
          @Override
          public String load(String key) {
            return null;
          }

          @Override
          public Map<String, String> loadAll(Collection<String> keys) {
            return null;
          }

          @Override
          public Set<String> loadAllKeys() {
            return null;
          }
        });
    Config config = getConfig();
    config.getMapConfig("testIssue991EvictedNullIssue").setMapStoreConfig(mapStoreConfig);
    HazelcastInstance hc = createHazelcastInstance(config);
    IMap<Object, Object> map = hc.getMap("testIssue991EvictedNullIssue");
    map.get("key");
    assertNull(map.get("key"));
    map.put("key", "value");
    Thread.sleep(2000);
    assertEquals("value", map.get("key"));
  }
  @Test(timeout = 120000)
  public void testOneMemberFlush() throws Exception {
    TestMapStore testMapStore = new TestMapStore(1, 1, 1);
    testMapStore.setLoadAllKeys(false);
    int size = 100;
    Config config = newConfig(testMapStore, 200);
    TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(3);
    HazelcastInstance h1 = nodeFactory.newHazelcastInstance(config);
    IMap map = h1.getMap("default");
    assertEquals(0, map.size());
    for (int i = 0; i < size; i++) {
      map.put(i, i);
    }
    assertEquals(size, map.size());
    assertEquals(0, testMapStore.getStore().size());
    assertEquals(size, map.getLocalMapStats().getDirtyEntryCount());
    map.flush();
    assertEquals(size, testMapStore.getStore().size());
    assertEquals(0, map.getLocalMapStats().getDirtyEntryCount());
    assertEquals(size, map.size());

    for (int i = 0; i < size / 2; i++) {
      map.remove(i);
    }
    assertEquals(size / 2, map.size());
    assertEquals(size, testMapStore.getStore().size());
    map.flush();
    assertEquals(size / 2, testMapStore.getStore().size());
    assertEquals(size / 2, map.size());
  }
 @Test(timeout = 120000)
 public void testIssue583MapReplaceShouldTriggerMapStore() {
   final ConcurrentMap<String, Long> store = new ConcurrentHashMap<String, Long>();
   final MapStore<String, Long> myMapStore = new SimpleMapStore<String, Long>(store);
   Config config = getConfig();
   config
       .getMapConfig("myMap")
       .setMapStoreConfig(new MapStoreConfig().setImplementation(myMapStore));
   TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(3);
   HazelcastInstance hc = nodeFactory.newHazelcastInstance(config);
   IMap<String, Long> myMap = hc.getMap("myMap");
   myMap.put("one", 1L);
   assertEquals(1L, myMap.get("one").longValue());
   assertEquals(1L, store.get("one").longValue());
   myMap.putIfAbsent("two", 2L);
   assertEquals(2L, myMap.get("two").longValue());
   assertEquals(2L, store.get("two").longValue());
   myMap.putIfAbsent("one", 5L);
   assertEquals(1L, myMap.get("one").longValue());
   assertEquals(1L, store.get("one").longValue());
   myMap.replace("one", 1L, 111L);
   assertEquals(111L, myMap.get("one").longValue());
   assertEquals(111L, store.get("one").longValue());
   myMap.replace("one", 1L);
   assertEquals(1L, myMap.get("one").longValue());
   assertEquals(1L, store.get("one").longValue());
 }
  // ignored due to: https://github.com/hazelcast/hazelcast/issues/5035
  @Ignore
  @Test
  public void testMapLoaderLoadUpdatingIndex() throws Exception {
    final int nodeCount = 3;
    String mapName = randomString();
    SampleIndexableObjectMapLoader loader = new SampleIndexableObjectMapLoader();

    Config config = createMapConfig(mapName, loader);

    NodeBuilder nodeBuilder = new NodeBuilder(nodeCount, config).build();
    HazelcastInstance node = nodeBuilder.getRandomNode();

    IMap<Integer, SampleIndexableObject> map = node.getMap(mapName);
    for (int i = 0; i < 10; i++) {
      map.put(i, new SampleIndexableObject("My-" + i, i));
    }

    final SqlPredicate predicate = new SqlPredicate("name='My-5'");
    assertPredicateResultCorrect(map, predicate);

    map.destroy();
    loader.preloadValues = true;

    node = nodeBuilder.getRandomNode();
    map = node.getMap(mapName);

    assertLoadAllKeysCount(loader, 1);
    assertPredicateResultCorrect(map, predicate);
  }
  @Test
  public void testRemovedEntry_shouldNotBeReached_afterMigration() throws Exception {
    String mapName = randomMapName();
    TestHazelcastInstanceFactory factory = new TestHazelcastInstanceFactory(2);
    MapStoreTest.SimpleMapStore<Integer, Integer> store =
        new MapStoreTest.SimpleMapStore<Integer, Integer>();
    store.store.put(1, 0);

    Config config = createConfig(mapName, store);
    HazelcastInstance node1 = factory.newHazelcastInstance(config);

    IMap<Integer, Integer> map = node1.getMap(mapName);

    map.put(1, 1);
    map.delete(1);

    HazelcastInstance node2 = factory.newHazelcastInstance(config);

    map = node2.getMap(mapName);

    Integer value = map.get(1);
    factory.shutdownAll();

    assertNull(value);
  }
  @Test
  public void testHeartbeatResumedEvent() throws InterruptedException {
    hazelcastFactory.newHazelcastInstance();
    HazelcastInstance client = hazelcastFactory.newHazelcastClient(getClientConfig());
    final HazelcastInstance instance2 = hazelcastFactory.newHazelcastInstance();

    // make sure client is connected to instance2
    String keyOwnedByInstance2 = generateKeyOwnedBy(instance2);
    IMap<String, String> map = client.getMap(randomString());
    map.put(keyOwnedByInstance2, randomString());

    HazelcastClientInstanceImpl clientImpl = getHazelcastClientInstanceImpl(client);
    ClientConnectionManager connectionManager = clientImpl.getConnectionManager();
    final CountDownLatch countDownLatch = new CountDownLatch(1);
    connectionManager.addConnectionHeartbeatListener(
        new ConnectionHeartbeatListener() {
          @Override
          public void heartbeatResumed(Connection connection) {
            assertEquals(
                instance2.getCluster().getLocalMember().getAddress(), connection.getEndPoint());
            countDownLatch.countDown();
          }

          @Override
          public void heartbeatStopped(Connection connection) {}
        });

    blockMessagesFromInstance(instance2, client);
    sleepMillis(HEARTBEAT_TIMEOUT_MILLIS * 2);
    unblockMessagesFromInstance(instance2, client);

    assertOpenEventually(countDownLatch);
  }
  @Test
  public void testInvocation_whenHeartbeatStopped() throws InterruptedException {
    hazelcastFactory.newHazelcastInstance();
    HazelcastInstance client = hazelcastFactory.newHazelcastClient(getClientConfig());
    HazelcastInstance instance2 = hazelcastFactory.newHazelcastInstance();

    // make sure client is connected to instance2
    String keyOwnedByInstance2 = generateKeyOwnedBy(instance2);
    IMap<String, String> map = client.getMap(randomString());
    map.put(keyOwnedByInstance2, randomString());
    blockMessagesFromInstance(instance2, client);

    expectedException.expect(TargetDisconnectedException.class);
    expectedException.expectMessage(containsString("heartbeat problems"));
    map.put(keyOwnedByInstance2, randomString());
  }