Skip to content

Preserve interrupt flag in H2 pool close #962

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

Merged
merged 2 commits into from
Dec 18, 2018
Merged
Show file tree
Hide file tree
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
@@ -0,0 +1,5 @@
{
"category": "Netty NIO HTTP Client",
"type": "bugfix",
"description": "Preserve interrupted flag if `Http2MultiplexedChannelPool#close()` interrupted."
}
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ public void close() {
}
connectionPool.close();
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
throw new RuntimeException(ie);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,17 @@
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InOrder;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.Collections;
import java.util.concurrent.CompletableFuture;

import static org.assertj.core.api.Assertions.assertThat;


/**
* Tests for {@link Http2MultiplexedChannelPool}.
*/
@RunWith(MockitoJUnitRunner.class)
public class Http2MultiplexedChannelPoolTest {
private static EventLoopGroup loopGroup;

Expand Down Expand Up @@ -93,4 +90,45 @@ public void acquireAfterCloseFails() throws InterruptedException {

assertThat(h2Pool.acquire().await().isSuccess()).isFalse();
}

@Test(timeout = 5_000)
public void interruptDuringClosePreservesFlag() throws InterruptedException {
SocketChannel channel = new NioSocketChannel();
try {
loopGroup.register(channel).awaitUninterruptibly();
Promise<Channel> channelPromise = new DefaultPromise<>(loopGroup.next());
channelPromise.setSuccess(channel);

ChannelPool connectionPool = Mockito.mock(ChannelPool.class);
Promise<Void> releasePromise = Mockito.spy(new DefaultPromise<>(loopGroup.next()));

Mockito.when(connectionPool.release(Mockito.eq(channel))).thenReturn(releasePromise);

MultiplexedChannelRecord record = new MultiplexedChannelRecord(channelPromise,
channel,
8,
(ch, rec) -> {
});
Http2MultiplexedChannelPool h2Pool = new Http2MultiplexedChannelPool(connectionPool, loopGroup.next(), 2, Collections.singletonList(record));

CompletableFuture<Boolean> interrupteFlagPreserved = new CompletableFuture<>();

Thread t = new Thread(() -> {
try {
h2Pool.close();
} catch (Exception e) {
if (e.getCause() instanceof InterruptedException && Thread.currentThread().isInterrupted()) {
interrupteFlagPreserved.complete(true);
}
}
});

t.start();
t.interrupt();
t.join();
assertThat(interrupteFlagPreserved.join()).isTrue();
} finally {
channel.close().awaitUninterruptibly();
}
}
}