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

View File

@ -102,7 +102,7 @@ public class ArrayBlockingQueueTest {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
queue.put(3); queue.put(3);
long end = System.currentTimeMillis(); 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()); assertEquals(3, queue.remove().intValue());
} }
@ -127,7 +127,7 @@ public class ArrayBlockingQueueTest {
long end = System.currentTimeMillis(); long end = System.currentTimeMillis();
int b = queue.take(); 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(1, a);
assertEquals(2, b); assertEquals(2, b);
} }

View File

@ -64,7 +64,7 @@ public class BrowserRunStrategy implements TestRunStrategy {
private AtomicInteger idGenerator = new AtomicInteger(0); private AtomicInteger idGenerator = new AtomicInteger(0);
private AtomicReference<Session> wsSession = new AtomicReference<>(); private AtomicReference<Session> wsSession = new AtomicReference<>();
private CountDownLatch wsSessionReady = new CountDownLatch(1); 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(); private ObjectMapper objectMapper = new ObjectMapper();
public BrowserRunStrategy(File baseDir, String type, Function<String, Process> browserRunner) { public BrowserRunStrategy(File baseDir, String type, Function<String, Process> browserRunner) {
@ -138,7 +138,20 @@ public class BrowserRunStrategy implements TestRunStrategy {
return; return;
} }
int id = idGenerator.incrementAndGet(); 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(); JsonNodeFactory nf = objectMapper.getNodeFactory();
ObjectNode node = nf.objectNode(); ObjectNode node = nf.objectNode();
@ -160,6 +173,12 @@ public class BrowserRunStrategy implements TestRunStrategy {
String message = node.toString(); String message = node.toString();
ws.getRemote().sendStringByFuture(message); ws.getRemote().sendStringByFuture(message);
try {
latch.await();
} catch (InterruptedException e) {
// do nothing
}
} }
class TestCodeServlet extends HttpServlet { class TestCodeServlet extends HttpServlet {
@ -292,8 +311,8 @@ public class BrowserRunStrategy implements TestRunStrategy {
public void onWebSocketClose(int statusCode, String reason) { public void onWebSocketClose(int statusCode, String reason) {
if (ready.get()) { if (ready.get()) {
System.err.println("Browser has disconnected"); System.err.println("Browser has disconnected");
for (TestRun run : awaitingRuns.values()) { for (TestRunCallback run : awaitingRuns.values()) {
run.getCallback().error(new RuntimeException("Browser disconnected unexpectedly")); run.error(new RuntimeException("Browser disconnected unexpectedly"));
} }
} }
} }
@ -312,7 +331,7 @@ public class BrowserRunStrategy implements TestRunStrategy {
} }
int id = node.get("id").asInt(); int id = node.get("id").asInt();
TestRun run = awaitingRuns.remove(id); TestRunCallback run = awaitingRuns.remove(id);
if (run == null) { if (run == null) {
System.err.println("Unexpected run id: " + id); System.err.println("Unexpected run id: " + id);
return; return;
@ -337,9 +356,9 @@ public class BrowserRunStrategy implements TestRunStrategy {
String status = resultNode.get("status").asText(); String status = resultNode.get("status").asText();
if (status.equals("OK")) { if (status.equals("OK")) {
run.getCallback().complete(); run.complete();
} else { } 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": case "browser-chrome":
jsRunStrategy = new BrowserRunStrategy(outputDir, "JAVASCRIPT", this::chromeBrowser); jsRunStrategy = new BrowserRunStrategy(outputDir, "JAVASCRIPT", this::chromeBrowser);
break; break;
case "browser-firefox":
jsRunStrategy = new BrowserRunStrategy(outputDir, "JAVASCRIPT", this::firefoxBrowser);
break;
case "none": case "none":
jsRunStrategy = null; jsRunStrategy = null;
break; 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) { private void logStream(InputStream stream, String name) {
new Thread(() -> { new Thread(() -> {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream))) { try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream))) {