Skip to content

Commit c9c8e89

Browse files
committed
Documenting and at least smoke-testing AutoCloseable on step logs.
1 parent e25e1ec commit c9c8e89

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

src/main/java/org/jenkinsci/plugins/workflow/log/LogStorage.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,17 @@ public interface LogStorage {
5050

5151
/**
5252
* Provides an alternate way of emitting output from a build.
53-
* May implement {@link AutoCloseable} to clean up at the end of a build.
53+
* <p>May implement {@link AutoCloseable} to clean up at the end of a build;
54+
* it may or may not be closed during Jenkins shutdown while a build is running.
5455
* @return a (remotable) build listener; do not bother overriding anything except {@link TaskListener#getLogger}
5556
* @see FlowExecutionOwner#getListener
5657
*/
5758
@Nonnull BuildListener overallListener() throws IOException, InterruptedException;
5859

5960
/**
6061
* Provides an alternate way of emitting output from a node (such as a step).
61-
* Currently not closed at the end of a step (TBD if this would be useful).
62+
* <p>May implement {@link AutoCloseable} to clean up at the end of a node ({@link FlowNode#isActive});
63+
* it may or may not be closed during Jenkins shutdown while a build is running.
6264
* @param node a running node
6365
* @return a (remotable) task listener; do not bother overriding anything except {@link TaskListener#getLogger}
6466
* @see StepContext#get

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,18 @@ public abstract class LogStorageTestBase {
100100
assertStepLog("1", 999, "", false);
101101
} catch (EOFException x) {}
102102
step2Pos = assertStepLog("2", step2Pos, "", true);
103-
((AutoCloseable) overall).close();
103+
close(overall);
104104
ls = createStorage();
105105
overall = ls.overallListener();
106106
overall.getLogger().println("resuming");
107107
step1 = ls.nodeListener(new MockNode("1"));
108108
step1.getLogger().println("one #4");
109+
close(step1);
109110
TaskListener step3 = ls.nodeListener(new MockNode("3"));
110111
step3.getLogger().println("three #1");
112+
close(step3);
111113
overall.getLogger().println("ending");
112-
((AutoCloseable) overall).close();
114+
close(overall);
113115
overallHtmlPos = assertOverallLog(overallHtmlPos, "resuming\n<span class=\"pipeline-node-1\">one #4\n</span><span class=\"pipeline-node-3\">three #1\n</span>ending\n", true);
114116
assertEquals(overallHtmlPos, assertOverallLog(overallHtmlPos, "", true));
115117
assertLength(overallHtmlPos);
@@ -121,17 +123,23 @@ public abstract class LogStorageTestBase {
121123
ls = createStorage();
122124
TaskListener step4 = ls.nodeListener(new MockNode("4"));
123125
step4.getLogger().println(HyperlinkNote.encodeTo("http://nowhere.net/", "nikde"));
124-
((AutoCloseable) overall).close();
126+
close(overall);
125127
long step4Pos = assertStepLog("4", 0, "<a href='http://nowhere.net/'>nikde</a>\n", true);
126128
assertLength("4", step4Pos);
127129
overall = ls.overallListener();
128130
overall.getLogger().println("really ending");
129-
((AutoCloseable) overall).close();
131+
close(overall);
130132
overallHtmlPos = assertOverallLog(overallHtmlPos, "<span class=\"pipeline-node-4\"><a href='http://nowhere.net/'>nikde</a>\n</span>really ending\n", true);
131133
assertEquals(overallHtmlPos, assertOverallLog(overallHtmlPos, "", true));
132134
assertLength(overallHtmlPos);
133135
}
134136

137+
private static void close(TaskListener listener) throws Exception {
138+
if (listener instanceof AutoCloseable) {
139+
((AutoCloseable) listener).close();
140+
}
141+
}
142+
135143
@Test public void remoting() throws Exception {
136144
LogStorage ls = createStorage();
137145
TaskListener overall = ls.overallListener();

0 commit comments

Comments
 (0)