/
TestTcpSession.java
192 lines (172 loc) · 6.28 KB
/
TestTcpSession.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*******************************************************************************
* Copyright (c) 2009, 2019 Xored Software Inc and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Xored Software Inc - initial API and implementation and/or initial documentation
*******************************************************************************/
package org.eclipse.rcptt.ecl.internal.client.tcp.tests;
import com.google.common.base.Function;
import com.google.common.io.Closer;
import static org.junit.Assert.assertTrue;
import java.io.Closeable;
import java.io.IOException;
import java.net.InetAddress;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.rcptt.ecl.client.tcp.EclTcpClientManager;
import org.eclipse.rcptt.ecl.client.tcp.tests.EclInjectedCommandService;
import org.eclipse.rcptt.ecl.core.Command;
import org.eclipse.rcptt.ecl.platform.commands.CommandsFactory;
import org.eclipse.rcptt.ecl.platform.commands.ListPlugins;
import org.eclipse.rcptt.ecl.runtime.IPipe;
import org.eclipse.rcptt.ecl.runtime.IProcess;
import org.eclipse.rcptt.ecl.runtime.ISession;
import org.eclipse.rcptt.ecl.server.tcp.EclTcpServerManager;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import junit.framework.TestCase;
public class TestTcpSession {
private static final int PORT = EclTcpClientManager.DEFAULT_PORT + 1;
private static final String LOCALHOST = "127.0.0.1";
private final Closer closer = Closer.create();
private ISession session;
@Before
public void before() throws IOException {
EclTcpServerManager.Instance.startServer(PORT);
closer.register(new Closeable() {
@Override
public void close() throws IOException {
EclTcpServerManager.Instance.stopServer(PORT);
}
});
session = EclTcpClientManager.Instance.startClientSession(InetAddress.getByName(LOCALHOST), PORT);
closer.register(new Closeable() {
@Override
public void close() throws IOException {
try {
session.close();
} catch (CoreException e) {
throw new IOException(e);
}
}
});
EclInjectedCommandService.inject(ignored -> Status.OK_STATUS);
}
@After
public void closeServer() throws CoreException, IOException {
closer.close();
}
@Test
public void simpleExecution() throws CoreException, InterruptedException {
EclInjectedCommandService.inject(ignored -> new Status(IStatus.INFO, "id", "message"));
IStatus status = executeCommand();
Assert.assertFalse(status.getMessage(), status.matches(IStatus.ERROR | IStatus.WARNING | IStatus.CANCEL));
Assert.assertTrue(status.matches(IStatus.INFO));
Assert.assertEquals("message", status.getMessage());
}
private IStatus executeCommand() throws CoreException, InterruptedException {
IProcess process = session
.execute(org.eclipse.rcptt.ecl.client.tcp.tests.TestsFactory.eINSTANCE.createEclInjectedCommand());
IStatus status = process.waitFor();
return status;
}
@Test
public void errorIsReported() throws CoreException, InterruptedException {
EclInjectedCommandService.inject(ignored -> new Status(IStatus.ERROR, "id", "message"));
IStatus status = executeCommand();
Assert.assertTrue(status.matches(IStatus.ERROR));
Assert.assertEquals("message", status.getMessage());
}
private static void absurdFunctionThatThrows() {
IStatus status = new Status(IStatus.ERROR, "id", "message2", new IllegalStateException("message3"));
CoreException coreException = new CoreException(status);
throw new RuntimeException(coreException);
}
@Test
public void istatusPropagatesTraces() throws CoreException, InterruptedException {
EclInjectedCommandService.inject( ignored -> {
absurdFunctionThatThrows();
return Status.OK_STATUS;
});
IStatus status = executeCommand();
Throwable e = status.getException();
while (! (e instanceof CoreException)) {
if (e == null)
Assert.fail("CoreException is not found");
e = e.getCause();
}
CoreException coreException = (CoreException) e;
IllegalStateException illegalStateException = (IllegalStateException) coreException.getCause();
Assert.assertEquals("message3", illegalStateException.getMessage());
boolean found = false;
for (StackTraceElement element: illegalStateException.getStackTrace()) {
if (element.getMethodName().endsWith("absurdFunctionThatThrows")) {
found = true;
break;
}
}
Assert.assertTrue(found);
}
@Test
public void testCommandExecution001() throws Throwable {
ListPlugins listPlugins = CommandsFactory.eINSTANCE.createListPlugins();
IProcess process = session.execute(listPlugins);
IPipe input = process.getOutput();
int count1 = 0;
int count2 = 0;
while (true) {
Object object = input.take(Long.MAX_VALUE);
if (object == null || object instanceof IStatus) {
break;
}
count1++;
}
IProcess process2 = session.execute(listPlugins);
IPipe input2 = process2.getOutput();
while (true) {
Object object = input2.take(Long.MAX_VALUE);
if (object == null || object instanceof IStatus) {
break;
}
count2++;
}
TestCase.assertEquals(count1, count2);
}
@Test(timeout=10000)
public void processDiesIfSessionIsClosed() throws CoreException, InterruptedException {
AtomicBoolean isAlive = new AtomicBoolean();
CountDownLatch start = new CountDownLatch(1);
CountDownLatch stop = new CountDownLatch(1);
EclInjectedCommandService.inject((ignored, process) -> {
do {
isAlive.set(process.isAlive());
start.countDown();
} while (isAlive.get());
stop.countDown();
return Status.CANCEL_STATUS;
});
CompletableFuture.runAsync(() -> {
try {
executeCommand();
} catch (CoreException | InterruptedException e) {
throw new AssertionError(e);
}
});
assertTrue(start.await(1, TimeUnit.SECONDS));
assertTrue(isAlive.get());
session.close();
assertTrue(stop.await(1, TimeUnit.SECONDS));
Assert.assertFalse(isAlive.get());
}
}