1.1 --- a/json-tck/src/main/java/net/java/html/js/tests/GCBodyTest.java Tue Dec 09 10:38:46 2014 +0100
1.2 +++ b/json-tck/src/main/java/net/java/html/js/tests/GCBodyTest.java Fri Dec 12 11:22:40 2014 +0100
1.3 @@ -52,8 +52,9 @@
1.4 */
1.5 public class GCBodyTest {
1.6 Reference<?> ref;
1.7 + int[] arr;
1.8
1.9 - @KOTest public void callbackWithParameters() throws InterruptedException {
1.10 + @KOTest public void callbackInterfaceCanDisappear() throws InterruptedException {
1.11 if (ref != null) {
1.12 assertGC(ref, "Can disappear!");
1.13 return;
1.14 @@ -66,24 +67,60 @@
1.15 assertGC(ref, "Can disappear!");
1.16 }
1.17
1.18 + private Object assignInst() {
1.19 + Object obj = Bodies.instance(0);
1.20 + Object s = new EmptyInstance();
1.21 + Bodies.setX(obj, s);
1.22 + assert s == Bodies.readX(obj);
1.23 + ref = new WeakReference<Object>(s);
1.24 + return obj;
1.25 +}
1.26 +
1.27 @KOTest public void holdObjectAndReleaseObject() throws InterruptedException {
1.28 if (ref != null) {
1.29 assertGC(ref, "Can disappear!");
1.30 return;
1.31 }
1.32 - Sum s = new Sum();
1.33 - Object obj = Bodies.instance(0);
1.34 - Bodies.setX(obj, s);
1.35
1.36 - ref = new WeakReference<Object>(s);
1.37 - s = null;
1.38 - assertNotGC(ref, "Cannot disappear!");
1.39 + Object obj = assignInst();
1.40 + assert ref != null;
1.41
1.42 Bodies.setX(obj, null);
1.43 obj = null;
1.44
1.45 assertGC(ref, "Can disappear!");
1.46 }
1.47 +
1.48 + private static Reference<?> sendRunnable(final int[] arr) {
1.49 + Runnable r = new Runnable() {
1.50 + @Override
1.51 + public void run() {
1.52 + arr[0]++;
1.53 + }
1.54 + };
1.55 + Bodies.asyncCallback(r);
1.56 + return new WeakReference<Object>(r);
1.57 + }
1.58 +
1.59 + private static class EmptyInstance {
1.60 + }
1.61 +
1.62 + @KOTest public void parametersNeedToRemainInAsyncMode() throws InterruptedException {
1.63 + if (ref != null) {
1.64 + if (arr[0] != 1) {
1.65 + throw new InterruptedException();
1.66 + }
1.67 + assertGC(ref, "Now the runnable can disappear");
1.68 + return;
1.69 + }
1.70 + arr = new int[] { 0 };
1.71 + ref = sendRunnable(arr);
1.72 + if (arr[0] == 1) {
1.73 + return;
1.74 + }
1.75 + assertNotGC(ref, false, "The runnable should not be GCed");
1.76 + throw new InterruptedException();
1.77 + }
1.78
1.79 private static void assertGC(Reference<?> ref, String msg) throws InterruptedException {
1.80 for (int i = 0; i < 100; i++) {
1.81 @@ -109,12 +146,14 @@
1.82 return ref.get() == null;
1.83 }
1.84
1.85 - private static void assertNotGC(Reference<?> ref, String msg) throws InterruptedException {
1.86 + private static void assertNotGC(Reference<?> ref, boolean clearJS, String msg) throws InterruptedException {
1.87 for (int i = 0; i < 10; i++) {
1.88 if (ref.get() == null) {
1.89 throw new IllegalStateException(msg);
1.90 }
1.91 - int size = Bodies.gc(Math.pow(2.0, i));
1.92 + if (clearJS) {
1.93 + Bodies.gc(Math.pow(2.0, i));
1.94 + }
1.95 try {
1.96 System.gc();
1.97 System.runFinalization();