Skip to content

Commit 831180a

Browse files
committed
Use the correct method in context storage context duplication.
Motivation: gRPC IO context storage requires to copy the vertx context, at the moment the copy is hand rolled and only affects the context local map. Instead it should use the copying variant of duplicate. Changes: Replace the hand rolled duplication by the copying variant of ContexInternal#duplicate
1 parent 290182e commit 831180a

File tree

5 files changed

+45
-9
lines changed

5 files changed

+45
-9
lines changed

vertx-grpcio-context-storage/src/main/java/io/grpc/override/ContextStorageOverride.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,12 @@ public ContextStorageOverride() {
2121
// Do not remove, empty constructor required by gRPC
2222
}
2323

24-
private static ContextInternal duplicate(ContextInternal context) {
25-
ContextInternal dup = context.duplicate();
26-
if (context.isDuplicate()) {
27-
dup.localContextData().putAll(context.localContextData()); // For now hand rolled but should be handled by context duplication
28-
}
29-
return dup;
30-
}
31-
3224
@Override
3325
public Context doAttach(Context toAttach) {
3426
ContextInternal vertxContext = vertxContext();
3527
Context toRestoreLater;
3628
if (vertxContext != null) {
37-
ContextInternal next = duplicate(vertxContext);
29+
ContextInternal next = vertxContext.duplicate(true);
3830
ContextInternal prev = next.beginDispatch();
3931
next.putLocal(ContextStorageService.CONTEXT_LOCAL, SAME_THREAD, new GrpcStorage(toAttach, prev));
4032
GrpcStorage local = next.getLocal(ContextStorageService.CONTEXT_LOCAL, SAME_THREAD);

vertx-grpcio-context-storage/src/test/java/io/vertx/grpc/context/storage/ContextStorageTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,21 +153,28 @@ public void testNonVertxThread(TestContext should) {
153153
should.assertNull(key2.get());
154154
}
155155

156+
@SuppressWarnings("removal")
156157
@Test
157158
public void testNestedDuplicate(TestContext should) {
158159
Async async = should.async();
159160
io.vertx.core.internal.ContextInternal context = ((ContextInternal)vertx.getOrCreateContext()).duplicate();
160161
context.putLocal("local", "local-value-1");
162+
CopiableObject value = new CopiableObject();
163+
context.putLocal(MockContextStorage.CONTEXT_LOCAL, value);
161164
context.runOnContext(v1 -> {
162165
should.assertEquals("local-value-1", context.getLocal("local"));
166+
should.assertEquals(value, context.getLocal(MockContextStorage.CONTEXT_LOCAL));
163167
Context ctx1 = Context.ROOT.withValue(key1, "value-1");
164168
ctx1.run(() -> {
165169
io.vertx.core.internal.ContextInternal current = (ContextInternal) vertx.getOrCreateContext();
166170
should.assertNotEquals(context, current);
167171
should.assertEquals("local-value-1", current.getLocal("local"));
172+
should.assertEquals(value, current.getLocal(MockContextStorage.CONTEXT_LOCAL).ref);
168173
current.putLocal("local", "local-value-2");
174+
current.putLocal(MockContextStorage.CONTEXT_LOCAL, new CopiableObject());
169175
});
170176
should.assertEquals("local-value-1", context.getLocal("local"));
177+
should.assertEquals(value, context.getLocal(MockContextStorage.CONTEXT_LOCAL));
171178
async.complete();
172179
});
173180
async.awaitSuccess();
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.vertx.grpc.context.storage;
2+
3+
public class CopiableObject {
4+
5+
public final CopiableObject ref;
6+
7+
public CopiableObject(CopiableObject ref) {
8+
this.ref = ref;
9+
}
10+
11+
public CopiableObject() {
12+
this.ref = null;
13+
}
14+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package io.vertx.grpc.context.storage;
2+
3+
import io.vertx.core.internal.VertxBootstrap;
4+
import io.vertx.core.spi.VertxServiceProvider;
5+
import io.vertx.core.spi.context.storage.ContextLocal;
6+
import io.vertx.grpc.contextstorage.GrpcStorage;
7+
8+
import java.util.HashSet;
9+
import java.util.Set;
10+
11+
/**
12+
* Register the local storage.
13+
*/
14+
public class MockContextStorage implements VertxServiceProvider {
15+
16+
public static final ContextLocal<CopiableObject> CONTEXT_LOCAL = ContextLocal.registerLocal(CopiableObject.class,
17+
CopiableObject::new);
18+
19+
@Override
20+
public void init(VertxBootstrap builder) {
21+
}
22+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
io.vertx.grpc.context.storage.MockContextStorage

0 commit comments

Comments
 (0)