[英]Is it possible to close a stream when performing Selenium-rc programmatically?
I am trying to run multiple testsuites for Selenium-rc programmatically with Selenese. 我正在尝试使用Selenese以编程方式为Selenium-rc运行多个测试套件。 I analyzed the java code for the selenium-server-standalone-2.37.0.jar
and determined that I had to use the main method of the GridLauncher
class. 我分析了selenium-server-standalone-2.37.0.jar
的Java代码,并确定我必须使用GridLauncher
类的main方法。 And because I want to run multiple test suites, i put in a loop. 而且因为我想运行多个测试套件,所以我陷入了循环。
for (String[] arguments : argumentsList) {
GridLauncher.main(arguments);
}
The first problem I encountered was that GridLauncher
uses System.exit()
to close itself. 我遇到的第一个问题是GridLauncher
使用System.exit()
关闭自身。 But that causes my process to stop also. 但这也会导致我的过程停止。 The way that I solved this was to make the System.exit()
throw an exception. 我解决此问题的方法是使System.exit()
引发异常。 This idea I got from this question in Stackoverflow. 我从Stackoverflow 这个问题中得到的想法。
private static class ExitTrappedException extends SecurityException {
static void forbidSystemExitCall() {
final SecurityManager securityManager = new SecurityManager() {
@Override
public void checkPermission( Permission permission ) {
if( "exitVM".equals( permission.getName() ) ) {
throw new ExitTrappedException() ;
}
}
@Override
public void checkExit(int status) {
throw new SecurityException();
}
} ;
System.setSecurityManager( securityManager ) ;
}
static void enableSystemExitCall() {
System.setSecurityManager( null ) ;
}
}
for (String[] arguments : argumentsList) {
ExitTrappedException.forbidSystemExitCall();
try {
GridLauncher.main(arguments);
} catch (SecurityException se) {
//catching System.exit()
} finally {
ExitTrappedException.enableSystemExitCall() ;
}
}
The problem that I encounter now is that the stream doesn't close and i get an exception when the loop tries to perform the Selenium test: WARN - Failed to start: SocketListener1@0.0.0.0:4444
I tried to look for ways to close the stream but can't find anything. 我现在遇到的问题是流没有关闭,并且在循环尝试执行Selenium测试时出现异常WARN - Failed to start: SocketListener1@0.0.0.0:4444
我试图寻找关闭方法流,但找不到任何东西。 Is it possible to close it? 是否可以关闭它? If so, how? 如果是这样,怎么办?
You might want to try garbage-collecting the streams and have them finalized (as such classes tend to have a finalize() method which releases resources). 您可能想要尝试对流进行垃圾收集并将其终结(因为此类倾向于使用finalize()方法来释放资源)。 You would basically System.gc()
followed by System.runFinalization()
. 您基本上将在System.gc()
之后跟随System.runFinalization()
。 For details see this question: How to ensure finalize() is always called (Thinking in Java exercise) 有关详细信息,请参见以下问题: 如何确保始终调用finalize()(Java练习中的思考)
You may also need to get rid of some threads before you do System.gc()
. 在执行System.gc()
之前,您可能还需要摆脱一些线程。 See question Get a List of all Threads currently running in Java for how to list threads. 有关如何列出线程,请参阅问题“ 获取Java中当前正在运行的所有线程的列表”。 Another question discusses how to stop a thread: How do you kill a thread in Java? 另一个问题讨论了如何停止线程: 如何在Java中杀死线程? . 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.