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

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

* make socketio-plugin testcase stable
上级 db0b7486
......@@ -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();
......
/*
* 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 {
@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();
......
......@@ -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));
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册