Skip to content

Commit

Permalink
Remove un-necessary usage of context internal unwrap
Browse files Browse the repository at this point in the history
  • Loading branch information
vietj committed Aug 1, 2024
1 parent 7045785 commit b07db80
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import io.vertx.core.*;
import io.vertx.core.eventbus.Message;
import io.vertx.core.internal.ContextInternal;
import io.vertx.core.internal.VertxInternal;

import java.util.HashMap;
import java.util.LinkedList;
Expand All @@ -32,16 +31,10 @@ public class Serializer implements Closeable {
private final Map<String, SerializerQueue> queues;

private Serializer(ContextInternal context) {
ContextInternal unwrapped = context.unwrap();
if (unwrapped.isEventLoopContext()) {
ctx = unwrapped;
} else {
VertxInternal vertx = unwrapped.owner();
ctx = vertx.createEventLoopContext(unwrapped.nettyEventLoop(), unwrapped.workerPool(), unwrapped.classLoader());
}
ctx = context.owner().createEventLoopContext(context.nettyEventLoop(), context.workerPool(), context.classLoader());
queues = new HashMap<>();
if (unwrapped.isDeployment()) {
unwrapped.addCloseHook(this);
if (context.isDeployment()) {
context.addCloseHook(this);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public class WorkerExecutor implements EventExecutor {
public static io.vertx.core.impl.WorkerExecutor unwrapWorkerExecutor() {
ContextInternal ctx = (ContextInternal) Vertx.currentContext();
if (ctx != null) {
ctx = ctx.unwrap();
Executor executor = ctx.executor();
if (executor instanceof io.vertx.core.impl.WorkerExecutor) {
return (io.vertx.core.impl.WorkerExecutor) executor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,8 @@ public interface ConnectionPool<C> {
* it returns a new event-loop context that reuses the Netty event-loop of the context argument.
*/
Function<ContextInternal, ContextInternal> EVENT_LOOP_CONTEXT_PROVIDER = ctx -> {
ctx = ctx.unwrap();
if (ctx.isEventLoopContext()) {
return ctx;
} else {
VertxInternal vertx = ctx.owner();
return vertx.createEventLoopContext(ctx.nettyEventLoop(), ctx.workerPool(), ctx.classLoader());
}
VertxInternal vertx = ctx.owner();
return vertx.createEventLoopContext(ctx.nettyEventLoop(), vertx.getWorkerPool(), null);
};

static <C> ConnectionPool<C> pool(PoolConnector<C> connector, int[] maxSizes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import io.vertx.core.*;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.*;
import io.vertx.core.internal.ContextInternal;
import io.vertx.core.internal.buffer.BufferInternal;
import io.vertx.core.http.impl.Http2UpgradeClientConnection;
import io.vertx.core.http.impl.HttpClientConnectionInternal;
Expand Down Expand Up @@ -1306,15 +1307,15 @@ public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers
});
ChannelFuture s = bootstrap.bind(DEFAULT_HTTPS_HOST, DEFAULT_HTTPS_PORT).sync();
try {
Context ctx = vertx.getOrCreateContext();
ContextInternal ctx = (ContextInternal) vertx.getOrCreateContext();
client.close();
ctx.runOnContext(v -> {
client = vertx.createHttpClient(createBaseClientOptions());
client = vertx.httpClientBuilder()
.with(clientOptions)
.withConnectHandler(conn -> {
conn.exceptionHandler(err -> {
assertSame(ctx, Vertx.currentContext());
assertSame(ctx.nettyEventLoop(), ((ContextInternal)Vertx.currentContext()).nettyEventLoop());
if (err instanceof Http2Exception) {
complete();
}
Expand Down Expand Up @@ -1795,13 +1796,13 @@ private void testIdleTimeout(HttpServerOptions serverOptions, HttpClientOptions
});
startServer(testAddress);
client.close();
Context ctx = vertx.getOrCreateContext();
ContextInternal ctx = (ContextInternal) vertx.getOrCreateContext();
ctx.runOnContext(v1 -> {
client = vertx.httpClientBuilder()
.with(clientOptions.setIdleTimeout(2))
.withConnectHandler(conn -> {
conn.closeHandler(v2 -> {
assertSame(ctx, Vertx.currentContext());
assertSame(ctx.nettyEventLoop(), ((ContextInternal)Vertx.currentContext()).nettyEventLoop());
complete();
});
})
Expand Down
5 changes: 3 additions & 2 deletions vertx-core/src/test/java/io/vertx/tests/http/HttpTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.vertx.core.http.*;
import io.vertx.core.http.impl.CleanableHttpClient;
import io.vertx.core.http.impl.HttpClientImpl;
import io.vertx.core.internal.ContextInternal;
import io.vertx.core.internal.http.HttpServerRequestInternal;
import io.vertx.core.http.impl.ServerCookie;
import io.vertx.core.http.impl.headers.HeadersMultiMap;
Expand Down Expand Up @@ -3635,12 +3636,12 @@ public void testClientGlobalConnectionHandler() throws Exception {
req.response().end();
});
startServer(testAddress);
Context ctx = vertx.getOrCreateContext();
ContextInternal ctx = (ContextInternal) vertx.getOrCreateContext();
client.close();
client = vertx.httpClientBuilder()
.with(createBaseClientOptions())
.withConnectHandler(conn -> {
assertSame(ctx, Vertx.currentContext());
assertSame(ctx.nettyEventLoop(), ((ContextInternal)Vertx.currentContext()).nettyEventLoop());
complete();
})
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ public void testConnect() {
Connection expected = new Connection();
pool.acquire(context, 0, onSuccess(lease -> {
assertSame(expected, lease.get());
assertSame(context, Vertx.currentContext());
assertSame(context.nettyEventLoop(), ((ContextInternal)Vertx.currentContext()).nettyEventLoop());
assertEquals(0, pool.requests());
testComplete();
}));
assertEquals(1, pool.requests());
ConnectionRequest request = mgr.assertRequest();
assertSame(context, request.context);
assertSame(context.nettyEventLoop(), request.context.nettyEventLoop());
request.connect(expected, 0);
await();
}
Expand All @@ -74,12 +74,12 @@ public void testAcquireRecycledConnection() throws Exception {
latch.countDown();
}));
ConnectionRequest request = mgr.assertRequest();
assertSame(context, request.context);
assertSame(context.nettyEventLoop(), request.context.nettyEventLoop());
request.connect(expected, 0);
awaitLatch(latch);
pool.acquire(context, 0, onSuccess(lease -> {
assertSame(expected, lease.get());
assertSame(context, Vertx.currentContext());
assertSame(context.nettyEventLoop(), ((ContextInternal)Vertx.currentContext()).nettyEventLoop());
testComplete();
}));
await();
Expand All @@ -105,7 +105,7 @@ public void testRecycleRemovedConnection() throws Exception {
Connection expected2 = new Connection();
pool.acquire(context, 0, onSuccess(lease -> {
assertSame(expected2, lease.get());
assertSame(context, Vertx.currentContext());
assertSame(context.nettyEventLoop(), ((ContextInternal)Vertx.currentContext()).nettyEventLoop());
testComplete();
}));
ConnectionRequest request2 = mgr.assertRequest();
Expand Down Expand Up @@ -255,7 +255,7 @@ public void testWaiter() throws Exception {
AtomicBoolean recycled = new AtomicBoolean();
ContextInternal ctx2 = vertx.createEventLoopContext();
pool.acquire(ctx2, 0, onSuccess(lease2 -> {
assertSame(ctx1, Vertx.currentContext());
assertSame(ctx1.nettyEventLoop(), ((ContextInternal)Vertx.currentContext()).nettyEventLoop());
assertTrue(recycled.get());
testComplete();
}));
Expand Down Expand Up @@ -318,7 +318,7 @@ public void testRemoveSingleConnectionWithWaiter() throws Exception {
Connection conn2 = new Connection();
ContextInternal ctx2 = vertx.createEventLoopContext();
pool.acquire(ctx2, 0, onSuccess(lease2 -> {
assertSame(ctx2, Vertx.currentContext());
assertSame(ctx2.nettyEventLoop(), ((ContextInternal)Vertx.currentContext()).nettyEventLoop());
assertTrue(evicted.get());
assertSame(conn2, lease2.get());
testComplete();
Expand Down Expand Up @@ -885,7 +885,7 @@ public void testConnectionSelector() throws Exception {
assertEquals(1, pooled.available());
assertEquals(1, pooled.concurrency());
assertSame(conn1, pooled.get());
assertSame(context, pooled.context());
assertSame(context.nettyEventLoop(), pooled.context().nettyEventLoop());
assertSame(context, waiter.context());
return pooled;
});
Expand Down Expand Up @@ -936,7 +936,7 @@ public void testDefaultContextProviderUnwrap() {
}));
assertEquals(1, pool.requests());
ConnectionRequest request = mgr.assertRequest();
assertSame(context, request.context);
assertSame(context.nettyEventLoop(), request.context.nettyEventLoop());
}

@Test
Expand Down

0 comments on commit b07db80

Please sign in to comment.