Silk Test Open Agent locks trace file - java

We have testing java application.
This application performs different types of testing. On one step it starts Silk Test.
This application writes a lot of traces using System.out.println.
We redirect this traces to file app.trace in our cmd file.
Something like:
java com.test.app > app.trace
When this testing application stops it is not possible to remove app.trace file because it is locked by Silk Test Open Agent.
I do not understand how this application can lock our trace file.
We do not start this application directly from our code.
We use Silk4J lib to start Silk Test.
As far as I know this library connects to Silk Test windows service which starts Silk Test Open Agent.
Does anyone can explain me - why and how Silk Test Open Agent locks our trace file?

The cause of this is because Open Agent does not close down when the test finishes. I just kill Open Agent when my suite is done.
public class ProcessKill {
public void killOpenAgent () {
kill ("openAgent.exe");
}
public void kill (String processName) {
String command = "cmd /c taskkill";
String parameter = " /F /IM " + processName;
System.out.println("Killing process: " + processName);
try {
Runtime.getRuntime().exec(command + parameter);
} catch (IOException e) {
e.printStackTrace();
}
}
}
I am using TestNG to control my tests, so I just call this from the #AfterSuite method to always make sure that Open Agent is killed after each run. This also helps to release the licence.

The reason is that child processes inherit the open files of their parents, in this case the redirected output stream to the file. This makes sense, as is allows you to also capture the output of those child processes.
However, otherwise than David Genrich suggested in his answer, I would not forcefully kill the agent as it may then fail to release some resources and clean up properly. This might lead to follow-up problems.
I suggest separately starting the OpenAgent before you run your tests so it's not a child process of the test runner.

Related

Cannot run program "START": error=2, No such file or directory while trying to kill Chrome process in MAC

My IDE is eclipse and I'm using Selenium Webdriver on MAC. Every time I execute my code below, I want to kill/close the previously opened browser and then start a new instance and leave it on. As a result, only one instance of browser should be open at a time. I am not doing this for testing purpose. I'm just doing this because I want to automate a task.
This is the error I am getting everytime I execute my code:
Cannot run program "START": error=2, No such file or directory
This is my code:
public class Demo {
public static void main(String[] args){
System.setProperty("webdriver.chrome.driver", "/Users/Downloads/chromedriver/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com/");
try{
Runtime.getRuntime().exec("START taskkill /F /IM chrome.exe");
System.exit(0);
}
catch(IOException io){
System.out.println(io.getMessage());
}
}
}
This is working fine on windows. How do I fix this on MAC/unix/ environment or what changes should I make in my code?
I was told this error is something to do with admin privileges so I should run eclipse as an admin. So I did this: sudo open Eclipse.app/ . So the eclipse opened as admin and executed the code but I was still getting the same error.
As per your question using the following line of code :
Runtime.getRuntime().exec("taskkill /F /IM chrome.exe");
to kill the Browser Client process will be against all the Best Practices.
While automating through Selenium as per the best practices you should invoke the quit() method within the tearDown() {}. As per the WebDriver W3C Editor's Draft invoking quit() DELETEs the current browsing session through sending "quit" command with {"flags":["eForceQuit"]} and finally sends the GET request on /shutdown EndPoint.
As an example incase of GeckoDriver (W3C compliant WebDriver variant) here are the sequence of events :
1503397488598 webdriver::server DEBUG -> DELETE /session/8e457516-3335-4d3b-9140-53fb52aa8b74
1503397488607 geckodriver::marionette TRACE -> 37:[0,4,"quit",{"flags":["eForceQuit"]}]
1503397488821 webdriver::server DEBUG -> GET /shutdown
So on invoking quit() method, the Web Client session and the WebDriver instance gets killed completely. Hence you don't have to incorporate any additional steps which will be an overhead.
Solution
Replace the line of code :
Runtime.getRuntime().exec("taskkill /F /IM chrome.exe");
With :
driver.quit();
Here you can find a detailed discussion on Selenium : How to stop geckodriver process impacting PC memory, without calling driver.quit()?
Additional Considerations
Ensure that the WebDriver Binary is having executable permission for non-root users. (chmod 777)
Ensure that the WebDriver Binary is present in the specified location.
Execute your Test as a non-root user.
Always invoke driver.quit() within tearDown(){} method to close & destroy the WebDriver and Web Client instances gracefully.

