Пример #1
0
 @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());
 }
  @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"));
  }
 @Test
 public void putIfAbsent() {
   HazelcastClient hClient = getHazelcastClient();
   IMap<String, String> map = hClient.getMap("putIfAbsent");
   String result = map.put("1", "CBDEF");
   assertNull(result);
   assertNull(map.putIfAbsent("2", "C"));
   assertEquals("C", map.putIfAbsent("2", "D"));
 }
 @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());
 }
Пример #5
0
  @Test(timeout = 120000)
  public void testIssue1115EnablingMapstoreMutatingValue() throws InterruptedException {
    Config config = getConfig();
    String mapName = "testIssue1115";
    MapStore mapStore = new ProcessingStore();
    MapStoreConfig mapStoreConfig = new MapStoreConfig();
    mapStoreConfig.setEnabled(true);
    mapStoreConfig.setImplementation(mapStore);
    config.getMapConfig(mapName).setMapStoreConfig(mapStoreConfig);
    TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);
    HazelcastInstance instance1 = nodeFactory.newHazelcastInstance(config);
    HazelcastInstance instance2 = nodeFactory.newHazelcastInstance(config);
    IMap<Integer, Employee> map = instance1.getMap(mapName);
    Random random = new Random();
    // testing put with new object
    for (int i = 0; i < 10; i++) {
      Employee emp = new Employee();
      emp.setAge(random.nextInt(20) + 20);
      map.put(i, emp);
    }

    for (int i = 0; i < 10; i++) {
      Employee employee = map.get(i);
      assertEquals(employee.getAge() * 1000, employee.getSalary(), 0);
    }

    // testing put with existing object
    for (int i = 0; i < 10; i++) {
      Employee emp = map.get(i);
      emp.setAge(random.nextInt(20) + 20);
      map.put(i, emp);
    }

    for (int i = 0; i < 10; i++) {
      Employee employee = map.get(i);
      assertEquals(employee.getAge() * 1000, employee.getSalary(), 0);
    }

    // testing put with replace
    for (int i = 0; i < 10; i++) {
      Employee emp = map.get(i);
      emp.setAge(random.nextInt(20) + 20);
      map.replace(i, emp);
    }

    for (int i = 0; i < 10; i++) {
      Employee employee = map.get(i);
      assertEquals(employee.getAge() * 1000, employee.getSalary(), 0);
    }

    // testing put with putIfAbsent
    for (int i = 10; i < 20; i++) {
      Employee emp = new Employee();
      emp.setAge(random.nextInt(20) + 20);
      map.putIfAbsent(i, emp);
    }

    for (int i = 10; i < 20; i++) {
      Employee employee = map.get(i);
      assertEquals(employee.getAge() * 1000, employee.getSalary(), 0);
    }
  }