Skip to content

Commit 4df3e81

Browse files
author
Tibor Digana
committed
synchronized substituted by thread safe collection
1 parent 1b7312e commit 4df3e81

File tree

2 files changed

+343
-43
lines changed

2 files changed

+343
-43
lines changed

Diff for: src/main/java/org/junit/runner/notification/RunNotifier.java

+27-43
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
package org.junit.runner.notification;
22

3-
import static java.util.Arrays.asList;
4-
53
import java.util.ArrayList;
6-
import java.util.Collections;
7-
import java.util.Iterator;
4+
import java.util.Arrays;
5+
import java.util.Collection;
6+
import java.util.LinkedList;
87
import java.util.List;
9-
8+
import java.util.concurrent.ConcurrentLinkedQueue;
109
import org.junit.internal.AssumptionViolatedException;
1110
import org.junit.runner.Description;
1211
import org.junit.runner.Result;
@@ -21,9 +20,8 @@
2120
* @since 4.0
2221
*/
2322
public class RunNotifier {
24-
private final List<RunListener> fListeners =
25-
Collections.synchronizedList(new ArrayList<RunListener>());
26-
private volatile boolean fPleaseStop = false;
23+
private final ConcurrentLinkedQueue<RunListener> fListeners= new ConcurrentLinkedQueue<RunListener>();
24+
private volatile boolean fPleaseStop= false;
2725

2826
/**
2927
* Internal use only
@@ -40,32 +38,29 @@ public void removeListener(RunListener listener) {
4038
}
4139

4240
private abstract class SafeNotifier {
43-
private final List<RunListener> fCurrentListeners;
41+
private final Collection<RunListener> fCurrentListeners;
4442

4543
SafeNotifier() {
4644
this(fListeners);
4745
}
4846

49-
SafeNotifier(List<RunListener> currentListeners) {
50-
fCurrentListeners = currentListeners;
47+
SafeNotifier(Collection<RunListener> currentListeners) {
48+
fCurrentListeners= currentListeners;
5149
}
5250

5351
void run() {
54-
synchronized (fListeners) {
55-
List<RunListener> safeListeners = new ArrayList<RunListener>();
56-
List<Failure> failures = new ArrayList<Failure>();
57-
for (Iterator<RunListener> all = fCurrentListeners.iterator(); all
58-
.hasNext(); ) {
59-
try {
60-
RunListener listener = all.next();
61-
notifyListener(listener);
62-
safeListeners.add(listener);
63-
} catch (Exception e) {
64-
failures.add(new Failure(Description.TEST_MECHANISM, e));
65-
}
52+
int capacity= fCurrentListeners.size();
53+
ArrayList<RunListener> safeListeners= new ArrayList<RunListener>(capacity);
54+
ArrayList<Failure> failures= new ArrayList<Failure>(capacity);
55+
for (RunListener listener : fCurrentListeners) {
56+
try {
57+
notifyListener(listener);
58+
safeListeners.add(listener);
59+
} catch (Exception e) {
60+
failures.add(new Failure(Description.TEST_MECHANISM, e));
6661
}
67-
fireTestFailures(safeListeners, failures);
6862
}
63+
fireTestFailures(safeListeners, failures);
6964
}
7065

7166
abstract protected void notifyListener(RunListener each) throws Exception;
@@ -80,8 +75,6 @@ public void fireTestRunStarted(final Description description) {
8075
protected void notifyListener(RunListener each) throws Exception {
8176
each.testRunStarted(description);
8277
}
83-
84-
;
8578
}.run();
8679
}
8780

@@ -94,8 +87,6 @@ public void fireTestRunFinished(final Result result) {
9487
protected void notifyListener(RunListener each) throws Exception {
9588
each.testRunFinished(result);
9689
}
97-
98-
;
9990
}.run();
10091
}
10192

@@ -114,8 +105,6 @@ public void fireTestStarted(final Description description) throws StoppedByUserE
114105
protected void notifyListener(RunListener each) throws Exception {
115106
each.testStarted(description);
116107
}
117-
118-
;
119108
}.run();
120109
}
121110

@@ -125,22 +114,18 @@ protected void notifyListener(RunListener each) throws Exception {
125114
* @param failure the description of the test that failed and the exception thrown
126115
*/
127116
public void fireTestFailure(Failure failure) {
128-
fireTestFailures(fListeners, asList(failure));
117+
fireTestFailures(fListeners, Arrays.asList(failure));
129118
}
130119

131-
private void fireTestFailures(List<RunListener> listeners,
132-
final List<Failure> failures) {
120+
private void fireTestFailures(Collection<RunListener> listeners, final List<Failure> failures) {
133121
if (!failures.isEmpty()) {
134122
new SafeNotifier(listeners) {
135123
@Override
136-
protected void notifyListener(RunListener listener)
137-
throws Exception {
124+
protected void notifyListener(RunListener listener) throws Exception {
138125
for (Failure each : failures) {
139126
listener.testFailure(each);
140127
}
141128
}
142-
143-
;
144129
}.run();
145130
}
146131
}
@@ -158,8 +143,6 @@ public void fireTestAssumptionFailed(final Failure failure) {
158143
protected void notifyListener(RunListener each) throws Exception {
159144
each.testAssumptionFailure(failure);
160145
}
161-
162-
;
163146
}.run();
164147
}
165148

@@ -190,8 +173,6 @@ public void fireTestFinished(final Description description) {
190173
protected void notifyListener(RunListener each) throws Exception {
191174
each.testFinished(description);
192175
}
193-
194-
;
195176
}.run();
196177
}
197178

@@ -202,13 +183,16 @@ protected void notifyListener(RunListener each) throws Exception {
202183
* to be shared amongst the many runners involved.
203184
*/
204185
public void pleaseStop() {
205-
fPleaseStop = true;
186+
fPleaseStop= true;
206187
}
207188

208189
/**
209190
* Internal use only. The Result's listener must be first.
210191
*/
211192
public void addFirstListener(RunListener listener) {
212-
fListeners.add(0, listener);
193+
LinkedList<RunListener> listeners= new LinkedList<RunListener>(fListeners);
194+
listeners.addFirst(listener);
195+
fListeners.clear();
196+
fListeners.addAll(listeners);
213197
}
214198
}

0 commit comments

Comments
 (0)