Verifying a parameter to a method can be GCed by a test that is passing. Verifying the knockout model sticks in a memory when no longer needed by a failing test.
1.1 --- a/json-tck/src/main/java/net/java/html/js/tests/Bodies.java Sun Nov 09 16:07:41 2014 +0100
1.2 +++ b/json-tck/src/main/java/net/java/html/js/tests/Bodies.java Mon Nov 10 18:28:45 2014 +0100
1.3 @@ -172,6 +172,15 @@
1.4 )
1.5 static native Object[] forIn(Object[] in);
1.6
1.7 + @JavaScriptBody(args = { "max" }, body =
1.8 + "var arr = [];\n"
1.9 + + "for (var i = 0; i < max; i++) {\n"
1.10 + + " arr.push(i);\n"
1.11 + + "}\n"
1.12 + + "return arr.length;"
1.13 + )
1.14 + static native int gc(double max);
1.15 +
1.16 @JavaScriptBody(args = {}, javacall = true, body =
1.17 "return @net.java.html.js.tests.Bodies::problematicString()();"
1.18 )
2.1 --- a/json-tck/src/main/java/net/java/html/js/tests/JavaScriptBodyTest.java Sun Nov 09 16:07:41 2014 +0100
2.2 +++ b/json-tck/src/main/java/net/java/html/js/tests/JavaScriptBodyTest.java Mon Nov 10 18:28:45 2014 +0100
2.3 @@ -43,6 +43,8 @@
2.4 package net.java.html.js.tests;
2.5
2.6 import java.io.StringReader;
2.7 +import java.lang.ref.Reference;
2.8 +import java.lang.ref.WeakReference;
2.9 import java.util.Arrays;
2.10 import java.util.concurrent.Callable;
2.11 import org.netbeans.html.boot.spi.Fn;
2.12 @@ -211,9 +213,13 @@
2.13 assert b == Boolean.TRUE : "Should return true";
2.14 }
2.15
2.16 - @KOTest public void callbackWithParameters() {
2.17 - int res = Bodies.sumIndirect(new Sum());
2.18 + @KOTest public void callbackWithParameters() throws InterruptedException {
2.19 + Sum s = new Sum();
2.20 + int res = Bodies.sumIndirect(s);
2.21 assert res == 42 : "Expecting 42";
2.22 + Reference<?> ref = new WeakReference<Object>(s);
2.23 + s = null;
2.24 + assertGC(ref, "Can disappear!");
2.25 }
2.26
2.27 @KOTest public void selectFromStringJavaArray() {
2.28 @@ -401,4 +407,20 @@
2.29 return Boolean.TRUE;
2.30 }
2.31 }
2.32 +
2.33 + private static void assertGC(Reference<?> ref, String msg) throws InterruptedException {
2.34 + for (int i = 0; i < 100; i++) {
2.35 + if (ref.get() == null) {
2.36 + return;
2.37 + }
2.38 + int size = Bodies.gc(Math.pow(2.0, i));
2.39 + try {
2.40 + System.gc();
2.41 + System.runFinalization();
2.42 + } catch (Error err) {
2.43 + err.printStackTrace();
2.44 + }
2.45 + }
2.46 + throw new OutOfMemoryError(msg);
2.47 + }
2.48 }
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
3.2 +++ b/json-tck/src/main/java/net/java/html/json/tests/GCTest.java Mon Nov 10 18:28:45 2014 +0100
3.3 @@ -0,0 +1,113 @@
3.4 +/**
3.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3.6 + *
3.7 + * Copyright 2013-2014 Oracle and/or its affiliates. All rights reserved.
3.8 + *
3.9 + * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
3.10 + * Other names may be trademarks of their respective owners.
3.11 + *
3.12 + * The contents of this file are subject to the terms of either the GNU
3.13 + * General Public License Version 2 only ("GPL") or the Common
3.14 + * Development and Distribution License("CDDL") (collectively, the
3.15 + * "License"). You may not use this file except in compliance with the
3.16 + * License. You can obtain a copy of the License at
3.17 + * http://www.netbeans.org/cddl-gplv2.html
3.18 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
3.19 + * specific language governing permissions and limitations under the
3.20 + * License. When distributing the software, include this License Header
3.21 + * Notice in each file and include the License file at
3.22 + * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
3.23 + * particular file as subject to the "Classpath" exception as provided
3.24 + * by Oracle in the GPL Version 2 section of the License file that
3.25 + * accompanied this code. If applicable, add the following below the
3.26 + * License Header, with the fields enclosed by brackets [] replaced by
3.27 + * your own identifying information:
3.28 + * "Portions Copyrighted [year] [name of copyright owner]"
3.29 + *
3.30 + * Contributor(s):
3.31 + *
3.32 + * The Original Software is NetBeans. The Initial Developer of the Original
3.33 + * Software is Oracle. Portions Copyright 2013-2014 Oracle. All Rights Reserved.
3.34 + *
3.35 + * If you wish your version of this file to be governed by only the CDDL
3.36 + * or only the GPL Version 2, indicate your decision by adding
3.37 + * "[Contributor] elects to include this software in this distribution
3.38 + * under the [CDDL or GPL Version 2] license." If you do not indicate a
3.39 + * single choice of license, a recipient has the option to distribute
3.40 + * your version of this file under either the CDDL, the GPL Version 2 or
3.41 + * to extend the choice of license to its licensees as provided above.
3.42 + * However, if you add GPL Version 2 code and therefore, elected the GPL
3.43 + * Version 2 license, then the option applies only if the new code is
3.44 + * made subject to such option by the copyright holder.
3.45 + */
3.46 +package net.java.html.json.tests;
3.47 +
3.48 +import java.lang.ref.Reference;
3.49 +import java.lang.ref.WeakReference;
3.50 +import net.java.html.BrwsrCtx;
3.51 +import net.java.html.json.Model;
3.52 +import net.java.html.json.Models;
3.53 +import net.java.html.json.Property;
3.54 +import org.netbeans.html.json.tck.KOTest;
3.55 +
3.56 +@Model(className = "GC", properties = {
3.57 + @Property(name = "all", type = Fullname.class, array = true)
3.58 +})
3.59 +public class GCTest {
3.60 + @Model(className = "Fullname", properties = {
3.61 + @Property(name = "firstName", type = String.class),
3.62 + @Property(name = "lastName", type = String.class)
3.63 + })
3.64 + static class FullnameCntrl {
3.65 + }
3.66 +
3.67 + @KOTest public void noLongerNeededArrayElementsCanDisappear() throws Exception {
3.68 + BrwsrCtx ctx = Utils.newContext(GCTest.class);
3.69 + Object exp = Utils.exposeHTML(GCTest.class,
3.70 + "<ul id='ul' data-bind='foreach: all'>\n"
3.71 + + " <li data-bind='text: firstName'/>\n"
3.72 + + "</ul>\n"
3.73 + );
3.74 + GC m = Models.bind(new GC(), ctx);
3.75 + m.getAll().add(new Fullname("Jarda", "Tulach"));
3.76 + m.applyBindings();
3.77 +
3.78 + int cnt = Utils.countChildren(GCTest.class, "ul");
3.79 + assert cnt == 1 : "One child, but was " + cnt;
3.80 +
3.81 + m.getAll().add(new Fullname("HTML", "Java"));
3.82 +
3.83 + cnt = Utils.countChildren(GCTest.class, "ul");
3.84 + assert cnt == 2 : "Now two " + cnt;
3.85 +
3.86 + Fullname removed = m.getAll().get(0);
3.87 + m.getAll().remove(0);
3.88 +
3.89 + cnt = Utils.countChildren(GCTest.class, "ul");
3.90 + assert cnt == 1 : "Again One " + cnt;
3.91 +
3.92 + Reference<?> ref = new WeakReference<Object>(removed);
3.93 + removed = null;
3.94 + assertGC(ref, "Can removed object disappear?");
3.95 + }
3.96 +
3.97 + private void assertGC(Reference<?> ref, String msg) throws Exception {
3.98 + for (int i = 0; i < 100; i++) {
3.99 + if (ref.get() == null) {
3.100 + return;
3.101 + }
3.102 + String gc = "var max = arguments[0];\n"
3.103 + + "var arr = [];\n"
3.104 + + "for (var i = 0; i < max; i++) {\n"
3.105 + + " arr.push(i);\n"
3.106 + + "}\n"
3.107 + + "return arr.length;";
3.108 + Object cnt = Utils.executeScript(GCTest.class, gc, Math.pow(2.0, i));
3.109 + System.err.println("cnt: " + cnt);
3.110 + System.gc();
3.111 + System.runFinalization();
3.112 + }
3.113 + throw new OutOfMemoryError(msg);
3.114 + }
3.115 +
3.116 +}
4.1 --- a/json-tck/src/main/java/org/netbeans/html/json/tck/KnockoutTCK.java Sun Nov 09 16:07:41 2014 +0100
4.2 +++ b/json-tck/src/main/java/org/netbeans/html/json/tck/KnockoutTCK.java Mon Nov 10 18:28:45 2014 +0100
4.3 @@ -46,6 +46,7 @@
4.4 import java.util.Map;
4.5 import net.java.html.BrwsrCtx;
4.6 import net.java.html.json.tests.ConvertTypesTest;
4.7 +import net.java.html.json.tests.GCTest;
4.8 import net.java.html.json.tests.JSONTest;
4.9 import net.java.html.json.tests.KnockoutTest;
4.10 import net.java.html.json.tests.MinesTest;
4.11 @@ -124,7 +125,8 @@
4.12 KnockoutTest.class,
4.13 MinesTest.class,
4.14 OperationsTest.class,
4.15 - WebSocketTest.class
4.16 + WebSocketTest.class,
4.17 + GCTest.class
4.18 };
4.19 }
4.20