-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add mocking test to confirm transaction IDs are being generated…
… on demand Signed-off-by: Daniel Akhterov <[email protected]>
- Loading branch information
1 parent
127ea15
commit 1d369d9
Showing
4 changed files
with
98 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
sdk/src/test/java/com/hedera/hashgraph/sdk/RegenerateTransactionIdsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.hedera.hashgraph.sdk; | ||
|
||
import com.hedera.hashgraph.sdk.proto.*; | ||
import com.hedera.hashgraph.sdk.proto.Transaction; | ||
import com.hedera.hashgraph.sdk.proto.TransactionResponse; | ||
import io.grpc.Status; | ||
import java8.util.function.Function; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.concurrent.TimeoutException; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
public class RegenerateTransactionIdsTest { | ||
@Test | ||
void regeneratesTransactionIdsWhenTransactionExpiredIsReturned() throws PrecheckStatusException, TimeoutException, InterruptedException { | ||
var transactionIds = new HashSet<TransactionId>(); | ||
AtomicInteger count = new AtomicInteger(0); | ||
|
||
var responses = List.of( | ||
TransactionResponse.newBuilder().setNodeTransactionPrecheckCode(ResponseCodeEnum.TRANSACTION_EXPIRED).build(), | ||
TransactionResponse.newBuilder().setNodeTransactionPrecheckCode(ResponseCodeEnum.TRANSACTION_EXPIRED).build(), | ||
TransactionResponse.newBuilder().setNodeTransactionPrecheckCode(ResponseCodeEnum.TRANSACTION_EXPIRED).build(), | ||
TransactionResponse.newBuilder().setNodeTransactionPrecheckCode(ResponseCodeEnum.OK).build() | ||
); | ||
|
||
var call = (Function<Object, Object>) o -> { | ||
try { | ||
var transaction = (Transaction) o; | ||
var signedTransaction = SignedTransaction.parseFrom(transaction.getSignedTransactionBytes()); | ||
var transactionBody = TransactionBody.parseFrom(signedTransaction.getBodyBytes()); | ||
var transactionId = TransactionId.fromProtobuf(transactionBody.getTransactionID()); | ||
|
||
if (transactionIds.contains(transactionId)) { | ||
return Status.Code.ABORTED.toStatus().asRuntimeException(); | ||
} | ||
|
||
transactionIds.add(transactionId); | ||
|
||
return responses.get(count.getAndIncrement()); | ||
} catch (Throwable e) { | ||
return new RuntimeException(e); | ||
} | ||
}; | ||
|
||
List<Object> responses1 = List.of( | ||
call, call, call, call | ||
); | ||
|
||
try (var mocker = Mocker.withResponses(List.of(responses1))) { | ||
new FileCreateTransaction().execute(mocker.client); | ||
} | ||
} | ||
} |