use of java.nio.channels.AsynchronousCloseException in project jimfs by google.
the class JimfsFileChannelTest method testAsynchronousClose.
@Test
public void testAsynchronousClose() throws Exception {
RegularFile file = regularFile(10);
final FileChannel channel = channel(file, READ, WRITE);
// ensure all operations on the channel will block
file.writeLock().lock();
ExecutorService executor = Executors.newCachedThreadPool();
CountDownLatch latch = new CountDownLatch(BLOCKING_OP_COUNT);
List<Future<?>> futures = queueAllBlockingOperations(channel, executor, latch);
// wait for all the threads to have started running
latch.await();
// then ensure time for operations to start blocking
Uninterruptibles.sleepUninterruptibly(20, MILLISECONDS);
// close channel on this thread
channel.close();
// AsynchronousCloseException
for (Future<?> future : futures) {
try {
future.get();
fail();
} catch (ExecutionException expected) {
assertThat(expected.getCause()).named("blocking thread exception").isInstanceOf(AsynchronousCloseException.class);
}
}
}
use of java.nio.channels.AsynchronousCloseException in project jimfs by google.
the class JimfsFileChannelTest method testCloseByInterrupt.
@Test
public void testCloseByInterrupt() throws Exception {
RegularFile file = regularFile(10);
final FileChannel channel = channel(file, READ, WRITE);
// ensure all operations on the channel will block
file.writeLock().lock();
ExecutorService executor = Executors.newCachedThreadPool();
final CountDownLatch threadStartLatch = new CountDownLatch(1);
final SettableFuture<Throwable> interruptException = SettableFuture.create();
// This thread, being the first to run, will be blocking on the interruptible lock (the byte
// file's write lock) and as such will be interrupted properly... the other threads will be
// blocked on the lock that guards the position field and the specification that only one method
// on the channel will be in progress at a time. That lock is not interruptible, so we must
// interrupt this thread.
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
threadStartLatch.countDown();
try {
channel.write(ByteBuffer.allocate(20));
interruptException.set(null);
} catch (Throwable e) {
interruptException.set(e);
}
}
});
thread.start();
// let the thread start running
threadStartLatch.await();
// then ensure time for thread to start blocking on the write lock
Uninterruptibles.sleepUninterruptibly(10, MILLISECONDS);
CountDownLatch blockingStartLatch = new CountDownLatch(BLOCKING_OP_COUNT);
List<Future<?>> futures = queueAllBlockingOperations(channel, executor, blockingStartLatch);
// wait for all blocking threads to start
blockingStartLatch.await();
// then ensure time for the operations to start blocking
Uninterruptibles.sleepUninterruptibly(20, MILLISECONDS);
// interrupting this blocking thread closes the channel and makes all the other threads
// throw AsynchronousCloseException... the operation on this thread should throw
// ClosedByInterruptException
thread.interrupt();
// get the exception that caused the interrupted operation to terminate
assertThat(interruptException.get(200, MILLISECONDS)).named("interrupted thread exception").isInstanceOf(ClosedByInterruptException.class);
// different thread, closed the channel)
for (Future<?> future : futures) {
try {
future.get();
fail();
} catch (ExecutionException expected) {
assertThat(expected.getCause()).named("blocking thread exception").isInstanceOf(AsynchronousCloseException.class);
}
}
}
use of java.nio.channels.AsynchronousCloseException in project gradle by gradle.
the class InputForwarder method start.
public InputForwarder start() {
lifecycleLock.lock();
try {
if (started) {
throw new IllegalStateException("input forwarder has already been started");
}
disconnectableInput = new DisconnectableInputStream(input, bufferSize);
outputBuffer = new LineBufferingOutputStream(handler, bufferSize);
forwardingExecuter = executorFactory.create("forward input");
forwardingExecuter.execute(new Runnable() {
public void run() {
byte[] buffer = new byte[bufferSize];
int readCount;
Throwable readFailure = null;
try {
while (true) {
try {
readCount = disconnectableInput.read(buffer, 0, bufferSize);
if (readCount < 0) {
break;
}
} catch (AsynchronousCloseException e) {
break;
} catch (IOException e) {
readFailure = e;
break;
}
outputBuffer.write(buffer, 0, readCount);
}
// will flush any unterminated lines out synchronously
outputBuffer.flush();
} catch (IOException e) {
// should not happen
throw UncheckedException.throwAsUncheckedException(e);
} finally {
handler.endOfStream(readFailure);
}
}
});
started = true;
} finally {
lifecycleLock.unlock();
}
return this;
}
use of java.nio.channels.AsynchronousCloseException in project jetty.project by eclipse.
the class InputStreamResponseListener method onContent.
@Override
public void onContent(Response response, ByteBuffer content, Callback callback) {
if (content.remaining() == 0) {
if (LOG.isDebugEnabled())
LOG.debug("Skipped empty content {}", content);
callback.succeeded();
return;
}
boolean closed;
synchronized (lock) {
closed = this.closed;
if (!closed) {
if (LOG.isDebugEnabled())
LOG.debug("Queueing content {}", content);
chunks.add(new DeferredContentProvider.Chunk(content, callback));
lock.notifyAll();
}
}
if (closed) {
if (LOG.isDebugEnabled())
LOG.debug("InputStream closed, ignored content {}", content);
callback.failed(new AsynchronousCloseException());
}
}
use of java.nio.channels.AsynchronousCloseException in project jetty.project by eclipse.
the class HttpDestination method close.
public void close() {
abort(new AsynchronousCloseException());
if (LOG.isDebugEnabled())
LOG.debug("Closed {}", this);
connectionPool.close();
}
Aggregations