我尝试用我自己的PrintStream替换默认默认值后评估脚本。
ScriptingContainer container = new ScriptingContainer();
container.setOut( my new output target printstream);
container.runScriptlet("puts \"*value*\"";);
无聊的包装Stacktrace
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.jruby.exceptions.RaiseException: (Errno::EBADF) Bad file descriptor
org.jruby.embed.EvalFailedException: (Errno::EBADF) Bad file descriptor
at org.jruby.embed.internal.EmbedEvalUnitImpl.run(EmbedEvalUnitImpl.java:127)
at org.jruby.embed.ScriptingContainer.runUnit(ScriptingContainer.java:1231)
at org.jruby.embed.ScriptingContainer.runScriptlet(ScriptingContainer.java:1224)
... my boring test case stuff
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at junit.framework.TestCase.runTest(TestCase.java:168)
at junit.framework.TestCase.runBare(TestCase.java:134)
有趣的包装异常
org.jruby.exceptions.RaiseException: (Errno::EBADF) Bad file descriptor
at org.jruby.RubyIO.write(org/jruby/RubyIO.java:1319)
at org.jruby.RubyIO.write(org/jruby/RubyIO.java:2297)
at org.jruby.RubyIO.puts(org/jruby/RubyIO.java:2252)
at org.jruby.RubyKernel.puts(org/jruby/RubyKernel.java:522)
at #<Class:0x101e4f266>.(root)(<script>:1)
请注意,我的PrintStream仅打印到StringBuilder,然后在测试中针对它进行断言。 PrintStream.checkError()始终返回false,并且将其附加到StringBuilder,它永远不会失败。
每个测试都会对ScriptingContainer执行以下清理操作。我添加了resetXXX调用只是为了很好,无论是否安装它们,问题仍然存在。
final ScriptingContainer container = ...
container.resetWriter();
container.resetErrorWriter();
container.terminate();
更新资料
似乎单独运行测试,一切正常,但是由于某些事情被搞砸了,因此一次又一次地执行puts测试失败。奇怪的是,鉴于我要重置作家并终止容器,为什么要拧紧它。在两次测试之间拆解Jruby时,我还需要杀死其他东西吗?
最佳答案
看来我的测试没有关闭我的ScriptingContainer。傻虫。那就是说ScriptingContainer.resetWriter()和ScriptingContainer.resetError()确实不起作用。
公共类JRubyShellPutsProblem {
public static void main(String[] args) {
final StringBuilder b = new StringBuilder();
for (int i = 0; i < 3; i++) {
final ScriptingContainer container = new ScriptingContainer(LocalContextScope.CONCURRENT, LocalVariableBehavior.PERSISTENT);
final PrintStream printStream = PrintStreams.printer(Printers.stringBuilder(b), LineEnding.CR, Charset.defaultCharset());
container.setOutput(printStream);
container.setError(printStream); // yes same as out, still works.
container.runScriptlet("puts \"hello" + i + "\"\n");
printStream.flush();
System.out.println(b.toString());
// container.resetWriter(); <-- blows up
// container.resetErrorWriter();
container.terminate();
}
}
}