8039403: Write sanity tests for persistent caching

Wed, 09 Apr 2014 10:14:34 +0200

author
mnunez
date
Wed, 09 Apr 2014 10:14:34 +0200
changeset 833
997c08a0a6c0
parent 832
06b8bf8df928
child 834
47dc7a83b3c4

8039403: Write sanity tests for persistent caching
Reviewed-by: hannesw, sundar

test/src/jdk/nashorn/internal/runtime/CodeStoreAndPathTest.java file | annotate | diff | comparison | revisions
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/src/jdk/nashorn/internal/runtime/CodeStoreAndPathTest.java	Wed Apr 09 10:14:34 2014 +0200
     1.3 @@ -0,0 +1,159 @@
     1.4 +/*
     1.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +package jdk.nashorn.internal.runtime;
    1.29 +
    1.30 +import java.io.File;
    1.31 +import java.io.IOException;
    1.32 +import java.nio.file.Files;
    1.33 +import java.nio.file.DirectoryStream;
    1.34 +import java.nio.file.Path;
    1.35 +import java.nio.file.FileSystems;
    1.36 +import javax.script.ScriptException;
    1.37 +import org.testng.annotations.Test;
    1.38 +import javax.script.ScriptEngine;
    1.39 +import jdk.nashorn.api.scripting.NashornScriptEngineFactory;
    1.40 +import static org.testng.Assert.assertFalse;
    1.41 +import static org.testng.Assert.assertEquals;
    1.42 +
    1.43 +/**
    1.44 + * @test
    1.45 + * @bug 8039185 8039403
    1.46 + * @summary  Test for persistent code cache and path handling
    1.47 + * @run testng jdk.nashorn.internal.runtime.CodeStoreAndPathTest
    1.48 + */
    1.49 +
    1.50 +public class CodeStoreAndPathTest {
    1.51 +
    1.52 +    final String code1 = "var code1; var x = 'Hello Script'; var x1 = 'Hello Script'; "
    1.53 +                + "var x2 = 'Hello Script'; var x3 = 'Hello Script'; "
    1.54 +                + "var x4 = 'Hello Script'; var x5 = 'Hello Script';"
    1.55 +                + "var x6 = 'Hello Script'; var x7 = 'Hello Script'; "
    1.56 +                + "var x8 = 'Hello Script'; var x9 = 'Hello Script'; "
    1.57 +                + "var x10 = 'Hello Script';"
    1.58 +                + "function f() {x ='Bye Script'; x1 ='Bye Script'; x2='Bye Script';"
    1.59 +                + "x3='Bye Script'; x4='Bye Script'; x5='Bye Script'; x6='Bye Script';"
    1.60 +                + "x7='Bye Script'; x8='Bye Script'; var x9 = 'Hello Script'; "
    1.61 +                + "var x10 = 'Hello Script';}"
    1.62 +                + "function g() {x ='Bye Script'; x1 ='Bye Script'; x2='Bye Script';"
    1.63 +                + "x3='Bye Script'; x4='Bye Script'; x5='Bye Script'; x6='Bye Script';"
    1.64 +                + "x7='Bye Script'; x8='Bye Script'; var x9 = 'Hello Script'; "
    1.65 +                + "var x10 = 'Hello Script';}"
    1.66 +                + "function h() {x ='Bye Script'; x1 ='Bye Script'; x2='Bye Script';"
    1.67 +                + "x3='Bye Script'; x4='Bye Script'; x5='Bye Script'; x6='Bye Script';"
    1.68 +                + "x7='Bye Script'; x8='Bye Script'; var x9 = 'Hello Script'; "
    1.69 +                + "var x10 = 'Hello Script';}"
    1.70 +                + "function i() {x ='Bye Script'; x1 ='Bye Script'; x2='Bye Script';"
    1.71 +                + "x3='Bye Script'; x4='Bye Script'; x5='Bye Script'; x6='Bye Script';"
    1.72 +                + "x7='Bye Script'; x8='Bye Script'; var x9 = 'Hello Script'; "
    1.73 +                + "var x10 = 'Hello Script';}";
    1.74 +    final String code2 = "var code2; var x = 'Hello Script'; var x1 = 'Hello Script'; "
    1.75 +                + "var x2 = 'Hello Script'; var x3 = 'Hello Script'; "
    1.76 +                + "var x4 = 'Hello Script'; var x5 = 'Hello Script';"
    1.77 +                + "var x6 = 'Hello Script'; var x7 = 'Hello Script'; "
    1.78 +                + "var x8 = 'Hello Script'; var x9 = 'Hello Script'; "
    1.79 +                + "var x10 = 'Hello Script';"
    1.80 +                + "function f() {x ='Bye Script'; x1 ='Bye Script'; x2='Bye Script';"
    1.81 +                + "x3='Bye Script'; x4='Bye Script'; x5='Bye Script'; x6='Bye Script';"
    1.82 +                + "x7='Bye Script'; x8='Bye Script'; var x9 = 'Hello Script'; "
    1.83 +                + "var x10 = 'Hello Script';}"
    1.84 +                + "function g() {x ='Bye Script'; x1 ='Bye Script'; x2='Bye Script';"
    1.85 +                + "x3='Bye Script'; x4='Bye Script'; x5='Bye Script'; x6='Bye Script';"
    1.86 +                + "x7='Bye Script'; x8='Bye Script'; var x9 = 'Hello Script'; "
    1.87 +                + "var x10 = 'Hello Script';}"
    1.88 +                + "function h() {x ='Bye Script'; x1 ='Bye Script'; x2='Bye Script';"
    1.89 +                + "x3='Bye Script'; x4='Bye Script'; x5='Bye Script'; x6='Bye Script';"
    1.90 +                + "x7='Bye Script'; x8='Bye Script'; var x9 = 'Hello Script'; "
    1.91 +                + "var x10 = 'Hello Script';}"
    1.92 +                + "function i() {x ='Bye Script'; x1 ='Bye Script'; x2='Bye Script';"
    1.93 +                + "x3='Bye Script'; x4='Bye Script'; x5='Bye Script'; x6='Bye Script';"
    1.94 +                + "x7='Bye Script'; x8='Bye Script'; var x9 = 'Hello Script'; "
    1.95 +                + "var x10 = 'Hello Script';}";
    1.96 +    // Script size < Default minimum size for storing a compiled script class
    1.97 +    final String code3 = "var code3; var x = 'Hello Script'; var x1 = 'Hello Script'; ";
    1.98 +    final String codeCache = "build/nashorn_code_cache";
    1.99 +    final String oldUserDir = System.getProperty("user.dir");
   1.100 +
   1.101 +    public void checkCompiledScripts(DirectoryStream<Path> stream, int numberOfScripts) throws IOException {
   1.102 +        for (Path file : stream) {
   1.103 +            numberOfScripts--;
   1.104 +        }
   1.105 +        stream.close();
   1.106 +        assertEquals(numberOfScripts,0);
   1.107 +    }
   1.108 +
   1.109 +    @Test
   1.110 +    public void pathHandlingTest() throws ScriptException, IOException {
   1.111 +        System.setProperty("nashorn.persistent.code.cache", codeCache);
   1.112 +        String[] options = new String[]{"--persistent-code-cache"};
   1.113 +        NashornScriptEngineFactory fac = new NashornScriptEngineFactory();
   1.114 +        ScriptEngine e = fac.getScriptEngine(options);
   1.115 +        Path expectedCodeCachePath = FileSystems.getDefault().getPath(oldUserDir + File.separator + codeCache);
   1.116 +        Path actualCodeCachePath = FileSystems.getDefault().getPath(System.getProperty(
   1.117 +                            "nashorn.persistent.code.cache")).toAbsolutePath();
   1.118 +        // Check that nashorn code cache is created in current working directory
   1.119 +        assertEquals(actualCodeCachePath, expectedCodeCachePath);
   1.120 +        // Check that code cache dir exists and it's not empty
   1.121 +        File file = new File(actualCodeCachePath.toUri());
   1.122 +        assertFalse(!file.isDirectory(), "No code cache directory was created!");
   1.123 +        assertFalse(file.list().length == 0, "Code cache directory is empty!");
   1.124 +    }
   1.125 +
   1.126 +    @Test
   1.127 +    public void changeUserDirTest() throws ScriptException, IOException {
   1.128 +        System.setProperty("nashorn.persistent.code.cache", codeCache);
   1.129 +        String[] options = new String[]{"--persistent-code-cache"};
   1.130 +        NashornScriptEngineFactory fac = new NashornScriptEngineFactory();
   1.131 +        ScriptEngine e = fac.getScriptEngine(options);
   1.132 +        Path codeCachePath = FileSystems.getDefault().getPath(System.getProperty(
   1.133 +                            "nashorn.persistent.code.cache")).toAbsolutePath();
   1.134 +        String newUserDir = "build/newUserDir";
   1.135 +        // Now changing current working directory
   1.136 +        System.setProperty("user.dir", System.getProperty("user.dir") + File.separator + newUserDir);
   1.137 +        // Check that a new compiled script is stored in exisitng code cache
   1.138 +        e.eval(code1);
   1.139 +        DirectoryStream<Path> stream = Files.newDirectoryStream(codeCachePath);
   1.140 +        // Already one compiled script has been stored in the cache during initialization
   1.141 +        checkCompiledScripts(stream, 2);
   1.142 +        // Setting to default current working dir
   1.143 +        System.setProperty("user.dir", oldUserDir);
   1.144 +    }
   1.145 +
   1.146 +    @Test
   1.147 +    public void codeCacheTest() throws ScriptException, IOException {
   1.148 +        System.setProperty("nashorn.persistent.code.cache", codeCache);
   1.149 +        String[] options = new String[]{"--persistent-code-cache"};
   1.150 +        NashornScriptEngineFactory fac = new NashornScriptEngineFactory();
   1.151 +        ScriptEngine e = fac.getScriptEngine(options);
   1.152 +        Path codeCachePath = FileSystems.getDefault().getPath(System.getProperty(
   1.153 +                            "nashorn.persistent.code.cache")).toAbsolutePath();
   1.154 +        e.eval(code1);
   1.155 +        e.eval(code2);
   1.156 +        e.eval(code3);// less than minimum size for storing
   1.157 +        // Already one compiled script has been stored in the cache during initialization
   1.158 +        // adding code1 and code2.
   1.159 +        DirectoryStream<Path> stream = Files.newDirectoryStream(codeCachePath);
   1.160 +        checkCompiledScripts(stream, 3);
   1.161 +    }
   1.162 +}

mercurial