JxBrowser with Java Swing (IntelliJ plugin) - "Received signal 10 BUS_ADRERR"

I'm writing an IntelliJ plugin, and attempting to integrate JxBrowser into the plugin's tool window via Java Swing.
I'm using the toolWindow extension to keep the tool window integration simple.
plugin.xml
<extensions defaultExtensionNs="com.intellij">
<toolWindow id="pluginid" anchor="right" factoryClass="com.solutionloft.codeclippy.MainWindowFactory" />
</extensions>
And so my main factory class looks like this:
public class MainWindowFactory implements ToolWindowFactory {
#Override
public void createToolWindowContent(#NotNull Project project, #NotNull ToolWindow toolWindow) {
Browser browser = new Browser();
BrowserView view = new BrowserView(browser);
Content content = toolWindow.getContentManager().getFactory().createContent(view, "", false);
toolWindow.getContentManager().addContent(content);
browser.loadHTML("<html><body><h1>Hello World!</h1></body></html>");
}
}
This appears to work when I run the plugin locally initially (the tool window comes up, and I can see Hello World), but if I terminate the process and then try to run it again, I run into this error:
Received signal 10 BUS_ADRERR 000103bc3000
[0x00017cd9540c]
[0x00017cd95301]
[0x7fff572eef5a]
[0x7fbe7e9f5000]
[end of stack trace]
Process finished with exit code 138 (interrupted by signal 10: SIGBUS)
Am I missing some kind of cleanup step? I'm not sure what could still be running - the only workaround I've found at this point is to do a full computer restart, so I guess some process must be still running that's causing it to conflict. What's the proper way to clean up? Does it have anything to do with browser.dispose()? I haven't had much luck finding documentation on when .dispose() would be appropriate / if it's needed.
I'm using:
* macOS High Sierra
* Java 1.8.0_151 as my JDK
* PyCharm Ultimate as my JRE
Thanks!
Update: Noticed if I kill this process /System/Library/Frameworks/LocalAuthentication.framework/Support/coreauthd, the problem goes away for the next few runs. But sometimes this process doesn't exist and killing a still-running java process is the fix... odd.
According to TeamDev support, the solution is to set the system property jxbrowser.ipc.external=true. Calling System.setProperty("jxbrowser.ipc.external", "true") before you create your browser instance should do the trick. The catch is that the JxBrowser will run in lightweight mode.
You may also ensure that you're properly disposing all browser instances via browser.dispose() and the Chromium engine via BrowserCore.shutdown().
According to the article, all browser instances should disposed when you don't need them. Please try disposing all browser instances before closing your application.

Terminating an AWS Code Deployment with Java jar return value

I have an aws instance running with webservices that I want to test with a java application running JUnit. If I have a script that runs the jar during the validate services step of the code deploy process how does the code deployment handle the return value of the jar if a test fails?
Is it required that I catch exceptions thrown by the jar for the failing tests and call system.exit(-1) to have the script terminate the code deployment or does the exception thrown automatically give the return value of non-zero?
If you are running JUnit using JunitCore, it should return a non-zero exit code if any of the tests fail.
main
public static void main(String... args)
Run the tests contained in the classes named in the args. If all tests run successfully, exit with a status of 0. Otherwise exit with a status of 1. Write feedback while tests are running and write stack traces for all failed tests after the tests all complete.
Parameters:
args - names of classes in which to find tests to run
From: http://junit.org/apidocs/org/junit/runner/JUnitCore.html#main%28java.lang.String...%29

