Remove time dependency from BlockTimer tests (#513)

tmohay 6 years ago committed by GitHub
parent 3136e311bb
commit d3a353e30a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 44
      consensus/ibft/src/test/java/tech/pegasys/pantheon/consensus/ibft/BlockTimerTest.java

@ -13,8 +13,6 @@
package tech.pegasys.pantheon.consensus.ibft; package tech.pegasys.pantheon.consensus.ibft;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
@ -30,7 +28,6 @@ import tech.pegasys.pantheon.ethereum.core.BlockHeader;
import tech.pegasys.pantheon.ethereum.core.BlockHeaderTestFixture; import tech.pegasys.pantheon.ethereum.core.BlockHeaderTestFixture;
import java.time.Clock; import java.time.Clock;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -44,6 +41,7 @@ import org.mockito.junit.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.StrictStubs.class) @RunWith(MockitoJUnitRunner.StrictStubs.class)
public class BlockTimerTest { public class BlockTimerTest {
private ScheduledExecutorService mockExecutorService; private ScheduledExecutorService mockExecutorService;
private IbftEventQueue mockQueue; private IbftEventQueue mockQueue;
private Clock mockClock; private Clock mockClock;
@ -95,7 +93,7 @@ public class BlockTimerTest {
} }
@Test @Test
public void aBlockTimerExpiryEventIsAddedToTheQueueOnExpiry() { public void aBlockTimerExpiryEventIsAddedToTheQueueOnExpiry() throws InterruptedException {
final long MINIMAL_TIME_BETWEEN_BLOCKS_SECONDS = 1; final long MINIMAL_TIME_BETWEEN_BLOCKS_SECONDS = 1;
final long NOW_MILLIS = 300_500L; final long NOW_MILLIS = 300_500L;
final long BLOCK_TIME_STAMP = 300; final long BLOCK_TIME_STAMP = 300;
@ -108,27 +106,33 @@ public class BlockTimerTest {
final ConsensusRoundIdentifier round = final ConsensusRoundIdentifier round =
new ConsensusRoundIdentifier(0xFEDBCA9876543210L, 0x12345678); new ConsensusRoundIdentifier(0xFEDBCA9876543210L, 0x12345678);
final ScheduledFuture<?> mockedFuture = mock(ScheduledFuture.class);
Mockito.<ScheduledFuture<?>>when(
mockExecutorService.schedule(any(Runnable.class), anyLong(), any()))
.thenReturn(mockedFuture);
final IbftEventQueue eventQueue = new IbftEventQueue();
final BlockTimer timer = final BlockTimer timer =
new BlockTimer( new BlockTimer(
mockQueue, eventQueue, MINIMAL_TIME_BETWEEN_BLOCKS_SECONDS, mockExecutorService, mockClock);
MINIMAL_TIME_BETWEEN_BLOCKS_SECONDS,
Executors.newSingleThreadScheduledExecutor(),
mockClock);
timer.startTimer(round, header); timer.startTimer(round, header);
// Verify that the event will not be added to the queue immediately // Verify that the event will not be added to the queue immediately
verify(mockQueue, never()).add(any()); assertThat(eventQueue.isEmpty()).isTrue();
await() // Verify that a task is sceheduled for EXPECTED_DELAY milliseconds in the future
.atMost(EXPECTED_DELAY + 200, TimeUnit.MILLISECONDS) ArgumentCaptor<Runnable> expiryTask = ArgumentCaptor.forClass(Runnable.class);
.atLeast(EXPECTED_DELAY - 200, TimeUnit.MILLISECONDS) verify(mockExecutorService, times(1))
.until(timer::isRunning, equalTo(false)); .schedule(expiryTask.capture(), eq(EXPECTED_DELAY), eq(TimeUnit.MILLISECONDS));
final ArgumentCaptor<IbftEvent> ibftEventCaptor = ArgumentCaptor.forClass(IbftEvent.class); // assert that the task puts a BlockExpired event into the queue
verify(mockQueue).add(ibftEventCaptor.capture()); final Runnable scheduledTask = expiryTask.getValue();
assertThat(eventQueue.isEmpty()).isTrue();
assertThat(ibftEventCaptor.getValue() instanceof BlockTimerExpiry).isTrue(); scheduledTask.run();
assertThat(((BlockTimerExpiry) ibftEventCaptor.getValue()).getRoundIndentifier()) assertThat(eventQueue.size()).isEqualTo(1);
final IbftEvent queuedEvent = eventQueue.poll(0, TimeUnit.SECONDS);
assertThat(queuedEvent).isInstanceOf(BlockTimerExpiry.class);
assertThat(((BlockTimerExpiry) queuedEvent).getRoundIndentifier())
.isEqualToComparingFieldByField(round); .isEqualToComparingFieldByField(round);
} }

Loading…
Cancel
Save