ffang commented on PR #3723:
URL: https://github.com/apache/cxf/pull/3723#issuecomment-6931370625

   Hi @ppalaga , a follow-up thought on where the check lives, again as a 
suggestion, not a blocker. 
   
   The fix keys on 
`org.apache.cxf.ws.addressing.MAPAggregator.decoupledDestination`. That isn't 
new (the same 202 block already reads it for the callback, under the "REVISIT 
move the decoupled destination property name into api" comment), but it would 
be nice not to rely on it more. A transport-neutral alternative: don't let the 
202 replace the response of an exchange that `ClientImpl` has already finished:
   
   ```diff
   -                    exchange.put("IN_CHAIN_COMPLETE", Boolean.TRUE);
   +                    synchronized (exchange) {
   +                        if 
(Boolean.TRUE.equals(exchange.get(ClientImpl.FINISHED))) {
   +                            // the response has already arrived separately 
(e.g. at a decoupled endpoint)
   +                            // and completed the exchange: this 
acknowledgement must not replace it
   +                            return;
   +                        }
   +                        exchange.put("IN_CHAIN_COMPLETE", Boolean.TRUE);
   
   -                    exchange.setInMessage(inMessage);
   +                        exchange.setInMessage(inMessage);
   +                    }
   ```
   
   `ClientImpl.onMessage()` sets `FINISHED` and the in message under the same 
lock, so there's no window between the check and the update. A 202 that arrives 
before the reply is handled as it is today. It also covers a 202 with a body 
that the client doesn't process (the `in == null` case), without having to know 
about WS-Addressing.
   
   Your `HTTPConduitDecoupledResponseTest` passes with it, apart from 
`testAcknowledgementBeforeReply`. That test asserts that an early decoupled 202 
leaves no in message and no `IN_CHAIN_COMPLETE`, which is specific to the 
current approach. I changed it to check the outcome instead, and added two 
tests:
   - `testAcknowledgementWithUnprocessedBodyAfterReply`: a 202 with a body the 
client doesn't process arrives after the reply.
   - `testConcurrentReplyAndAcknowledgementRepeated`: the concurrent test, 
repeated 500 times with a fresh exchange each time. This needs the per-call 
fields to be reset (`reset()`).
   
   ```diff
        private final boolean propagate;
   -    private final Exchange exchange = new ExchangeImpl();
   -    private final Message out = new MessageImpl();
   -    private final Message reply = new MessageImpl();
   -    private final ClientCallback callback = new ClientCallback();
   +    private Exchange exchange;
   +    private Message out;
   +    private Message reply;
   +    private ClientCallback callback;
        private final List<Message> observed = new ArrayList<>();
   +    private Endpoint endpoint;
   @@
   -        Endpoint endpoint = mock(Endpoint.class);
   +        endpoint = mock(Endpoint.class);
            when(endpoint.getEndpointInfo()).thenReturn(endpointInfo);
   -        exchange.put(Endpoint.class, endpoint);
   -        exchange.put(ClientCallback.class, callback);
   -        exchange.setOutMessage(out);
   -        out.put(Message.PROPAGATE_202_RESPONSE_ONEWAY_OR_PARTIAL, 
propagate);
   -        reply.setContent(List.class, List.of("Hello Paul!"));
   +        reset();
   @@
   +    /** a new call: new exchange, request and reply */
   +    private void reset() {
   +        exchange = new ExchangeImpl();
   +        out = new MessageImpl();
   +        reply = new MessageImpl();
   +        callback = new ClientCallback();
   +        observed.clear();
   +        exchange.put(Endpoint.class, endpoint);
   +        exchange.put(ClientCallback.class, callback);
   +        exchange.setOutMessage(out);
   +        out.put(Message.PROPAGATE_202_RESPONSE_ONEWAY_OR_PARTIAL, 
propagate);
   +        reply.setContent(List.class, List.of("Hello Paul!"));
   +    }
   @@
        @Test
        public void testAcknowledgementBeforeReply() throws Exception {
            respond(202, "");
   -        assertNull(exchange.getInMessage());
   +        // the exchange is not finished, and the callback is left to the 
reply
            assertNull(exchange.get(ClientImpl.FINISHED));
   -        assertAcknowledgementIgnored();
   +        assertSame(callback, exchange.get(ClientCallback.class));
   +        assertFalse(callback.isDone());
   +        completeReply();
   +        assertSame(reply, exchange.getInMessage());
   +        assertEquals(List.of("Hello Paul!"), 
exchange.getInMessage().getContent(List.class));
   +    }
   +
   +    @Test
   +    public void testAcknowledgementWithUnprocessedBodyAfterReply() throws 
Exception {
   +        // a 202 with a body the client does not process (no 
processOneWayResponse) is an acknowledgement too
            completeReply();
   +        respond(202, "<soap:Envelope/>");
            assertReplyPreserved();
        }
   
   +    @Test
   +    public void testConcurrentReplyAndAcknowledgementRepeated() throws 
Exception {
   +        ExecutorService executor = Executors.newFixedThreadPool(2);
   +        try {
   +            for (int i = 0; i < 500; i++) {
   +                reset();
   +                CountDownLatch start = new CountDownLatch(1);
   +                Future<?> acknowledgement = executor.submit(() -> {
   +                    assertTrue(start.await(5, TimeUnit.SECONDS));
   +                    respond(202, "");
   +                    return null;
   +                });
   +                Future<?> delivery = executor.submit(() -> {
   +                    assertTrue(start.await(5, TimeUnit.SECONDS));
   +                    completeReply();
   +                    return null;
   +                });
   +                start.countDown();
   +                acknowledgement.get(5, TimeUnit.SECONDS);
   +                delivery.get(5, TimeUnit.SECONDS);
   +                assertSame("iteration " + i, reply, 
exchange.getInMessage());
   +            }
   +        } finally {
   +            executor.shutdownNow();
   +        }
   +    }
   ```
   
   One difference: with `propagate202Response` set *and* a decoupled endpoint, 
a 202 that arrives first still reaches the observer, as it does today. Only the 
JAX-RS client sets that flag, and it has no decoupled endpoints, so I don't 
think it matters.
   
   Cheers
   Freeman


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to