You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shenyu.apache.org by im...@apache.org on 2022/06/14 14:18:42 UTC
[incubator-shenyu] branch master updated: [type: test]: Add unit test for mqtt plugin (#3559)
This is an automated email from the ASF dual-hosted git repository.
impactcn pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new 56f8f50f2 [type: test]: Add unit test for mqtt plugin (#3559)
56f8f50f2 is described below
commit 56f8f50f253994dfc2bd26ac9dd37355d328d894
Author: 尔等同学 <37...@users.noreply.github.com>
AuthorDate: Tue Jun 14 22:18:37 2022 +0800
[type: test]: Add unit test for mqtt plugin (#3559)
* add mqtt plugin unit test
* fix ci
* fix CI
Co-Authored-By: Sinsy <im...@apache.org>
* fix CI
Co-Authored-By: Sinsy <im...@apache.org>
* optimize code
Co-authored-by: Sinsy <im...@apache.org>
---
.../plugin/mqtt/handler/MqttPluginDataHandler.java | 30 +++++++++++++++++++---
.../mqtt/handler/MqttPluginDataHandlerTest.java | 6 ++---
2 files changed, 29 insertions(+), 7 deletions(-)
diff --git a/shenyu-plugin/shenyu-plugin-mqtt/src/main/java/org/apache/shenyu/plugin/mqtt/handler/MqttPluginDataHandler.java b/shenyu-plugin/shenyu-plugin-mqtt/src/main/java/org/apache/shenyu/plugin/mqtt/handler/MqttPluginDataHandler.java
index 4c1a2cacd..ae9049971 100644
--- a/shenyu-plugin/shenyu-plugin-mqtt/src/main/java/org/apache/shenyu/plugin/mqtt/handler/MqttPluginDataHandler.java
+++ b/shenyu-plugin/shenyu-plugin-mqtt/src/main/java/org/apache/shenyu/plugin/mqtt/handler/MqttPluginDataHandler.java
@@ -25,6 +25,9 @@ import org.apache.shenyu.protocol.mqtt.BootstrapServer;
import org.apache.shenyu.protocol.mqtt.MqttBootstrapServer;
import org.apache.shenyu.protocol.mqtt.MqttServerConfiguration;
+import java.net.InetAddress;
+import java.net.Socket;
+
/**
* The type Mqtt plugin data handler.
*/
@@ -34,13 +37,15 @@ public class MqttPluginDataHandler implements PluginDataHandler {
@Override
public void handlerPlugin(final PluginData pluginData) {
+ MqttServerConfiguration configuration = GsonUtils.getInstance().fromJson(pluginData.getConfig(), MqttServerConfiguration.class);
+ configuration.afterPropertiesSet();
if (pluginData.getEnabled()) {
- MqttServerConfiguration configuration = GsonUtils.getInstance().fromJson(pluginData.getConfig(), MqttServerConfiguration.class);
- configuration.afterPropertiesSet();
server.init();
server.start();
} else {
- server.shutdown();
+ if (isPortUsing(configuration.getPort())) {
+ server.shutdown();
+ }
}
}
@@ -49,4 +54,23 @@ public class MqttPluginDataHandler implements PluginDataHandler {
return PluginEnum.MQTT.getName();
}
+ /**
+ * Ture is use else false.
+ *
+ * @param port server port
+ * @return boolean
+ */
+ private boolean isPortUsing(final int port) {
+ boolean flag = false;
+ try {
+ InetAddress address = InetAddress.getByName("127.0.0.1");
+ Socket socket = new Socket(address, port);
+ flag = true;
+ } catch (Exception ignored) {
+
+ }
+ return flag;
+
+ }
+
}
diff --git a/shenyu-plugin/shenyu-plugin-mqtt/src/test/java/org/apache/shenyu/plugin/mqtt/handler/MqttPluginDataHandlerTest.java b/shenyu-plugin/shenyu-plugin-mqtt/src/test/java/org/apache/shenyu/plugin/mqtt/handler/MqttPluginDataHandlerTest.java
index 292716b00..14ca4fbe0 100644
--- a/shenyu-plugin/shenyu-plugin-mqtt/src/test/java/org/apache/shenyu/plugin/mqtt/handler/MqttPluginDataHandlerTest.java
+++ b/shenyu-plugin/shenyu-plugin-mqtt/src/test/java/org/apache/shenyu/plugin/mqtt/handler/MqttPluginDataHandlerTest.java
@@ -19,7 +19,6 @@ package org.apache.shenyu.plugin.mqtt.handler;
import org.apache.shenyu.common.dto.PluginData;
import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import java.net.InetAddress;
@@ -41,8 +40,7 @@ public class MqttPluginDataHandlerTest {
}
@Test
- @Disabled
- public void testEnableConfiguration() {
+ public void testEnableConfiguration() throws InterruptedException {
final PluginData enablePluginData = new PluginData("pluginId", "pluginName", "{\n"
+ " \"port\": 9500,"
+ " \"bossGroupThreadCount\": 1,"
@@ -56,7 +54,6 @@ public class MqttPluginDataHandlerTest {
+ "}", "0", true);
mqttPluginDataHandlerUnderTest.handlerPlugin(enablePluginData);
assertTrue(isPortUsing());
-
final PluginData disablePluginData = new PluginData("pluginId", "pluginName", "{\n"
+ " \"port\": 9500,"
+ " \"bossGroupThreadCount\": 1,"
@@ -69,6 +66,7 @@ public class MqttPluginDataHandlerTest {
+ " \"leakDetectorLevel\": \"DISABLED\""
+ "}", "0", false);
mqttPluginDataHandlerUnderTest.handlerPlugin(disablePluginData);
+ Thread.sleep(5000);
assertFalse(isPortUsing());
}