AsyncBroker { public NoticeableFuture<V> post(K key) { DefaultNoticeableFuture<V> defaultNoticeableFuture = new DefaultNoticeableFuture<V>() { @Override protected void finalize() { cancel(true); } }; NoticeableFuture<V> previousNoticeableFuture = _post( key, defaultNoticeableFuture); if (previousNoticeableFuture == null) { return defaultNoticeableFuture; } return previousNoticeableFuture; }  Map<K, NoticeableFuture<V>> getOpenBids(); NoticeableFuture<V> post(K key); NoticeableFuture<V> post(K key, boolean[] newMarker); NoticeableFuture<V> take(K key); boolean takeWithException(K key, Throwable throwable); boolean takeWithResult(K key, V result);  }
@Test public void testOrphanCancellation() throws InterruptedException { AsyncBroker<String, String> asyncBroker = new AsyncBroker<>(); NoticeableFuture<String> noticeableFuture = asyncBroker.post(_KEY); AtomicBoolean completedMarker = new AtomicBoolean(); noticeableFuture.addFutureListener(future -> completedMarker.set(true)); noticeableFuture = null; GCUtil.gc(true); FinalizeManagerUtil.drainPendingFinalizeActions(); Assert.assertTrue(completedMarker.get()); }
@Test public void testPost() throws Exception { AsyncBroker<String, String> asyncBroker = new AsyncBroker<>(); Map<String, DefaultNoticeableFuture<String>> defaultNoticeableFutures = ReflectionTestUtil.getFieldValue( asyncBroker, "_defaultNoticeableFutures"); NoticeableFuture<String> noticeableFuture = asyncBroker.post(_KEY); Assert.assertEquals( defaultNoticeableFutures.toString(), 1, defaultNoticeableFutures.size()); Assert.assertSame(noticeableFuture, defaultNoticeableFutures.get(_KEY)); Assert.assertSame(noticeableFuture, asyncBroker.post(_KEY)); Assert.assertEquals( defaultNoticeableFutures.toString(), 1, defaultNoticeableFutures.size()); Assert.assertTrue(noticeableFuture.cancel(true)); Assert.assertTrue( defaultNoticeableFutures.toString(), defaultNoticeableFutures.isEmpty()); boolean[] newMarker = new boolean[1]; noticeableFuture = asyncBroker.post(_KEY, newMarker); Assert.assertNotNull(noticeableFuture); Assert.assertTrue(Arrays.toString(newMarker), newMarker[0]); Assert.assertSame(noticeableFuture, asyncBroker.post(_KEY, newMarker)); Assert.assertFalse(Arrays.toString(newMarker), newMarker[0]); Assert.assertTrue(noticeableFuture.cancel(true)); Assert.assertTrue( defaultNoticeableFutures.toString(), defaultNoticeableFutures.isEmpty()); }