-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Remove extra newlines in container log output #3752
Remove extra newlines in container log output #3752
Conversation
Fixes the following issues: - #1763 - testcontainers#1854
core/src/test/java/org/testcontainers/containers/output/NewLineContainerTest.java
Outdated
Show resolved
Hide resolved
stringBuffer.write(outputFrame.getBytes()); | ||
stringBuffer.flush(); | ||
firstLine = false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This essentially reverts the change done here: https://github.com/testcontainers/testcontainers-java/pull/643/files#diff-321431024ed34199142ebf2d70a46ba89f8a09e66582d40527885123e429b4d9R22-R24
It's not entirely clear to me if this will work for all use cases; it could very well be that this breaks (so it might be that we'll have to conditionalize it). I was unable to run all the tests locally, so maybe CI will shed some light on this.
@bsideup Any progress on this? We are seeing some intermittent failures in our CI tests, and it would be interesting to see if this (the extra newline being added after each logging frame) could be a potential root cause. Thanks in advance. 🙇 |
@perlun we're currently at low capacity and trying to catch up with some old PRs first, unless something is broken. Since there is a workaround (including a custom consumer), I am afraid we need a bit more time to get to this PR, but eventually we will, I promise! :D |
@bsideup Thanks, that's perfectly understandable. I know how it can be at times. 👍 For my particular use case (using a So, my problem is: We have a |
@perlun have you tried adjusting the pattern so that it ignores newlines? |
To make things more complex, I have my own One ugly workaround I thought about: use the public class LogMessageContainsWaitStrategy extends AbstractWaitStrategy {
private static final int TIMES = 1;
private final String needle;
public LogMessageContainsWaitStrategy( String needle ) {
this.needle = needle;
}
@Override
protected void waitUntilReady() {
WaitingConsumer waitingConsumer = new WaitingConsumer();
LogContainerCmd cmd = DockerClientFactory.instance().client().logContainerCmd( waitStrategyTarget.getContainerId() )
.withFollowStream( true )
.withSince( 0 )
.withStdOut( true )
.withStdErr( true );
try ( FrameConsumerResultCallback callback = new FrameConsumerResultCallback() ) {
callback.addConsumer( STDOUT, waitingConsumer );
callback.addConsumer( STDERR, waitingConsumer );
cmd.exec( callback );
// This is the actual predicate that gets checked each time a new output frame is received
Predicate<OutputFrame> waitPredicate = outputFrame ->
outputFrame.getUtf8String().contains( needle );
try {
waitingConsumer.waitUntil( waitPredicate, startupTimeout.getSeconds(), TimeUnit.SECONDS, TIMES );
}
catch ( TimeoutException e ) {
throw new ContainerLaunchException( "Timed out waiting for log output including '" + needle + "'" );
}
}
catch ( IOException e ) {
throw new UncheckedIOException( e );
}
}
@Override
public String toString() {
return getClass().getSimpleName() + ", looking for log output including '" + needle + "'";
}
} |
@perlun I just applied some fixes and added more tests. Will merge once green 👍 |
Thanks a lot for this. 🙏 Let me know if/when we get a new release including this, and I'll happily test it on our code base to see it it resolves the intermittent issues we're seeing. |
@perlun the release engines are running now :) |
FWIW: this fix did not help me in my original problem (briefly described in #3752 (comment)). What did seem to help, at least to the point of removing spurious newlines in the Docker output is to create the container like this. (In our particular case, the Docker output comes from a Tomcat container running a bunch of Java web applications, with SLF4J + Logback writing to stdout.) withCreateContainerCmdModifier( cmd -> cmd.withTty( true ) ); With this in place, I am no longer getting any spurious newlines in the middle of the logs. Posting this here in the help that it might help others as well. |
Now this sounds more like a bug with Docker :D Anyways, I am glad that you were able to fix it 👍 |
It could very well be. I get the feeling that there is some kind of output buffering (auto-flushing disabled) going on, which is weird since the Logback I even attached a debugger to the Tomcat inside the Docker container, with the Tomcat sources loaded so I could debug it. (If anyone runs into the same problem and figures this out, please post the details here so we can all learn from it and benefit. 🙏) |
Fixes the following issues: