Fix tests. Add headless firefox test runner

This commit is contained in:
Alexey Andreev 2021-03-08 12:00:13 +03:00
parent 61db54e848
commit 1eab7e41b1
4 changed files with 73 additions and 18 deletions

View File

@ -154,7 +154,7 @@ public class ArrayDequeTest {
@Test
public void removeFirstShouldNotContainTheFirstAddedObject() {
TArrayDeque<Object> arrayDeque1 = new TArrayDeque<>();
ArrayDeque<Object> arrayDeque1 = new ArrayDeque<>();
Object object1 = new Object();
Object object2 = new Object();
Object object3 = new Object();
@ -166,7 +166,7 @@ public class ArrayDequeTest {
Assert.assertTrue(arrayDeque1.contains(object2));
Assert.assertTrue(arrayDeque1.contains(object3));
TArrayDeque<Object> arrayDeque2 = new TArrayDeque<>();
ArrayDeque<Object> arrayDeque2 = new ArrayDeque<>();
arrayDeque2.add(object1);
arrayDeque2.add(object2);
arrayDeque2.add(object3);
@ -175,7 +175,7 @@ public class ArrayDequeTest {
Assert.assertTrue(arrayDeque2.size() == 1);
Assert.assertTrue(arrayDeque2.contains(object3));
TArrayDeque<Object> arrayDeque3 = new TArrayDeque<>();
ArrayDeque<Object> arrayDeque3 = new ArrayDeque<>();
arrayDeque3.add(object1);
arrayDeque3.add(object2);
arrayDeque3.add(object3);
@ -184,7 +184,7 @@ public class ArrayDequeTest {
Assert.assertTrue(arrayDeque3.size() == 1);
Assert.assertTrue(arrayDeque3.contains(object3));
TArrayDeque<Object> arrayDeque4 = new TArrayDeque<>();
ArrayDeque<Object> arrayDeque4 = new ArrayDeque<>();
arrayDeque4.add(object1);
arrayDeque4.add(object2);
arrayDeque4.add(object3);
@ -196,7 +196,7 @@ public class ArrayDequeTest {
@Test
public void removeLastShouldNotContainTheLastAddedObject() {
TArrayDeque<Object> arrayDeque1 = new TArrayDeque<>();
ArrayDeque<Object> arrayDeque1 = new ArrayDeque<>();
Object object1 = new Object();
Object object2 = new Object();
Object object3 = new Object();
@ -208,7 +208,7 @@ public class ArrayDequeTest {
Assert.assertTrue(arrayDeque1.contains(object1));
Assert.assertTrue(arrayDeque1.contains(object2));
TArrayDeque<Object> arrayDeque2 = new TArrayDeque<>();
ArrayDeque<Object> arrayDeque2 = new ArrayDeque<>();
arrayDeque2.add(object1);
arrayDeque2.add(object2);
arrayDeque2.add(object3);
@ -217,7 +217,7 @@ public class ArrayDequeTest {
Assert.assertTrue(arrayDeque2.size() == 1);
Assert.assertTrue(arrayDeque2.contains(object1));
TArrayDeque<Object> arrayDeque3 = new TArrayDeque<>();
ArrayDeque<Object> arrayDeque3 = new ArrayDeque<>();
arrayDeque3.add(object1);
arrayDeque3.add(object2);
arrayDeque3.add(object3);
@ -226,7 +226,7 @@ public class ArrayDequeTest {
Assert.assertTrue(arrayDeque3.size() == 1);
Assert.assertTrue(arrayDeque3.contains(object1));
TArrayDeque<Object> arrayDeque4 = new TArrayDeque<>();
ArrayDeque<Object> arrayDeque4 = new ArrayDeque<>();
arrayDeque4.add(object1);
arrayDeque4.add(object2);
arrayDeque4.add(object3);
@ -238,7 +238,7 @@ public class ArrayDequeTest {
@Test
public void removeElementInWrappedArray() {
TArrayDeque<Object> arrayDeque = new TArrayDeque<>(8);
ArrayDeque<Object> arrayDeque = new ArrayDeque<>(8);
for (int i = 0; i < 4; ++i) {
arrayDeque.addLast(0);
}

View File

@ -102,7 +102,7 @@ public class ArrayBlockingQueueTest {
long start = System.currentTimeMillis();
queue.put(3);
long end = System.currentTimeMillis();
assertTrue(start + 50 < end && start + 250 > end);
assertTrue("Wait time " + (end - start), start + 50 < end && start + 500 > end);
assertEquals(3, queue.remove().intValue());
}
@ -127,7 +127,7 @@ public class ArrayBlockingQueueTest {
long end = System.currentTimeMillis();
int b = queue.take();
assertTrue(start + 100 < end && start + 500 > end);
assertTrue("Wait time " + (end - start), start + 100 < end && start + 900 > end);
assertEquals(1, a);
assertEquals(2, b);
}

View File

@ -64,7 +64,7 @@ public class BrowserRunStrategy implements TestRunStrategy {
private AtomicInteger idGenerator = new AtomicInteger(0);
private AtomicReference<Session> wsSession = new AtomicReference<>();
private CountDownLatch wsSessionReady = new CountDownLatch(1);
private ConcurrentMap<Integer, TestRun> awaitingRuns = new ConcurrentHashMap<>();
private ConcurrentMap<Integer, TestRunCallback> awaitingRuns = new ConcurrentHashMap<>();
private ObjectMapper objectMapper = new ObjectMapper();
public BrowserRunStrategy(File baseDir, String type, Function<String, Process> browserRunner) {
@ -138,7 +138,20 @@ public class BrowserRunStrategy implements TestRunStrategy {
return;
}
int id = idGenerator.incrementAndGet();
awaitingRuns.put(id, run);
CountDownLatch latch = new CountDownLatch(1);
awaitingRuns.put(id, new TestRunCallback() {
@Override
public void complete() {
latch.countDown();
run.getCallback().complete();
}
@Override
public void error(Throwable e) {
latch.countDown();
run.getCallback().error(e);
}
});
JsonNodeFactory nf = objectMapper.getNodeFactory();
ObjectNode node = nf.objectNode();
@ -160,6 +173,12 @@ public class BrowserRunStrategy implements TestRunStrategy {
String message = node.toString();
ws.getRemote().sendStringByFuture(message);
try {
latch.await();
} catch (InterruptedException e) {
// do nothing
}
}
class TestCodeServlet extends HttpServlet {
@ -292,8 +311,8 @@ public class BrowserRunStrategy implements TestRunStrategy {
public void onWebSocketClose(int statusCode, String reason) {
if (ready.get()) {
System.err.println("Browser has disconnected");
for (TestRun run : awaitingRuns.values()) {
run.getCallback().error(new RuntimeException("Browser disconnected unexpectedly"));
for (TestRunCallback run : awaitingRuns.values()) {
run.error(new RuntimeException("Browser disconnected unexpectedly"));
}
}
}
@ -312,7 +331,7 @@ public class BrowserRunStrategy implements TestRunStrategy {
}
int id = node.get("id").asInt();
TestRun run = awaitingRuns.remove(id);
TestRunCallback run = awaitingRuns.remove(id);
if (run == null) {
System.err.println("Unexpected run id: " + id);
return;
@ -337,9 +356,9 @@ public class BrowserRunStrategy implements TestRunStrategy {
String status = resultNode.get("status").asText();
if (status.equals("OK")) {
run.getCallback().complete();
run.complete();
} else {
run.getCallback().error(new RuntimeException(resultNode.get("errorMessage").asText()));
run.error(new RuntimeException(resultNode.get("errorMessage").asText()));
}
}
}

View File

@ -172,6 +172,9 @@ public class TeaVMTestRunner extends Runner implements Filterable {
case "browser-chrome":
jsRunStrategy = new BrowserRunStrategy(outputDir, "JAVASCRIPT", this::chromeBrowser);
break;
case "browser-firefox":
jsRunStrategy = new BrowserRunStrategy(outputDir, "JAVASCRIPT", this::firefoxBrowser);
break;
case "none":
jsRunStrategy = null;
break;
@ -227,6 +230,39 @@ public class TeaVMTestRunner extends Runner implements Filterable {
}
}
private Process firefoxBrowser(String url) {
File temp;
try {
temp = File.createTempFile("teavm", "teavm");
temp.delete();
temp.mkdirs();
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
deleteDir(temp);
}));
System.out.println("Running firefox with user data dir: " + temp.getAbsolutePath());
ProcessBuilder pb = new ProcessBuilder(
"firefox",
"--headless",
"--profile",
temp.getAbsolutePath(),
url
);
Process process = pb.start();
logStream(process.getInputStream(), "Firefox stdout");
logStream(process.getErrorStream(), "Firefox stderr");
new Thread(() -> {
try {
System.out.println("Firefox process terminated with code: " + process.waitFor());
} catch (InterruptedException e) {
// ignore
}
});
return process;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private void logStream(InputStream stream, String name) {
new Thread(() -> {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream))) {