Пример #1
0
  private void testPutAll(Config config) throws TimeoutException {
    TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);
    HazelcastInstance instance1 = nodeFactory.newHazelcastInstance(config);
    HazelcastInstance instance2 = nodeFactory.newHazelcastInstance(config);
    final ReplicatedMap<String, String> map1 = instance1.getReplicatedMap("default");
    final ReplicatedMap<String, String> map2 = instance2.getReplicatedMap("default");

    final Map<String, String> mapTest = new HashMap<String, String>();
    for (int i = 0; i < 100; i++) {
      mapTest.put("foo-" + i, "bar");
    }

    WatchedOperationExecutor executor = new WatchedOperationExecutor();
    executor.execute(
        new Runnable() {
          @Override
          public void run() {
            map1.putAll(mapTest);
          }
        },
        60,
        EntryEventType.ADDED,
        map1,
        map2);

    for (Entry<String, String> entry : map2.entrySet()) {
      assertStartsWith("foo-", entry.getKey());
      assertEquals("bar", entry.getValue());
    }
    for (Entry<String, String> entry : map1.entrySet()) {
      assertStartsWith("foo-", entry.getKey());
      assertEquals("bar", entry.getValue());
    }
  }
Пример #2
0
  private void testSize(Config config) throws Exception {
    TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);

    HazelcastInstance instance1 = nodeFactory.newHazelcastInstance(config);
    HazelcastInstance instance2 = nodeFactory.newHazelcastInstance(config);

    final ReplicatedMap<Integer, Integer> map1 = instance1.getReplicatedMap("default");
    final ReplicatedMap<Integer, Integer> map2 = instance2.getReplicatedMap("default");

    final AbstractMap.SimpleEntry<Integer, Integer>[] testValues = buildTestValues();

    WatchedOperationExecutor executor = new WatchedOperationExecutor();
    executor.execute(
        new Runnable() {
          @Override
          public void run() {
            int half = testValues.length / 2;
            for (int i = 0; i < testValues.length; i++) {
              final ReplicatedMap<Integer, Integer> map = i < half ? map1 : map2;
              final AbstractMap.SimpleEntry<Integer, Integer> entry = testValues[i];
              map.put(entry.getKey(), entry.getValue());
            }
          }
        },
        60,
        EntryEventType.ADDED,
        100,
        1,
        map1,
        map2);

    assertMatchSuccessfulOperationQuota(1, map1.size(), map2.size());
  }
Пример #3
0
  private void testAddEntryListener(Config config) throws TimeoutException {
    TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);

    HazelcastInstance instance1 = nodeFactory.newHazelcastInstance(config);
    HazelcastInstance instance2 = nodeFactory.newHazelcastInstance(config);

    final ReplicatedMap<String, String> map1 = instance1.getReplicatedMap("default");
    final ReplicatedMap<String, String> map2 = instance2.getReplicatedMap("default");

    SimpleEntryListener listener = new SimpleEntryListener(1, 0);
    map2.addEntryListener(listener, "foo-18");

    final int operations = 100;
    WatchedOperationExecutor executor = new WatchedOperationExecutor();
    executor.execute(
        new Runnable() {
          @Override
          public void run() {
            for (int i = 0; i < operations; i++) {
              map1.put("foo-" + i, "bar");
            }
          }
        },
        60,
        EntryEventType.ADDED,
        operations,
        1,
        map1,
        map2);

    assertOpenEventually(listener.addLatch);
  }
 private void execute(
     final Runnable runnable,
     final EntryEventType type,
     final int operations,
     final double minExpectation,
     ReplicatedMap... maps)
     throws TimeoutException {
   final WatchedOperationExecutor executor = new WatchedOperationExecutor();
   executor.execute(runnable, 60, type, operations, minExpectation, maps);
 }
