r2dbc/r2dbc-proxy

Context's transitivity is broken

liuzzzz opened this issue · 3 comments

MethodInvocationSubscriber, QueryInvocationSubscriber and ResultInvocationSubscriber implements CoreSubscriber but not override currentContext. So, it always returns Context0。

@liuzzzz Thanks for the report.

Can you provide a sample code or snippet to show the use case?
I have a change in my local.
I'd like to double-check the usage.

@ttddyy

    @Test
    @SuppressWarnings("unchecked")
    void contextWriteWithQueryExecution() throws Throwable {
        ConnectionInfo connectionInfo = mock(ConnectionInfo.class);
        ProxyConfig proxyConfig = ProxyConfig.builder().build();
        Batch batch = mock(Batch.class);
        AtomicReference<Boolean> contextHasKey = new AtomicReference<>();
        Publisher<Result> mockExecution = Flux.deferWithContext(context -> {
            boolean hasKey = context.hasKey(Optional.class);
            contextHasKey.set(hasKey);
            // if MethodInvocationSubscriber and QueryInvocationSubscriber override CoreSubscriber#currentContext the {hasKey} will be true;
            // now it's false
            return Flux.empty();
        });
        OngoingStubbing<Publisher<? extends Result>> when = when(batch.execute());
        when.thenReturn(mockExecution); // mock batch execution

        BatchCallbackHandler callback = new BatchCallbackHandler(batch, connectionInfo, proxyConfig);
        Object result = callback.invoke(batch, EXECUTE_METHOD, new String[]{});
        Flux<?> test = Flux.from((Publisher<?>) result)
                // maybe set dynamic datasource key (that's why I found it) or other something
                .subscriberContext(context -> context.put(Optional.class, Optional.of("test")));
        StepVerifier.create((Publisher<? extends Result>) test)
                .verifyComplete();
        assertThat(contextHasKey).hasValue(true);
    }

@liuzzzz
I pushed my change and also verified that the above is fixed.
Thanks for the sample.