From 62006152e49fe7b05b792a15032b24c86a543bec Mon Sep 17 00:00:00 2001 From: Bernard Ladenthin Date: Wed, 27 Mar 2024 10:45:14 +0100 Subject: [PATCH] Improve tests. --- .../bitcoinaddressfinder/ConsumerJava.java | 5 --- .../bitcoinaddressfinder/AwaitTimeTest.java | 34 +++++++++++++++++++ .../ConsumerJavaTest.java | 6 ++-- .../bitcoinaddressfinder/FinderTest.java | 6 ++-- .../bitcoinaddressfinder/MainTest.java | 14 +++++++- 5 files changed, 51 insertions(+), 14 deletions(-) create mode 100644 src/test/java/net/ladenthin/bitcoinaddressfinder/AwaitTimeTest.java diff --git a/src/main/java/net/ladenthin/bitcoinaddressfinder/ConsumerJava.java b/src/main/java/net/ladenthin/bitcoinaddressfinder/ConsumerJava.java index ed3ab96..eaee439 100644 --- a/src/main/java/net/ladenthin/bitcoinaddressfinder/ConsumerJava.java +++ b/src/main/java/net/ladenthin/bitcoinaddressfinder/ConsumerJava.java @@ -55,11 +55,6 @@ public class ConsumerJava implements Consumer { @VisibleForTesting static Duration AWAIT_DURATION_QUEUE_EMPTY = Duration.ofMinutes(1); - /** - * The duration for a cyclic check to test the keys queue is empty. - */ - private static final Duration DURATION_CYCLIC_CHECK_KEYS_QUEUE_EMPTY = Duration.ofMillis(100L); - public static final String MISS_PREFIX = "miss: Could not find the address: "; public static final String HIT_PREFIX = "hit: Found the address: "; public static final String VANITY_HIT_PREFIX = "vanity pattern match: "; diff --git a/src/test/java/net/ladenthin/bitcoinaddressfinder/AwaitTimeTest.java b/src/test/java/net/ladenthin/bitcoinaddressfinder/AwaitTimeTest.java new file mode 100644 index 0000000..952e1e1 --- /dev/null +++ b/src/test/java/net/ladenthin/bitcoinaddressfinder/AwaitTimeTest.java @@ -0,0 +1,34 @@ +// @formatter:off +/** + * Copyright 2024 Bernard Ladenthin bernard.ladenthin@gmail.com + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +// @formatter:on +package net.ladenthin.bitcoinaddressfinder; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.SOURCE) +@Target(ElementType.METHOD) + +/** + * It is an await time test. This tests changes timing durations. Especially for {@link java.util.concurrent.ExecutorService#awaitTermination}. + */ +public @interface AwaitTimeTest { + +} \ No newline at end of file diff --git a/src/test/java/net/ladenthin/bitcoinaddressfinder/ConsumerJavaTest.java b/src/test/java/net/ladenthin/bitcoinaddressfinder/ConsumerJavaTest.java index 032e316..a04137c 100644 --- a/src/test/java/net/ladenthin/bitcoinaddressfinder/ConsumerJavaTest.java +++ b/src/test/java/net/ladenthin/bitcoinaddressfinder/ConsumerJavaTest.java @@ -123,12 +123,10 @@ public void startStatisticsTimer_invalidparameter_throwsException() throws IOExc consumerJava.startStatisticsTimer(); } - /** - * Attention, this is an await time test. This tests changes {@link ConsumerJava#AWAIT_DURATION_QUEUE_EMPTY}. - */ + @AwaitTimeTest @Test public void interrupt_keysQueueNotEmpty_consumerNotRunningWaitedInternallyForTheDuration() throws IOException, InterruptedException, MnemonicException.MnemonicLengthException { - // Attention: Change the duration. + // Change await duration ConsumerJava.AWAIT_DURATION_QUEUE_EMPTY = AwaitTimeTests.AWAIT_DURATION; CConsumerJava cConsumerJava = new CConsumerJava(); diff --git a/src/test/java/net/ladenthin/bitcoinaddressfinder/FinderTest.java b/src/test/java/net/ladenthin/bitcoinaddressfinder/FinderTest.java index ac3b3e5..fb69b02 100644 --- a/src/test/java/net/ladenthin/bitcoinaddressfinder/FinderTest.java +++ b/src/test/java/net/ladenthin/bitcoinaddressfinder/FinderTest.java @@ -118,12 +118,10 @@ public void shutdownAndAwaitTermination_producersSetAndNotInitialized_shutdownCa assertThat(finder.producerExecutorService.isTerminated(), is(equalTo(Boolean.TRUE))); } - /** - * Attention, this is an await time test. This tests changes {@link Finder#AWAIT_DURATION_TERMINATE}. - */ + @AwaitTimeTest @Test public void shutdownAndAwaitTermination_producersSetAndInitialized_shutdownCalledAndAwaitTermination() throws IOException { - // Attention: Change the duration. + // Change await duration Finder.AWAIT_DURATION_TERMINATE = AwaitTimeTests.AWAIT_DURATION; // Attention: During the long duration, this test produce a lot of debug and warn output, prevent it by set the log details diff --git a/src/test/java/net/ladenthin/bitcoinaddressfinder/MainTest.java b/src/test/java/net/ladenthin/bitcoinaddressfinder/MainTest.java index 92fc9c7..83d0b2e 100644 --- a/src/test/java/net/ladenthin/bitcoinaddressfinder/MainTest.java +++ b/src/test/java/net/ladenthin/bitcoinaddressfinder/MainTest.java @@ -26,6 +26,9 @@ import java.io.IOException; import java.nio.file.Path; import java.util.List; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; import net.ladenthin.bitcoinaddressfinder.cli.Main; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; @@ -69,7 +72,6 @@ public void testRoundtrip_configurationsGiven_lmdbCreatedExportedAndRunFindSecre } // - // @Test @OpenCLTest @@ -82,6 +84,16 @@ public void testRoundtripOpenCLProducer_configurationsGiven_lmdbCreatedAndRunFin new LogLevelChange().setLevel(Level.DEBUG); Main mainFind_1OpenCLDevice = Main.createFromConfigurationFile(config_Find_1OpenCLDevice_js); + + + ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(); + + // interrupt the act after 10 seconds + scheduledExecutorService.schedule(() -> { + mainFind_1OpenCLDevice.interrupt(); + }, 10, TimeUnit.SECONDS); + + // act mainFind_1OpenCLDevice.run(); } //