gnodet-bot commented on code in PR #26541:
URL: https://github.com/apache/camel/pull/26541#discussion_r4034518804


##########
components/camel-vertx/camel-vertx-websocket/src/test/java/org/apache/camel/component/vertx/websocket/VertxWebsocketProducerNoPeerTest.java:
##########
@@ -0,0 +1,93 @@
+/*
+ * 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.assertj.core.api.Assertions.assertThat;
+
+/**
+ * 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);
+        });
+
+        assertThat(result).isTrue();
+        assertThat(callbacks).hasValue(1);
+        assertThat(doneSync).hasValue(true);
+        assertThat(exchange.getException()).isNull();
+    }
+
+    @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);

Review Comment:
   **Missing callback-count assertion.** 
`anExchangeWithNoPeerIsDoneSynchronously` asserts `callbacks.hasValue(1)` to 
catch double-invocation. This test uses a bare `doneSync::set` method 
reference, so if the callback were invoked twice it would overwrite silently 
and the test would still pass.
   
   ```suggestion
           AtomicInteger callbacks = new AtomicInteger();
           AtomicReference<Boolean> doneSync = new AtomicReference<>();
           boolean result = producer.process(exchange, sync -> {
               callbacks.incrementAndGet();
               doneSync.set(sync);
           });
   ```



##########
components/camel-vertx/camel-vertx-websocket/src/test/java/org/apache/camel/component/vertx/websocket/VertxWebsocketProducerNoPeerTest.java:
##########
@@ -0,0 +1,93 @@
+/*
+ * 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.assertj.core.api.Assertions.assertThat;
+
+/**
+ * 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);
+        });
+
+        assertThat(result).isTrue();
+        assertThat(callbacks).hasValue(1);
+        assertThat(doneSync).hasValue(true);
+        assertThat(exchange.getException()).isNull();
+    }
+
+    @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);
+
+        assertThat(result).isTrue();
+        assertThat(doneSync).hasValue(true);

Review Comment:
   **If you apply the previous suggestion**, also add 
`assertThat(callbacks).hasValue(1)` here, matching the assertion triple in the 
first test.



##########
components/camel-vertx/camel-vertx-websocket/src/test/java/org/apache/camel/component/vertx/websocket/VertxWebsocketProducerNoPeerTest.java:
##########
@@ -0,0 +1,93 @@
+/*
+ * 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.assertj.core.api.Assertions.assertThat;
+
+/**
+ * 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);
+        });
+
+        assertThat(result).isTrue();
+        assertThat(callbacks).hasValue(1);
+        assertThat(doneSync).hasValue(true);
+        assertThat(exchange.getException()).isNull();
+    }

Review Comment:
   **No test for the unmatched connection-key WARN path.** The PR adds 
`LOG.warn()` when a `CONNECTION_KEY` header value has no matching peer, but 
neither test exercises it. A third test setting 
`VertxWebsocketConstants.CONNECTION_KEY` to a value absent from the peer 
registry would confirm: (a) the exchange completes synchronously, (b) 
`exchange.getException()` is null, and (c) the callback fires exactly once. 
Without it, a future refactor that silently drops the exchange again would go 
undetected.



-- 
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