Skip to content

Commit 269b2ee

Browse files
committed
Remove TeePrintStream, remove synchronization, update mangled test
1 parent b2a5025 commit 269b2ee

File tree

6 files changed

+120
-212
lines changed

6 files changed

+120
-212
lines changed

src/main/java/org/jenkinsci/plugins/workflow/log/tee/TeeBuildListener.java

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,25 @@
77
import java.io.PrintStream;
88
import java.io.Serial;
99
import java.util.List;
10-
import java.util.stream.Stream;
1110
import org.jenkinsci.plugins.workflow.log.OutputStreamTaskListener;
1211

13-
class TeeBuildListener implements BuildListener, OutputStreamTaskListener, AutoCloseable {
12+
class TeeBuildListener extends OutputStreamTaskListener.Default
13+
implements BuildListener, OutputStreamTaskListener, AutoCloseable {
1414

1515
@Serial
1616
private static final long serialVersionUID = 1L;
1717

1818
private final transient TeeLogStorage teeLogStorage;
1919

20-
private final BuildListener primary;
20+
private final TaskListener primary;
2121

22-
private final List<BuildListener> secondaries;
22+
private final List<TaskListener> secondaries;
2323

2424
private transient OutputStream outputStream;
2525

2626
private transient PrintStream printStream;
2727

28-
TeeBuildListener(TeeLogStorage teeLogStorage, BuildListener primary, BuildListener... secondaries) {
28+
TeeBuildListener(TeeLogStorage teeLogStorage, TaskListener primary, TaskListener... secondaries) {
2929
if (!(primary instanceof OutputStreamTaskListener)) {
3030
throw new ClassCastException("Primary is not an instance of OutputStreamTaskListener: " + primary);
3131
}
@@ -39,41 +39,20 @@ class TeeBuildListener implements BuildListener, OutputStreamTaskListener, AutoC
3939
this.secondaries = List.of(secondaries);
4040
}
4141

42-
TeeBuildListener(TeeLogStorage teeLogStorage, TaskListener primary, TaskListener... secondaries) {
43-
this(
44-
teeLogStorage,
45-
(BuildListener) primary,
46-
Stream.of(secondaries)
47-
.map(secondary -> (BuildListener) secondary)
48-
.toArray(BuildListener[]::new));
49-
}
50-
5142
@NonNull
5243
@Override
5344
public synchronized OutputStream getOutputStream() {
5445
if (outputStream == null) {
5546
outputStream = new TeeOutputStream(
5647
teeLogStorage,
57-
((OutputStreamTaskListener) primary).getOutputStream(),
48+
OutputStreamTaskListener.getOutputStream(primary),
5849
secondaries.stream()
59-
.map(secondary -> ((OutputStreamTaskListener) secondary).getOutputStream())
50+
.map(OutputStreamTaskListener::getOutputStream)
6051
.toArray(OutputStream[]::new));
6152
}
6253
return outputStream;
6354
}
6455

65-
@NonNull
66-
@Override
67-
public synchronized PrintStream getLogger() {
68-
if (printStream == null) {
69-
printStream = new TeePrintStream(
70-
teeLogStorage,
71-
primary.getLogger(),
72-
secondaries.stream().map(TaskListener::getLogger).toArray(PrintStream[]::new));
73-
}
74-
return printStream;
75-
}
76-
7756
@Override
7857
public void close() throws Exception {
7958
Exception exception = null;
@@ -84,7 +63,7 @@ public void close() throws Exception {
8463
exception = e;
8564
}
8665
}
87-
for (BuildListener secondary : secondaries) {
66+
for (TaskListener secondary : secondaries) {
8867
if (secondary instanceof AutoCloseable) {
8968
try {
9069
((AutoCloseable) secondary).close();

src/main/java/org/jenkinsci/plugins/workflow/log/tee/TeeLogStorage.java

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,40 +38,24 @@ public TeeLogStorage(@NonNull LogStorage primary, LogStorage... secondaries) {
3838
}
3939
}
4040

41-
@Override
42-
public LogStorage getPrimary() {
43-
return primary;
44-
}
45-
46-
@Override
47-
public List<LogStorage> getSecondaries() {
48-
return secondaries;
49-
}
50-
5141
@NonNull
5242
@Override
5343
public BuildListener overallListener() throws IOException, InterruptedException {
54-
synchronized (this) {
55-
List<BuildListener> secondaryListeners = new ArrayList<>();
56-
for (LogStorage secondary : secondaries) {
57-
secondaryListeners.add(secondary.overallListener());
58-
}
59-
return new TeeBuildListener(
60-
this, primary.overallListener(), secondaryListeners.toArray(BuildListener[]::new));
44+
List<BuildListener> secondaryListeners = new ArrayList<>();
45+
for (LogStorage secondary : secondaries) {
46+
secondaryListeners.add(secondary.overallListener());
6147
}
48+
return new TeeBuildListener(this, primary.overallListener(), secondaryListeners.toArray(BuildListener[]::new));
6249
}
6350

6451
@NonNull
6552
@Override
6653
public TaskListener nodeListener(@NonNull FlowNode node) throws IOException, InterruptedException {
67-
synchronized (this) {
68-
List<TaskListener> secondaryListeners = new ArrayList<>();
69-
for (LogStorage secondary : secondaries) {
70-
secondaryListeners.add(secondary.nodeListener(node));
71-
}
72-
return new TeeBuildListener(
73-
this, primary.nodeListener(node), secondaryListeners.toArray(TaskListener[]::new));
54+
List<TaskListener> secondaryListeners = new ArrayList<>();
55+
for (LogStorage secondary : secondaries) {
56+
secondaryListeners.add(secondary.nodeListener(node));
7457
}
58+
return new TeeBuildListener(this, primary.nodeListener(node), secondaryListeners.toArray(TaskListener[]::new));
7559
}
7660

7761
@NonNull

src/main/java/org/jenkinsci/plugins/workflow/log/tee/TeeOutputStream.java

Lines changed: 52 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -18,89 +18,81 @@ class TeeOutputStream extends OutputStream {
1818

1919
@Override
2020
public void write(int b) throws IOException {
21-
synchronized (teeLogStorage) {
22-
IOException exception = null;
23-
primary.write(b);
24-
for (OutputStream secondary : secondaries) {
25-
try {
26-
secondary.write(b);
27-
} catch (IOException e) {
28-
if (exception == null) {
29-
exception = e;
30-
} else {
31-
exception.addSuppressed(e);
32-
}
21+
IOException exception = null;
22+
primary.write(b);
23+
for (OutputStream secondary : secondaries) {
24+
try {
25+
secondary.write(b);
26+
} catch (IOException e) {
27+
if (exception == null) {
28+
exception = e;
29+
} else {
30+
exception.addSuppressed(e);
3331
}
3432
}
35-
if (exception != null) {
36-
throw exception;
37-
}
33+
}
34+
if (exception != null) {
35+
throw exception;
3836
}
3937
}
4038

4139
@Override
4240
public void write(byte[] b, int off, int len) throws IOException {
43-
synchronized (teeLogStorage) {
44-
IOException exception = null;
45-
primary.write(b, off, len);
46-
for (OutputStream secondary : secondaries) {
47-
try {
48-
secondary.write(b, off, len);
49-
} catch (IOException e) {
50-
if (exception == null) {
51-
exception = e;
52-
} else {
53-
exception.addSuppressed(e);
54-
}
41+
IOException exception = null;
42+
primary.write(b, off, len);
43+
for (OutputStream secondary : secondaries) {
44+
try {
45+
secondary.write(b, off, len);
46+
} catch (IOException e) {
47+
if (exception == null) {
48+
exception = e;
49+
} else {
50+
exception.addSuppressed(e);
5551
}
5652
}
57-
if (exception != null) {
58-
throw exception;
59-
}
53+
}
54+
if (exception != null) {
55+
throw exception;
6056
}
6157
}
6258

6359
@Override
6460
public void flush() throws IOException {
65-
synchronized (teeLogStorage) {
66-
IOException exception = null;
67-
primary.flush();
68-
for (OutputStream secondary : secondaries) {
69-
try {
70-
secondary.flush();
71-
} catch (IOException e) {
72-
if (exception == null) {
73-
exception = e;
74-
} else {
75-
exception.addSuppressed(e);
76-
}
61+
IOException exception = null;
62+
primary.flush();
63+
for (OutputStream secondary : secondaries) {
64+
try {
65+
secondary.flush();
66+
} catch (IOException e) {
67+
if (exception == null) {
68+
exception = e;
69+
} else {
70+
exception.addSuppressed(e);
7771
}
7872
}
79-
if (exception != null) {
80-
throw exception;
81-
}
73+
}
74+
if (exception != null) {
75+
throw exception;
8276
}
8377
}
8478

8579
@Override
8680
public void close() throws IOException {
87-
synchronized (teeLogStorage) {
88-
IOException exception = null;
89-
primary.close();
90-
for (OutputStream secondary : secondaries) {
91-
try {
92-
secondary.close();
93-
} catch (IOException e) {
94-
if (exception == null) {
95-
exception = e;
96-
} else {
97-
exception.addSuppressed(e);
98-
}
81+
IOException exception = null;
82+
primary.close();
83+
for (OutputStream secondary : secondaries) {
84+
try {
85+
secondary.close();
86+
} catch (IOException e) {
87+
if (exception == null) {
88+
exception = e;
89+
} else {
90+
exception.addSuppressed(e);
9991
}
10092
}
101-
if (exception != null) {
102-
throw exception;
103-
}
93+
}
94+
if (exception != null) {
95+
throw exception;
10496
}
10597
}
10698
}

src/main/java/org/jenkinsci/plugins/workflow/log/tee/TeePrintStream.java

Lines changed: 0 additions & 87 deletions
This file was deleted.

src/test/java/org/jenkinsci/plugins/workflow/log/LogStorageTestBase.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,10 @@ private static final class GC extends MasterToSlaveCallable<Void, Exception> {
230230
*/
231231
@Test public void mangledLines() throws Exception {
232232
Random r = new Random();
233-
BiFunction<Character, TaskListener, Thread> thread = (c, l) -> new Thread(() -> {
233+
BiFunction<String, TaskListener, Thread> thread = (alphabet, l) -> new Thread(() -> {
234+
var len = alphabet.length();
234235
for (int i = 0; i < 1000; i++) {
235-
l.getLogger().print(c);
236+
l.getLogger().print(alphabet.charAt(i % len));
236237
if (r.nextDouble() < 0.1) {
237238
l.getLogger().println();
238239
}
@@ -247,9 +248,9 @@ private static final class GC extends MasterToSlaveCallable<Void, Exception> {
247248
});
248249
List<Thread> threads = new ArrayList<>();
249250
LogStorage ls = createStorage();
250-
threads.add(thread.apply('.', ls.overallListener()));
251-
threads.add(thread.apply('1', ls.nodeListener(new MockNode("1"))));
252-
threads.add(thread.apply('2', ls.nodeListener(new MockNode("2"))));
251+
threads.add(thread.apply("0123456789", ls.overallListener()));
252+
threads.add(thread.apply("abcdefghijklmnopqrstuvwxyz", ls.nodeListener(new MockNode("1"))));
253+
threads.add(thread.apply("ABCDEFGHIJKLMNOPQRSTUVWXYZ", ls.nodeListener(new MockNode("2"))));
253254
threads.forEach(Thread::start);
254255
threads.forEach(t -> {
255256
try {

0 commit comments

Comments
 (0)