Skip to content

Commit 35be2ad

Browse files
hydraxmanshbu
andauthored
Add TestRunThreadContext and move ITestRun to the same package. (#233)
* Add TestRunThreadContext and move ITestRun to the same package. * Reserve the method invoke point * add copy right Co-authored-by: shbu <shaopeng@microsoft.com>
1 parent 519e744 commit 35be2ad

File tree

5 files changed

+45
-6
lines changed

5 files changed

+45
-6
lines changed

agent/src/main/java/com/microsoft/hydralab/agent/runner/TestRunner.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public void runTestOnDevice(TestTask testTask, DeviceInfo deviceInfo, Logger log
5757

5858
private void runByFutureTask(DeviceInfo deviceInfo, TestTask testTask, TestRun testRun) throws Exception {
5959
FutureTask<String> futureTask = new FutureTask<>(() -> {
60+
initTestRunThreadContext(testRun);
6061
run(deviceInfo, testTask, testRun);
6162
return null;
6263
});
@@ -74,6 +75,14 @@ private void runByFutureTask(DeviceInfo deviceInfo, TestTask testTask, TestRun t
7475
}
7576
}
7677

78+
/**
79+
* TODO Call {@link TestRunThreadContext#init(ITestRun)}
80+
* This method must be called in the test run execution thread.
81+
*/
82+
private void initTestRunThreadContext(TestRun testRun) {
83+
84+
}
85+
7786
private static void saveErrorSummary(TestRun testRun, Exception e) {
7887
String errorStr = e.getClass().getName() + ": " + e.getMessage();
7988
if (errorStr.length() > 255) {
@@ -201,7 +210,7 @@ protected void reInstallApp(DeviceInfo deviceInfo, TestTask testTask, Logger rep
201210
}
202211

203212
protected void reInstallTestApp(DeviceInfo deviceInfo, TestTask testTask, Logger reportLogger) throws Exception {
204-
if(!shouldInstallTestPackageAsApp()){
213+
if (!shouldInstallTestPackageAsApp()) {
205214
return;
206215
}
207216
if (testTask.getTestAppFile() == null) {

common/src/main/java/com/microsoft/hydralab/common/entity/common/TestRun.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import com.alibaba.fastjson.JSONArray;
66
import com.alibaba.fastjson.JSONObject;
7-
import com.microsoft.hydralab.ITestRun;
7+
import com.microsoft.hydralab.agent.runner.ITestRun;
88
import com.microsoft.hydralab.common.util.Const;
99
import lombok.Data;
1010
import org.slf4j.Logger;

common/src/main/java/com/microsoft/hydralab/performance/PerformanceTestManagementService.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
13
package com.microsoft.hydralab.performance;
24

3-
import com.microsoft.hydralab.ITestRun;
5+
import com.microsoft.hydralab.agent.runner.ITestRun;
6+
import com.microsoft.hydralab.agent.runner.TestRunThreadContext;
47
import com.microsoft.hydralab.performance.inspectors.AndroidBatteryInfoInspector;
58
import org.jetbrains.annotations.NotNull;
69
import org.springframework.util.Assert;
@@ -52,11 +55,10 @@ private static PerformanceTestResult createPerformanceTestResult(PerformanceInsp
5255
}
5356

5457
/**
55-
* TODO
5658
* @return the test run object from TestRunThreadContext
5759
*/
5860
private ITestRun getTestRun() {
59-
return null;
61+
return TestRunThreadContext.getTestRun();
6062
}
6163

6264
@Override

sdk/src/main/java/com/microsoft/hydralab/ITestRun.java renamed to sdk/src/main/java/com/microsoft/hydralab/agent/runner/ITestRun.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.microsoft.hydralab;
1+
package com.microsoft.hydralab.agent.runner;
22

33
import java.io.File;
44

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
package com.microsoft.hydralab.agent.runner;
4+
5+
/**
6+
* We will gradually deprecate ThreadParam and AppiumParam, and migrate to this.
7+
*
8+
*/
9+
public class TestRunThreadContext {
10+
private static final InheritableThreadLocal<ITestRun> testRunThreadLocal = new InheritableThreadLocal<>();
11+
12+
/**
13+
* Should be called in the TestRunner setup lifecycle
14+
* @param testRun
15+
*/
16+
static void init(ITestRun testRun) {
17+
clean();
18+
testRunThreadLocal.set(testRun);
19+
}
20+
21+
public static void clean() {
22+
testRunThreadLocal.remove();
23+
}
24+
25+
public static ITestRun getTestRun() {
26+
return testRunThreadLocal.get();
27+
}
28+
}

0 commit comments

Comments
 (0)