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 33cfab91c63ea028a9771a64b73ef362c252b7df..133fa6c97e6d883b0d2070e3a9b893edccb764c1 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 5c99e635fccd7d050f278e814aeb4118d79b60a1..0000000000000000000000000000000000000000 --- 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 ad4a73c7ca871ecf1a9054002fb5c0dc8b430e9f..b158ffd0d653a4f1337dbb64e5a150957a7a35b7 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 ff2b266cb3c8fc8bbf44dde538fef5de1463f052..912b3f6c14148ba011d48bf6650e6ee5cdd94ae5 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 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 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)); } }