Skip to content

Commit b091881

Browse files
committed
jenkins-client-217 - Revert to sync pooling. Fix for unit tests
1 parent d52b5c1 commit b091881

File tree

3 files changed

+53
-49
lines changed

3 files changed

+53
-49
lines changed

jenkins-client/src/main/java/com/offbytwo/jenkins/helper/BuildConsoleStreamListener.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@ public interface BuildConsoleStreamListener {
1212
*/
1313
void onData(String newLogChunk);
1414

15-
/**
16-
* Called by api when error happens
17-
* @param error
18-
*/
19-
void onError(Exception error);
20-
2115
/**
2216
* Called when streaming console output is finished
2317
*/

jenkins-client/src/main/java/com/offbytwo/jenkins/model/BuildWithDetails.java

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -402,47 +402,35 @@ public String getConsoleOutputHtml() throws IOException {
402402
* @param poolingTimeout pooling timeout (seconds) used to break pooling in case build stuck
403403
*
404404
*/
405-
public void streamConsoleOutput(final BuildConsoleStreamListener listener, final int poolingInterval, final int poolingTimeout) {
405+
public void streamConsoleOutput(final BuildConsoleStreamListener listener, final int poolingInterval, final int poolingTimeout) throws InterruptedException, IOException {
406406
// Calculate start and timeout
407407
final long startTime = System.currentTimeMillis();
408408
final long timeoutTime = startTime + (poolingTimeout * 1000);
409409

410-
// Pool for logs in separate thread
411-
new Thread(new Runnable() {
412-
public void run() {
413-
int bufferOffset = 0;
414-
while (true) {
415-
try {
416-
Thread.sleep(poolingInterval * 1000);
417-
} catch (InterruptedException e) {
418-
listener.onError(e);
419-
}
420-
ConsoleLog consoleLog = null;
421-
try {
422-
consoleLog = getConsoleOutputText(bufferOffset);
423-
} catch (IOException e) {
424-
listener.onError(e);
425-
}
426-
String logString = consoleLog.getConsoleLog();
427-
if (logString != null && !logString.isEmpty()) {
428-
listener.onData(logString);
429-
}
430-
if (consoleLog.getHasMoreData()) {
431-
bufferOffset = consoleLog.getCurrentBufferSize();
432-
} else {
433-
listener.finished();
434-
break;
435-
}
436-
long currentTime = System.currentTimeMillis();
437-
438-
if(currentTime> timeoutTime){
439-
LOGGER.warn("Pooling for build {0} for {2} timeout! Check if job stuck in jenkins",
440-
BuildWithDetails.this.getDisplayName(), BuildWithDetails.this.getNumber());
441-
break;
442-
}
443-
}
410+
int bufferOffset = 0;
411+
while (true) {
412+
Thread.sleep(poolingInterval * 1000);
413+
414+
ConsoleLog consoleLog = null;
415+
consoleLog = getConsoleOutputText(bufferOffset);
416+
String logString = consoleLog.getConsoleLog();
417+
if (logString != null && !logString.isEmpty()) {
418+
listener.onData(logString);
419+
}
420+
if (consoleLog.getHasMoreData()) {
421+
bufferOffset = consoleLog.getCurrentBufferSize();
422+
} else {
423+
listener.finished();
424+
break;
444425
}
445-
}).start();
426+
long currentTime = System.currentTimeMillis();
427+
428+
if (currentTime > timeoutTime) {
429+
LOGGER.warn("Pooling for build {0} for {2} timeout! Check if job stuck in jenkins",
430+
BuildWithDetails.this.getDisplayName(), BuildWithDetails.this.getNumber());
431+
break;
432+
}
433+
}
446434
}
447435

448436
/**

jenkins-client/src/test/java/com/offbytwo/jenkins/model/BuildWithDetailsTest.java

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,12 @@
55
import com.offbytwo.jenkins.helper.BuildConsoleStreamListener;
66
import org.apache.http.HttpResponse;
77
import org.apache.http.NameValuePair;
8-
import org.apache.http.concurrent.BasicFuture;
98
import org.apache.http.entity.StringEntity;
109
import org.apache.http.message.BasicHeader;
1110
import org.junit.Before;
1211
import org.junit.Test;
1312

1413
import java.io.IOException;
15-
import java.util.concurrent.CompletableFuture;
16-
import java.util.concurrent.Future;
1714

1815
import static org.assertj.core.api.Assertions.assertThat;
1916
import static org.assertj.core.api.Assertions.fail;
@@ -76,25 +73,50 @@ public void poolBuildLog() throws InterruptedException {
7673
given(response.getFirstHeader(BuildWithDetails.MORE_DATA_HEADER)).willReturn(moreDataHeader);
7774
given(response.getFirstHeader(BuildWithDetails.TEXT_SIZE_HEADER)).willReturn(textSizeHeader);
7875
given(client.post_form_with_result(anyString(),anyListOf(NameValuePair.class),anyBoolean())).willReturn(response);
76+
final StringBuffer buffer=new StringBuffer();
7977
buildWithDetails.streamConsoleOutput(new BuildConsoleStreamListener() {
8078
@Override
8179
public void onData(String newLogChunk) {
8280
assertThat(newLogChunk).isEqualTo(text);
81+
buffer.append(text);
8382
}
8483

8584
@Override
86-
public void onError(Exception error) {
87-
fail("No error expected",error);
85+
public void finished() {
86+
assertThat(buffer.toString()).isEqualTo(text);
87+
}
88+
},1,2);
89+
} catch (IOException e) {
90+
fail("Should not return exception",e);
91+
}
92+
}
93+
94+
@Test
95+
public void poolBuildLogShouldTimeout() throws InterruptedException {
96+
try {
97+
HttpResponse response = mock(HttpResponse.class);
98+
final String text = "test test test";
99+
int textLength = text.length();
100+
given(response.getEntity()).willReturn(new StringEntity(text));
101+
BasicHeader moreDataHeader = new BasicHeader(BuildWithDetails.MORE_DATA_HEADER, "true");
102+
BasicHeader textSizeHeader = new BasicHeader(BuildWithDetails.TEXT_SIZE_HEADER, Integer.toString(textLength));
103+
given(response.getFirstHeader(BuildWithDetails.MORE_DATA_HEADER)).willReturn(moreDataHeader);
104+
given(response.getFirstHeader(BuildWithDetails.TEXT_SIZE_HEADER)).willReturn(textSizeHeader);
105+
given(client.post_form_with_result(anyString(),anyListOf(NameValuePair.class),anyBoolean())).willReturn(response);
106+
buildWithDetails.streamConsoleOutput(new BuildConsoleStreamListener() {
107+
@Override
108+
public void onData(String newLogChunk) {
109+
assertThat(newLogChunk).isEqualTo(text);
88110
}
89111

90112
@Override
91113
public void finished() {
114+
fail("Should timeout");
92115
}
93116
},1,2);
94-
95-
96117
} catch (IOException e) {
97118
fail("Should not return exception",e);
98119
}
99120
}
121+
100122
}

0 commit comments

Comments
 (0)