Skip to content

Commit e62faca

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 d9f378b commit e62faca

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

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

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@
3232
import java.util.Set;
3333
import java.util.UUID;
3434

35-
import org.neo4j.driver.internal.util.ServerVersion;
3635
import org.neo4j.driver.Session;
3736
import org.neo4j.driver.Value;
37+
import org.neo4j.driver.internal.util.ServerVersion;
3838
import org.neo4j.driver.summary.ResultSummary;
3939
import org.neo4j.driver.summary.StatementType;
4040
import org.neo4j.driver.util.DatabaseExtension;
@@ -45,6 +45,7 @@
4545
import static java.nio.charset.StandardCharsets.UTF_8;
4646
import static java.util.Arrays.asList;
4747
import static org.hamcrest.Matchers.containsString;
48+
import static org.hamcrest.Matchers.emptyString;
4849
import static org.hamcrest.Matchers.equalTo;
4950
import static org.hamcrest.Matchers.greaterThan;
5051
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
@@ -459,20 +460,29 @@ void testSlf4jLogging() throws Exception
459460
{
460461
// log file is defined in logback-test.xml configuration file
461462
Path logFile = Paths.get( "target", "test.log" );
462-
Files.deleteIfExists( logFile );
463+
if ( Files.exists( logFile ) )
464+
{
465+
// delete file made this test flaky
466+
// erase content instead
467+
Files.write( logFile, new byte[0] );
468+
}
463469

470+
// verify erased
471+
String logFileContent = new String( Files.readAllBytes( logFile ), UTF_8 );
472+
assertThat( logFileContent, is( emptyString() ) );
473+
474+
String randomString = UUID.randomUUID().toString();
464475
try ( Slf4jLoggingExample example = new Slf4jLoggingExample( uri, USER, PASSWORD ) )
465476
{
466-
String randomString = UUID.randomUUID().toString();
467477
Object result = example.runReturnQuery( randomString );
468478
assertEquals( randomString, result );
479+
}
480+
assertTrue( Files.exists( logFile ) );
469481

470-
assertTrue( Files.exists( logFile ) );
482+
logFileContent = new String( Files.readAllBytes( logFile ), UTF_8 );
483+
assertThat( logFileContent, containsString( "RETURN $x" ) );
484+
assertThat( logFileContent, containsString( randomString ) );
471485

472-
String logFileContent = new String( Files.readAllBytes( logFile ), UTF_8 );
473-
assertThat( logFileContent, containsString( "RETURN $x" ) );
474-
assertThat( logFileContent, containsString( randomString ) );
475-
}
476486
}
477487

478488
@Test

0 commit comments

Comments
 (0)