Last active
February 20, 2017 11:55
-
-
Save joaovarandas/f80a9cb5548a9d620e4da1ace2729911 to your computer and use it in GitHub Desktop.
Nashorn Thread Safety Test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var cl = (function() { | |
var scope = {}; | |
var x = 0; | |
var _id = Math.random() * 999999; | |
scope.plus = function(v) { | |
x = x + v; | |
return x; | |
}; | |
scope.getId = function() { | |
return _id; | |
}; | |
return scope; | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test.com.inpaas.nashorn; | |
import javax.script.ScriptEngine; | |
import javax.script.ScriptEngineManager; | |
import javax.script.ScriptException; | |
import jdk.nashorn.api.scripting.ScriptObjectMirror; | |
import org.junit.Assert; | |
import org.junit.Before; | |
import org.junit.Test; | |
public class NashornThreadTest { | |
private ScriptEngine e; | |
private ScriptObjectMirror fn; | |
@Before | |
public void prepare() { | |
try { | |
ScriptEngineManager m = new ScriptEngineManager(); | |
this.e = m.getEngineByName("nashorn"); | |
this.fn = (ScriptObjectMirror) e.eval("load('classpath:test/com/inpaas/nashorn/closure-jstest.js'); cl;"); | |
} catch (Exception e) { | |
throw new RuntimeException("Could not prepare test because: " + e.getMessage(), e); | |
} | |
} | |
@Test | |
public void singleThread() throws ScriptException { | |
Object v = e.eval("var x = cl(); x.plus(2);"); | |
Assert.assertTrue("number is not Double.", v instanceof Double); | |
Assert.assertEquals("value is not 2.", 2.0, ((Number) v).doubleValue(), 0.0); | |
v = e.eval("x.plus(2);"); | |
Assert.assertEquals("value is not 4.", 4.0, ((Number) v).doubleValue(), 0.0); | |
ScriptObjectMirror y = (ScriptObjectMirror) e.eval("cl();"); | |
double v_original = ((Number) v).doubleValue(); | |
v = y.callMember("plus", v); | |
Assert.assertEquals("value is not equal to "+ v_original, v_original, ((Number) v).doubleValue(), 0.0); | |
v = e.eval("x.plus(2);"); | |
Assert.assertEquals("value is not 6.", 6.0, ((Number) v).doubleValue(), 0.0); | |
} | |
@Test | |
public void multiThread() throws ScriptException, InterruptedException { | |
Thread[] t = new Thread[10]; | |
ScriptObjectMirror[] y = new ScriptObjectMirror[10]; | |
double[] d = new double[10]; | |
double[] id = new double[10]; | |
double v = 0.0; | |
for(int j = 0; j < 1000; j++) { | |
v+=j; | |
} | |
for (int i = 0; i < t.length; i++) { | |
final int fi = i; | |
t[i] = new Thread(() -> { | |
try { | |
y[fi] = (ScriptObjectMirror) e.eval("cl();"); | |
for(int j = 0; j < 1000; j++) { | |
d[fi] = (double) y[fi].callMember("plus", j); | |
Thread.sleep(10); | |
} | |
} catch(Exception e) { | |
d[fi] = fi; | |
} | |
}); | |
t[i].start(); | |
} | |
for (int i = 0; i < t.length; i++) { | |
t[i].join(); | |
} | |
for (int i = 1; i < t.length; i++) { | |
id[i] = (double) y[i].callMember("getId"); | |
Assert.assertNotEquals("ids should be different from each closure.", id[i], id[i-1], 0.0); | |
Assert.assertEquals("values should be equal to sample from all closures.", v, d[i], 0.0); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment