提交 41bb3b5c 编写于 作者: D Daming 提交者: wu-sheng

[hotfix] Make socketio-plugin test case stable (#3861)

* make socketio-plugin testcase stable
上级 db0b7486
...@@ -18,7 +18,9 @@ ...@@ -18,7 +18,9 @@
package org.apache.skywalking.apm.testcase.netty.socketio; 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.ServletException;
import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServlet;
...@@ -26,6 +28,8 @@ import javax.servlet.http.HttpServletRequest; ...@@ -26,6 +28,8 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
public class CaseServlet extends HttpServlet { public class CaseServlet extends HttpServlet {
...@@ -33,13 +37,35 @@ public class CaseServlet extends HttpServlet { ...@@ -33,13 +37,35 @@ public class CaseServlet extends HttpServlet {
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// create socket io client and send data // create socket io client and send data
// test send message interceptor // test send message interceptor
SocketIOClient client = SocketIOStarter.server.getAllClients().iterator().next(); try {
client.sendEvent(SocketIOStarter.SEND_EVENT_NAME, "data"); 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 socket = IO.socket("http://localhost:" + SocketIOStarter.SERVER_PORT);
// test for get message from client interceptor final CountDownLatch latch = new CountDownLatch(1);
SocketIOStarter.client.emit(SocketIOStarter.LISTEN_EVENT_NAME, "hello"); 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 printWriter = resp.getWriter();
printWriter.write("success"); printWriter.write("success");
printWriter.flush(); printWriter.flush();
......
/*
* 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();
}
}
...@@ -29,10 +29,10 @@ public class HealthCheckServlet extends HttpServlet { ...@@ -29,10 +29,10 @@ public class HealthCheckServlet extends HttpServlet {
@Override @Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// start socket io server and client on heath check // start socket io server and client on heath check
SocketIOStarter.startServer();
try { try {
SocketIOStarter.startClientAndWaitConnect(); SocketIOStarter.getInstance().healthCheck();
} catch (Exception e) { } catch (InterruptedException e) {
throw new IOException(e);
} }
PrintWriter writer = resp.getWriter(); PrintWriter writer = resp.getWriter();
......
...@@ -19,13 +19,8 @@ package org.apache.skywalking.apm.testcase.netty.socketio; ...@@ -19,13 +19,8 @@ package org.apache.skywalking.apm.testcase.netty.socketio;
import com.corundumstudio.socketio.Configuration; import com.corundumstudio.socketio.Configuration;
import com.corundumstudio.socketio.SocketIOServer; import com.corundumstudio.socketio.SocketIOServer;
import io.socket.client.IO; import io.netty.util.concurrent.Future;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;
import java.net.URISyntaxException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
/** /**
...@@ -37,15 +32,15 @@ public class SocketIOStarter { ...@@ -37,15 +32,15 @@ public class SocketIOStarter {
public static final String LISTEN_EVENT_NAME = "send_data"; public static final String LISTEN_EVENT_NAME = "send_data";
public static final String SEND_EVENT_NAME = "get_data"; public static final String SEND_EVENT_NAME = "get_data";
public static SocketIOServer server; private SocketIOServer server;
public static Socket client; 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() { public SocketIOStarter() {
if (server != null) {
return;
}
Configuration config = new Configuration(); Configuration config = new Configuration();
config.setHostname("localhost"); config.setHostname("localhost");
config.setPort(SERVER_PORT); config.setPort(SERVER_PORT);
...@@ -53,29 +48,15 @@ public class SocketIOStarter { ...@@ -53,29 +48,15 @@ public class SocketIOStarter {
config.setWorkerThreads(1); config.setWorkerThreads(1);
server = new SocketIOServer(config); server = new SocketIOServer(config);
startFuture = server.startAsync();
server.start();
} }
public static void startClientAndWaitConnect() throws URISyntaxException, InterruptedException { public boolean healthCheck() throws InterruptedException {
if (client != null) { return startFuture.await(1L, TimeUnit.SECONDS);
// 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();
// wait connect to server public void sendEvent(String message) {
connectedCountDownLatch.await(5, TimeUnit.SECONDS); server.getAllClients().forEach(e -> e.sendEvent(SEND_EVENT_NAME, message));
} }
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册