Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MINOR] move Clique tests to junit5 #4975

Merged
merged 5 commits into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion consensus/clique/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ dependencies {
testImplementation project(path: ':ethereum:core', configuration: 'testSupportArtifacts')
testImplementation project(':testutil')

testImplementation 'junit:junit'
testImplementation 'org.assertj:assertj-core'
testImplementation 'org.junit.jupiter:junit-jupiter'
testImplementation 'org.mockito:mockito-core'
testImplementation 'org.mockito:mockito-junit-jupiter'

testRuntimeOnly 'org.junit.vintage:junit-vintage-engine'
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
import java.util.stream.IntStream;

import com.google.common.collect.Lists;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class CliqueBlockChoiceTests {
private List<KeyPair> keyPairs;
Expand All @@ -57,7 +57,7 @@ private Block createEmptyBlock(final KeyPair blockSigner) {
return new Block(header, new BlockBody(Lists.newArrayList(), Lists.newArrayList()));
}

@Before
@BeforeEach
public void setup() {
keyPairs =
IntStream.range(0, 8)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
import java.util.List;

import org.apache.tuweni.bytes.Bytes;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class CliqueBlockHashingTest {

Expand All @@ -43,7 +43,7 @@ public class CliqueBlockHashingTest {
private static final Hash KNOWN_BLOCK_HASH =
Hash.fromHexString("0x8b27a29300811af926039b90288d3d384dcb55931049c17c4f762e45c116776e");

@Before
@BeforeEach
public void setup() {
expectedHeader = createKnownHeaderFromCapturedData();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import java.util.List;
import java.util.Optional;

import org.junit.Test;
import org.junit.jupiter.api.Test;

public class CliqueBlockInterfaceTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
import java.util.List;

import com.google.common.collect.Lists;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class CliqueDifficultyCalculatorTest {

Expand All @@ -46,7 +46,7 @@ public class CliqueDifficultyCalculatorTest {
private BlockHeaderTestFixture blockHeaderBuilder;
private final CliqueBlockInterface blockInterface = new CliqueBlockInterface();

@Before
@BeforeEach
public void setup() {
localAddr = Util.publicKeyToAddress(proposerKeyPair.getPublicKey());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import com.google.common.collect.Lists;
import org.apache.tuweni.bytes.Bytes;
import org.bouncycastle.util.encoders.Hex;
import org.junit.Test;
import org.junit.jupiter.api.Test;

public class CliqueExtraDataTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

import java.time.Instant;

import org.junit.Test;
import org.junit.jupiter.api.Test;

public class CliqueProtocolScheduleTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
import java.util.List;

import com.google.common.collect.Lists;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class NodeCanProduceNextBlockTest {

Expand All @@ -62,7 +62,7 @@ private Block createEmptyBlock(final KeyPair blockSigner) {
return new Block(header, new BlockBody(Lists.newArrayList(), Lists.newArrayList()));
}

@Before
@BeforeEach
public void setup() {
localAddress = Util.publicKeyToAddress(proposerKeyPair.getPublicKey());
validatorList.add(localAddress);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@
import com.google.common.collect.Lists;
import org.apache.tuweni.bytes.Bytes;
import org.assertj.core.api.Java6Assertions;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class CliqueBlockCreatorTest {

Expand All @@ -84,7 +84,7 @@ public class CliqueBlockCreatorTest {
private ValidatorProvider validatorProvider;
private VoteProvider voteProvider;

@Before
@BeforeEach
public void setup() {
protocolSchedule =
CliqueProtocolSchedule.create(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
import java.util.List;

import com.google.common.collect.Lists;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class CliqueBlockSchedulerTest {

Expand All @@ -45,7 +45,7 @@ public class CliqueBlockSchedulerTest {
private ValidatorProvider validatorProvider;
private BlockHeaderTestFixture blockHeaderBuilder;

@Before
@BeforeEach
public void setup() {
localAddr = Util.publicKeyToAddress(proposerKeyPair.getPublicKey());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@

import com.google.common.collect.Lists;
import org.apache.tuweni.bytes.Bytes;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class CliqueMinerExecutorTest {

Expand All @@ -69,7 +69,7 @@ public class CliqueMinerExecutorTest {
private final MetricsSystem metricsSystem = new NoOpMetricsSystem();
private final CliqueBlockInterface blockInterface = new CliqueBlockInterface();

@Before
@BeforeEach
public void setup() {
localAddress = Util.publicKeyToAddress(proposerNodeKey.getPublicKey());
validatorList.add(localAddress);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import org.assertj.core.util.Lists;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.jupiter.MockitoExtension;

@RunWith(MockitoJUnitRunner.class)
@ExtendWith(MockitoExtension.class)
public class CliqueMiningCoordinatorTest {

private static final Supplier<SignatureAlgorithm> SIGNATURE_ALGORITHM =
Expand All @@ -81,18 +81,13 @@ public class CliqueMiningCoordinatorTest {
@Mock private SyncState syncState;
@Mock private ValidatorProvider validatorProvider;

@Before
@BeforeEach
public void setup() {

headerTestFixture.number(1);
Block genesisBlock = createEmptyBlock(0, Hash.ZERO, proposerKeys); // not normally signed but ok
blockChain = createInMemoryBlockchain(genesisBlock);

when(validatorProvider.getValidatorsAfterBlock(any())).thenReturn(validators);
final CliqueContext cliqueContext = new CliqueContext(validatorProvider, null, blockInterface);

when(protocolContext.getConsensusContext(CliqueContext.class)).thenReturn(cliqueContext);
when(protocolContext.getBlockchain()).thenReturn(blockChain);
when(minerExecutor.startAsyncMining(any(), any(), any())).thenReturn(Optional.of(blockMiner));
when(syncState.isInSync()).thenReturn(true);

Expand All @@ -101,6 +96,8 @@ public void setup() {

@Test
public void outOfTurnBlockImportedDoesNotInterruptInTurnMiningOperation() {
setupCliqueContextAndBlockchain();

// As the head of the blockChain is 0 (which effectively doesn't have a signer, all validators
// are able to propose.

Expand Down Expand Up @@ -159,6 +156,8 @@ public void outOfTurnBlockImportedAtHigherLevelInterruptsMiningOperation() {

@Test
public void outOfTurnBlockImportedInterruptsOutOfTurnMiningOperation() {
setupCliqueContextAndBlockchain();

blockChain.appendBlock(
createEmptyBlock(1, blockChain.getChainHeadHash(), validatorKeys), Collections.emptyList());

Expand Down Expand Up @@ -190,6 +189,8 @@ public void outOfTurnBlockImportedInterruptsOutOfTurnMiningOperation() {

@Test
public void outOfTurnBlockImportedInterruptsNonRunningMiner() {
setupCliqueContextAndBlockchain();

blockChain.appendBlock(
createEmptyBlock(1, blockChain.getChainHeadHash(), proposerKeys), Collections.emptyList());

Expand Down Expand Up @@ -251,4 +252,13 @@ private Block createEmptyBlock(
TestHelpers.createCliqueSignedBlockHeader(headerTestFixture, signer, validators);
return new Block(header, new BlockBody(Collections.emptyList(), Collections.emptyList()));
}

private void setupCliqueContextAndBlockchain() {
when(validatorProvider.getValidatorsAfterBlock(any())).thenReturn(validators);

final CliqueContext cliqueContext = new CliqueContext(validatorProvider, null, blockInterface);
when(protocolContext.getConsensusContext(CliqueContext.class)).thenReturn(cliqueContext);

when(protocolContext.getBlockchain()).thenReturn(blockChain);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
import java.util.Arrays;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class CliqueProposerSelectorTest {

Expand All @@ -40,7 +40,7 @@ public class CliqueProposerSelectorTest {
AddressHelpers.ofValue(4));
private ValidatorProvider validatorProvider;

@Before
@BeforeEach
public void setup() {
validatorProvider = mock(ValidatorProvider.class);
when(validatorProvider.getValidatorsAfterBlock(any())).thenReturn(validatorList);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
import java.util.List;

import com.google.common.collect.Lists;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class CliqueDifficultyValidationRuleTest {

Expand All @@ -47,7 +47,7 @@ public class CliqueDifficultyValidationRuleTest {
private BlockHeaderTestFixture blockHeaderBuilder;
private final CliqueBlockInterface blockInterface = new CliqueBlockInterface();

@Before
@BeforeEach
public void setup() {
final Address localAddress = Util.publicKeyToAddress(proposerKeyPair.getPublicKey());
validatorList.add(localAddress);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@

import com.google.common.collect.Lists;
import org.apache.tuweni.bytes.Bytes;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class CliqueExtraDataValidationRuleTest {

Expand All @@ -50,7 +50,7 @@ public class CliqueExtraDataValidationRuleTest {
private final List<Address> validatorList = Lists.newArrayList();
private ProtocolContext cliqueProtocolContext;

@Before
@BeforeEach
public void setup() {
localAddr = Util.publicKeyToAddress(proposerKeyPair.getPublicKey());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,39 +20,33 @@
import org.hyperledger.besu.ethereum.core.BlockHeader;
import org.hyperledger.besu.ethereum.core.BlockHeaderTestFixture;

import java.util.Arrays;
import java.util.Collection;
import java.util.stream.Stream;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;

@RunWith(Parameterized.class)
public class VoteValidationRuleTest {

@Parameters
public static Collection<Object[]> data() {
return Arrays.asList(
new Object[][] {
{CliqueBlockInterface.DROP_NONCE, true},
{CliqueBlockInterface.ADD_NONCE, true},
{0x01L, false},
{0xFFFFFFFFFFFFFFFEL, false}
});
static class CliqueVoteValidationArgumentsProvider implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(final ExtensionContext context) {
return Stream.of(
Arguments.of(CliqueBlockInterface.DROP_NONCE, true),
Arguments.of(CliqueBlockInterface.ADD_NONCE, true),
Arguments.of(0x01L, false),
Arguments.of(0xFFFFFFFFFFFFFFFEL, false));
}
}

@Parameter public long actualVote;

@Parameter(1)
public boolean expectedResult;

@Test
public void test() {
@ParameterizedTest
@ArgumentsSource(CliqueVoteValidationArgumentsProvider.class)
public void test(final long input, final boolean expectedResult) {
final VoteValidationRule uut = new VoteValidationRule();
final BlockHeaderTestFixture blockBuilder = new BlockHeaderTestFixture();
blockBuilder.nonce(actualVote);
blockBuilder.nonce(input);

final BlockHeader header = blockBuilder.buildHeader();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
import java.util.Optional;
import java.util.stream.LongStream;

import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class CliqueGetSignerMetricsTest {

Expand All @@ -58,7 +58,7 @@ public class CliqueGetSignerMetricsTest {
private BlockchainQueries blockchainQueries;
private BlockInterface blockInterface;

@Before
@BeforeEach
public void setup() {
validatorProvider = mock(ValidatorProvider.class);
blockchainQueries = mock(BlockchainQueries.class);
Expand Down
Loading