Пример #5
0
  private void testClear(Config config) throws Exception {
    TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);

    HazelcastInstance instance1 = nodeFactory.newHazelcastInstance(config);
    HazelcastInstance instance2 = nodeFactory.newHazelcastInstance(config);

    final ReplicatedMap<String, String> map1 = instance1.getReplicatedMap("default");
    final ReplicatedMap<String, String> map2 = instance2.getReplicatedMap("default");

    final int operations = 100;
    WatchedOperationExecutor executor = new WatchedOperationExecutor();
    executor.execute(
        new Runnable() {
          @Override
          public void run() {
            for (int i = 0; i < operations; i++) {
              map1.put("foo-" + i, "bar");
            }
          }
        },
        60,
        EntryEventType.ADDED,
        operations,
        1,
        map1,
        map2);

    for (Entry<String, String> entry : map2.entrySet()) {
      assertStartsWith("foo-", entry.getKey());
      assertEquals("bar", entry.getValue());
    }

    for (Entry<String, String> entry : map1.entrySet()) {
      assertStartsWith("foo-", entry.getKey());
      assertEquals("bar", entry.getValue());
    }

    final AtomicBoolean happened = new AtomicBoolean(false);
    for (int i = 0; i < 10; i++) {
      map1.clear();
      Thread.sleep(1000);
      try {
        assertEquals(0, map1.size());
        assertEquals(0, map2.size());
        happened.set(true);
      } catch (AssertionError ignore) {
        // ignore and retry
      }
      if (happened.get()) {
        break;
      }
    }
  }
Пример #6
0
  private void testEntrySet(Config config) throws Exception {
    TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);

    HazelcastInstance instance1 = nodeFactory.newHazelcastInstance(config);
    HazelcastInstance instance2 = nodeFactory.newHazelcastInstance(config);

    final ReplicatedMap<Integer, Integer> map1 = instance1.getReplicatedMap("default");
    final ReplicatedMap<Integer, Integer> map2 = instance2.getReplicatedMap("default");

    final AbstractMap.SimpleEntry<Integer, Integer>[] testValues = buildTestValues();
    WatchedOperationExecutor executor = new WatchedOperationExecutor();
    executor.execute(
        new Runnable() {
          @Override
          public void run() {
            int half = testValues.length / 2;
            for (int i = 0; i < testValues.length; i++) {
              final ReplicatedMap<Integer, Integer> map = i < half ? map1 : map2;
              final AbstractMap.SimpleEntry<Integer, Integer> entry = testValues[i];
              map.put(entry.getKey(), entry.getValue());
            }
          }
        },
        60,
        EntryEventType.ADDED,
        100,
        1,
        map1,
        map2);

    List<Entry<Integer, Integer>> entrySet1 = copyToList(map1.entrySet());
    List<Entry<Integer, Integer>> entrySet2 = copyToList(map2.entrySet());

    int map2Contains = 0;
    for (Entry<Integer, Integer> entry : entrySet2) {
      System.out.println("Entry: " + entry);
      Integer value = findValue(entry.getKey(), testValues);
      if (value.equals(entry.getValue())) {
        map2Contains++;
      }
    }

    int map1Contains = 0;
    for (Entry<Integer, Integer> entry : entrySet1) {
      Integer value = findValue(entry.getKey(), testValues);
      if (value.equals(entry.getValue())) {
        map1Contains++;
      }
    }

    assertMatchSuccessfulOperationQuota(1, testValues.length, map1Contains, map2Contains);
  }
Пример #7
0
  private void testKeySet(Config config) throws Exception {
    TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);

    HazelcastInstance instance1 = nodeFactory.newHazelcastInstance(config);
    HazelcastInstance instance2 = nodeFactory.newHazelcastInstance(config);

    final ReplicatedMap<Integer, Integer> map1 = instance1.getReplicatedMap("default");
    final ReplicatedMap<Integer, Integer> map2 = instance2.getReplicatedMap("default");

    final AbstractMap.SimpleEntry<Integer, Integer>[] testValues = buildTestValues();

    final List<Integer> keySetTestValues = new ArrayList<Integer>(testValues.length);
    WatchedOperationExecutor executor = new WatchedOperationExecutor();
    executor.execute(
        new Runnable() {
          @Override
          public void run() {
            int half = testValues.length / 2;
            for (int i = 0; i < testValues.length; i++) {
              final ReplicatedMap<Integer, Integer> map = i < half ? map1 : map2;
              final AbstractMap.SimpleEntry<Integer, Integer> entry = testValues[i];
              map.put(entry.getKey(), entry.getValue());
              keySetTestValues.add(entry.getKey());
            }
          }
        },
        60,
        EntryEventType.ADDED,
        100,
        1,
        map1,
        map2);

    List<Integer> keySet1 = copyToList(map1.keySet());
    List<Integer> keySet2 = copyToList(map2.keySet());

    int map1Contains = 0;
    int map2Contains = 0;
    for (Integer value : keySetTestValues) {
      if (keySet2.contains(value)) {
        map2Contains++;
      }
      if (keySet1.contains(value)) {
        map1Contains++;
      }
    }

    assertMatchSuccessfulOperationQuota(1, testValues.length, map1Contains, map2Contains);
  }
