AtomicReference<Object> cache = new AtomicReference<Object>(); Object cachedValue = new Object(); cache.set(cachedValue); //... time passes ... Object cachedValueToUpdate = cache.get(); //... do some work to transform cachedValueToUpdate into a new version Object newValue = someFunctionOfOld(cachedValueToUpdate); boolean success = cache.compareAndSet(cachedValue,cachedValueToUpdate);
@Test @Ignore("Null values not permitted") public void testSerializeNull() { final AtomicReference<Subscriber<Integer>> serial = new AtomicReference<Subscriber<Integer>>(); TestSubscriber<Integer> ts = new TestSubscriber<Integer>() { @Override public void onNext(Integer t) { if (t != null && t == 0) { serial.get().onNext(null); } super.onNext(t); } }; SerializedSubscriber<Integer> sobs = new SerializedSubscriber<Integer>(ts); serial.set(sobs); sobs.onNext(0); ts.assertValues(0, null); }
@Test public void test() throws InterruptedException { Integer actual = (Integer) message.getHeaders().getOrDefault("seq", -1); if (actual != expected) { result.set("Expected: " + expected + ", but was: " + actual); latch.countDown(); return; result.set(ex.toString()); latch.countDown(); result.set("Done"); latch.countDown(); assertEquals("Done", result.get());
@Subscribe public void lifecycleChanged(Lifecycle lifecycle) { LOG.debug("Lifecycle is now {}", lifecycle); // if we switch to RUNNING from STARTING (or unknown) the server is ready to accept connections on inputs. // we want to postpone opening the inputs earlier, so we don't get swamped with messages before // we can actually process them. if (lifecycle == Lifecycle.RUNNING && previousLifecycle.get() == Lifecycle.STARTING || previousLifecycle.get() == Lifecycle.UNINITIALIZED) { LOG.info("Triggering launching persisted inputs, node transitioned from {} to {}", previousLifecycle.get(), lifecycle); // Set lifecycle BEFORE counting down the latch to avoid race conditions! previousLifecycle.set(lifecycle); startLatch.countDown(); } // if we failed to start up due to some other service aborting, we need to get over the barrier. if (lifecycle == Lifecycle.FAILED) { startLatch.countDown(); } }
@Test @Ignore("Null values not permitted") public void testSerializeNull() { final AtomicReference<Observer<Integer>> serial = new AtomicReference<Observer<Integer>>(); TestObserver<Integer> to = new TestObserver<Integer>() { @Override public void onNext(Integer t) { if (t != null && t == 0) { serial.get().onNext(null); } super.onNext(t); } }; SerializedObserver<Integer> sobs = new SerializedObserver<Integer>(to); serial.set(sobs); sobs.onNext(0); to.assertValues(0, null); }
@Test public void testExecuteBlockingTTCL() throws Exception { ClassLoader cl = Thread.currentThread().getContextClassLoader(); assertNotNull(cl); CountDownLatch latch = new CountDownLatch(1); AtomicReference<ClassLoader> blockingTCCL = new AtomicReference<>(); vertx.<String>executeBlocking(future -> { future.complete("whatever"); blockingTCCL.set(Thread.currentThread().getContextClassLoader()); }, ar -> { assertTrue(ar.succeeded()); assertEquals("whatever", ar.result()); latch.countDown(); }); assertSame(cl, Thread.currentThread().getContextClassLoader()); awaitLatch(latch); assertSame(cl, blockingTCCL.get()); }
/** * Get (and initialize, if not initialized yet) the required object * * @return lazily initialized object * @throws ConcurrentException if the initialization of the object causes an * exception */ @Override public final T get() throws ConcurrentException { T result; while ((result = reference.get()) == null) { if (factory.compareAndSet(null, this)) { reference.set(initialize()); } } return result; }
@Test public void testFactory() throws Exception { AtomicReference<Thread> metricsThread = new AtomicReference<>(); AtomicReference<Context> metricsContext = new AtomicReference<>(); VertxMetricsFactory factory = (options) -> { metricsThread.set(Thread.currentThread()); metricsContext.set(Vertx.currentContext()); return DummyVertxMetrics.INSTANCE; }; vertx(new VertxOptions().setMetricsOptions(new MetricsOptions().setEnabled(true).setFactory(factory))); assertSame(Thread.currentThread(), metricsThread.get()); assertNull(metricsContext.get()); }
@Test public void testCallbackInvokedOnFailure() throws Exception { // will trigger java.net.UnknownHostException String hostName = "zoom.zoom.zen.tld"; VertxOptions options = new VertxOptions() .setClusterManager(new FakeClusterManager()) .setClusterHost(hostName); AtomicReference<AsyncResult<Vertx>> resultRef = new AtomicReference<>(); CountDownLatch latch = new CountDownLatch(1); Vertx.clusteredVertx(options, ar -> { resultRef.set(ar); latch.countDown(); }); awaitLatch(latch); assertFalse(resultRef.get() == null); assertTrue(resultRef.get().failed()); assertTrue("Was expecting failure to be an instance of UnknownHostException", resultRef.get().cause() instanceof UnknownHostException); } }