Skip to content

Commit

Permalink
fix: do not match cr/nl only in line ending
Browse files Browse the repository at this point in the history
some tools use \r that will appear multuple time in the same frame so we
should handle that otherwise logs are only flushed in the end.
  • Loading branch information
Martin GUIBERT committed Oct 13, 2023
1 parent d69a513 commit 574f53f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

public class DockerScriptRunner {
private static final ReadableBytesTypeConverter READABLE_BYTES_TYPE_CONVERTER = new ReadableBytesTypeConverter();
public static final Pattern NEWLINE_PATTERN = Pattern.compile("[\\r\\n]+$");
public static final Pattern NEWLINE_PATTERN = Pattern.compile("([^\\r\\n]+)[\\r\\n]+");

private final RetryUtils retryUtils;

Expand Down Expand Up @@ -127,13 +127,21 @@ public void onNext(Frame frame) {
String frameStr = new String(frame.getPayload());

Matcher newLineMatcher = NEWLINE_PATTERN.matcher(frameStr);
logBuffers.computeIfAbsent(frame.getStreamType(), streamType -> new StringBuilder())
.append(newLineMatcher.replaceAll(""));

if (newLineMatcher.reset().find()) {
int lastIndex = 0;
while (newLineMatcher.find()) {
String fragment = newLineMatcher.group(1);
logBuffers.computeIfAbsent(frame.getStreamType(), streamType -> new StringBuilder())
.append(fragment);

StringBuilder logBuffer = logBuffers.get(frame.getStreamType());
defaultLogConsumer.accept(logBuffer.toString(), frame.getStreamType() == StreamType.STDERR);
logBuffer.setLength(0);
lastIndex = newLineMatcher.end();
}
if (lastIndex < frameStr.length()) {
logBuffers.computeIfAbsent(frame.getStreamType(), streamType -> new StringBuilder())
.append(frameStr.substring(lastIndex));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,37 @@ public String getType() {
assertThat(run.getLogConsumer().getStdOutCount(), is(2));
assertThat(run.getLogConsumer().getOutputs().get("someOutput"), is(outputValue));
}

@Test
void carryreturn() throws Exception {
Task task = new Task() {
@Override
public String getId() {
return "id";
}

@Override
public String getType() {
return "type";
}
};
RunContext runContext = TestsUtils.mockRunContext(runContextFactory, task, ImmutableMap.of());
StringBuilder outputValue = new StringBuilder();
for (int i = 0; i < 3; i++) {
outputValue.append(Integer.toString(i).repeat(100)).append("\r")
.append(Integer.toString(i).repeat(800)).append("\r")
.append(Integer.toString(i).repeat(2000)).append("\r");
}
RunnerResult run = new DockerScriptRunner(applicationContext).run(
new CommandsWrapper(runContext).withCommands(List.of(
"/bin/sh", "-c",
"echo " + outputValue +
"echo -n another line"
)),
DockerOptions.builder().image("alpine").build()
);

Await.until(() -> run.getLogConsumer().getStdOutCount() == 10, null, Duration.ofSeconds(5));
assertThat(run.getLogConsumer().getStdOutCount(), is(10));
}
}

0 comments on commit 574f53f

Please sign in to comment.