提交 7ebb3107 编写于 作者: 静夜思朝颜's avatar 静夜思朝颜 提交者: wu-sheng

fix deficient code, "/socket.io/" http segment declare (#3717)

* fix deficient code, "/socket.io/" http segment declare

* add agent test and fix expectedData.yaml file

* add listener on tomcat start, work for reduce socket io server start time
change "/socket.io/" operation "http.method", cause get or post will invoke both

* resolve style check error

* change socket io server and client start to health check servlet

* if startClientAndWaitConnect invoke on multi-thread, it will only check client ref, no check connect state, fix it.
上级 f44b0948
...@@ -104,6 +104,12 @@ pipeline { ...@@ -104,6 +104,12 @@ pipeline {
sh 'bash test/plugin/run.sh --build_id=wl1_${BUILD_ID} spring-async-scenario' sh 'bash test/plugin/run.sh --build_id=wl1_${BUILD_ID} spring-async-scenario'
} }
} }
stage('netty-socketio 1.x (4)') {
steps {
sh 'bash test/plugin/run.sh --build_id=wl1_${BUILD_ID} netty-socketio-scenario'
}
}
} }
} }
stage('Group2') { stage('Group2') {
......
...@@ -29,39 +29,39 @@ segmentItems: ...@@ -29,39 +29,39 @@ segmentItems:
segments: segments:
- segmentId: not null - segmentId: not null
spans: spans:
- operationName: SocketIO/onConnect - operationName: /netty-socketio-scenario/case/netty-socketio
operationId: 0 operationId: 0
parentSpanId: -1 parentSpanId: -1
spanId: 0 spanId: 0
spanLayer: Http spanLayer: Http
startTime: nq 0 startTime: nq 0
endTime: nq 0 endTime: nq 0
componentId: 76 componentId: 1
componentName: '' componentName: ''
isError: false isError: false
spanType: Entry spanType: Entry
peer: '' peer: ''
peerId: 0 peerId: 0
tags: tags:
- {key: from, value: not null} - {key: url, value: 'http://localhost:8080/netty-socketio-scenario/case/netty-socketio'}
- {key: http.method, value: GET}
- segmentId: not null - segmentId: not null
spans: spans:
- operationName: /netty-socketio-scenario/case/netty-socketio - operationName: SocketIO/onConnect
operationId: 0 operationId: 0
parentSpanId: -1 parentSpanId: -1
spanId: 0 spanId: 0
spanLayer: Http spanLayer: Http
startTime: nq 0 startTime: nq 0
endTime: nq 0 endTime: nq 0
componentId: 1 componentId: 76
componentName: '' componentName: ''
isError: false isError: false
spanType: Entry spanType: Entry
peer: '' peer: ''
peerId: 0 peerId: 0
tags: tags:
- {key: url, value: 'http://localhost:8080/netty-socketio-scenario/case/netty-socketio'} - {key: from, value: not null}
- {key: http.method, value: GET}
- segmentId: not null - segmentId: not null
spans: spans:
- operationName: SocketIO/send_data/receive - operationName: SocketIO/send_data/receive
...@@ -77,3 +77,21 @@ segmentItems: ...@@ -77,3 +77,21 @@ segmentItems:
spanType: Entry spanType: Entry
peer: '' peer: ''
peerId: 0 peerId: 0
- segmentId: not null
spans:
- operationName: /socket.io/
operationId: 0
parentSpanId: -1
spanId: 0
spanLayer: Http
startTime: nq 0
endTime: nq 0
componentId: 12
componentName: ''
isError: false
spanType: Exit
peer: not null
peerId: 0
tags:
- {key: http.method, value: not null}
- {key: url, value: not null}
\ No newline at end of file
...@@ -29,17 +29,6 @@ import java.io.PrintWriter; ...@@ -29,17 +29,6 @@ import java.io.PrintWriter;
public class CaseServlet extends HttpServlet { public class CaseServlet extends HttpServlet {
static {
// start socket io server
SocketIOStarter.startServer();
// start client
try {
SocketIOStarter.startClientAndWaitConnect();
} catch (Exception e) {
}
}
@Override @Override
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
......
/*
* 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();
}
}
...@@ -28,6 +28,13 @@ public class HealthCheckServlet extends HttpServlet { ...@@ -28,6 +28,13 @@ 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
SocketIOStarter.startServer();
try {
SocketIOStarter.startClientAndWaitConnect();
} catch (Exception e) {
}
PrintWriter writer = resp.getWriter(); PrintWriter writer = resp.getWriter();
writer.write("Success"); writer.write("Success");
writer.flush(); writer.flush();
......
...@@ -17,17 +17,14 @@ ...@@ -17,17 +17,14 @@
package org.apache.skywalking.apm.testcase.netty.socketio; package org.apache.skywalking.apm.testcase.netty.socketio;
import com.corundumstudio.socketio.AckRequest;
import com.corundumstudio.socketio.Configuration; import com.corundumstudio.socketio.Configuration;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIOServer; import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.listener.ConnectListener;
import com.corundumstudio.socketio.listener.DataListener;
import io.socket.client.IO; import io.socket.client.IO;
import io.socket.client.Socket; import io.socket.client.Socket;
import io.socket.emitter.Emitter; import io.socket.emitter.Emitter;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
...@@ -43,53 +40,42 @@ public class SocketIOStarter { ...@@ -43,53 +40,42 @@ public class SocketIOStarter {
public static SocketIOServer server; public static SocketIOServer server;
public static Socket client; public static Socket client;
private static CountDownLatch connectedCountDownLatch = new CountDownLatch(1);
public static void startServer() { public static void startServer() {
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);
config.setBossThreads(1);
config.setWorkerThreads(1);
server = new SocketIOServer(config); server = new SocketIOServer(config);
server.addEventListener(LISTEN_EVENT_NAME, String.class, new DataListener<String>() {
@Override
public void onData(SocketIOClient client, String data, AckRequest ackRequest) {
// get message
}
});
server.addConnectListener(new ConnectClientListener());
server.start(); server.start();
// close server on kill signal
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
server.stop();
}
});
}
private static class ConnectClientListener implements ConnectListener {
@Override
public void onConnect(SocketIOClient client) {
// connect client
}
} }
public static void startClientAndWaitConnect() throws URISyntaxException, InterruptedException { 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); client = IO.socket("http://localhost:" + SERVER_PORT);
LinkedBlockingQueue<Boolean> connected = new LinkedBlockingQueue<>(1); LinkedBlockingQueue<Boolean> connected = new LinkedBlockingQueue<>(1);
client.on(Socket.EVENT_CONNECT, new Emitter.Listener() { client.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
@Override @Override
public void call(Object... objects) { public void call(Object... objects) {
connected.add(true); connectedCountDownLatch.countDown();
} }
}); });
client.connect(); client.connect();
// wait connect to server // wait connect to server
connected.poll(5, TimeUnit.SECONDS); connectedCountDownLatch.await(5, TimeUnit.SECONDS);
} }
} }
...@@ -41,5 +41,4 @@ ...@@ -41,5 +41,4 @@
</servlet-mapping> </servlet-mapping>
</web-app> </web-app>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册