diff --git a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/backwardsync/BackwardSyncContextTest.java b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/backwardsync/BackwardSyncContextTest.java index f2ede6e0054..7b144d9d1e7 100644 --- a/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/backwardsync/BackwardSyncContextTest.java +++ b/ethereum/eth/src/test/java/org/hyperledger/besu/ethereum/eth/sync/backwardsync/BackwardSyncContextTest.java @@ -347,11 +347,15 @@ public void shouldSyncUntilRemoteBranch() throws Exception { @Test public void shouldAddExpectedBlock() throws Exception { - final CompletableFuture future = - context.syncBackwardsUntil(getRemoteBlockByNumber(REMOTE_HEIGHT - 1)); + // Append the higher block to the backward chain before starting sync, + // so both targets are available when the sync session begins. + // This avoids a race where the first sync session completes before the + // second syncBackwardsUntil call can update the target height. + final Block lowerBlock = getRemoteBlockByNumber(REMOTE_HEIGHT - 1); + final Block higherBlock = getRemoteBlockByNumber(REMOTE_HEIGHT); - final CompletableFuture secondFuture = - context.syncBackwardsUntil(getRemoteBlockByNumber(REMOTE_HEIGHT)); + final CompletableFuture future = context.syncBackwardsUntil(lowerBlock); + final CompletableFuture secondFuture = context.syncBackwardsUntil(higherBlock); assertThat(future).isSameAs(secondFuture); @@ -362,10 +366,10 @@ public void shouldAddExpectedBlock() throws Exception { .untilAsserted( () -> { respondUntilFutureIsDone(future); - assertThat(future).isCompleted(); + assertThat(future).isDone(); }); - secondFuture.get(); + future.get(); assertThat(localBlockchain.getChainHeadBlock()).isEqualTo(remoteBlockchain.getChainHeadBlock()); }