Skip to content

Commit

Permalink
Fix process resource release issue
Browse files Browse the repository at this point in the history
  • Loading branch information
hydraxman committed Sep 17, 2023
1 parent 9d369ae commit 3d85bc4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,21 @@ public SmartTestUtil(String location) {
stringFolderPath = testBaseDir.getAbsolutePath() + "/" + Const.SmartTestConfig.STRING_FOLDER_NAME
+ "/";

try {
InputStream resourceAsStream = FileUtils.class.getClassLoader().getResourceAsStream(name);
File smartTestZip = new File(testBaseDir, name);
File smartTestFolder = new File(testBaseDir, folderName);
if (smartTestZip.exists()) {
FileUtil.deleteFileRecursively(smartTestZip);
}
if (smartTestFolder.exists()) {
FileUtil.deleteFileRecursively(smartTestFolder);
}

try (InputStream resourceAsStream = FileUtils.class.getClassLoader().getResourceAsStream(name);
OutputStream out = new FileOutputStream(smartTestZip)) {
if (resourceAsStream == null) {
return;
}
File smartTestZip = new File(testBaseDir, name);
File smartTestFolder = new File(testBaseDir, folderName);
if (smartTestZip.exists()) {
FileUtil.deleteFileRecursively(smartTestZip);
}
if (smartTestFolder.exists()) {
FileUtil.deleteFileRecursively(smartTestFolder);
}
OutputStream out = new FileOutputStream(smartTestZip);
IOUtils.copy(Objects.requireNonNull(resourceAsStream), out);
out.close();
FileUtil.unzipFile(smartTestZip.getAbsolutePath(), testBaseDir.getAbsolutePath());
if (smartTestZip.exists()) {
FileUtil.deleteFileRecursively(smartTestZip);
Expand Down Expand Up @@ -83,13 +82,21 @@ public String runPYFunction(SmartTestParam smartTestParam, Logger logger) throws
for (String tempArg : runArgs) {
logger.info(tempArg);
}

Process proc = Runtime.getRuntime().exec(runArgs);
SmartTestLog err = new SmartTestLog(proc.getErrorStream(), logger);
SmartTestLog out = new SmartTestLog(proc.getInputStream(), logger);
err.start();
out.start();
res = out.getContent();
proc.waitFor();

try (InputStream errorInput = proc.getErrorStream();
InputStream inputStream = proc.getInputStream()) {
SmartTestLog err = new SmartTestLog(errorInput, logger);
SmartTestLog out = new SmartTestLog(inputStream, logger);
err.start();
out.start();
proc.waitFor();
res = out.getContent();
} finally {
proc.destroy();
}


return res;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,9 @@ private ArrayList<String> runXctest(TestRunDevice testRunDevice, Logger logger,
String command = String.format(commFormat, deviceId, resultPath);
testRun.addNewTimeTag("testRunStarted", System.currentTimeMillis() - testRun.getTestStartTimeMillis());
ArrayList<String> result;
Process proc = null;
try {
Process proc = Runtime.getRuntime().exec(command);
proc = Runtime.getRuntime().exec(command);
XCTestCommandReceiver err = new XCTestCommandReceiver(proc.getErrorStream(), logger);
XCTestCommandReceiver out = new XCTestCommandReceiver(proc.getInputStream(), logger);
err.start();
Expand All @@ -126,6 +127,10 @@ private ArrayList<String> runXctest(TestRunDevice testRunDevice, Logger logger,
testRunDeviceOrchestrator.addGifFrameAsyncDelay(testRunDevice, agentManagementService.getScreenshotDir(), 0, logger);
} catch (Exception e) {
throw new RuntimeException("Execute XCTest failed");
} finally {
if (proc != null) {
proc.destroy();
}
}

if (result == null) {
Expand Down

0 comments on commit 3d85bc4

Please sign in to comment.