Skip to content

Commit a2588ae

Browse files
committed
Use Duration for retry period in BlockchainAdapterService
1 parent 9ce38c6 commit a2588ae

File tree

3 files changed

+17
-14
lines changed

3 files changed

+17
-14
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ All notable changes to this project will be documented in this file.
88

99
- Send up to 2 blockchain transactions per block.
1010
With a big enough latency, the nonce is properly computed in web3j library against the pending block. (#111)
11-
- Add `BlockchainAdapterService` class to implement interactions with REST API. (#117 #118 #119)
11+
- Add `BlockchainAdapterService` class to implement interactions with REST API. (#117 #118 #119 126)
1212
- Expose version through prometheus endpoint and through VersionController. (#122 #123)
1313

1414
### Bug Fixes

iexec-blockchain-adapter-api-library/src/main/java/com/iexec/blockchain/api/BlockchainAdapterService.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,18 @@
2020
import lombok.extern.slf4j.Slf4j;
2121
import org.apache.commons.lang3.StringUtils;
2222

23+
import java.time.Duration;
2324
import java.util.Optional;
2425
import java.util.function.Function;
2526

2627
@Slf4j
2728
public class BlockchainAdapterService {
2829

2930
private final BlockchainAdapterApiClient apiClient;
30-
private final long period;
31+
private final Duration period;
3132
private final int maxAttempts;
3233

33-
public BlockchainAdapterService(BlockchainAdapterApiClient apiClient, long period, int maxAttempts) {
34+
public BlockchainAdapterService(BlockchainAdapterApiClient apiClient, Duration period, int maxAttempts) {
3435
this.apiClient = apiClient;
3536
this.period = period;
3637
this.maxAttempts = maxAttempts;
@@ -73,7 +74,7 @@ public Optional<String> requestInitialize(String chainDealId, int taskIndex) {
7374
*/
7475
public Optional<Boolean> isInitialized(String chainTaskId) {
7576
return isCommandCompleted(apiClient::getStatusForInitializeTaskRequest,
76-
chainTaskId, period, maxAttempts);
77+
chainTaskId, maxAttempts);
7778
}
7879

7980
// endregion
@@ -119,7 +120,7 @@ public Optional<String> requestFinalize(String chainTaskId,
119120
*/
120121
public Optional<Boolean> isFinalized(String chainTaskId) {
121122
return isCommandCompleted(apiClient::getStatusForFinalizeTaskRequest,
122-
chainTaskId, period, maxAttempts);
123+
chainTaskId, maxAttempts);
123124
}
124125

125126
// endregion
@@ -129,7 +130,6 @@ public Optional<Boolean> isFinalized(String chainTaskId) {
129130
*
130131
* @param getCommandStatusFunction method for fetching the current command status from the adapter
131132
* @param chainTaskId ID of the task
132-
* @param period period in ms between consecutive checks
133133
* @param maxAttempts maximum number of attempts
134134
* @return an optional which will be:
135135
* <ul>
@@ -140,7 +140,7 @@ public Optional<Boolean> isFinalized(String chainTaskId) {
140140
*/
141141
Optional<Boolean> isCommandCompleted(
142142
Function<String, CommandStatus> getCommandStatusFunction,
143-
String chainTaskId, long period, int maxAttempts) {
143+
String chainTaskId, int maxAttempts) {
144144
for (int attempt = 0; attempt < maxAttempts; attempt++) {
145145
try {
146146
CommandStatus status = getCommandStatusFunction.apply(chainTaskId);
@@ -156,7 +156,7 @@ Optional<Boolean> isCommandCompleted(
156156
}
157157

158158
try {
159-
Thread.sleep(period);
159+
Thread.sleep(period.toMillis());
160160
} catch (InterruptedException e) {
161161
log.error("Polling on blockchain command was interrupted", e);
162162
Thread.currentThread().interrupt();

iexec-blockchain-adapter-api-library/src/test/java/com/iexec/blockchain/api/BlockchainAdapterServiceTests.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.mockito.Mock;
2424
import org.mockito.MockitoAnnotations;
2525

26+
import java.time.Duration;
2627
import java.util.Optional;
2728
import java.util.concurrent.CancellationException;
2829
import java.util.concurrent.ExecutorService;
@@ -39,7 +40,7 @@ class BlockchainAdapterServiceTests {
3940
static final String CHAIN_TASK_ID = "CHAIN_TASK_ID";
4041
static final String LINK = "link";
4142
static final String CALLBACK = "callback";
42-
static final int PERIOD = 10;
43+
static final Duration PERIOD = Duration.ofMillis(10);
4344
static final int MAX_ATTEMPTS = 3;
4445

4546
@Mock
@@ -134,7 +135,7 @@ void isCommandCompletedTrueWhenSuccess() {
134135
.thenReturn(CommandStatus.SUCCESS);
135136

136137
Optional<Boolean> commandCompleted = blockchainAdapterService.isCommandCompleted(
137-
blockchainAdapterClient::getStatusForInitializeTaskRequest, CHAIN_TASK_ID, PERIOD, MAX_ATTEMPTS);
138+
blockchainAdapterClient::getStatusForInitializeTaskRequest, CHAIN_TASK_ID, MAX_ATTEMPTS);
138139
assertEquals(Optional.of(true), commandCompleted);
139140
}
140141

@@ -146,7 +147,7 @@ void isCommandCompletedFalseWhenFailure() {
146147
.thenReturn(CommandStatus.FAILURE);
147148

148149
Optional<Boolean> commandCompleted = blockchainAdapterService.isCommandCompleted(
149-
blockchainAdapterClient::getStatusForInitializeTaskRequest, CHAIN_TASK_ID, PERIOD, MAX_ATTEMPTS);
150+
blockchainAdapterClient::getStatusForInitializeTaskRequest, CHAIN_TASK_ID, MAX_ATTEMPTS);
150151
assertEquals(Optional.of(false), commandCompleted);
151152
}
152153

@@ -155,7 +156,7 @@ void isCommandCompletedFalseWhenMaxAttempts() {
155156
when(blockchainAdapterClient.getStatusForInitializeTaskRequest(CHAIN_TASK_ID))
156157
.thenReturn(CommandStatus.PROCESSING);
157158
Optional<Boolean> commandCompleted = blockchainAdapterService.isCommandCompleted(
158-
blockchainAdapterClient::getStatusForInitializeTaskRequest, CHAIN_TASK_ID, PERIOD, MAX_ATTEMPTS);
159+
blockchainAdapterClient::getStatusForInitializeTaskRequest, CHAIN_TASK_ID, MAX_ATTEMPTS);
159160
assertEquals(Optional.empty(), commandCompleted);
160161
}
161162

@@ -164,18 +165,20 @@ void isCommandCompletedFalseWhenFeignException() {
164165
when(blockchainAdapterClient.getStatusForFinalizeTaskRequest(CHAIN_TASK_ID))
165166
.thenThrow(FeignException.class);
166167
Optional<Boolean> commandCompleted = blockchainAdapterService.isCommandCompleted(
167-
blockchainAdapterClient::getStatusForFinalizeTaskRequest, CHAIN_TASK_ID, PERIOD, MAX_ATTEMPTS);
168+
blockchainAdapterClient::getStatusForFinalizeTaskRequest, CHAIN_TASK_ID, MAX_ATTEMPTS);
168169
assertEquals(Optional.empty(), commandCompleted);
169170
}
170171

171172
@Test
172173
void isCommandCompletedFalseWhenInterrupted() throws InterruptedException {
174+
blockchainAdapterService = new BlockchainAdapterService(blockchainAdapterClient, Duration.ofSeconds(5), MAX_ATTEMPTS);
175+
173176
when(blockchainAdapterClient.getStatusForFinalizeTaskRequest(CHAIN_TASK_ID))
174177
.thenReturn(CommandStatus.PROCESSING);
175178
ExecutorService service = Executors.newSingleThreadExecutor();
176179
Future<?> future = service.submit(() ->
177180
blockchainAdapterService.isCommandCompleted(blockchainAdapterClient::getStatusForFinalizeTaskRequest,
178-
CHAIN_TASK_ID, 5000L, MAX_ATTEMPTS));
181+
CHAIN_TASK_ID, MAX_ATTEMPTS));
179182
Thread.sleep(1000L);
180183
future.cancel(true);
181184
assertThrows(CancellationException.class, future::get);

0 commit comments

Comments
 (0)