Skip to content

Commit

Permalink
Add the logger to TestRunDevice to get prepared for next stage refact…
Browse files Browse the repository at this point in the history
…or (#430)

* Add the logger to TestRunDevice to get prepared for next level refactor

* update createTestRun

* Update assert
  • Loading branch information
hydraxman authored Apr 17, 2023
1 parent 5b441f8 commit 0c97b3c
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,16 @@ public DeviceTaskControl runForAllDeviceAsync(Collection<TestRunDevice> allDevic
log.info("RunForAllDeviceAsync: [BUSY] device is testing: {}", testRunDeviceOrchestrator.getName(device));
continue;
}
Logger logger = null;
if (logging) {
logger = testRunDeviceOrchestrator.getDeviceLogger(device);
device.setLogger(testRunDeviceOrchestrator.getDeviceLogger(device));
}
final Logger fLogger = logger;
devices.add(device);
Runnable run = () -> {
try {
if (logging) {
DeviceTaskControlExecutor.log.info("start do task: {}", testRunDeviceOrchestrator.getName(device));
}
task.doTask(device, fLogger);
task.doTask(device);
} catch (Exception e) {
DeviceTaskControlExecutor.log.error(e.getMessage(), e);
} finally {
Expand All @@ -96,7 +94,7 @@ public DeviceTaskControl runForAllDeviceAsync(Collection<TestRunDevice> allDevic
}

public interface DeviceTask {
boolean doTask(TestRunDevice testRunDevice, Logger logger) throws Exception;
boolean doTask(TestRunDevice testRunDevice) throws Exception;
}

public interface TaskCompletion {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@ public TestRunner(AgentManagementService agentManagementService, TestTaskRunCall
this.testRunDeviceOrchestrator = testRunDeviceOrchestrator;
}

public void runTestOnDevice(TestTask testTask, TestRunDevice testRunDevice, Logger logger) {
public void runTestOnDevice(TestTask testTask, TestRunDevice testRunDevice) {
checkTestTaskCancel(testTask);
logger.info("Start running tests {}, timeout {}s", testTask.getTestSuite(), testTask.getTimeOutSecond());
Assert.notNull(testRunDevice.getLogger(), "testRunDevice.getLogger() is null, but it's required for a test run");
testRunDevice.getLogger().info("Start running tests {}, timeout {}s", testTask.getTestSuite(), testTask.getTimeOutSecond());

TestRun testRun = createTestRun(testRunDevice, testTask, logger);
TestRun testRun = createTestRun(testRunDevice, testTask);
checkTestTaskCancel(testTask);

try {
Expand Down Expand Up @@ -103,10 +104,11 @@ protected void checkTestTaskCancel(TestTask testTask) {
Assert.isFalse(testTask.isCanceled(), "Task {} is canceled", testTask.getId());
}

protected TestRun createTestRun(TestRunDevice testRunDevice, TestTask testTask, Logger parentLogger) {
protected TestRun createTestRun(TestRunDevice testRunDevice, TestTask testTask) {
TestRun testRun = new TestRun(testRunDeviceOrchestrator.getSerialNum(testRunDevice), testRunDeviceOrchestrator.getName(testRunDevice), testTask.getId());
testRun.setDevice(testRunDevice);
File testRunResultFolder = new File(testTask.getResourceDir(), testRunDevice.getDeviceInfo().getSerialNum());
Logger parentLogger = testRunDevice.getLogger();
parentLogger.info("DeviceTestResultFolder {}", testRunResultFolder);
if (!testRunResultFolder.exists()) {
if (!testRunResultFolder.mkdirs()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ public TestTask runTestTask(TestTask testTask) {
DeviceTaskControl deviceTaskControl = deviceTaskControlExecutor.runForAllDeviceAsync(chosenDevices,
new DeviceTaskControlExecutor.DeviceTask() {
@Override
public boolean doTask(TestRunDevice testRunDevice, Logger logger) throws Exception {
runner.runTestOnDevice(testTask, testRunDevice, logger);
public boolean doTask(TestRunDevice testRunDevice) throws Exception {
runner.runTestOnDevice(testTask, testRunDevice);
return false;
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ public void createTestRunnerAndInitDeviceTest() {
testTask.setResourceDir(resourceDir);
testTask.setTestSuite("TestSuite");

TestRun testRun = espressoRunner.createTestRun(new TestRunDevice(deviceInfo, deviceInfo.getType()), testTask, logger);
TestRunDevice testRunDevice = new TestRunDevice(deviceInfo, deviceInfo.getType());
testRunDevice.setLogger(logger);
TestRun testRun = espressoRunner.createTestRun(testRunDevice, testTask);

testRun.getLogger().info("Test TestRun logging function");
testRun.getLogger().info("TestRun InstrumentReportPath {}", testRun.getInstrumentReportPath());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import lombok.Getter;
import lombok.Setter;
import org.openqa.selenium.WebDriver;
import org.slf4j.Logger;

import java.io.File;

Expand All @@ -25,6 +26,7 @@ public class TestRunDevice {
private File gifFile;

private WebDriver webDriver;
private transient Logger logger;

public TestRunDevice(DeviceInfo deviceInfo, String tag) {
this.deviceInfo = deviceInfo;
Expand Down

0 comments on commit 0c97b3c

Please sign in to comment.