Пример #8
0
  private void testContainsKey(Config config) throws Exception {
    TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);

    HazelcastInstance instance1 = nodeFactory.newHazelcastInstance(config);
    HazelcastInstance instance2 = nodeFactory.newHazelcastInstance(config);

    final ReplicatedMap<String, String> map1 = instance1.getReplicatedMap("default");
    final ReplicatedMap<String, String> map2 = instance2.getReplicatedMap("default");

    final int operations = 100;
    WatchedOperationExecutor executor = new WatchedOperationExecutor();
    executor.execute(
        new Runnable() {
          @Override
          public void run() {
            for (int i = 0; i < operations; i++) {
              map1.put("foo-" + i, "bar");
            }
          }
        },
        60,
        EntryEventType.ADDED,
        operations,
        1,
        map1,
        map2);

    int map2Contains = 0;
    for (int i = 0; i < operations; i++) {
      if (map2.containsKey("foo-" + i)) {
        map2Contains++;
      }
    }
    int map1Contains = 0;
    for (int i = 0; i < operations; i++) {
      if (map1.containsKey("foo-" + i)) {
        map1Contains++;
      }
    }

    assertMatchSuccessfulOperationQuota(1, operations, map1Contains, map2Contains);
  }
Пример #9
0
  private void testRemove(Config config) throws Exception {
    TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);

    HazelcastInstance instance1 = nodeFactory.newHazelcastInstance(config);
    HazelcastInstance instance2 = nodeFactory.newHazelcastInstance(config);

    final ReplicatedMap<String, String> map1 = instance1.getReplicatedMap("default");
    final ReplicatedMap<String, String> map2 = instance2.getReplicatedMap("default");

    final int operations = 100;
    WatchedOperationExecutor executor = new WatchedOperationExecutor();
    executor.execute(
        new Runnable() {
          @Override
          public void run() {
            for (int i = 0; i < operations; i++) {
              map1.put("foo-" + i, "bar");
            }
          }
        },
        60,
        EntryEventType.ADDED,
        operations,
        1,
        map1,
        map2);

    for (Entry<String, String> entry : map2.entrySet()) {
      assertStartsWith("foo-", entry.getKey());
      assertEquals("bar", entry.getValue());
    }
    for (Entry<String, String> entry : map1.entrySet()) {
      assertStartsWith("foo-", entry.getKey());
      assertEquals("bar", entry.getValue());
    }
    executor.execute(
        new Runnable() {
          @Override
          public void run() {
            for (int i = 0; i < operations; i++) {
              map2.remove("foo-" + i);
            }
          }
        },
        60,
        EntryEventType.REMOVED,
        operations,
        1,
        map1,
        map2);

    int map2Updated = 0;
    for (int i = 0; i < operations; i++) {
      Object value = map2.get("foo-" + i);
      if (value == null) {
        map2Updated++;
      }
    }
    int map1Updated = 0;
    for (int i = 0; i < operations; i++) {
      Object value = map1.get("foo-" + i);
      if (value == null) {
        map1Updated++;
      }
    }

    assertMatchSuccessfulOperationQuota(1, operations, map1Updated, map2Updated);
  }
