Skip to content

Commit 69a1892

Browse files
committed
Add synchronization to iterations of ServiceLoader collections
1 parent 573276e commit 69a1892

File tree

3 files changed

+50
-28
lines changed

3 files changed

+50
-28
lines changed

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -151,22 +151,28 @@ public static void intercept(@This final Object runner, @SuperCall final Callabl
151151
CHILD_TO_PARENT.put(child, runner);
152152
}
153153

154-
if (NOTIFIERS.add(notifier)) {
155-
Description description = invoke(runner, "getDescription");
156-
for (RunListener listener : runListenerLoader) {
157-
notifier.addListener(listener);
158-
listener.testRunStarted(description);
154+
synchronized(NOTIFIERS) {
155+
if (NOTIFIERS.add(notifier)) {
156+
Description description = invoke(runner, "getDescription");
157+
for (RunListener listener : runListenerLoader) {
158+
notifier.addListener(listener);
159+
listener.testRunStarted(description);
160+
}
159161
}
160162
}
161163

162-
for (RunnerWatcher watcher : runnerWatcherLoader) {
163-
watcher.runStarted(runner);
164+
synchronized(runnerWatcherLoader) {
165+
for (RunnerWatcher watcher : runnerWatcherLoader) {
166+
watcher.runStarted(runner);
167+
}
164168
}
165169

166170
callProxy(proxy);
167171

168-
for (RunnerWatcher watcher : runnerWatcherLoader) {
169-
watcher.runFinished(runner);
172+
synchronized(runnerWatcherLoader) {
173+
for (RunnerWatcher watcher : runnerWatcherLoader) {
174+
watcher.runFinished(runner);
175+
}
170176
}
171177
}
172178

@@ -210,8 +216,10 @@ public static Object intercept(@This final Object runner,
210216
TARGET_TO_TESTCLASS.put(testObj, getTestClassOf(runner));
211217
applyTimeout(testObj);
212218

213-
for (TestObjectWatcher watcher : objectWatcherLoader) {
214-
watcher.testObjectCreated(testObj, TARGET_TO_TESTCLASS.get(testObj));
219+
synchronized(objectWatcherLoader) {
220+
for (TestObjectWatcher watcher : objectWatcherLoader) {
221+
watcher.testObjectCreated(testObj, TARGET_TO_TESTCLASS.get(testObj));
222+
}
215223
}
216224

217225
return testObj;

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,11 @@ static int getMaxRetry(Object runner, final FrameworkMethod method) {
129129
* @return {@code true} if test should be retried; otherwise {@code false}
130130
*/
131131
static boolean isRetriable(final FrameworkMethod method, final Throwable thrown) {
132-
for (JUnitRetryAnalyzer analyzer : retryAnalyzerLoader) {
133-
if (analyzer.retry(method, thrown)) {
134-
return true;
132+
synchronized(retryAnalyzerLoader) {
133+
for (JUnitRetryAnalyzer analyzer : retryAnalyzerLoader) {
134+
if (analyzer.retry(method, thrown)) {
135+
return true;
136+
}
135137
}
136138
}
137139
return false;

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

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,21 @@ public static Object intercept(@This final Object callable, @SuperCall final Cal
7777

7878
Object result = null;
7979
Throwable thrown = null;
80-
for (MethodWatcher watcher : methodWatcherLoader) {
81-
watcher.beforeInvocation(target, method, params);
80+
synchronized(methodWatcherLoader) {
81+
for (MethodWatcher watcher : methodWatcherLoader) {
82+
watcher.beforeInvocation(target, method, params);
83+
}
8284
}
8385

8486
try {
8587
result = LifecycleHooks.callProxy(proxy);
8688
} catch (Throwable t) {
8789
thrown = t;
8890
} finally {
89-
for (MethodWatcher watcher : methodWatcherLoader) {
90-
watcher.afterInvocation(target, method, thrown);
91+
synchronized(methodWatcherLoader) {
92+
for (MethodWatcher watcher : methodWatcherLoader) {
93+
watcher.afterInvocation(target, method, thrown);
94+
}
9195
}
9296
}
9397

@@ -108,8 +112,10 @@ public static Object intercept(@This final Object callable, @SuperCall final Cal
108112
static void fireTestStarted(TestClass testClass, Runnable runnable) {
109113
AtomicTest atomicTest = createAtomicTest(testClass, runnable);
110114
if (atomicTest != null) {
111-
for (RunWatcher watcher : runWatcherLoader) {
112-
watcher.testStarted(atomicTest.getIdentity(), atomicTest.getTestClass());
115+
synchronized(runWatcherLoader) {
116+
for (RunWatcher watcher : runWatcherLoader) {
117+
watcher.testStarted(atomicTest.getIdentity(), atomicTest.getTestClass());
118+
}
113119
}
114120
}
115121
}
@@ -122,9 +128,11 @@ static void fireTestStarted(TestClass testClass, Runnable runnable) {
122128
static void fireTestFinished(TestClass testClass) {
123129
AtomicTest atomicTest = TESTCLASS_TO_ATOMICTEST.get(testClass);
124130
if (atomicTest != null) {
125-
for (RunWatcher watcher : runWatcherLoader) {
126-
notifyIfTestFailed(watcher, atomicTest);
127-
watcher.testFinished(atomicTest.getIdentity(), atomicTest.getTestClass());
131+
synchronized(runWatcherLoader) {
132+
for (RunWatcher watcher : runWatcherLoader) {
133+
notifyIfTestFailed(watcher, atomicTest);
134+
watcher.testFinished(atomicTest.getIdentity(), atomicTest.getTestClass());
135+
}
128136
}
129137
}
130138
}
@@ -155,8 +163,10 @@ private static void notifyIfTestFailed(RunWatcher watcher, AtomicTest atomicTest
155163
*/
156164
static void fireTestIgnored(Object runner, FrameworkMethod method) {
157165
TestClass testClass = getTestClassOf(runner);
158-
for (RunWatcher watcher : runWatcherLoader) {
159-
watcher.testIgnored(method, testClass);
166+
synchronized(runWatcherLoader) {
167+
for (RunWatcher watcher : runWatcherLoader) {
168+
watcher.testIgnored(method, testClass);
169+
}
160170
}
161171
}
162172

@@ -183,9 +193,11 @@ public static Object getTargetFor(FrameworkMethod method) {
183193
public static Optional<MethodWatcher> getAttachedWatcher(
184194
Class<? extends MethodWatcher> watcherType) {
185195
Objects.requireNonNull(watcherType, "[watcherType] must be non-null");
186-
for (MethodWatcher watcher : methodWatcherLoader) {
187-
if (watcher.getClass() == watcherType) {
188-
return Optional.of(watcher);
196+
synchronized(methodWatcherLoader) {
197+
for (MethodWatcher watcher : methodWatcherLoader) {
198+
if (watcher.getClass() == watcherType) {
199+
return Optional.of(watcher);
200+
}
189201
}
190202
}
191203
return Optional.empty();

0 commit comments

Comments
 (0)