Skip to content

Commit

Permalink
Refactoring. Add some AbstractProducer tests. Improve logging.
Browse files Browse the repository at this point in the history
  • Loading branch information
bernardladenthin committed Mar 20, 2024
1 parent f872389 commit 872aa2a
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,13 @@ public AbstractProducer(CProducer cProducer, Consumer consumer, KeyUtility keyUt

@Override
public void initProducer() {
logger.info("Init producer.");
this.state = ProducerState.INITIALIZED;
}

@Override
public void releaseProducer() {
logger.info("Release producer.");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ private void consumeKeysRunner() {
}
try {
consumeKeys(threadLocalReuseableByteBuffer);
// the consumeKeys method is looped inside, if the method returns it means the queue is empty
emptyConsumer.incrementAndGet();
Thread.sleep(consumerJava.delayEmptyConsumer);
} catch (InterruptedException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ public ProducerJavaSecretsFiles(CProducerJavaSecretsFiles producerJavaSecretsFil
super(producerJavaSecretsFiles, consumer, keyUtility, secretFactory);
this.producerJavaSecretsFiles = producerJavaSecretsFiles;
}

@Override
public void initProducer() {
}

@Override
public void produceKeys() {
Expand Down Expand Up @@ -88,10 +84,6 @@ private void logProgress() {
logger.info("Progress: Unsupported: " + readStatistic.unsupported + ". Errors: " + readStatistic.errors.size() + ". Current File progress: " + String.format("%.2f", readStatistic.currentFileProgress) + "%.");
}

@Override
public void releaseProducer() {
}

@Override
public void interrupt() {
super.interrupt();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ public ProducerOpenCL(CProducerOpenCL producerOpenCL, Consumer consumer, KeyUtil

@Override
public void initProducer() {
super.initProducer();
openCLContext = new OpenCLContext(producerOpenCL);
try {
openCLContext.init();
this.state = ProducerState.INITIALIZED;
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -110,6 +110,7 @@ int getFreeThreads() {

@Override
public void releaseProducer() {
super.releaseProducer();
if (openCLContext != null) {
openCLContext.release();
openCLContext = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,36 @@ public class AbstractProducerTest {
@Rule
public TemporaryFolder folder = new TemporaryFolder();

private ArgumentCaptor<String> logCaptor = ArgumentCaptor.forClass(String.class);

protected final NetworkParameters networkParameters = MainNetParams.get();
protected final KeyUtility keyUtility = new KeyUtility(networkParameters, new ByteBufferUtility(false));

// <editor-fold defaultstate="collapsed" desc="initProducer">
@Test
public void initProducer_configurationGiven_stateInitializedAndLogged() throws IOException, InterruptedException {
CProducer cProducer = new CProducer();
MockConsumer mockConsumer = new MockConsumer();
Random random = new Random(1);
MockSecretFactory mockSecretFactory = new MockSecretFactory(keyUtility, random);
AbstractProducerTestImpl abstractProducerTestImpl = new AbstractProducerTestImpl(cProducer, mockConsumer, keyUtility, mockSecretFactory);

verifyInitProducer(abstractProducerTestImpl);
}
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="initProducer">
@Test
public void releaseProducer_configurationGiven_stateInitializedAndLogged() throws IOException, InterruptedException {
CProducer cProducer = new CProducer();
MockConsumer mockConsumer = new MockConsumer();
Random random = new Random(1);
MockSecretFactory mockSecretFactory = new MockSecretFactory(keyUtility, random);
AbstractProducerTestImpl abstractProducerTestImpl = new AbstractProducerTestImpl(cProducer, mockConsumer, keyUtility, mockSecretFactory);

verifyReleaseProducer(abstractProducerTestImpl);
}
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="createSecretBase">
@Test
@UseDataProvider(value = CommonDataProvider.DATA_PROVIDER_CREATE_SECRET_BASE_LOGGED, location = CommonDataProvider.class)
public void createSecretBase_secretGiven_bitsKilledAndLogged(String givenSecret, int gridNumBits, String expectedSecretBase, String logInfo0, String logTrace0, String logTrace1, String logTrace2, String logTrace3, String logTrace4) throws IOException, InterruptedException, DecoderException {
Expand All @@ -81,6 +106,7 @@ public void createSecretBase_secretGiven_bitsKilledAndLogged(String givenSecret,
// assert
assertThat(Hex.encodeHexString(secretBase.toByteArray()), is(equalTo(expectedSecretBase)));

ArgumentCaptor<String> logCaptor = ArgumentCaptor.forClass(String.class);
List<String> arguments = logCaptor.getAllValues();
// assert log secret base
{
Expand Down Expand Up @@ -120,6 +146,7 @@ public void createSecretBase_secretGivenAndLogSecretBaseDisabledTraceEnabled_bit
// act
abstractProducerTestImpl.createSecretBase(secret, logSecretBase);

ArgumentCaptor<String> logCaptor = ArgumentCaptor.forClass(String.class);
// assert
// assert log secret base
{
Expand Down Expand Up @@ -153,6 +180,7 @@ public void createSecretBase_secretGivenAndLogSecretBaseEnabledTraceDisabled_bit
// act
abstractProducerTestImpl.createSecretBase(secret, logSecretBase);

ArgumentCaptor<String> logCaptor = ArgumentCaptor.forClass(String.class);
// assert
// assert log secret base
{
Expand All @@ -163,5 +191,50 @@ public void createSecretBase_secretGivenAndLogSecretBaseEnabledTraceDisabled_bit
verify(logger, times(0)).trace(logCaptor.capture());
}
}
// </editor-fold>

public static void verifyReleaseProducer(AbstractProducer abstractProducer) {
Logger logger = mock(Logger.class);
when(logger.isTraceEnabled()).thenReturn(true);
abstractProducer.setLogger(logger);

abstractProducer.initProducer();

// act
abstractProducer.releaseProducer();

// assert
assertThat(abstractProducer.state, is(equalTo(ProducerState.INITIALIZED)));

ArgumentCaptor<String> logCaptor = ArgumentCaptor.forClass(String.class);
List<String> arguments = logCaptor.getAllValues();
// assert log initProducer
{
verify(logger, times(2)).info(logCaptor.capture());
assertThat(arguments.get(1), is(equalTo("Release producer.")));
}
}

public static void verifyInitProducer(AbstractProducer abstractProducer) {
Logger logger = mock(Logger.class);
when(logger.isTraceEnabled()).thenReturn(true);
abstractProducer.setLogger(logger);

// pre-assert
assertThat(abstractProducer.state, is(equalTo(ProducerState.UNINITIALIZED)));

// act
abstractProducer.initProducer();

// assert
assertThat(abstractProducer.state, is(equalTo(ProducerState.INITIALIZED)));

ArgumentCaptor<String> logCaptor = ArgumentCaptor.forClass(String.class);
List<String> arguments = logCaptor.getAllValues();
// assert log initProducer
{
verify(logger, times(1)).info(logCaptor.capture());
assertThat(arguments.get(0), is(equalTo("Init producer.")));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,34 @@ public String getHex() {
return ECKey.fromPrivate(getBigInteger(), false).getPrivateKeyAsHex();
}
}

// <editor-fold defaultstate="collapsed" desc="initProducer">
@Test
public void initProducer_configurationGiven_stateInitializedAndLogged() throws IOException, InterruptedException {
CProducerJavaSecretsFiles cProducerJavaSecretsFiles = new CProducerJavaSecretsFiles();
MockConsumer mockConsumer = new MockConsumer();
Random random = new Random(1);
MockSecretFactory mockSecretFactory = new MockSecretFactory(keyUtility, random);
ProducerJava producerJava = new ProducerJavaSecretsFiles(cProducerJavaSecretsFiles, mockConsumer, keyUtility, mockSecretFactory);

AbstractProducerTest.verifyInitProducer(producerJava);
}
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="releaseProducer">
@Test
public void releaseProducer_configurationGiven_stateInitializedAndLogged() throws IOException, InterruptedException {
CProducerJavaSecretsFiles cProducerJavaSecretsFiles = new CProducerJavaSecretsFiles();
MockConsumer mockConsumer = new MockConsumer();
Random random = new Random(1);
MockSecretFactory mockSecretFactory = new MockSecretFactory(keyUtility, random);
ProducerJava producerJava = new ProducerJavaSecretsFiles(cProducerJavaSecretsFiles, mockConsumer, keyUtility, mockSecretFactory);

AbstractProducerTest.verifyReleaseProducer(producerJava);
}
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="produceKeys">
@Test
public void produceKeys_noFileConfigured_noKeysCreated() throws IOException, InterruptedException {
CProducerJavaSecretsFiles cProducerJavaSecretsFiles = new CProducerJavaSecretsFiles();
Expand Down Expand Up @@ -145,6 +172,7 @@ public void produceKeys_filesConfigured_keysCreated(CSecretFormat cSecretFormat)
assertThat(mockConsumer.publicKeyBytesArrayList.get(4)[0], is(equalTo(PrivateKey.WITH_COMMENT.getPublicKeyBytes())));
assertThat(mockConsumer.publicKeyBytesArrayList.get(5)[0], is(equalTo(PrivateKey.WITH_SPECIAL_CHARACTER.getPublicKeyBytes())));
}
// </editor-fold>

private List<File> createSecretsFiles(CSecretFormat secretFormat) throws IOException {
List<File> fileList = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,33 @@ public class ProducerJavaTest {
protected final NetworkParameters networkParameters = MainNetParams.get();
protected final KeyUtility keyUtility = new KeyUtility(networkParameters, new ByteBufferUtility(false));

// <editor-fold defaultstate="collapsed" desc="initProducer">
@Test
public void initProducer_configurationGiven_stateInitializedAndLogged() throws IOException, InterruptedException {
CProducerJava cProducerJava = new CProducerJava();
MockConsumer mockConsumer = new MockConsumer();
Random random = new Random(1);
MockSecretFactory mockSecretFactory = new MockSecretFactory(keyUtility, random);
ProducerJava producerJava = new ProducerJava(cProducerJava, mockConsumer, keyUtility, mockSecretFactory);

AbstractProducerTest.verifyInitProducer(producerJava);
}
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="releaseProducer">
@Test
public void releaseProducer_configurationGiven_stateInitializedAndLogged() throws IOException, InterruptedException {
CProducerJava cProducerJava = new CProducerJava();
MockConsumer mockConsumer = new MockConsumer();
Random random = new Random(1);
MockSecretFactory mockSecretFactory = new MockSecretFactory(keyUtility, random);
ProducerJava producerJava = new ProducerJava(cProducerJava, mockConsumer, keyUtility, mockSecretFactory);

AbstractProducerTest.verifyReleaseProducer(producerJava);
}
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="produceKeys">
@Test
public void produceKeys_GridNumBitsEqualsKeyMaxNumBits_noExceptionThrown() throws IOException, InterruptedException {
CProducerJava cProducerJava = new CProducerJava();
Expand Down Expand Up @@ -142,5 +169,6 @@ public void produceKeys_SomeBitRanges_consumerContainsData() throws IOException,
assertThat(mockConsumer.publicKeyBytesArrayList.get(0)[6], is(equalTo(PublicKeyBytes.fromPrivate(BigInteger.valueOf(62)))));
assertThat(mockConsumer.publicKeyBytesArrayList.get(0)[7], is(equalTo(PublicKeyBytes.fromPrivate(BigInteger.valueOf(63)))));
}
// </editor-fold>

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,27 +42,59 @@ public class ProducerOpenCLTest {

protected final NetworkParameters networkParameters = MainNetParams.get();
protected final KeyUtility keyUtility = new KeyUtility(networkParameters, new ByteBufferUtility(false));


// <editor-fold defaultstate="collapsed" desc="initProducer">
@OpenCLTest
@Test
public void initProducer_XYZ_openCLContextSet() throws IOException, InterruptedException {
public void initProducer_configurationGiven_stateInitializedAndLogged() throws IOException, InterruptedException {
new OpenCLPlatformAssume().assumeOpenCLLibraryLoadableAndOneOpenCL2_0OrGreaterDeviceAvailable();
CProducerOpenCL cProducerOpenCL = new CProducerOpenCL();
MockConsumer mockConsumer = new MockConsumer();
Random random = new Random(1);
MockSecretFactory mockSecretFactory = new MockSecretFactory(keyUtility, random);
ProducerOpenCL producerOpenCL = new ProducerOpenCL(cProducerOpenCL, mockConsumer, keyUtility, mockSecretFactory);

AbstractProducerTest.verifyInitProducer(producerOpenCL);
}
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="releaseProducer">
@OpenCLTest
@Test
public void releaseProducer_configurationGiven_stateInitializedAndLogged() throws IOException, InterruptedException {
new OpenCLPlatformAssume().assumeOpenCLLibraryLoadableAndOneOpenCL2_0OrGreaterDeviceAvailable();
CProducerOpenCL cProducerOpenCL = new CProducerOpenCL();
MockConsumer mockConsumer = new MockConsumer();
Random random = new Random(1);
MockSecretFactory mockSecretFactory = new MockSecretFactory(keyUtility, random);
ProducerOpenCL producerOpenCL = new ProducerOpenCL(cProducerOpenCL, mockConsumer, keyUtility, mockSecretFactory);

AbstractProducerTest.verifyReleaseProducer(producerOpenCL);
}
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="initProducer">
@OpenCLTest
@Test
public void initProducer_configurationGiven_stateInitializedAndOpenCLContextSet() throws IOException, InterruptedException {
new OpenCLPlatformAssume().assumeOpenCLLibraryLoadableAndOneOpenCL2_0OrGreaterDeviceAvailable();
CProducerOpenCL cProducerOpenCL = new CProducerOpenCL();
MockConsumer mockConsumer = new MockConsumer();
Random random = new Random(1);
MockSecretFactory mockSecretFactory = new MockSecretFactory(keyUtility, random);
ProducerOpenCL producerOpenCL = new ProducerOpenCL(cProducerOpenCL, mockConsumer, keyUtility, mockSecretFactory);

// pre-assert
assertThat(producerOpenCL.openCLContext, nullValue());
assertThat(producerOpenCL.state, is(equalTo(ProducerState.UNINITIALIZED)));

// act
producerOpenCL.initProducer();

// assert
assertThat(producerOpenCL.openCLContext, notNullValue());
assertThat(producerOpenCL.state, is(equalTo(ProducerState.INITIALIZED)));
}
// </editor-fold>

Expand Down Expand Up @@ -104,7 +136,7 @@ public void releaseProducers_initialized_noExceptionThrownAndOpenCLContextFreed(
}
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="releaseProducers">
// <editor-fold defaultstate="collapsed" desc="getFreeThreads">
@Test
public void getFreeThreads_notInitialized_numberOfFreeThreadsReturned() throws IOException, InterruptedException {
CProducerOpenCL cProducerOpenCL = new CProducerOpenCL();
Expand Down

0 comments on commit 872aa2a

Please sign in to comment.