This is an automated email from the ASF dual-hosted git repository.
wusheng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/skywalking.git
The following commit(s) were added to refs/heads/master by this push:
new 41bb3b5 [hotfix] Make socketio-plugin test case stable (#3861)
41bb3b5 is described below
commit 41bb3b5c0aba9ae4e9d5826bf8fc354bce311f59
Author: Daming <[email protected]>
AuthorDate: Thu Nov 14 22:28:26 2019 +0800
[hotfix] Make socketio-plugin test case stable (#3861)
* make socketio-plugin testcase stable
---
.../apm/testcase/netty/socketio/CaseServlet.java | 38 ++++++++++++++---
.../testcase/netty/socketio/ContextListener.java | 45 ---------------------
.../netty/socketio/HealthCheckServlet.java | 6 +--
.../testcase/netty/socketio/SocketIOStarter.java | 47 +++++++---------------
4 files changed, 49 insertions(+), 87 deletions(-)
diff --git
a/test/plugin/scenarios/netty-socketio-scenario/src/main/java/org/apache/skywalking/apm/testcase/netty/socketio/CaseServlet.java
b/test/plugin/scenarios/netty-socketio-scenario/src/main/java/org/apache/skywalking/apm/testcase/netty/socketio/CaseServlet.java
index 33cfab9..133fa6c 100644
---
a/test/plugin/scenarios/netty-socketio-scenario/src/main/java/org/apache/skywalking/apm/testcase/netty/socketio/CaseServlet.java
+++
b/test/plugin/scenarios/netty-socketio-scenario/src/main/java/org/apache/skywalking/apm/testcase/netty/socketio/CaseServlet.java
@@ -18,7 +18,9 @@
package org.apache.skywalking.apm.testcase.netty.socketio;
-import com.corundumstudio.socketio.SocketIOClient;
+import io.socket.client.IO;
+import io.socket.client.Socket;
+import io.socket.emitter.Emitter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
@@ -26,6 +28,8 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
public class CaseServlet extends HttpServlet {
@@ -33,13 +37,35 @@ public class CaseServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// create socket io client and send data
// test send message interceptor
- SocketIOClient client =
SocketIOStarter.server.getAllClients().iterator().next();
- client.sendEvent(SocketIOStarter.SEND_EVENT_NAME, "data");
+ try {
+ Socket socket = null;
+ try {
+ // client send message to server
+ // test for get message from client interceptor
+ SocketIOStarter.getInstance().sendEvent("data");
- // client send message to server
- // test for get message from client interceptor
- SocketIOStarter.client.emit(SocketIOStarter.LISTEN_EVENT_NAME,
"hello");
+ socket = IO.socket("http://localhost:" +
SocketIOStarter.SERVER_PORT);
+ final CountDownLatch latch = new CountDownLatch(1);
+ socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
+ @Override
+ public void call(Object... objects) {
+ latch.countDown();
+ }
+ });
+ socket.connect();
+ socket.emit(SocketIOStarter.LISTEN_EVENT_NAME, "hello");
+ latch.await(5, TimeUnit.SECONDS);
+ } catch (Exception e) {
+ throw e;
+ } finally {
+ if (socket != null) {
+ socket.disconnect();
+ }
+ }
+ } catch (Exception e) {
+ throw new IOException(e);
+ }
PrintWriter printWriter = resp.getWriter();
printWriter.write("success");
printWriter.flush();
diff --git
a/test/plugin/scenarios/netty-socketio-scenario/src/main/java/org/apache/skywalking/apm/testcase/netty/socketio/ContextListener.java
b/test/plugin/scenarios/netty-socketio-scenario/src/main/java/org/apache/skywalking/apm/testcase/netty/socketio/ContextListener.java
deleted file mode 100644
index 5c99e63..0000000
---
a/test/plugin/scenarios/netty-socketio-scenario/src/main/java/org/apache/skywalking/apm/testcase/netty/socketio/ContextListener.java
+++ /dev/null
@@ -1,45 +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.skywalking.apm.testcase.netty.socketio;
-
-import javax.servlet.ServletContextEvent;
-import javax.servlet.ServletContextListener;
-
-/**
- * @author MrPro
- */
-public class ContextListener implements ServletContextListener {
-
- @Override
- public void contextInitialized(ServletContextEvent servletContextEvent) {
- // start socket io server on tomcat start
- SocketIOStarter.startServer();
-
- // start client
- try {
- SocketIOStarter.startClientAndWaitConnect();
- } catch (Exception e) {
- }
- }
-
- @Override
- public void contextDestroyed(ServletContextEvent servletContextEvent) {
- SocketIOStarter.server.stop();
- }
-}
diff --git
a/test/plugin/scenarios/netty-socketio-scenario/src/main/java/org/apache/skywalking/apm/testcase/netty/socketio/HealthCheckServlet.java
b/test/plugin/scenarios/netty-socketio-scenario/src/main/java/org/apache/skywalking/apm/testcase/netty/socketio/HealthCheckServlet.java
index ad4a73c..b158ffd 100644
---
a/test/plugin/scenarios/netty-socketio-scenario/src/main/java/org/apache/skywalking/apm/testcase/netty/socketio/HealthCheckServlet.java
+++
b/test/plugin/scenarios/netty-socketio-scenario/src/main/java/org/apache/skywalking/apm/testcase/netty/socketio/HealthCheckServlet.java
@@ -29,10 +29,10 @@ public class HealthCheckServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// start socket io server and client on heath check
- SocketIOStarter.startServer();
try {
- SocketIOStarter.startClientAndWaitConnect();
- } catch (Exception e) {
+ SocketIOStarter.getInstance().healthCheck();
+ } catch (InterruptedException e) {
+ throw new IOException(e);
}
PrintWriter writer = resp.getWriter();
diff --git
a/test/plugin/scenarios/netty-socketio-scenario/src/main/java/org/apache/skywalking/apm/testcase/netty/socketio/SocketIOStarter.java
b/test/plugin/scenarios/netty-socketio-scenario/src/main/java/org/apache/skywalking/apm/testcase/netty/socketio/SocketIOStarter.java
index ff2b266..912b3f6 100644
---
a/test/plugin/scenarios/netty-socketio-scenario/src/main/java/org/apache/skywalking/apm/testcase/netty/socketio/SocketIOStarter.java
+++
b/test/plugin/scenarios/netty-socketio-scenario/src/main/java/org/apache/skywalking/apm/testcase/netty/socketio/SocketIOStarter.java
@@ -19,13 +19,8 @@ package org.apache.skywalking.apm.testcase.netty.socketio;
import com.corundumstudio.socketio.Configuration;
import com.corundumstudio.socketio.SocketIOServer;
-import io.socket.client.IO;
-import io.socket.client.Socket;
-import io.socket.emitter.Emitter;
+import io.netty.util.concurrent.Future;
-import java.net.URISyntaxException;
-import java.util.concurrent.CountDownLatch;
-import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
/**
@@ -37,15 +32,15 @@ public class SocketIOStarter {
public static final String LISTEN_EVENT_NAME = "send_data";
public static final String SEND_EVENT_NAME = "get_data";
- public static SocketIOServer server;
- public static Socket client;
+ private SocketIOServer server;
+ private Future<Void> startFuture;
+ private static final SocketIOStarter INSTANCE = new SocketIOStarter();
- private static CountDownLatch connectedCountDownLatch = new
CountDownLatch(1);
+ public static final SocketIOStarter getInstance() {
+ return INSTANCE;
+ }
- public static void startServer() {
- if (server != null) {
- return;
- }
+ public SocketIOStarter() {
Configuration config = new Configuration();
config.setHostname("localhost");
config.setPort(SERVER_PORT);
@@ -53,29 +48,15 @@ public class SocketIOStarter {
config.setWorkerThreads(1);
server = new SocketIOServer(config);
-
- server.start();
+ startFuture = server.startAsync();
}
- public static void startClientAndWaitConnect() throws URISyntaxException,
InterruptedException {
- if (client != null) {
- // check client is connected again
- // if this method invoke on multi thread, client will return but
not connected
- connectedCountDownLatch.await(5, TimeUnit.SECONDS);
- return;
- }
- client = IO.socket("http://localhost:" + SERVER_PORT);
- LinkedBlockingQueue<Boolean> connected = new LinkedBlockingQueue<>(1);
- client.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
- @Override
- public void call(Object... objects) {
- connectedCountDownLatch.countDown();
- }
- });
- client.connect();
+ public boolean healthCheck() throws InterruptedException {
+ return startFuture.await(1L, TimeUnit.SECONDS);
+ }
- // wait connect to server
- connectedCountDownLatch.await(5, TimeUnit.SECONDS);
+ public void sendEvent(String message) {
+ server.getAllClients().forEach(e -> e.sendEvent(SEND_EVENT_NAME,
message));
}
}