Skip to content

Commit f3312f9

Browse files
author
Zhen Li
committed
Fix a flaky test
OS has some delay to create or delete file. With a shorter shutdown time, the file might not deleted or created successfully.
1 parent 0704e21 commit f3312f9

File tree

1 file changed

+17
-7
lines changed

1 file changed

+17
-7
lines changed

examples/src/test/java/org/neo4j/docs/driver/ExamplesIT.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import static java.nio.charset.StandardCharsets.UTF_8;
4848
import static java.util.Arrays.asList;
4949
import static org.hamcrest.Matchers.containsString;
50+
import static org.hamcrest.Matchers.emptyString;
5051
import static org.hamcrest.Matchers.equalTo;
5152
import static org.hamcrest.Matchers.greaterThan;
5253
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
@@ -462,20 +463,29 @@ void testSlf4jLogging() throws Exception
462463
{
463464
// log file is defined in logback-test.xml configuration file
464465
Path logFile = Paths.get( "target", "test.log" );
465-
Files.deleteIfExists( logFile );
466+
if ( Files.exists( logFile ) )
467+
{
468+
// delete file made this test flaky
469+
// erase content instead
470+
Files.write( logFile, new byte[0] );
471+
}
472+
473+
// verify erased
474+
String logFileContent = new String( Files.readAllBytes( logFile ), UTF_8 );
475+
assertThat( logFileContent, is( emptyString() ) );
466476

477+
String randomString = UUID.randomUUID().toString();
467478
try ( Slf4jLoggingExample example = new Slf4jLoggingExample( uri, USER, PASSWORD ) )
468479
{
469-
String randomString = UUID.randomUUID().toString();
470480
Object result = example.runReturnQuery( randomString );
471481
assertEquals( randomString, result );
482+
}
483+
assertTrue( Files.exists( logFile ) );
472484

473-
assertTrue( Files.exists( logFile ) );
485+
logFileContent = new String( Files.readAllBytes( logFile ), UTF_8 );
486+
assertThat( logFileContent, containsString( "RETURN $x" ) );
487+
assertThat( logFileContent, containsString( randomString ) );
474488

475-
String logFileContent = new String( Files.readAllBytes( logFile ), UTF_8 );
476-
assertThat( logFileContent, containsString( "RETURN $x" ) );
477-
assertThat( logFileContent, containsString( randomString ) );
478-
}
479489
}
480490

481491
@Test

0 commit comments

Comments
 (0)