• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

bernardladenthin / BitcoinAddressFinder / #358

25 Jul 2025 10:40PM UTC coverage: 72.611% (+0.2%) from 72.394%
#358

push

bernardladenthin
Extract AbstractKeyProducerQueueBuffered

101 of 108 new or added lines in 8 files covered. (93.52%)

2 existing lines in 1 file now uncovered.

1649 of 2271 relevant lines covered (72.61%)

0.73 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.63
/src/main/java/net/ladenthin/bitcoinaddressfinder/keyproducer/AbstractKeyProducerQueueBuffered.java
1
// @formatter:off
2
/**
3
 * Copyright 2025 Bernard Ladenthin bernard.ladenthin@gmail.com
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *    http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 */
18
// @formatter:on
19
package net.ladenthin.bitcoinaddressfinder.keyproducer;
20

21
import java.math.BigInteger;
22
import java.util.concurrent.BlockingQueue;
23
import java.util.concurrent.LinkedBlockingQueue;
24
import java.util.concurrent.TimeUnit;
25
import net.ladenthin.bitcoinaddressfinder.KeyUtility;
26
import net.ladenthin.bitcoinaddressfinder.PublicKeyBytes;
27
import net.ladenthin.bitcoinaddressfinder.configuration.CKeyProducerJavaReceiver;
28
import org.slf4j.Logger;
29

30
/**
31
 * Base class to manage secret buffering using a blocking queue.
32
 * Intended for streaming protocols like WebSocket or ZMQ.
33
 */
34
public abstract class AbstractKeyProducerQueueBuffered<T extends CKeyProducerJavaReceiver> extends KeyProducerJava<T> {
35

36
    protected final KeyUtility keyUtility;
37
    protected final BlockingQueue<byte[]> secretQueue = new LinkedBlockingQueue<>();
1✔
38
    protected volatile boolean shouldStop = false;
1✔
39
    
40
    public AbstractKeyProducerQueueBuffered(T config, KeyUtility keyUtility, Logger logger) {
41
        super(config, logger);
1✔
42
        this.keyUtility = keyUtility;
1✔
43
    }
1✔
44

45
    @Override
46
    public BigInteger[] createSecrets(int overallWorkSize, boolean returnStartSecretOnly) throws NoMoreSecretsAvailableException {
47
        verifyWorkSize(overallWorkSize, cKeyProducerJava.maxWorkSize);
1✔
48

49
        int length = returnStartSecretOnly ? 1 : overallWorkSize;
1✔
50
        BigInteger[] secrets = new BigInteger[length];
1✔
51

52
        for (int i = 0; i < length; i++) {
1✔
53
            if (shouldStop) {
1✔
54
                throw new NoMoreSecretsAvailableException("Interrupted while waiting for secrets");
1✔
55
            }
56

57
            try {
58
                byte[] secret = secretQueue.poll(getReadTimeout(), TimeUnit.MILLISECONDS);
1✔
59

60
                if (secret == null) {
1✔
61
                    throw new NoMoreSecretsAvailableException("Timeout while waiting for secret");
1✔
62
                }
63

64
                if (secret.length != PublicKeyBytes.PRIVATE_KEY_MAX_NUM_BYTES) {
1✔
65
                    throw new NoMoreSecretsAvailableException("Invalid secret length: " + secret.length);
1✔
66
                }
67

68
                secrets[i] = keyUtility.bigIntegerFromUnsignedByteArray(secret);
1✔
69

70
                if (cKeyProducerJava.logReceivedSecret) {
1✔
71
                    logger.info("Received key: {}", keyUtility.bigIntegerToFixedLengthHex(secrets[i]));
1✔
72
                }
73

NEW
74
            } catch (InterruptedException e) {
×
NEW
75
                Thread.currentThread().interrupt();
×
NEW
76
                throw new NoMoreSecretsAvailableException("Interrupted while polling secret", e);
×
77
            }
1✔
78
        }
79

80
        return secrets;
1✔
81
    }
82
    
83
    protected void sleep(int millis) {
84
        try {
85
            Thread.sleep(millis);
1✔
86
        } catch (InterruptedException ignored) {
1✔
87
            Thread.currentThread().interrupt();
1✔
88
        }
1✔
89
    }
1✔
90

91
    /**
92
     * Add a raw secret (e.g. from socket, ZMQ, websocket).
93
     */
94
    protected void addSecret(byte[] secret) {
95
        if (!shouldStop) {
1✔
96
            secretQueue.offer(secret);
1✔
97
        }
98
    }
1✔
99

100
    /**
101
     * Override to define how long createSecrets() should block.
102
     */
103
    protected abstract int getReadTimeout();
104
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc