gnodet-bot commented on code in PR #26541: URL: https://github.com/apache/camel/pull/26541#discussion_r4060274323
########## components/camel-vertx/camel-vertx-websocket/src/test/java/org/apache/camel/component/vertx/websocket/VertxWebsocketProducerNoPeerTest.java: ########## @@ -0,0 +1,95 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.vertx.websocket; + +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicReference; + +import org.apache.camel.Exchange; +import org.apache.camel.impl.DefaultCamelContext; +import org.apache.camel.support.DefaultExchange; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * An exchange with no peer to send to is finished by the producer itself, so it has to report that it was done + * synchronously. Builds the endpoint directly, so nothing reaches a server. + */ +class VertxWebsocketProducerNoPeerTest { + + private DefaultCamelContext context; + + @AfterEach + void tearDown() { + if (context != null) { + context.stop(); + } + } + + private VertxWebsocketProducer producer() throws Exception { + context = new DefaultCamelContext(); + + VertxWebsocketComponent component = new VertxWebsocketComponent(); + component.setCamelContext(context); + + VertxWebsocketEndpoint endpoint + = (VertxWebsocketEndpoint) component.createEndpoint("vertx-websocket:localhost:1234/test"); + return (VertxWebsocketProducer) endpoint.createProducer(); + } + + @Test + void anExchangeWithNoPeerIsDoneSynchronously() throws Exception { + VertxWebsocketProducer producer = producer(); + + Exchange exchange = new DefaultExchange(context); + exchange.getIn().setBody("a message nobody is listening for"); + // broadcasting to an empty host registry is the one path that reaches no peer without opening a connection + exchange.getIn().setHeader(VertxWebsocketConstants.SEND_TO_ALL, true); + + AtomicInteger callbacks = new AtomicInteger(); + AtomicReference<Boolean> doneSync = new AtomicReference<>(); + + // the callback used to be completed with doneSync=true while the method returned false, which says the + // opposite: that the exchange would be finished from a write handler that never runs + boolean result = producer.process(exchange, sync -> { + callbacks.incrementAndGet(); + doneSync.set(sync); + }); + + assertTrue(result, "process must report that it finished the exchange itself"); + assertEquals(1, callbacks.get()); + assertTrue(doneSync.get()); + assertNull(exchange.getException()); + } + + @Test + void anExchangeWithNoBodyIsDoneSynchronously() throws Exception { + VertxWebsocketProducer producer = producer(); + + Exchange exchange = new DefaultExchange(context); + + AtomicReference<Boolean> doneSync = new AtomicReference<>(); + boolean result = producer.process(exchange, doneSync::set); + + assertTrue(result, "process must report that it finished the exchange itself"); + assertTrue(doneSync.get()); Review Comment: **Not addressed from previous review — callback-count assertion still missing.** The `doneSync::set` method reference silently overwrites on a double invocation; a second `callback.done(true)` would leave the test green. The first test fixed this by counting invocations with an `AtomicInteger` — the same pattern needs to be applied here. ```suggestion AtomicInteger callbacks = new AtomicInteger(); AtomicReference<Boolean> doneSync = new AtomicReference<>(); boolean result = producer.process(exchange, sync -> { callbacks.incrementAndGet(); doneSync.set(sync); }); assertTrue(result, "process must report that it finished the exchange itself"); assertEquals(1, callbacks.get()); assertTrue(doneSync.get()); ``` ########## components/camel-vertx/camel-vertx-websocket/src/test/java/org/apache/camel/component/vertx/websocket/VertxWebsocketProducerNoPeerTest.java: ########## @@ -0,0 +1,95 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.vertx.websocket; + +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicReference; + +import org.apache.camel.Exchange; +import org.apache.camel.impl.DefaultCamelContext; +import org.apache.camel.support.DefaultExchange; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * An exchange with no peer to send to is finished by the producer itself, so it has to report that it was done + * synchronously. Builds the endpoint directly, so nothing reaches a server. + */ +class VertxWebsocketProducerNoPeerTest { + + private DefaultCamelContext context; + + @AfterEach + void tearDown() { + if (context != null) { + context.stop(); + } + } + + private VertxWebsocketProducer producer() throws Exception { + context = new DefaultCamelContext(); + + VertxWebsocketComponent component = new VertxWebsocketComponent(); + component.setCamelContext(context); + + VertxWebsocketEndpoint endpoint + = (VertxWebsocketEndpoint) component.createEndpoint("vertx-websocket:localhost:1234/test"); + return (VertxWebsocketProducer) endpoint.createProducer(); + } + + @Test + void anExchangeWithNoPeerIsDoneSynchronously() throws Exception { + VertxWebsocketProducer producer = producer(); + + Exchange exchange = new DefaultExchange(context); + exchange.getIn().setBody("a message nobody is listening for"); + // broadcasting to an empty host registry is the one path that reaches no peer without opening a connection + exchange.getIn().setHeader(VertxWebsocketConstants.SEND_TO_ALL, true); + + AtomicInteger callbacks = new AtomicInteger(); + AtomicReference<Boolean> doneSync = new AtomicReference<>(); + + // the callback used to be completed with doneSync=true while the method returned false, which says the + // opposite: that the exchange would be finished from a write handler that never runs + boolean result = producer.process(exchange, sync -> { + callbacks.incrementAndGet(); + doneSync.set(sync); + }); + + assertTrue(result, "process must report that it finished the exchange itself"); + assertEquals(1, callbacks.get()); + assertTrue(doneSync.get()); + assertNull(exchange.getException()); + } + + @Test + void anExchangeWithNoBodyIsDoneSynchronously() throws Exception { + VertxWebsocketProducer producer = producer(); + + Exchange exchange = new DefaultExchange(context); + + AtomicReference<Boolean> doneSync = new AtomicReference<>(); + boolean result = producer.process(exchange, doneSync::set); + + assertTrue(result, "process must report that it finished the exchange itself"); + assertTrue(doneSync.get()); + } +} Review Comment: **Not addressed from previous review — missing test for the unmatched `CONNECTION_KEY` path.** The PR adds `LOG.warn()` in `getConnectedPeers()` when a connection key is not in the peer registry (lines 126-139 of the producer diff), but no test exercises this path. A future refactor that silently drops the exchange again instead of completing it would go undetected. A third test that sets `VertxWebsocketConstants.CONNECTION_KEY` to a value absent from the peer registry should assert: (a) `process()` returns `true`, (b) the callback fires exactly once with `sync=true`, and (c) `exchange.getException()` is null. -- 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]
