Skip to content

Commit

Permalink
[Bc2lkk3N] Fix flaky test
Browse files Browse the repository at this point in the history
  • Loading branch information
loveleif committed Nov 7, 2023
1 parent 50adcfe commit ecb7195
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
4 changes: 2 additions & 2 deletions core/src/test/java/apoc/cypher/CypherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import static apoc.util.TestUtil.testFail;
import static apoc.util.TestUtil.testResult;
import static apoc.util.TransactionTestUtil.checkTerminationGuard;
import static apoc.util.TransactionTestUtil.checkTransactionTime;
import static apoc.util.TransactionTestUtil.checkTransactionTimeReasonable;
import static apoc.util.TransactionTestUtil.lastTransactionChecks;
import static apoc.util.TransactionTestUtil.terminateTransactionAsync;
import static apoc.util.Util.map;
Expand Down Expand Up @@ -228,7 +228,7 @@ public void testRunTimeboxedWithTerminationInnerTransaction1() {
// check that the query returns nothing and terminate before `timeout`
long timeout = 5L;
db.executeTransactionally(query, Map.of("innerQuery", innerQuery, "timeout", timeout), Result::resultAsString);
checkTransactionTime(timeout, timeBefore);
checkTransactionTimeReasonable(timeout, timeBefore);
}

@Test
Expand Down
22 changes: 15 additions & 7 deletions test-utils/src/main/java/apoc/util/TransactionTestUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static void checkTerminationGuard(
terminateTransactionAsync(db, timeout, query);

// check that the procedure/function fails with TransactionFailureException when transaction is terminated
long timePassed = System.currentTimeMillis();
long startTimeMs = System.currentTimeMillis();
try (Transaction transaction = db.beginTx(timeout, TimeUnit.SECONDS)) {
transaction.execute(query, params).resultAsString();
transaction.commit();
Expand All @@ -68,25 +68,33 @@ public static void checkTerminationGuard(
Stream.of("terminated", "failed", "closed").anyMatch(msg::contains));
}

lastTransactionChecks(db, timeout, query, timePassed);
lastTransactionChecks(db, timeout, query, startTimeMs);
}

public static void lastTransactionChecks(GraphDatabaseService db, long timeout, String query, long timePassed) {
checkTransactionTime(timeout, timePassed);
public static void lastTransactionChecks(GraphDatabaseService db, long timeout, String query, long startTimeMs) {
checkTransactionTimeReasonable(timeout, startTimeMs);
checkTransactionNotInList(db, query);
}

public static void lastTransactionChecks(GraphDatabaseService db, String query, long timeBefore) {
lastTransactionChecks(db, DEFAULT_TIMEOUT, query, timeBefore);
}

public static void checkTransactionTime(long timeout, long timePassed) {
double timePassedDouble = (System.currentTimeMillis() - timePassed) / 1000.0;
/*
* Assert that a tx finished within "reasonable" time compared to the timeout.
*
* Asserting that a transaction finishes within a certain timeout is hard.
* There are a lot of things that might cause a timeout to not be met,
* like gc pauses, thread starvation (we run tests in parallel), etc.
*/
public static void checkTransactionTimeReasonable(long timeout, long startTimeMs) {
final var reasonableFactor = 5;
double timePassedDouble = (System.currentTimeMillis() - startTimeMs) / 1000.0;

assertTrue(
"The transaction hasn't been terminated before the given timeout time (" + timeout + "), but after "
+ timePassedDouble + " seconds",
timePassedDouble <= timeout);
timePassedDouble <= (timeout * reasonableFactor));
}

public static void checkTransactionNotInList(GraphDatabaseService db, String query) {
Expand Down

0 comments on commit ecb7195

Please sign in to comment.