Use a more predicable isolate test with assertions.

This commit is contained in:
Brendan Dahl 2015-08-25 15:20:59 -07:00
Родитель 250814f0d7
Коммит 891f746802
5 изменённых файлов: 68 добавлений и 89 удалений

Просмотреть файл

@ -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") .thenOpen("http://localhost:8000/index.html?main=tests/isolate/TestIsolate&logLevel=info&logConsole=web,page,raw")
.withFrame(0, function() { .withFrame(0, function() {
casper.waitForText("DONE", function() { casper.waitForText("DONE", function() {
var output = this.fetchText('#raw-console'); test.assertTextDoesntExist("FAIL");
var expectedOutput = [ // Two sanity checks to make sure the two isolates ran.
"I 0: m", test.assertTextExists("PASS First isolate static value is correct.");
"I 2 0 a ma", test.assertTextExists("PASS Second isolate static value is correct.");
"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.");
}); });
}); });

Просмотреть файл

@ -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;
}
}

Просмотреть файл

@ -204,6 +204,8 @@ public abstract class TestHarness {
public TestHarness(Display d) { public TestHarness(Display d) {
display = d; display = d;
} }
public TestHarness() {
}
public void setScreenAndWait(Displayable s) { public void setScreenAndWait(Displayable s) {
display.setCurrent(s); display.setCurrent(s);

Просмотреть файл

@ -1,13 +1,22 @@
package tests.isolate; package tests.isolate;
import com.sun.cldc.isolate.*; import com.sun.cldc.isolate.*;
import gnu.testlet.DumpTestHarness;
public class IsolatedClass { public class IsolatedClass {
static public String val = "m"; static public String val = "a";
static public int testRun = 0;
public static void main(String args[]) { public static void main(String args[]) {
DumpTestHarness th = new DumpTestHarness();
val += args[0]; 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]);
}
}
} }
} }

Просмотреть файл

@ -2,88 +2,65 @@ package tests.isolate;
import com.sun.cldc.isolate.*; import com.sun.cldc.isolate.*;
import tests.isolate.IsolatedClass; import tests.isolate.IsolatedClass;
import gnu.testlet.DumpTestHarness;
import java.lang.String; import java.lang.String;
public class TestIsolate { 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[]) { 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(); Isolate myIso = Isolate.currentIsolate();
dump(myIso.id()); int myIsoId = myIso.id();
th.check(myIsoId > -1, "Valid isolate ID.");
Isolate[] isolates = Isolate.getIsolates(); Isolate[] isolates = Isolate.getIsolates();
if (isolates.length == 1) { th.check(isolates.length == 1, "Only one isolate exists.");
dump("1 isolate");
}
if (isolates[0].id() == myIso.id()) { th.check(isolates[0].id() == myIso.id(), "Isolate ID is correct.");
dump("Isolate ID correct");
}
try { try {
Isolate iso1 = new Isolate("tests.isolate.IsolatedClass", new String[] { "1" }); Isolate iso1 = new Isolate("tests.isolate.IsolatedClass", new String[] { "1", "new isolate" });
Isolate iso2 = new Isolate("tests.isolate.IsolatedClass", new String[] { "2" }); Isolate iso2 = new Isolate("tests.isolate.IsolatedClass", new String[] { "2", "new isolate" });
dump(iso1.id()); int iso1Id = iso1.id();
dump(iso2.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) { th.check(Isolate.getIsolates().length, 1, "1 isolate created.");
dump("1 isolate");
}
iso1.start(); iso1.start();
th.check(IsolatedClass.val, "ab", "IsolatedClass static value not modified by iso1 starting.");
dump(IsolatedClass.val);
iso2.start(); iso2.start();
th.check(IsolatedClass.val, "ab", "IsolatedClass static value not modified by iso2 starting.");
dump(IsolatedClass.val); th.check(Isolate.getIsolates().length, 3, "3 isolates created.");
if (Isolate.getIsolates().length == 3) {
dump("3 isolates");
}
iso1.waitForExit(); iso1.waitForExit();
iso2.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) { th.check(iso1.isTerminated(), "iso1 is terminated.");
dump("1 isolate"); th.check(iso2.isTerminated(), "iso2 is terminated.");
}
if (iso1.isTerminated() && iso2.isTerminated()) {
dump("Isolates terminated");
}
} catch(Exception e) { } catch(Exception e) {
e.printStackTrace(); e.printStackTrace();
th.fail("Exception: " + e);
} }
new IsolatedClass().main(new String[] { "r" });
dump(IsolatedClass.val);
new IsolatedClass().main(new String[] { "c" }); new IsolatedClass().main(new String[] { "c" });
dump(IsolatedClass.val); th.check(IsolatedClass.val, "abc", "IsolatedClass static val append still works.");
if (!myIso.isTerminated()) { th.check(!myIso.isTerminated(), "Main isolate still running");
dump("Main isolate still running");
}
System.out.println("DONE"); System.out.println("DONE");
} }