From 1eab7e41b1348b22e092f9c161f31d66c069a053 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Mon, 8 Mar 2021 12:00:13 +0300 Subject: [PATCH] Fix tests. Add headless firefox test runner --- .../classlib/java/util/ArrayDequeTest.java | 18 +++++----- .../concurrent/ArrayBlockingQueueTest.java | 4 +-- .../org/teavm/junit/BrowserRunStrategy.java | 33 +++++++++++++---- .../java/org/teavm/junit/TeaVMTestRunner.java | 36 +++++++++++++++++++ 4 files changed, 73 insertions(+), 18 deletions(-) diff --git a/tests/src/test/java/org/teavm/classlib/java/util/ArrayDequeTest.java b/tests/src/test/java/org/teavm/classlib/java/util/ArrayDequeTest.java index 26a9f732f..55b307ca4 100644 --- a/tests/src/test/java/org/teavm/classlib/java/util/ArrayDequeTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/util/ArrayDequeTest.java @@ -154,7 +154,7 @@ public class ArrayDequeTest { @Test public void removeFirstShouldNotContainTheFirstAddedObject() { - TArrayDeque arrayDeque1 = new TArrayDeque<>(); + ArrayDeque 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 arrayDeque2 = new TArrayDeque<>(); + ArrayDeque 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 arrayDeque3 = new TArrayDeque<>(); + ArrayDeque 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 arrayDeque4 = new TArrayDeque<>(); + ArrayDeque arrayDeque4 = new ArrayDeque<>(); arrayDeque4.add(object1); arrayDeque4.add(object2); arrayDeque4.add(object3); @@ -196,7 +196,7 @@ public class ArrayDequeTest { @Test public void removeLastShouldNotContainTheLastAddedObject() { - TArrayDeque arrayDeque1 = new TArrayDeque<>(); + ArrayDeque 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 arrayDeque2 = new TArrayDeque<>(); + ArrayDeque 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 arrayDeque3 = new TArrayDeque<>(); + ArrayDeque 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 arrayDeque4 = new TArrayDeque<>(); + ArrayDeque arrayDeque4 = new ArrayDeque<>(); arrayDeque4.add(object1); arrayDeque4.add(object2); arrayDeque4.add(object3); @@ -238,7 +238,7 @@ public class ArrayDequeTest { @Test public void removeElementInWrappedArray() { - TArrayDeque arrayDeque = new TArrayDeque<>(8); + ArrayDeque arrayDeque = new ArrayDeque<>(8); for (int i = 0; i < 4; ++i) { arrayDeque.addLast(0); } diff --git a/tests/src/test/java/org/teavm/classlib/java/util/concurrent/ArrayBlockingQueueTest.java b/tests/src/test/java/org/teavm/classlib/java/util/concurrent/ArrayBlockingQueueTest.java index e7eef8729..2e5d14611 100644 --- a/tests/src/test/java/org/teavm/classlib/java/util/concurrent/ArrayBlockingQueueTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/util/concurrent/ArrayBlockingQueueTest.java @@ -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); } diff --git a/tools/junit/src/main/java/org/teavm/junit/BrowserRunStrategy.java b/tools/junit/src/main/java/org/teavm/junit/BrowserRunStrategy.java index 660f8a63d..75799b121 100644 --- a/tools/junit/src/main/java/org/teavm/junit/BrowserRunStrategy.java +++ b/tools/junit/src/main/java/org/teavm/junit/BrowserRunStrategy.java @@ -64,7 +64,7 @@ public class BrowserRunStrategy implements TestRunStrategy { private AtomicInteger idGenerator = new AtomicInteger(0); private AtomicReference wsSession = new AtomicReference<>(); private CountDownLatch wsSessionReady = new CountDownLatch(1); - private ConcurrentMap awaitingRuns = new ConcurrentHashMap<>(); + private ConcurrentMap awaitingRuns = new ConcurrentHashMap<>(); private ObjectMapper objectMapper = new ObjectMapper(); public BrowserRunStrategy(File baseDir, String type, Function 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())); } } } diff --git a/tools/junit/src/main/java/org/teavm/junit/TeaVMTestRunner.java b/tools/junit/src/main/java/org/teavm/junit/TeaVMTestRunner.java index 0485c86f1..9a4e718f4 100644 --- a/tools/junit/src/main/java/org/teavm/junit/TeaVMTestRunner.java +++ b/tools/junit/src/main/java/org/teavm/junit/TeaVMTestRunner.java @@ -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))) {