public final void waitForLog ( final int timeout , final TimeUnit unit , final String ... messages ) throws DockerException , InterruptedException { final long startTime = System . currentTimeMillis ( ) ; final long timeoutTimeMillis = startTime + TimeUnit . MILLISECONDS . convert ( timeout , unit ) ; int n = 0 ; LOGGER . info ( "Tailing logs for \"{}\"" , messages [ n ] ) ; final LogStream logs = client . logs ( containerId , follow ( ) , stdout ( ) , stderr ( ) ) ; while ( logs . hasNext ( ) ) { if ( Thread . interrupted ( ) ) { // manual check for thread being terminated throw new InterruptedException ( ) ; } final String message = messages [ n ] ; final String log = StandardCharsets . UTF_8 . decode ( logs . next ( ) . content ( ) ) . toString ( ) ; if ( log . contains ( message ) ) { LOGGER . info ( "Successfully received \"{}\" after {}ms" , message , System . currentTimeMillis ( ) - startTime ) ; if ( ++ n >= messages . length ) { return ; } } if ( startTime > timeoutTimeMillis ) { throw new IllegalStateException ( "Timeout waiting for log \"" + message + "\"" ) ; } } }
Wait for nth occurrence of message to appear in docker logs within the specified timeframe .