This is an automated email from the ASF dual-hosted git repository.
Aias00 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new e6f0c667f6 fix: add null check when removing channel in
SubscribeRepository remove method (#6952)
e6f0c667f6 is described below
commit e6f0c667f62148b44c5c61fe6811a4b9ea004fc1
Author: wy471x <[email protected]>
AuthorDate: Sat Sep 5 12:32:27 2026 +0800
fix: add null check when removing channel in SubscribeRepository remove
method (#6952)
* fix: add null check when removing channel in SubscribeRepository remove
method
* fix: add unit test and remove unused import for SubscribeRepository
---------
Co-authored-by: aias00 <[email protected]>
---
.../mqtt/repositories/SubscribeRepository.java | 8 ++-
.../mqtt/repositories/SubscribeRepositoryTest.java | 72 ++++++++++++++++++++++
2 files changed, 79 insertions(+), 1 deletion(-)
diff --git
a/shenyu-protocol/shenyu-protocol-mqtt/src/main/java/org/apache/shenyu/protocol/mqtt/repositories/SubscribeRepository.java
b/shenyu-protocol/shenyu-protocol-mqtt/src/main/java/org/apache/shenyu/protocol/mqtt/repositories/SubscribeRepository.java
index 39e82c4400..a0e21742f3 100644
---
a/shenyu-protocol/shenyu-protocol-mqtt/src/main/java/org/apache/shenyu/protocol/mqtt/repositories/SubscribeRepository.java
+++
b/shenyu-protocol/shenyu-protocol-mqtt/src/main/java/org/apache/shenyu/protocol/mqtt/repositories/SubscribeRepository.java
@@ -19,6 +19,7 @@ package org.apache.shenyu.protocol.mqtt.repositories;
import io.netty.channel.Channel;
import io.netty.handler.codec.mqtt.MqttTopicSubscription;
+import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -72,7 +73,12 @@ public class SubscribeRepository implements
BaseRepository<List<String>, List<Ch
* @param channel channel
*/
public void remove(final List<String> topics, final Channel channel) {
- CompletableFuture.runAsync(() -> topics.parallelStream().forEach(topic
-> TOPIC_CHANNEL_FACTORY.get(topic).remove(channel)));
+ CompletableFuture.runAsync(() -> topics.parallelStream().forEach(topic
-> {
+ List<Channel> channels = TOPIC_CHANNEL_FACTORY.get(topic);
+ if (CollectionUtils.isNotEmpty(channels)) {
+ channels.remove(channel);
+ }
+ }));
}
@Override
diff --git
a/shenyu-protocol/shenyu-protocol-mqtt/src/test/java/org/apache/shenyu/protocol/mqtt/repositories/SubscribeRepositoryTest.java
b/shenyu-protocol/shenyu-protocol-mqtt/src/test/java/org/apache/shenyu/protocol/mqtt/repositories/SubscribeRepositoryTest.java
new file mode 100644
index 0000000000..90936503d4
--- /dev/null
+++
b/shenyu-protocol/shenyu-protocol-mqtt/src/test/java/org/apache/shenyu/protocol/mqtt/repositories/SubscribeRepositoryTest.java
@@ -0,0 +1,72 @@
+/*
+ * 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.shenyu.protocol.mqtt.repositories;
+
+import io.netty.channel.Channel;
+import io.netty.handler.codec.mqtt.MqttQoS;
+import io.netty.handler.codec.mqtt.MqttTopicSubscription;
+import org.junit.jupiter.api.Test;
+
+import java.time.Duration;
+import java.util.Collections;
+import java.util.concurrent.ForkJoinPool;
+import java.util.concurrent.TimeUnit;
+
+import static org.awaitility.Awaitility.await;
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+
+/**
+ * Test cases for {@link SubscribeRepository}.
+ */
+public final class SubscribeRepositoryTest {
+
+ private static final String EXISTING_TOPIC = "test/existing-topic";
+
+ private static final String ABSENT_TOPIC = "test/absent-topic";
+
+ private static final String KEPT_TOPIC = "test/kept-topic";
+
+ @Test
+ public void removeRemovesChannelFromExistingTopic() {
+ SubscribeRepository repository = new SubscribeRepository();
+ Channel channel = mock(Channel.class);
+ repository.add(channel, Collections.singletonList(new
MqttTopicSubscription(EXISTING_TOPIC, MqttQoS.AT_MOST_ONCE)));
+ await().atMost(Duration.ofSeconds(5)).until(() ->
repository.get(EXISTING_TOPIC).contains(channel));
+
+ repository.remove(Collections.singletonList(EXISTING_TOPIC), channel);
+
+ await().atMost(Duration.ofSeconds(5)).until(() ->
repository.get(EXISTING_TOPIC).isEmpty());
+ }
+
+ @Test
+ public void removeAbsentTopicDoesNotThrow() {
+ SubscribeRepository repository = new SubscribeRepository();
+ Channel channel = mock(Channel.class);
+ repository.add(channel, Collections.singletonList(new
MqttTopicSubscription(KEPT_TOPIC, MqttQoS.AT_MOST_ONCE)));
+ await().atMost(Duration.ofSeconds(5)).until(() ->
repository.get(KEPT_TOPIC).contains(channel));
+
+ assertDoesNotThrow(() ->
repository.remove(Collections.singletonList(ABSENT_TOPIC), channel));
+ await().atMost(Duration.ofSeconds(5))
+ .until(() -> ForkJoinPool.commonPool().awaitQuiescence(1,
TimeUnit.SECONDS));
+
+ assertTrue(repository.get(ABSENT_TOPIC).isEmpty());
+ assertTrue(repository.get(KEPT_TOPIC).contains(channel));
+ }
+}