From 891f746802c1949ebcfa01e839f52a2279af9d6f Mon Sep 17 00:00:00 2001 From: Brendan Dahl Date: Tue, 25 Aug 2015 15:20:59 -0700 Subject: [PATCH] Use a more predicable isolate test with assertions. --- tests/automation.js | 42 ++------------- tests/gnu/testlet/DumpTestHarness.java | 25 +++++++++ tests/gnu/testlet/TestHarness.java | 2 + tests/isolate/IsolatedClass.java | 15 ++++-- tests/isolate/TestIsolate.java | 73 +++++++++----------------- 5 files changed, 68 insertions(+), 89 deletions(-) create mode 100644 tests/gnu/testlet/DumpTestHarness.java diff --git a/tests/automation.js b/tests/automation.js index dc31f431..1122cbd9 100644 --- a/tests/automation.js +++ b/tests/automation.js @@ -240,44 +240,10 @@ casper.test.begin("unit tests", 39 + gfxTests.length, function(test) { .thenOpen("http://localhost:8000/index.html?main=tests/isolate/TestIsolate&logLevel=info&logConsole=web,page,raw") .withFrame(0, function() { casper.waitForText("DONE", function() { - var output = this.fetchText('#raw-console'); - var expectedOutput = [ - "I 0: m", - "I 2 0 a ma", - "I 1: ma", - "I 2: 2", - "I 3: 1 isolate", - "I 4: Isolate ID correct", - "I 5: 4", - "I 6: 5", - "I 7: 1 isolate", - "I 8: ma", - "I 9: ma", - "I 10: 3 isolates", - "I 5 0 2 m2", - "I 4 0 1 m1", - "I 11: ma", - "I 12: 1 isolate", - "I 13: Isolates terminated", - "I 2 1 r mar", - "I 14: mar", - "I 2 2 c marc", - "I 15: marc", - "I 16: Main isolate still running", - "I DONE", - "", - ]; - output = output.split("\n").sort(); - expectedOutput.sort(); - test.assert(expectedOutput.length === output.length, "Same number of lines output."); - var allMatch = true; - for (var i = 0; i < expectedOutput.length; i++) { - if (expectedOutput[i] !== output[i]) { - allMatch = false; - break; - } - } - test.assert(allMatch, "All lines are contained within output."); + test.assertTextDoesntExist("FAIL"); + // Two sanity checks to make sure the two isolates ran. + test.assertTextExists("PASS First isolate static value is correct."); + test.assertTextExists("PASS Second isolate static value is correct."); }); }); diff --git a/tests/gnu/testlet/DumpTestHarness.java b/tests/gnu/testlet/DumpTestHarness.java new file mode 100644 index 00000000..affd4c33 --- /dev/null +++ b/tests/gnu/testlet/DumpTestHarness.java @@ -0,0 +1,25 @@ +package gnu.testlet; +import gnu.testlet.TestHarness; + +public class DumpTestHarness extends TestHarness { + public int pass = 0; + public int fail = 0; + private String note = ""; + + public void check(boolean ok) { + if (ok) { + pass++; + System.out.println("PASS " + note); + } else { + fail++; + System.out.println("FAIL " + note); + } + note = ""; + } + + public void todo(boolean ok) { throw new UnsupportedOperationException(); } + public void debug(String msg) { } + public void setNote(String note) { + this.note = note; + } +} diff --git a/tests/gnu/testlet/TestHarness.java b/tests/gnu/testlet/TestHarness.java index d4224e43..0636ef06 100644 --- a/tests/gnu/testlet/TestHarness.java +++ b/tests/gnu/testlet/TestHarness.java @@ -204,6 +204,8 @@ public abstract class TestHarness { public TestHarness(Display d) { display = d; } + public TestHarness() { + } public void setScreenAndWait(Displayable s) { display.setCurrent(s); diff --git a/tests/isolate/IsolatedClass.java b/tests/isolate/IsolatedClass.java index c45e0eac..5c2d57cb 100644 --- a/tests/isolate/IsolatedClass.java +++ b/tests/isolate/IsolatedClass.java @@ -1,13 +1,22 @@ package tests.isolate; import com.sun.cldc.isolate.*; +import gnu.testlet.DumpTestHarness; public class IsolatedClass { - static public String val = "m"; - static public int testRun = 0; + static public String val = "a"; public static void main(String args[]) { + DumpTestHarness th = new DumpTestHarness(); val += args[0]; - System.out.println(Isolate.currentIsolate().id() + " " + (testRun++) + " " + args[0] + " " + val); + if (args.length > 1) { + if (args[0].equals("1")) { + th.check(val, "a1", "First isolate static value is correct."); + } else if (args[0].equals("2")) { + th.check(val, "a2", "Second isolate static value is correct."); + } else { + th.fail("Bad arg value: " + args[0]); + } + } } } diff --git a/tests/isolate/TestIsolate.java b/tests/isolate/TestIsolate.java index 43fb3729..4b4c22ba 100644 --- a/tests/isolate/TestIsolate.java +++ b/tests/isolate/TestIsolate.java @@ -2,88 +2,65 @@ package tests.isolate; import com.sun.cldc.isolate.*; import tests.isolate.IsolatedClass; +import gnu.testlet.DumpTestHarness; import java.lang.String; public class TestIsolate { - static int dumpNumber = 0; - public static void dump(String s) { - System.out.println((dumpNumber++) + ": " + s); - } - public static void dump(int s) { - dump(s + ""); - } - public static void main(String args[]) { - dump(IsolatedClass.val); + DumpTestHarness th = new DumpTestHarness(); + th.check(IsolatedClass.val, "a", "Initial IsolatedClass static value is correct."); - new IsolatedClass().main(new String[] { "a" } ); + new IsolatedClass().main(new String[] { "b" } ); - dump(IsolatedClass.val); + th.check(IsolatedClass.val, "ab", "IsolatedClass static val append works."); Isolate myIso = Isolate.currentIsolate(); - dump(myIso.id()); + int myIsoId = myIso.id(); + th.check(myIsoId > -1, "Valid isolate ID."); Isolate[] isolates = Isolate.getIsolates(); - if (isolates.length == 1) { - dump("1 isolate"); - } + th.check(isolates.length == 1, "Only one isolate exists."); - if (isolates[0].id() == myIso.id()) { - dump("Isolate ID correct"); - } + th.check(isolates[0].id() == myIso.id(), "Isolate ID is correct."); try { - Isolate iso1 = new Isolate("tests.isolate.IsolatedClass", new String[] { "1" }); - Isolate iso2 = new Isolate("tests.isolate.IsolatedClass", new String[] { "2" }); + Isolate iso1 = new Isolate("tests.isolate.IsolatedClass", new String[] { "1", "new isolate" }); + Isolate iso2 = new Isolate("tests.isolate.IsolatedClass", new String[] { "2", "new isolate" }); - dump(iso1.id()); - dump(iso2.id()); + int iso1Id = iso1.id(); + th.check(iso1Id > myIsoId, "First isolate id is larger than main isolate id."); + th.check(iso2.id() > iso1Id, "Second isolate is larger than first isolate id."); - if (Isolate.getIsolates().length == 1) { - dump("1 isolate"); - } + th.check(Isolate.getIsolates().length, 1, "1 isolate created."); iso1.start(); - - dump(IsolatedClass.val); + th.check(IsolatedClass.val, "ab", "IsolatedClass static value not modified by iso1 starting."); iso2.start(); + th.check(IsolatedClass.val, "ab", "IsolatedClass static value not modified by iso2 starting."); - dump(IsolatedClass.val); - - if (Isolate.getIsolates().length == 3) { - dump("3 isolates"); - } + th.check(Isolate.getIsolates().length, 3, "3 isolates created."); iso1.waitForExit(); iso2.waitForExit(); + th.check(IsolatedClass.val, "ab", "IsolatedClass static value not modified by new isolates after exit."); - dump(IsolatedClass.val); + th.check(Isolate.getIsolates().length, 1, "1 isolate left."); - if (Isolate.getIsolates().length == 1) { - dump("1 isolate"); - } - - if (iso1.isTerminated() && iso2.isTerminated()) { - dump("Isolates terminated"); - } + th.check(iso1.isTerminated(), "iso1 is terminated."); + th.check(iso2.isTerminated(), "iso2 is terminated."); } catch(Exception e) { e.printStackTrace(); + th.fail("Exception: " + e); } - new IsolatedClass().main(new String[] { "r" }); - - dump(IsolatedClass.val); - new IsolatedClass().main(new String[] { "c" }); - dump(IsolatedClass.val); + th.check(IsolatedClass.val, "abc", "IsolatedClass static val append still works."); - if (!myIso.isTerminated()) { - dump("Main isolate still running"); - } + th.check(!myIso.isTerminated(), "Main isolate still running"); System.out.println("DONE"); }