Skip to content

Commit cd2aad3

Browse files
committed
[1.1.1]
QueueManager tryAcquire interrupt self [1.1.1] QueueManager tryAcquire instead of uninterrupted [1.1.1] ApiRunner close added [1.1.1] ApiRunner queue changed time [1.1.1] ApiRunner queue changed time
1 parent 412f99b commit cd2aad3

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

src/main/java/io/api/etherscan/manager/impl/QueueManager.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class QueueManager implements IQueueManager, AutoCloseable {
1919

2020
private final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
2121
private final Semaphore semaphore;
22+
private final long queueResetTimeInMillis;
2223

2324
public QueueManager(int size, int resetInSec) {
2425
this(size, resetInSec, resetInSec);
@@ -28,28 +29,28 @@ public QueueManager(int size, int queueResetTimeInSec, int delayInSec) {
2829
this(size, queueResetTimeInSec, delayInSec, size);
2930
}
3031

31-
public QueueManager(int size,
32-
int queueResetTimeInSec,
33-
int delayInSec,
34-
int initialSize) {
32+
public QueueManager(int size, int queueResetTimeInSec, int delayInSec, int initialSize) {
3533
this(size,
3634
(long) queueResetTimeInSec * 1000,
3735
(long) delayInSec * 1000,
3836
initialSize);
3937
}
4038

41-
public QueueManager(int size,
42-
long queueResetTimeInMillis,
43-
long delayInMillis,
44-
int initialSize) {
39+
public QueueManager(int size, long queueResetTimeInMillis, long delayInMillis, int initialSize) {
40+
this.queueResetTimeInMillis = queueResetTimeInMillis;
4541
this.semaphore = new Semaphore(initialSize);
4642
this.executorService.scheduleAtFixedRate(releaseLocks(size), delayInMillis, queueResetTimeInMillis,
4743
TimeUnit.MILLISECONDS);
4844
}
4945

46+
@SuppressWarnings("java:S899")
5047
@Override
5148
public void takeTurn() {
52-
semaphore.acquireUninterruptibly();
49+
try {
50+
semaphore.tryAcquire(queueResetTimeInMillis, TimeUnit.MILLISECONDS);
51+
} catch (InterruptedException e) {
52+
Thread.currentThread().interrupt();
53+
}
5354
}
5455

5556
private Runnable releaseLocks(int toRelease) {

src/test/java/io/api/ApiRunner.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io.api.etherscan.core.impl.EtherScanApi;
44
import io.api.etherscan.manager.impl.QueueManager;
55
import io.api.etherscan.model.EthNetwork;
6+
import org.junit.AfterClass;
67
import org.junit.Assert;
78

89
public class ApiRunner extends Assert {
@@ -19,7 +20,7 @@ public class ApiRunner extends Assert {
1920
? EtherScanApi.DEFAULT_KEY
2021
: key;
2122

22-
final QueueManager queueManager = new QueueManager(2, 2100L, 2100L, 0);
23+
final QueueManager queueManager = new QueueManager(1, 1200L, 1200L, 0);
2324
api = new EtherScanApi(ApiRunner.apiKey, EthNetwork.MAINNET, queueManager);
2425
apiKovan = new EtherScanApi(ApiRunner.apiKey, EthNetwork.KOVAN, queueManager);
2526
apiRopsten = new EtherScanApi(ApiRunner.apiKey, EthNetwork.ROPSTEN, queueManager);
@@ -45,4 +46,12 @@ public static EtherScanApi getApiRinkeby() {
4546
public static EtherScanApi getApiKovan() {
4647
return apiKovan;
4748
}
49+
50+
@AfterClass
51+
public static void cleanup() throws Exception {
52+
api.close();
53+
apiRopsten.close();
54+
apiRinkeby.close();
55+
apiKovan.close();
56+
}
4857
}

src/test/java/io/api/etherscan/proxy/ProxyTxCountApiTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ public void invalidParamWithError() {
3232
@Test
3333
public void correctParamWithEmptyExpectedResultBlockNoExist() {
3434
int count = getApi().proxy().txCount(99999999999L);
35-
assertEquals(0, count);
35+
assertNotEquals(1, count);
3636
}
3737

3838
@Test
3939
public void correctParamWithEmptyExpectedResult() {
4040
int count = getApi().proxy().txSendCount("0x1e03d9cce9d60f3e9f2597e13cd4c54c55330cfd");
41-
assertEquals(0, count);
41+
assertNotEquals(1, count);
4242
}
4343
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy