< prev index next >

test/src/jdk/nashorn/internal/runtime/test/CodeStoreAndPathTest.java

Print this page
rev 1377 : 8077168: CodeStoreAndPathTest.java fails in jtreg mode on Mac


   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package jdk.nashorn.internal.runtime.test;
  26 
  27 import static org.testng.Assert.assertEquals;
  28 import static org.testng.Assert.assertFalse;

  29 import java.io.File;
  30 import java.io.IOException;
  31 import java.nio.file.DirectoryStream;
  32 import java.nio.file.FileSystems;
  33 import java.nio.file.Files;
  34 import java.nio.file.Path;
  35 import javax.script.ScriptEngine;
  36 import javax.script.ScriptException;
  37 import jdk.nashorn.api.scripting.NashornScriptEngineFactory;
  38 import org.testng.annotations.Test;
  39 
  40 /**
  41  * @ignore Fails with jtreg, but passes with ant test run. Ignore for now.
  42  * @test
  43  * @bug 8039185 8039403
  44  * @summary  Test for persistent code cache and path handling
  45  * @run testng jdk.nashorn.internal.runtime.test.CodeStoreAndPathTest
  46  */
  47 @SuppressWarnings("javadoc")
  48 public class CodeStoreAndPathTest {
  49 
  50     final String code1 = "var code1; var x = 'Hello Script'; var x1 = 'Hello Script'; "
  51                 + "var x2 = 'Hello Script'; var x3 = 'Hello Script'; "
  52                 + "var x4 = 'Hello Script'; var x5 = 'Hello Script';"
  53                 + "var x6 = 'Hello Script'; var x7 = 'Hello Script'; "
  54                 + "var x8 = 'Hello Script'; var x9 = 'Hello Script'; "
  55                 + "var x10 = 'Hello Script';"
  56                 + "function f() {x ='Bye Script'; x1 ='Bye Script'; x2='Bye Script';"
  57                 + "x3='Bye Script'; x4='Bye Script'; x5='Bye Script'; x6='Bye Script';"
  58                 + "x7='Bye Script'; x8='Bye Script'; var x9 = 'Hello Script'; "
  59                 + "var x10 = 'Hello Script';}"
  60                 + "function g() {x ='Bye Script'; x1 ='Bye Script'; x2='Bye Script';"
  61                 + "x3='Bye Script'; x4='Bye Script'; x5='Bye Script'; x6='Bye Script';"


  96     final String codeCache = "build/nashorn_code_cache";
  97     final String oldUserDir = System.getProperty("user.dir");
  98 
  99     private static final String[] ENGINE_OPTIONS_OPT   = new String[]{"--persistent-code-cache", "--optimistic-types=true"};
 100     private static final String[] ENGINE_OPTIONS_NOOPT = new String[]{"--persistent-code-cache", "--optimistic-types=false"};
 101 
 102     @Test
 103     public void pathHandlingTest() {
 104         System.setProperty("nashorn.persistent.code.cache", codeCache);
 105         final NashornScriptEngineFactory fac = new NashornScriptEngineFactory();
 106 
 107         fac.getScriptEngine(ENGINE_OPTIONS_NOOPT);
 108 
 109         final Path expectedCodeCachePath = FileSystems.getDefault().getPath(oldUserDir + File.separator + codeCache);
 110         final Path actualCodeCachePath = FileSystems.getDefault().getPath(System.getProperty(
 111                             "nashorn.persistent.code.cache")).toAbsolutePath();
 112         // Check that nashorn code cache is created in current working directory
 113         assertEquals(actualCodeCachePath, expectedCodeCachePath);
 114         // Check that code cache dir exists and it's not empty
 115         final File file = new File(actualCodeCachePath.toUri());
 116         assertFalse(!file.isDirectory(), "No code cache directory was created!");

 117         assertFalse(file.list().length == 0, "Code cache directory is empty!");
 118     }
 119 
 120     @Test
 121     public void changeUserDirTest() throws ScriptException, IOException {
 122         System.setProperty("nashorn.persistent.code.cache", codeCache);
 123         final NashornScriptEngineFactory fac = new NashornScriptEngineFactory();
 124         final ScriptEngine e = fac.getScriptEngine(ENGINE_OPTIONS_NOOPT);
 125         final Path codeCachePath = getCodeCachePath(false);
 126         final String newUserDir = "build/newUserDir";
 127         // Now changing current working directory
 128         System.setProperty("user.dir", System.getProperty("user.dir") + File.separator + newUserDir);
 129         try {
 130             // Check that a new compiled script is stored in existing code cache
 131             e.eval(code1);
 132             final DirectoryStream<Path> stream = Files.newDirectoryStream(codeCachePath);
 133             checkCompiledScripts(stream, 1);
 134             // Setting to default current working dir
 135         } finally {
 136             System.setProperty("user.dir", oldUserDir);


 157         final NashornScriptEngineFactory fac = new NashornScriptEngineFactory();
 158         final ScriptEngine e = fac.getScriptEngine(ENGINE_OPTIONS_OPT);
 159         final Path codeCachePath = getCodeCachePath(true);
 160         e.eval(code1);
 161         e.eval(code2);
 162         e.eval(code3);// less than minimum size for storing
 163         // adding code1 and code2.
 164         final DirectoryStream<Path> stream = Files.newDirectoryStream(codeCachePath);
 165         checkCompiledScripts(stream, 4);
 166     }
 167 
 168     private static Path getCodeCachePath(final boolean optimistic) {
 169         final String codeCache = System.getProperty("nashorn.persistent.code.cache");
 170         final Path codeCachePath = FileSystems.getDefault().getPath(codeCache).toAbsolutePath();
 171         final String[] files = codeCachePath.toFile().list();
 172         for (final String file : files) {
 173             if (file.endsWith("_opt") == optimistic) {
 174                 return codeCachePath.resolve(file);
 175             }
 176         }
 177         throw new AssertionError("Code cache path not found");
 178     }
 179 
 180     private static void checkCompiledScripts(final DirectoryStream<Path> stream, final int numberOfScripts) throws IOException {
 181         int n = numberOfScripts;
 182         for (@SuppressWarnings("unused") final Path file : stream) {
 183             n--;
 184         }
 185         stream.close();
 186         assertEquals(n, 0);
 187     }
 188 
 189 }


   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package jdk.nashorn.internal.runtime.test;
  26 
  27 import static org.testng.Assert.assertEquals;
  28 import static org.testng.Assert.assertFalse;
  29 import static org.testng.Assert.assertTrue;
  30 import java.io.File;
  31 import java.io.IOException;
  32 import java.nio.file.DirectoryStream;
  33 import java.nio.file.FileSystems;
  34 import java.nio.file.Files;
  35 import java.nio.file.Path;
  36 import javax.script.ScriptEngine;
  37 import javax.script.ScriptException;
  38 import jdk.nashorn.api.scripting.NashornScriptEngineFactory;
  39 import org.testng.annotations.Test;
  40 
  41 /**

  42  * @test
  43  * @bug 8039185 8039403
  44  * @summary  Test for persistent code cache and path handling
  45  * @run testng jdk.nashorn.internal.runtime.test.CodeStoreAndPathTest
  46  */
  47 @SuppressWarnings("javadoc")
  48 public class CodeStoreAndPathTest {
  49 
  50     final String code1 = "var code1; var x = 'Hello Script'; var x1 = 'Hello Script'; "
  51                 + "var x2 = 'Hello Script'; var x3 = 'Hello Script'; "
  52                 + "var x4 = 'Hello Script'; var x5 = 'Hello Script';"
  53                 + "var x6 = 'Hello Script'; var x7 = 'Hello Script'; "
  54                 + "var x8 = 'Hello Script'; var x9 = 'Hello Script'; "
  55                 + "var x10 = 'Hello Script';"
  56                 + "function f() {x ='Bye Script'; x1 ='Bye Script'; x2='Bye Script';"
  57                 + "x3='Bye Script'; x4='Bye Script'; x5='Bye Script'; x6='Bye Script';"
  58                 + "x7='Bye Script'; x8='Bye Script'; var x9 = 'Hello Script'; "
  59                 + "var x10 = 'Hello Script';}"
  60                 + "function g() {x ='Bye Script'; x1 ='Bye Script'; x2='Bye Script';"
  61                 + "x3='Bye Script'; x4='Bye Script'; x5='Bye Script'; x6='Bye Script';"


  96     final String codeCache = "build/nashorn_code_cache";
  97     final String oldUserDir = System.getProperty("user.dir");
  98 
  99     private static final String[] ENGINE_OPTIONS_OPT   = new String[]{"--persistent-code-cache", "--optimistic-types=true"};
 100     private static final String[] ENGINE_OPTIONS_NOOPT = new String[]{"--persistent-code-cache", "--optimistic-types=false"};
 101 
 102     @Test
 103     public void pathHandlingTest() {
 104         System.setProperty("nashorn.persistent.code.cache", codeCache);
 105         final NashornScriptEngineFactory fac = new NashornScriptEngineFactory();
 106 
 107         fac.getScriptEngine(ENGINE_OPTIONS_NOOPT);
 108 
 109         final Path expectedCodeCachePath = FileSystems.getDefault().getPath(oldUserDir + File.separator + codeCache);
 110         final Path actualCodeCachePath = FileSystems.getDefault().getPath(System.getProperty(
 111                             "nashorn.persistent.code.cache")).toAbsolutePath();
 112         // Check that nashorn code cache is created in current working directory
 113         assertEquals(actualCodeCachePath, expectedCodeCachePath);
 114         // Check that code cache dir exists and it's not empty
 115         final File file = new File(actualCodeCachePath.toUri());
 116         assertTrue(file.exists(), "No code cache directory was created!");
 117         assertTrue(file.isDirectory(), "Code cache location is not a directory!");
 118         assertFalse(file.list().length == 0, "Code cache directory is empty!");
 119     }
 120 
 121     @Test
 122     public void changeUserDirTest() throws ScriptException, IOException {
 123         System.setProperty("nashorn.persistent.code.cache", codeCache);
 124         final NashornScriptEngineFactory fac = new NashornScriptEngineFactory();
 125         final ScriptEngine e = fac.getScriptEngine(ENGINE_OPTIONS_NOOPT);
 126         final Path codeCachePath = getCodeCachePath(false);
 127         final String newUserDir = "build/newUserDir";
 128         // Now changing current working directory
 129         System.setProperty("user.dir", System.getProperty("user.dir") + File.separator + newUserDir);
 130         try {
 131             // Check that a new compiled script is stored in existing code cache
 132             e.eval(code1);
 133             final DirectoryStream<Path> stream = Files.newDirectoryStream(codeCachePath);
 134             checkCompiledScripts(stream, 1);
 135             // Setting to default current working dir
 136         } finally {
 137             System.setProperty("user.dir", oldUserDir);


 158         final NashornScriptEngineFactory fac = new NashornScriptEngineFactory();
 159         final ScriptEngine e = fac.getScriptEngine(ENGINE_OPTIONS_OPT);
 160         final Path codeCachePath = getCodeCachePath(true);
 161         e.eval(code1);
 162         e.eval(code2);
 163         e.eval(code3);// less than minimum size for storing
 164         // adding code1 and code2.
 165         final DirectoryStream<Path> stream = Files.newDirectoryStream(codeCachePath);
 166         checkCompiledScripts(stream, 4);
 167     }
 168 
 169     private static Path getCodeCachePath(final boolean optimistic) {
 170         final String codeCache = System.getProperty("nashorn.persistent.code.cache");
 171         final Path codeCachePath = FileSystems.getDefault().getPath(codeCache).toAbsolutePath();
 172         final String[] files = codeCachePath.toFile().list();
 173         for (final String file : files) {
 174             if (file.endsWith("_opt") == optimistic) {
 175                 return codeCachePath.resolve(file);
 176             }
 177         }
 178         throw new AssertionError("Code cache path not found: " + codeCachePath.toString());
 179     }
 180 
 181     private static void checkCompiledScripts(final DirectoryStream<Path> stream, final int numberOfScripts) throws IOException {
 182         int n = numberOfScripts;
 183         for (@SuppressWarnings("unused") final Path file : stream) {
 184             n--;
 185         }
 186         stream.close();
 187         assertEquals(n, 0);
 188     }
 189 
 190 }
< prev index next >