How to Terminate a Process Normally Created using ProcessBuilder

I am creating Processes using ProcessBuilder in my Java Application. The created process executes some FFMPEG commands which actually copy the RTSP streams in specified destination media file.
ProcessBuilder builder = new ProcessBuilder("ffmpeg", "-i", RTSP_URL, "-f", fileFormat, destFilePath);
Process processToExecute = builder.start();
I want to close the process before it completes its execution. So, If I run this FFMPEG command directly in windows CMD and then press 'CTRL+C' after 5 seconds then process get terminates with status '2'. And I can play the media file created so far.
So, If I do the same operation in my Java Application using:
process.destroy(); //I call this method after 5 sec
I get the status code '1' which means abnormal termination. I get the status by the following way:
processToExecute.destroy();
processToExecute.exitValue(); //This return me status '1'
And I can't play the media file and I think this is due to the abnormal termination of the process.
So how I can terminate the process created using ProcessBuilder in the same way we do in CMD with (CTRL+C) so that I may play the created media file ?
I want to terminate process (created using ProcessBuilder) in Java Application with status code of '2' that I get when I terminate process using CMD.
EDIT#01: --- Sharing Findings
So, when I try to delete that file once app terminates, I get the following error:
The Action Can't be Performed Because File is Opened in FFMPEG.exe
Which means that process is not terminating the command it is executing. That command still has occupied this file that's why I am not getting able to play it. Process gets terminate when I call:
processToExecute.destroy();
But, the task it is performing (that is execution of a command) is still active. Strange!!!!
EDIT#02: Sharing Ultimate Reason
Actually If I directly press 'CTRL+C' or 'q' in cmd when process is running then it terminates the process successfully and this process is no more visible in the currently executing processes lists.
And Programatically when I call method:
cmd> processToExecute.destroy();
It terminates the process but when I see the list of currently executing processes I can still see them over there.
And same scenario exists If I try to terminate this process using 'taskkill' or 'kill' command in another CMD by specifying their's name or pid that still process terminates abnormally.
P.S. I use the following command to see the running processes:
tasklist
So from this it proves that destroy() method from Application and 'taskkill or kill' command from another CMD is not terminating the process normally that pressing 'CTRL+C' and 'q' does.
Maybe try...
builder.inheritIO();
System.exit(2);
Or you could try to write to the stdin of the process...
process.getInputStream().write(exitCode);

Java process when terminated abnormally with Kill or pkill unix comamnds doesnot delete temporary files

When a tool developed in Java is launched, it creates temporary files in a folder. If terminated properly those files are getting deleted , but if terminated with kill or pkill commands those files are not getting deleted. Is there any way to send a signal to java process to delete those files before terminating the process?
Please help me to solve this issue.
Thanks in Advance
It seems like File.deleteOnExit() is fragile when it comes to process termination. In contrast, using the NIO API with the StandardOpenOption.DELETE_ON_CLOSE seems to be more reliable even though it’s specification only says: “If the close method is not invoked then a best effort attempt is made to delete the file when the Java virtual machine terminates”
E.g. when running the following program:
File f1=File.createTempFile("deleteOnExit", ".tmp");
f1.deleteOnExit();
final Path f2 = Files.createTempFile("deleteOnClose", ".tmp");
FileChannel ch = FileChannel.open(f2, StandardOpenOption.DELETE_ON_CLOSE);
System.out.println(f1);
System.out.println(f2);
LockSupport.parkNanos(Long.MAX_VALUE);
// the following statement is never reached, but it’s here to avoid
// early cleanup of the channel by garbage collector
ch.close();
and killing the process while it hangs at parkNanos, the JVM leaves the deleteOnExit tmp file while correctly deleting the deleteOnClose file on my machine.
You can add shutdown hook and clean everything you need explicitly.
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
//put your shutdown code here
}
});
This is actually the same what java.io.File#deleteOnExit does for you.

Categories