[英]JRuby Embedding prob: Puts appears to fails with RaiseException: (Errno::EBADF) Bad file descriptor
I am attempting to eval a script after replacing hte default out with my own PrintStream. 我尝试用我自己的PrintStream替换默认默认值后评估脚本。
ScriptingContainer container = new ScriptingContainer();
container.setOut( my new output target printstream);
container.runScriptlet("puts \"*value*\"";);
Boring wrapper Stacktrace 无聊的包装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)
Interesting wrapped exception 有趣的包装异常
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)
Note my PrintStream simply prints to a StringBuilder which i then assert against in the test. 请注意,我的PrintStream只是打印到StringBuilder,然后在测试中针对它进行断言。 The PrintStream.checkError() always returns false and given it appends to a StringBuilder it never fails.
PrintStream.checkError()始终返回false,并且将其附加到StringBuilder,它永远不会失败。
Each test does cleanup doing the following to the ScriptingContainer. 每个测试都会对ScriptingContainer执行以下清理操作。 I added the resetXXX calls just to be nice and the problem persists with or without them.
我添加了resetXXX调用只是为了很好,无论是否安装它们,问题仍然存在。
final ScriptingContainer container = ...
container.resetWriter();
container.resetErrorWriter();
container.terminate();
Update It would appear that running the test alone, everything works, but running the test with puts after another fails because something is screwed. 更新看来,单独运行测试可以正常工作,但是由于某些事情被搞砸了,因此一次又一次地使用puts运行测试失败。 THe strange thing is why is it screwed given i am resetting the writers and terminating the container.
奇怪的是,鉴于我要重置作家并终止容器,为什么要拧紧它。 Is there anything else i need to kill when i tear down Jruby between tests ?
在两次测试之间拆解Jruby时,我还需要杀死其他东西吗?
It would appear my tests were not shutting down my ScriptingContainer. 看来我的测试没有关闭我的ScriptingContainer。 Dumb bug.
傻虫。 That said ScriptingContainer.resetWriter() and ScriptingContainer.resetError() dont really work.
那就是说ScriptingContainer.resetWriter()和ScriptingContainer.resetError()确实不起作用。
public class JRubyShellPutsProblem { 公共类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();
}
}
} }
This works for me (JRuby 1.6.2, Java 1.6.0): 这对我有效(JRuby 1.6.2,Java 1.6.0):
import org.jruby.embed.*;
class HelloWorldApp {
public static void main(String[] args) {
ScriptingContainer container = new ScriptingContainer();
try{
container.setOutput(new java.io.PrintStream("log.txt"));
}
catch (Exception e)
{
System.out.println(e);
}
container.runScriptlet("puts \"*value*\"");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.