Пример #10
0
  private void testUpdateTtl(Config config) throws Exception {
    TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);

    HazelcastInstance instance1 = nodeFactory.newHazelcastInstance(config);
    HazelcastInstance instance2 = nodeFactory.newHazelcastInstance(config);

    final ReplicatedMap<String, String> map1 = instance1.getReplicatedMap("default");
    final ReplicatedMap<String, String> map2 = instance2.getReplicatedMap("default");

    final int operations = 100;
    WatchedOperationExecutor executor = new WatchedOperationExecutor();
    executor.execute(
        new Runnable() {
          @Override
          public void run() {
            for (int i = 0; i < operations; i++) {
              map1.put("foo-" + i, "bar");
            }
          }
        },
        60,
        EntryEventType.ADDED,
        operations,
        1,
        map1,
        map2);

    for (Entry<String, String> entry : map2.entrySet()) {
      assertStartsWith("foo-", entry.getKey());
      assertEquals("bar", entry.getValue());
    }
    for (Entry<String, String> entry : map1.entrySet()) {
      assertStartsWith("foo-", entry.getKey());
      assertEquals("bar", entry.getValue());
    }

    executor.execute(
        new Runnable() {
          @Override
          public void run() {
            for (int i = 0; i < operations; i++) {
              map2.put("foo-" + i, "bar", 10, TimeUnit.MINUTES);
            }
          }
        },
        60,
        EntryEventType.UPDATED,
        operations,
        1,
        map1,
        map2);

    int map2Updated = 0;
    for (Entry<String, String> entry : map2.entrySet()) {
      ReplicatedRecord record = getReplicatedRecord(map2, entry.getKey());
      if (record.getTtlMillis() > 0) {
        map2Updated++;
      }
    }
    int map1Updated = 0;
    for (Entry<String, String> entry : map1.entrySet()) {
      ReplicatedRecord record = getReplicatedRecord(map1, entry.getKey());
      if (record.getTtlMillis() > 0) {
        map1Updated++;
      }
    }

    assertMatchSuccessfulOperationQuota(1, operations, map1Updated, map2Updated);
  }
Пример #11
0
  private void testAddTtl(Config config) throws Exception {
    TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);

    HazelcastInstance instance1 = nodeFactory.newHazelcastInstance(config);
    HazelcastInstance instance2 = nodeFactory.newHazelcastInstance(config);

    final ReplicatedMap<String, String> map1 = instance1.getReplicatedMap("default");
    final ReplicatedMap<String, String> map2 = instance2.getReplicatedMap("default");

    final int operations = 100;
    WatchedOperationExecutor executor = new WatchedOperationExecutor();
    executor.execute(
        new Runnable() {
          @Override
          public void run() {
            for (int i = 0; i < operations; i++) {
              map1.put("foo-" + i, "bar", 10, TimeUnit.MINUTES);
            }
          }
        },
        60,
        EntryEventType.ADDED,
        operations,
        1,
        map1,
        map2);

    // Give a bit of time to process last batch of updates
    TimeUnit.SECONDS.sleep(2);

    Set<Entry<String, String>> map2entries = map2.entrySet();
    for (Entry<String, String> entry : map2entries) {
      assertStartsWith("foo-", entry.getKey());
      assertEquals("bar", entry.getValue());

      ReplicatedRecord<String, String> record = getReplicatedRecord(map2, entry.getKey());
      assertNotEquals(0, record.getTtlMillis());

      // Kill the record by setting timeout
      record.setValue(record.getValue(), 1);
    }

    Set<Entry<String, String>> map1entries = map1.entrySet();
    for (Entry<String, String> entry : map1entries) {
      assertStartsWith("foo-", entry.getKey());
      assertEquals("bar", entry.getValue());

      ReplicatedRecord<String, String> record = getReplicatedRecord(map1, entry.getKey());
      assertNotEquals(0, record.getTtlMillis());

      // Kill the record by setting timeout
      record.setValue(record.getValue(), 1);
    }

    TimeUnit.SECONDS.sleep(1);

    int map2Updated = 0;
    for (Entry<String, String> entry : map2entries) {
      if (map2.get(entry.getKey()) == null) {
        map2Updated++;
      }
    }

    int map1Updated = 0;
    for (Entry<String, String> entry : map1entries) {
      if (map1.get(entry.getKey()) == null) {
        map1Updated++;
      }
    }

    assertMatchSuccessfulOperationQuota(1, operations, map1Updated, map2Updated);
  }