public void setUp() throws Exception { super.setUp(); sharedData = vertx.sharedData(); }
@Test public void testAcquireDifferentLocks() { getVertx().sharedData().getLock("foo", ar -> { assertTrue(ar.succeeded()); long start = System.currentTimeMillis(); Lock lock = ar.result(); getVertx().sharedData().getLock("bar", ar2 -> { assertTrue(ar2.succeeded()); assertTrue(System.currentTimeMillis() - start < 2000); testComplete(); }); }); await(); }
@Test public void testIllegalArguments() throws Exception { assertNullPointerException(() -> getVertx().sharedData().getLock(null, ar -> {})); assertNullPointerException(() -> getVertx().sharedData().getLock("foo", null)); assertNullPointerException(() -> getVertx().sharedData().getLockWithTimeout(null, 1, ar -> {})); assertNullPointerException(() -> getVertx().sharedData().getLockWithTimeout("foo", 1, null)); assertIllegalArgumentException(() -> getVertx().sharedData().getLockWithTimeout("foo", -1, ar -> {})); }
@Test public void testAcquireTimeout() { getVertx().sharedData().getLock("foo", ar -> { assertTrue(ar.succeeded()); long start = System.currentTimeMillis(); getVertx().sharedData().getLockWithTimeout("foo", 1000, ar2 -> { assertFalse(ar2.succeeded()); // Should be delayed assertTrue(System.currentTimeMillis() - start >= 1000); testComplete(); }); }); await(); }
private void testLockReleased(Consumer<CountDownLatch> action) throws Exception { CountDownLatch lockAquiredLatch = new CountDownLatch(1); vertices[0].sharedData().getLockWithTimeout("pimpo", getLockTimeout(), onSuccess(lock -> { vertices[1].sharedData().getLockWithTimeout("pimpo", getLockTimeout(), onSuccess(lock2 -> { // Eventually acquired after node1 goes down testComplete(); })); lockAquiredLatch.countDown(); })); awaitLatch(lockAquiredLatch); CountDownLatch closeLatch = new CountDownLatch(1); action.accept(closeLatch); awaitLatch(closeLatch); await(); }
@Test public void testGet() { getVertx().sharedData().getCounter("foo", ar -> { assertTrue(ar.succeeded()); Counter counter = ar.result(); counter.get(ar2 -> { assertTrue(ar2.succeeded()); assertEquals(0l, ar2.result().longValue()); testComplete(); }); }); await(); }
protected void loadData(Map<JsonObject, Buffer> map, BiConsumer<Vertx, AsyncMap<JsonObject, Buffer>> test) { List<Future> futures = new ArrayList<>(map.size()); map.forEach((key, value) -> { Future future = Future.future(); getVertx().sharedData().getAsyncMap("foo", onSuccess(asyncMap -> { asyncMap.put(key, value, future); })); futures.add(future); }); CompositeFuture.all(futures).setHandler(onSuccess(cf -> { Vertx v = getVertx(); v.sharedData().<JsonObject, Buffer>getAsyncMap("foo", onSuccess(asyncMap -> { test.accept(v, asyncMap); })); })); }
@Test public void testGetMapWithNullResultHandler() throws Exception { assertNullPointerException(() -> getVertx().sharedData().<String, String>getAsyncMap("foo", null)); }
@Test public void testGetMapWithNullName() throws Exception { assertNullPointerException(() -> getVertx().sharedData().<String, String>getAsyncMap(null, ar -> {})); }
private void testMapOperationResult(BiFunction<LocalMap<String, ShareableObject>, String, ShareableObject> operation) { final String key = "key"; final ShareableObject value = new ShareableObject("some test data"); final LocalMap<String, ShareableObject> map = vertx.sharedData().getLocalMap("foo"); assertNull(map.put(key, value)); final ShareableObject result = operation.apply(map, key); assertEquals(value, result); assertNotSame(value, result); }
private <K, V> void testMapPutGet(K k, V v) { getVertx().sharedData().<K, V>getAsyncMap("foo", onSuccess(map -> { map.put(k, v, onSuccess(vd -> { getVertx().sharedData().<K, V>getAsyncMap("foo", onSuccess(map2 -> { map2.get(k, onSuccess(res -> { assertEquals(v, res); testComplete(); })); })); })); })); await(); }
@Test public void testMultipleMaps() { getVertx().sharedData().<String, String>getAsyncMap("foo", onSuccess(map -> { map.put("foo", "bar", onSuccess(v -> { getVertx().sharedData().<String, String>getAsyncMap("bar", onSuccess(map2 -> { map2.get("foo", onSuccess(res -> { assertNull(res); testComplete(); })); })); })); })); await(); }
private <K, V> void testMapRemove(K k, V v) { getVertx().sharedData().<K, V>getAsyncMap("foo", onSuccess(map -> { map.put(k, v, onSuccess(res -> { assertNull(res); getVertx().sharedData().<K, V>getAsyncMap("foo", onSuccess(map2 -> { map2.remove(k, onSuccess(res2 -> { assertEquals(v, res2); testComplete(); })); })); })); })); await(); }
@Test public void testPutNullKey() { getVertx().sharedData().<String, String>getAsyncMap("foo", onSuccess(map -> { assertIllegalArgumentException(() -> map.put(null, "foo", ar2 -> {})); testComplete(); })); await(); }
@Test public void testPutNullValue() { getVertx().sharedData().<String, String>getAsyncMap("foo", onSuccess(map -> { assertIllegalArgumentException(() -> map.put("foo", null, ar2 -> {})); testComplete(); })); await(); }
@Test public void testMapPutTtl() { getVertx().sharedData().<String, String>getAsyncMap("foo", onSuccess(map -> { map.put("pipo", "molo", 10, onSuccess(vd -> { vertx.setTimer(15, l -> { getVertx().sharedData().<String, String>getAsyncMap("foo", onSuccess(map2 -> { map2.get("pipo", onSuccess(res -> { assertNull(res); testComplete(); })); })); }); })); })); await(); }
@Test public void testPutIfAbsentInvalidValue() { getVertx().sharedData().<String, SomeObject>getAsyncMap("foo", onSuccess(map -> { assertIllegalArgumentException(() -> map.putIfAbsent("foo", new SomeObject(), ar2 -> {})); testComplete(); })); await(); }
@Test public void testPutInvalidKey() { getVertx().sharedData().<SomeObject, String>getAsyncMap("foo", onSuccess(map -> { assertIllegalArgumentException(() -> map.put(new SomeObject(), "foo", ar2 -> {})); testComplete(); })); await(); }
@Test public void testPutIfAbsentInvalidKey() { getVertx().sharedData().<SomeObject, String>getAsyncMap("foo", onSuccess(map -> { assertIllegalArgumentException(() -> map.putIfAbsent(new SomeObject(), "foo", ar2 -> {})); testComplete(); })); await(); }
@Test public void testPutInvalidValue() { getVertx().sharedData().<String, SomeObject>getAsyncMap("foo", onSuccess(map -> { assertIllegalArgumentException(() -> map.put("foo", new SomeObject(), ar2 -> {})); testComplete(); })); await(); }