Browse Source

Merge pull request #1351 in DESIGN/design from ~MAKSIM/design:bugfix/10.0 to bugfix/10.0

* commit '10c82630ee87b0b64a708a15b65e63a0be56f8cd':
  增加单元测试
  创建socket增加失败重试
bugfix/10.0
Maksim 5 years ago
parent
commit
63a005241f
  1. 167
      designer-realize/src/main/java/com/fr/design/mainframe/socketio/DesignerSocketIO.java
  2. 60
      designer-realize/src/test/java/com/fr/design/mainframe/socketio/DesignerSocketIOTest.java

167
designer-realize/src/main/java/com/fr/design/mainframe/socketio/DesignerSocketIO.java

@ -13,7 +13,6 @@ import com.fr.report.RemoteDesignConstants;
import com.fr.serialization.SerializerHelper;
import com.fr.stable.ArrayUtils;
import com.fr.third.apache.log4j.spi.LoggingEvent;
import com.fr.third.guava.base.Optional;
import com.fr.workspace.WorkContext;
import com.fr.workspace.Workspace;
import com.fr.workspace.base.WorkspaceConstants;
@ -36,28 +35,19 @@ public class DesignerSocketIO {
Disconnecting
}
private static Optional<Socket> socketIO = Optional.absent();
private static Socket socket = null;
private static Status status = Status.Disconnected;
//维护一个当前工作环境的uri列表
private static String[] uri;
//维护一个关于uri列表的计数器
private static int count;
private static final Emitter.Listener printLog = new Emitter.Listener() {
@Override
public void call(Object... objects) {
if (ArrayUtils.isNotEmpty(objects)) {
try {
LoggingEvent event = SerializerHelper.deserialize((byte[]) objects[0]);
DesignerLogger.log(event);
} catch (Exception e) {
FineLoggerFactory.getLogger().error(e.getMessage(), e);
}
}
}
};
public static void close() {
if (socketIO.isPresent()) {
if (socket != null) {
status = Status.Disconnecting;
socketIO.get().close();
socketIO = Optional.absent();
socket.close();
socket = null;
}
}
@ -66,57 +56,42 @@ public class DesignerSocketIO {
if (current.isLocal()) {
return;
}
//每当更换工作环境,更新uri列表,同时更新计数器count
try {
String[] uri = getSocketUri(current);
socketIO = Optional.of(IO.socket(new URI(uri[0])));
socketIO.get().on(WorkspaceConstants.WS_LOGRECORD, printLog);
socketIO.get().on(WorkspaceConstants.CONFIG_MODIFY, new Emitter.Listener() {
@Override
public void call(Object... objects) {
assert objects != null && objects.length == 1;
String param = (String) objects[0];
EventDispatcher.fire(RemoteConfigEvent.EDIT, param);
}
});
socketIO.get().on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {
@Override
public void call(Object... objects) {
/*
* todo 远程心跳断开不一定 socketio 断开 和远程紧密相关的业务都绑定在心跳上切换成心跳断开之后进行提醒
* socketio 只用推日志和通知配置变更
*/
if (status != Status.Disconnecting) {
try {
UIUtil.invokeAndWaitIfNeeded(new Runnable() {
@Override
public void run() {
JOptionPane.showMessageDialog(
DesignerContext.getDesignerFrame(),
Toolkit.i18nText("Fine-Design_Basic_Remote_Disconnected"),
UIManager.getString("OptionPane.messageDialogTitle"),
JOptionPane.ERROR_MESSAGE,
UIManager.getIcon("OptionPane.errorIcon"));
EnvChangeEntrance.getInstance().chooseEnv();
}
});
} catch (Exception e) {
FineLoggerFactory.getLogger().error(e.getMessage(), e);
}
}
status = Status.Disconnected;
}
});
socketIO.get().connect();
status = Status.Connected;
uri = getSocketUri();
} catch (IOException e) {
FineLoggerFactory.getLogger().error(e.getMessage(), e);
}
count = 0;
//建立socket并注册监听
createSocket();
}
private static void createSocket(){
//根据uri和计数器建立连接,并注册监听
try {
if(count<uri.length) {
socket = IO.socket(new URI(uri[count]));
socket.on(WorkspaceConstants.WS_LOGRECORD, printLog);
socket.on(WorkspaceConstants.CONFIG_MODIFY, modifyConfig);
socket.on(Socket.EVENT_CONNECT_ERROR, failRetry);
socket.on(Socket.EVENT_DISCONNECT, disConnectHint);
socket.connect();
status = Status.Connected;
}else {
//表示所有的uri都连接不成功
FineLoggerFactory.getLogger().warn("All uris failed to connect");
}
} catch (Exception e) {
FineLoggerFactory.getLogger().error(e.getMessage(), e);
}
}
private static String[] getSocketUri(Workspace current) throws IOException {
private static String[] getSocketUri() throws IOException {
Workspace current = WorkContext.getCurrent();
URL url = new URL(current.getPath());
Integer[] ports = WorkContext.getCurrent().get(SocketInfoOperator.class).getPort();
WorkspaceConnection connection = WorkContext.getCurrent().getConnection();
Integer[] ports = current.get(SocketInfoOperator.class).getPort();
WorkspaceConnection connection = current.getConnection();
String[] result = new String[ports.length];
for (int i = 0; i < ports.length; i++) {
result[i] = String.format("%s://%s:%s%s?%s=%s&%s=%s",
@ -130,6 +105,72 @@ public class DesignerSocketIO {
connection.getId());
}
return result;
}
//失败重试监听器:1、关闭失败的socket 2、计数器加1 3、调用创建socket方法
private static final Emitter.Listener failRetry = new Emitter.Listener() {
@Override
public void call(Object... args) {
status = Status.Disconnecting;
socket.close();
count++;
createSocket();
}
};
//日志输出监听器
private static final Emitter.Listener printLog = new Emitter.Listener() {
@Override
public void call(Object... objects) {
if (ArrayUtils.isNotEmpty(objects)) {
try {
LoggingEvent event = SerializerHelper.deserialize((byte[]) objects[0]);
DesignerLogger.log(event);
} catch (Exception e) {
FineLoggerFactory.getLogger().error(e.getMessage(), e);
}
}
}
};
//断开连接提醒监听器
private static final Emitter.Listener disConnectHint = new Emitter.Listener() {
@Override
public void call(Object... objects) {
/*
* todo 远程心跳断开不一定 socket 断开 和远程紧密相关的业务都绑定在心跳上切换成心跳断开之后进行提醒
* socket 只用推日志和通知配置变更
*/
if (status != Status.Disconnecting) {
try {
UIUtil.invokeAndWaitIfNeeded(new Runnable() {
@Override
public void run() {
JOptionPane.showMessageDialog(
DesignerContext.getDesignerFrame(),
Toolkit.i18nText("Fine-Design_Basic_Remote_Disconnected"),
UIManager.getString("OptionPane.messageDialogTitle"),
JOptionPane.ERROR_MESSAGE,
UIManager.getIcon("OptionPane.errorIcon"));
EnvChangeEntrance.getInstance().chooseEnv();
}
});
} catch (Exception e) {
FineLoggerFactory.getLogger().error(e.getMessage(), e);
}
}
status = Status.Disconnected;
}
};
//配置变更监听器
private static final Emitter.Listener modifyConfig = new Emitter.Listener() {
@Override
public void call(Object... objects) {
assert objects != null && objects.length == 1;
String param = (String) objects[0];
EventDispatcher.fire(RemoteConfigEvent.EDIT, param);
}
};
}

60
designer-realize/src/test/java/com/fr/design/mainframe/socketio/DesignerSocketIOTest.java

@ -0,0 +1,60 @@
package com.fr.design.mainframe.socketio;
import com.fr.invoke.Reflect;
import com.fr.workspace.WorkContext;
import com.fr.workspace.Workspace;
import io.socket.client.IO;
import io.socket.client.Socket;
import org.easymock.EasyMock;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
/**
* @author: Maksim
* @Date: Created in 2019/12/9
* @Description:
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({WorkContext.class,DesignerSocketIO.class, IO.class})
public class DesignerSocketIOTest {
@Test
public void close() throws Exception {
this.update();
DesignerSocketIO.close();
DesignerSocketIO.Status status = Reflect.on(DesignerSocketIO.class).field("status").get();
Socket socket = Reflect.on(DesignerSocketIO.class).field("socket").get();
Assert.assertEquals(DesignerSocketIO.Status.Disconnecting,status);
Assert.assertNull(socket);
}
@Test
public void update() throws Exception {
Workspace current = EasyMock.mock(Workspace.class);
EasyMock.expect(current.isLocal()).andReturn(false);
PowerMock.mockStatic(WorkContext.class);
EasyMock.expect(WorkContext.getCurrent()).andReturn(current);
String[] uri = {"http://127.0.0.1:8888/workspace","http://127.0.0.1:9999/workspace"};
PowerMock.mockStaticPartial(DesignerSocketIO.class,"getSocketUri");
PowerMock.expectPrivate(DesignerSocketIO.class,"getSocketUri").andReturn(uri);
EasyMock.replay(current);
PowerMock.replayAll();
DesignerSocketIO.update();
DesignerSocketIO.Status status = Reflect.on(DesignerSocketIO.class).field("status").get();
Socket socket = Reflect.on(DesignerSocketIO.class).field("socket").get();
Assert.assertEquals(DesignerSocketIO.Status.Connected,status);
Assert.assertNotNull(socket);
}
}
Loading…
Cancel
Save