# HG changeset patch # User Jaroslav Tulach # Date 1355833532 -3600 # Node ID 12debda84e3cad843f1a7c367d62f9ff6f7dd027 # Parent 3fe5a86bd1231a74896a68f2e808c9488c1bb0c9 Starting the HTTP server only once diff -r 3fe5a86bd123 -r 12debda84e3c vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/VMTest.java --- a/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/VMTest.java Tue Dec 18 13:10:56 2012 +0100 +++ b/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/VMTest.java Tue Dec 18 13:25:32 2012 +0100 @@ -26,6 +26,8 @@ import java.util.Enumeration; import java.util.Map; import java.util.WeakHashMap; +import java.util.logging.Level; +import java.util.logging.Logger; import javax.script.Invocable; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; @@ -33,6 +35,7 @@ import org.apidesign.vm4brwsr.Bck2Brwsr; import org.testng.Assert; import org.testng.ITest; +import org.testng.annotations.BeforeTest; import org.testng.annotations.Factory; import org.testng.annotations.Test; @@ -145,10 +148,16 @@ private Invocable code; private CharSequence codeSeq; private static final Map compiled = new WeakHashMap<>(); + private Object inst; private Run(Method m, int type) { this.m = m; this.type = type; + try { + initialize(); + } catch (Throwable ex) { + Logger.getLogger(VMTest.class.getName()).log(Level.SEVERE, null, ex); + } } private void compileTheCode(Class clazz) throws Exception { @@ -175,22 +184,27 @@ codeSeq = sb; compiled.put(clazz, new Object[] { code, codeSeq }); } + + private void initialize() throws Throwable { + if (type == 1) { + compileTheCode(m.getDeclaringClass()); + Object vm = code.invokeFunction("initVM"); + inst = code.invokeMethod(vm, "loadClass", m.getDeclaringClass().getName()); + } else if (type == 2) { + inst = addBrowserMethod(m.getDeclaringClass(), m.getName()); + } + } @Test(groups = "run") public void executeCode() throws Throwable { if (type == 1) { try { - compileTheCode(m.getDeclaringClass()); - Object vm = code.invokeFunction("initVM"); - Object inst = code.invokeMethod(vm, "loadClass", m.getDeclaringClass().getName()); value = code.invokeMethod(inst, m.getName() + "__" + computeSignature(m)); } catch (Exception ex) { throw new AssertionError(dumpJS(codeSeq)).initCause(ex); } } else if (type == 2) { - Bck2BrwsrLauncher l = new Bck2BrwsrLauncher(); - l.setTimeout(5000); - Bck2BrwsrLauncher.MethodInvocation c = l.addMethod(m.getDeclaringClass(), m.getName()); - l.execute(); + Bck2BrwsrLauncher.MethodInvocation c = (Bck2BrwsrLauncher.MethodInvocation) inst; + execBrowser(); value = c.toString(); } else { value = m.invoke(m.getDeclaringClass().newInstance()); @@ -269,4 +283,28 @@ } return new StringBuilder(f.getPath()); } + + private static Bck2BrwsrLauncher launcher; + + private static synchronized Bck2BrwsrLauncher.MethodInvocation addBrowserMethod( + Class clazz, String name + ) { + if (launcher == null) { + launcher = new Bck2BrwsrLauncher(); + launcher.setTimeout(5000); + } + return launcher.addMethod(clazz, name); + } + + private static void execBrowser() throws Exception { + Bck2BrwsrLauncher l = clearBrowser(); + if (l != null) { + l.execute(); + } + } + private static synchronized Bck2BrwsrLauncher clearBrowser() { + Bck2BrwsrLauncher l = launcher; + launcher = null; + return l; + } }