This is an automated email from the ASF dual-hosted git repository.
dinglei pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/rocketmq-mqtt.git
The following commit(s) were added to refs/heads/main by this push:
new f5a8efa codeCov for #22, add config and hook test for mqtt-cs (#62)
f5a8efa is described below
commit f5a8efa3b802ec8f47e54b73570d5ee4baf4eda2
Author: YongXing <[email protected]>
AuthorDate: Mon Apr 11 10:20:04 2022 +0800
codeCov for #22, add config and hook test for mqtt-cs (#62)
Co-authored-by: AhaThinking <[email protected]>
---
.../mqtt/cs/config/ConnectConfListener.java | 3 +-
.../mqtt/cs/test/TestDefaultChannelManager.java | 60 ----------------
.../cs/test/config/TestConnectConfListener.java | 82 ++++++++++++++++++++++
.../cs/test/hook/TestUpstreamHookManagerImpl.java | 73 +++++++++++++++++++
4 files changed, 157 insertions(+), 61 deletions(-)
diff --git
a/mqtt-cs/src/main/java/org/apache/rocketmq/mqtt/cs/config/ConnectConfListener.java
b/mqtt-cs/src/main/java/org/apache/rocketmq/mqtt/cs/config/ConnectConfListener.java
index 940d703..1ed2577 100644
---
a/mqtt-cs/src/main/java/org/apache/rocketmq/mqtt/cs/config/ConnectConfListener.java
+++
b/mqtt-cs/src/main/java/org/apache/rocketmq/mqtt/cs/config/ConnectConfListener.java
@@ -44,6 +44,7 @@ public class ConnectConfListener {
private File confFile;
private ScheduledThreadPoolExecutor scheduler;
private AtomicLong gmt = new AtomicLong();
+ private long refreshSecs = 3;
@PostConstruct
public void start() {
@@ -65,7 +66,7 @@ public class ConnectConfListener {
} catch (Exception e) {
logger.error("", e);
}
- }, 3, 3, TimeUnit.SECONDS);
+ }, refreshSecs, refreshSecs, TimeUnit.SECONDS);
}
}
diff --git
a/mqtt-cs/src/test/java/org/apache/rocketmq/mqtt/cs/test/TestDefaultChannelManager.java
b/mqtt-cs/src/test/java/org/apache/rocketmq/mqtt/cs/test/TestDefaultChannelManager.java
deleted file mode 100644
index 06dfd51..0000000
---
a/mqtt-cs/src/test/java/org/apache/rocketmq/mqtt/cs/test/TestDefaultChannelManager.java
+++ /dev/null
@@ -1,60 +0,0 @@
-/*
- *
- * * 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.rocketmq.mqtt.cs.test;
-
-import io.netty.channel.socket.nio.NioSocketChannel;
-import io.netty.util.Timeout;
-import org.apache.commons.lang3.reflect.FieldUtils;
-import org.apache.commons.lang3.reflect.MethodUtils;
-import org.apache.rocketmq.mqtt.cs.channel.ChannelInfo;
-import org.apache.rocketmq.mqtt.cs.channel.DefaultChannelManager;
-import org.apache.rocketmq.mqtt.cs.config.ConnectConf;
-import org.apache.rocketmq.mqtt.cs.session.infly.RetryDriver;
-import org.apache.rocketmq.mqtt.cs.session.loop.SessionLoop;
-import org.junit.Assert;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.junit.MockitoJUnitRunner;
-
-import java.lang.reflect.InvocationTargetException;
-
-import static org.mockito.Mockito.*;
-
-public class TestDefaultChannelManager {
-
- @Test
- public void test() throws IllegalAccessException, InterruptedException,
InvocationTargetException, NoSuchMethodException {
- DefaultChannelManager defaultChannelManager = new
DefaultChannelManager();
- SessionLoop sessionLoop = mock(SessionLoop.class);
- FieldUtils.writeDeclaredField(defaultChannelManager, "sessionLoop",
sessionLoop, true);
- FieldUtils.writeDeclaredField(defaultChannelManager, "connectConf",
mock(ConnectConf.class), true);
- FieldUtils.writeDeclaredField(defaultChannelManager, "retryDriver",
mock(RetryDriver.class), true);
- FieldUtils.writeStaticField(DefaultChannelManager.class,
"minBlankChannelSeconds", 1, true);
- defaultChannelManager.init();
- NioSocketChannel channel = spy(new NioSocketChannel());
- when(channel.isActive()).thenReturn(false);
- ChannelInfo.setClientId(channel, "test");
- ChannelInfo.setKeepLive(channel, 0);
- defaultChannelManager.addChannel(channel);
- MethodUtils.invokeMethod(defaultChannelManager, true, "doPing",
mock(Timeout.class), channel);
- verify(sessionLoop).unloadSession(anyString(), anyString());
- }
-
-}
diff --git
a/mqtt-cs/src/test/java/org/apache/rocketmq/mqtt/cs/test/config/TestConnectConfListener.java
b/mqtt-cs/src/test/java/org/apache/rocketmq/mqtt/cs/test/config/TestConnectConfListener.java
new file mode 100644
index 0000000..e212e2f
--- /dev/null
+++
b/mqtt-cs/src/test/java/org/apache/rocketmq/mqtt/cs/test/config/TestConnectConfListener.java
@@ -0,0 +1,82 @@
+/*
+ *
+ * * 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.rocketmq.mqtt.cs.test.config;
+
+import org.apache.commons.lang3.reflect.FieldUtils;
+import org.apache.rocketmq.mqtt.cs.config.ConnectConf;
+import org.apache.rocketmq.mqtt.cs.config.ConnectConfListener;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import java.io.File;
+
+import static org.mockito.Mockito.atLeast;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.when;
+
+@RunWith(MockitoJUnitRunner.class)
+public class TestConnectConfListener {
+
+ private ConnectConfListener confListener;
+ private final long confRefreshSecs = 1;
+ private final long originalModify = 1000L;
+
+ @Mock
+ private ConnectConf connectConf;
+
+ @Mock
+ private File confFile;
+
+ @Before
+ public void Before() throws IllegalAccessException {
+ confListener = new ConnectConfListener();
+ FieldUtils.writeDeclaredField(confListener, "connectConf",
connectConf, true);
+ FieldUtils.writeDeclaredField(confListener, "confFile", confFile,
true);
+ FieldUtils.writeDeclaredField(confListener, "refreshSecs",
confRefreshSecs, true);
+ }
+
+ @After
+ public void After() {}
+
+ @Test
+ public void testStart() {
+ when(connectConf.getConfFile()).thenReturn(confFile);
+ when(confFile.lastModified()).thenReturn(originalModify);
+
+ confListener.start();
+ verify(confFile, times(1)).lastModified();
+ verifyNoMoreInteractions(confFile);
+
+ // wait the next conf refresh check and make conf modified
+ when(confFile.lastModified()).thenReturn(2 * originalModify);
+ try {
+ Thread.sleep(2000 + confRefreshSecs);
+ } catch (InterruptedException ignored) {}
+
+ verify(confFile, atLeast(2)).lastModified();
+ verify(confFile, atLeast(1)).getAbsoluteFile();
+ }
+}
diff --git
a/mqtt-cs/src/test/java/org/apache/rocketmq/mqtt/cs/test/hook/TestUpstreamHookManagerImpl.java
b/mqtt-cs/src/test/java/org/apache/rocketmq/mqtt/cs/test/hook/TestUpstreamHookManagerImpl.java
new file mode 100644
index 0000000..d1db550
--- /dev/null
+++
b/mqtt-cs/src/test/java/org/apache/rocketmq/mqtt/cs/test/hook/TestUpstreamHookManagerImpl.java
@@ -0,0 +1,73 @@
+/*
+ *
+ * * 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.rocketmq.mqtt.cs.test.hook;
+
+import io.netty.handler.codec.mqtt.MqttMessage;
+import org.apache.commons.lang3.reflect.FieldUtils;
+import org.apache.rocketmq.mqtt.common.hook.UpstreamHook;
+import org.apache.rocketmq.mqtt.common.hook.UpstreamHookManager;
+import org.apache.rocketmq.mqtt.common.model.MqttMessageUpContext;
+import org.apache.rocketmq.mqtt.cs.hook.UpstreamHookManagerImpl;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+@RunWith(MockitoJUnitRunner.class)
+public class TestUpstreamHookManagerImpl {
+
+ private UpstreamHookManager upstreamHookManager;
+
+ @Mock
+ private UpstreamHook upstreamHook;
+
+ @Before
+ public void Before() {
+ upstreamHookManager = new UpstreamHookManagerImpl();
+ }
+
+ @After
+ public void After() {}
+
+ @Test(expected = IllegalArgumentException.class )
+ public void testAddHookIllegalArgException() throws IllegalAccessException
{
+ FieldUtils.writeDeclaredField(upstreamHookManager, "isAssembled", new
AtomicBoolean(true), true);
+ upstreamHookManager.addHook(0, upstreamHook);
+ }
+
+ @Test
+ public void testDoUpstreamHook() {
+ upstreamHookManager.addHook(0, upstreamHook);
+ upstreamHookManager.doUpstreamHook(mock(MqttMessageUpContext.class),
mock(MqttMessage.class));
+
+ verify(upstreamHook, times(1)).getNextHook();
+ verify(upstreamHook, times(1)).setNextHook(any());
+ verify(upstreamHook, times(1)).doHook(any(), any());
+ }
+}