Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CompletionListenerFuture enhanced tests #111

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
/**
* Copyright (c) 2011-2013 Terracotta, Inc.
* Copyright (c) 2011-2013 Oracle and/or its affiliates.
* Copyright 2011-2013 Terracotta, Inc.
* Copyright 2011-2013 Oracle and/or its affiliates.
*
* All rights reserved. Use is subject to license terms.
*/

package javax.cache.integration;

import org.junit.Test;
import sun.awt.SunToolkit;

import java.lang.UnsupportedOperationException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.*;

/**
* Added to complete code coverage
* Functional tests for CompletionListenerFuture
*
* @author Greg Luck
* @author Jens Wilke
*
* @see CompletionListenerFuture
*/
public class CompletionListenerFutureTest {

Expand Down Expand Up @@ -63,13 +68,26 @@ public void testTimedGetTimeOutException() throws TimeoutException, ExecutionExc
future.get(3L, TimeUnit.MILLISECONDS);
}

@Test(expected = TimeoutException.class)
public void testTimedGetTimeOutException0ms() throws TimeoutException, ExecutionException, InterruptedException {
CompletionListenerFuture future = new CompletionListenerFuture();
future.get(0L, TimeUnit.MILLISECONDS);
}

@Test(expected = ExecutionException.class)
public void testTimedGetOnException() throws TimeoutException, ExecutionException, InterruptedException {
CompletionListenerFuture future = new CompletionListenerFuture();
future.onException(new IllegalStateException());
future.get(3L, TimeUnit.MILLISECONDS);
}

@Test(expected = ExecutionException.class)
public void testTimedGetOnException0ms() throws TimeoutException, ExecutionException, InterruptedException {
CompletionListenerFuture future = new CompletionListenerFuture();
future.onException(new IllegalStateException());
future.get(0, TimeUnit.MILLISECONDS);
}

@Test
public void testGetCompleted() throws TimeoutException, ExecutionException, InterruptedException {
CompletionListenerFuture future = new CompletionListenerFuture();
Expand All @@ -83,4 +101,73 @@ public void testGetOnException() throws TimeoutException, ExecutionException, In
future.onException(new IllegalStateException());
future.get();
}

@Test
public void testIsDone() {
CompletionListenerFuture future = new CompletionListenerFuture();
assertFalse(future.isDone());
future.onCompletion();
assertTrue(future.isDone());
}

/**
* Test that thread(s) is correctly waking up when completed.
*
* @see <a href="https://github.com/jsr107/jsr107spec/issues/320">spec#320</a>
*/
@Test
public void testWakeup() throws InterruptedException {
/* Currently test only with 1 thread to be compatible with 1.0. Can be incremented later. */
final int THREAD_COUNT = 2;
final long TIMEOUT_MILLIS = 1 * 60 * 1000;
final CompletionListenerFuture future = new CompletionListenerFuture();
/* Waits until threads are running. */
final CountDownLatch countDown = new CountDownLatch(THREAD_COUNT + 1);
Thread[] threads = new Thread[THREAD_COUNT];
for (int i = 0; i < THREAD_COUNT; i++) {
Thread t = threads[i] = new Thread() {
@Override
public void run() {
try {
countDown.countDown();
countDown.await();
future.get();
} catch (Exception e) {
e.printStackTrace();
}
}
};
t.start();
}
countDown.countDown();
countDown.await();
Thread.sleep(42);
future.onCompletion();
for (int i = 0; i < THREAD_COUNT; i++) {
Thread t = threads[i];
t.join(TIMEOUT_MILLIS);
if (t.isAlive()) {
fail("thread not terminated");
}
}
}

/**
* Checks that get with timeout is honoring the timeout and waiting for the
* specified time.
*/
@Test
public void testCorrectWaitTime() throws Exception {
final long TIMEOUT_MILLIS = 42;
final CompletionListenerFuture future = new CompletionListenerFuture();
long t0 = System.currentTimeMillis();
try {
future.get(TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
fail("exception expected");
} catch (TimeoutException e) {
// expected
}
assertTrue("minimum time passed", System.currentTimeMillis() - t0 >= TIMEOUT_MILLIS);
}

}