Skip to content

Commit 674abc1

Browse files
authored
Revise runner tracking to handle threaded execution
1 parent 28a4619 commit 674abc1

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

src/main/java/com/nordstrom/automation/junit/CreateTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public class CreateTest {
2727

2828
private static final Map<Integer, Object> HASHCODE_TO_TARGET = new ConcurrentHashMap<>();
2929
private static final Map<String, FrameworkMethod> TARGET_TO_METHOD = new ConcurrentHashMap<>();
30+
private static final Map<String, Object> TARGET_TO_RUNNER = new ConcurrentHashMap<>();
3031
private static final ThreadLocal<ConcurrentMap<Integer, DepthGauge>> METHOD_DEPTH;
3132
private static final Function<Integer, DepthGauge> NEW_INSTANCE;
3233
private static final Logger LOGGER = LoggerFactory.getLogger(CreateTest.class);
@@ -69,6 +70,7 @@ public static Object intercept(@This final Object runner, @Argument(0) final Fra
6970
METHOD_DEPTH.get().remove(hashCode);
7071
LOGGER.debug("testObjectCreated: {}", target);
7172
TARGET_TO_METHOD.put(toMapKey(target), method);
73+
TARGET_TO_RUNNER.put(toMapKey(target), runner);
7274

7375
// apply parameter-based global timeout
7476
TimeoutUtils.applyTestTimeout(runner, method, target);
@@ -110,6 +112,16 @@ static FrameworkMethod getMethodFor(Object target) {
110112
return TARGET_TO_METHOD.get(toMapKey(target));
111113
}
112114

115+
/**
116+
* Get the runner associated with the specified test class instance.
117+
*
118+
* @param target test class instance
119+
* @return JUnit class runner
120+
*/
121+
static Object getRunnerFor(Object target) {
122+
return TARGET_TO_RUNNER.get(toMapKey(target));
123+
}
124+
113125
/**
114126
* Release the mappings associated with the specified runner/method/target group.
115127
*
@@ -121,6 +133,7 @@ static void releaseMappingsFor(Object runner, FrameworkMethod method, Object tar
121133
HASHCODE_TO_TARGET.remove(Objects.hash(runner, method));
122134
if (target != null) {
123135
TARGET_TO_METHOD.remove(toMapKey(target));
136+
TARGET_TO_RUNNER.remove(toMapKey(target));
124137
}
125138
}
126139
}

src/main/java/com/nordstrom/automation/junit/RunReflectiveCall.java

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,41 @@ public static Object intercept(@This final ReflectiveCallable callable, @SuperCa
6060
throws Exception {
6161

6262
Object child = null;
63+
Object runner = null;
64+
boolean didPush = false;
6365

6466
try {
6567
// get child object
6668
child = LifecycleHooks.getFieldValue(callable, "this$0");
69+
// get thread runner
70+
runner = Run.getThreadRunner();
71+
72+
// if runner unknown
73+
if (runner == null) {
74+
// get runner for child
75+
runner = Run.getParentOf(child);
76+
}
77+
78+
// if runner unknown
79+
if (runner == null) {
80+
// get test class instance
81+
Object target = LifecycleHooks.getFieldValue(callable, "val$target");
82+
// if target acquired
83+
if (target != null) {
84+
// get runner for target
85+
runner = CreateTest.getRunnerFor(target);
86+
87+
// if runner resolved
88+
if (runner != null) {
89+
Run.pushThreadRunner(runner);
90+
didPush = true;
91+
}
92+
}
93+
}
6794
} catch (IllegalAccessException | NoSuchFieldException | SecurityException | IllegalArgumentException e) {
6895
// handled below
6996
}
7097

71-
Object runner = Run.getParentOf(child);
72-
if (runner == null) {
73-
runner = Run.getThreadRunner();
74-
}
75-
7698
Object result = null;
7799
Throwable thrown = null;
78100

@@ -83,6 +105,9 @@ public static Object intercept(@This final ReflectiveCallable callable, @SuperCa
83105
thrown = t;
84106
} finally {
85107
fireAfterInvocation(runner, child, callable, thrown);
108+
if (didPush) {
109+
Run.popThreadRunner();
110+
}
86111
}
87112

88113
if (thrown != null) {

0 commit comments

Comments
 (0)