Search in sources :

Example 1 with Response

use of com.hedera.hashgraph.sdk.proto.Response in project hedera-sdk-java by hashgraph.

the class ExecutableTest method executeQueryDelay.

@Test
void executeQueryDelay() throws PrecheckStatusException, TimeoutException {
    when(node3.isHealthy()).thenReturn(true);
    when(node4.isHealthy()).thenReturn(true);
    when(node3.channelFailedToConnect()).thenReturn(false);
    when(node4.channelFailedToConnect()).thenReturn(false);
    AtomicInteger i = new AtomicInteger();
    var tx = new DummyQuery() {

        @Override
        Status mapResponseStatus(com.hedera.hashgraph.sdk.proto.Response response) {
            return Status.RECEIPT_NOT_FOUND;
        }

        @Override
        ExecutionState shouldRetry(Status status, Response response) {
            return i.getAndIncrement() == 0 ? ExecutionState.Retry : ExecutionState.Success;
        }
    };
    var nodeAccountIds = Arrays.asList(new AccountId(3), new AccountId(4), new AccountId(5));
    tx.setNodeAccountIds(nodeAccountIds);
    var receipt = com.hedera.hashgraph.sdk.proto.TransactionReceipt.newBuilder().setStatus(ResponseCodeEnum.OK).build();
    var receiptResp = com.hedera.hashgraph.sdk.proto.TransactionGetReceiptResponse.newBuilder().setReceipt(receipt).build();
    var resp = Response.newBuilder().setTransactionGetReceipt(receiptResp).build();
    tx.blockingUnaryCall = (grpcRequest) -> resp;
    TransactionReceipt rcp = (TransactionReceipt) tx.execute(client);
    verify(node3).channelFailedToConnect();
    verify(node4).channelFailedToConnect();
}
Also used : Response(com.hedera.hashgraph.sdk.proto.Response) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.jupiter.api.Test)

Example 2 with Response

use of com.hedera.hashgraph.sdk.proto.Response in project hedera-sdk-java by hashgraph.

the class ExecutableTest method executeWithAllUnhealthyNodes.

@Test
void executeWithAllUnhealthyNodes() throws PrecheckStatusException, TimeoutException {
    AtomicInteger i = new AtomicInteger();
    // 1st round, pick node3, fail channel connect
    // 2nd round, pick node4, fail channel connect
    // 3rd round, pick node5, fail channel connect
    // 4th round, pick node 3, wait for delay, channel connect ok
    when(node3.isHealthy()).thenAnswer((Answer<Boolean>) inv -> i.get() == 0);
    when(node4.isHealthy()).thenAnswer((Answer<Boolean>) inv -> i.get() == 0);
    when(node5.isHealthy()).thenAnswer((Answer<Boolean>) inv -> i.get() == 0);
    when(node3.channelFailedToConnect()).thenAnswer((Answer<Boolean>) inv -> i.get() == 0);
    when(node4.channelFailedToConnect()).thenAnswer((Answer<Boolean>) inv -> i.get() == 0);
    when(node5.channelFailedToConnect()).thenAnswer((Answer<Boolean>) inv -> i.getAndIncrement() == 0);
    when(node3.getRemainingTimeForBackoff()).thenReturn(500L);
    when(node4.getRemainingTimeForBackoff()).thenReturn(600L);
    when(node5.getRemainingTimeForBackoff()).thenReturn(700L);
    var now = org.threeten.bp.Instant.now();
    var tx = new DummyTransaction() {

        @Nullable
        @Override
        TransactionResponse mapResponse(com.hedera.hashgraph.sdk.proto.TransactionResponse response, AccountId nodeId, com.hedera.hashgraph.sdk.proto.Transaction request) {
            return new TransactionResponse(new AccountId(3), TransactionId.withValidStart(new AccountId(3), now), null, null);
        }
    };
    var nodeAccountIds = Arrays.asList(new AccountId(3), new AccountId(4), new AccountId(5));
    tx.setNodeAccountIds(nodeAccountIds);
    var txResp = com.hedera.hashgraph.sdk.proto.TransactionResponse.newBuilder().setNodeTransactionPrecheckCode(ResponseCodeEnum.OK).build();
    tx.blockingUnaryCall = (grpcRequest) -> txResp;
    com.hedera.hashgraph.sdk.TransactionResponse resp = (com.hedera.hashgraph.sdk.TransactionResponse) tx.execute(client);
    verify(node3, times(2)).channelFailedToConnect();
    verify(node4).channelFailedToConnect();
    verify(node5).channelFailedToConnect();
    assertThat(resp.nodeId).isEqualTo(new AccountId(3));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TimeoutException(java.util.concurrent.TimeoutException) Mockito.times(org.mockito.Mockito.times) Mockito.when(org.mockito.Mockito.when) ResponseCodeEnum(com.hedera.hashgraph.sdk.proto.ResponseCodeEnum) Mockito.verify(org.mockito.Mockito.verify) StatusRuntimeException(io.grpc.StatusRuntimeException) Test(org.junit.jupiter.api.Test) Duration(org.threeten.bp.Duration) TimeUnit(java.util.concurrent.TimeUnit) Answer(org.mockito.stubbing.Answer) List(java.util.List) CompletableFuture(java8.util.concurrent.CompletableFuture) ResponseHeader(com.hedera.hashgraph.sdk.proto.ResponseHeader) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Response(com.hedera.hashgraph.sdk.proto.Response) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) MethodDescriptor(io.grpc.MethodDescriptor) Nullable(javax.annotation.Nullable) QueryHeader(com.hedera.hashgraph.sdk.proto.QueryHeader) Mockito.mock(org.mockito.Mockito.mock) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.jupiter.api.Test)

Example 3 with Response

use of com.hedera.hashgraph.sdk.proto.Response in project hedera-mirror-node by hashgraph.

the class TransactionPublisherTest method publishRetrySameRequest.

@Test
@Timeout(3)
void publishRetrySameRequest() {
    ResponseCodeEnum errorResponseCode = ResponseCodeEnum.PLATFORM_NOT_ACTIVE;
    cryptoServiceStub.addTransactions(Mono.just(response(errorResponseCode)), Mono.just(response(errorResponseCode)));
    var request = request().build();
    transactionPublisher.publish(request).as(StepVerifier::create).expectErrorSatisfies(t -> assertThat(t).isInstanceOf(PublishException.class).hasMessageContaining("exceeded maximum attempts for request with last exception being").getRootCause().hasMessageContaining(errorResponseCode.toString())).verify(Duration.ofSeconds(2L));
    cryptoServiceStub.addTransactions(Mono.just(response(OK)));
    transactionPublisher.publish(request).as(StepVerifier::create).expectNextCount(1L).expectComplete().verify(Duration.ofSeconds(1L));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) CryptoServiceGrpc(com.hedera.hashgraph.sdk.proto.CryptoServiceGrpc) NodeProperties(com.hedera.mirror.monitor.NodeProperties) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) TransferTransaction(com.hedera.hashgraph.sdk.TransferTransaction) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) OperatorProperties(com.hedera.mirror.monitor.OperatorProperties) OK(com.hedera.hashgraph.sdk.proto.ResponseCodeEnum.OK) TransactionType(com.hedera.mirror.monitor.publish.transaction.TransactionType) TimeoutException(java.util.concurrent.TimeoutException) InProcessServerBuilder(io.grpc.inprocess.InProcessServerBuilder) ResponseCodeEnum(com.hedera.hashgraph.sdk.proto.ResponseCodeEnum) Query(com.hedera.hashgraph.sdk.proto.Query) Transaction(com.hedera.hashgraph.sdk.proto.Transaction) StreamObserver(io.grpc.stub.StreamObserver) ResponseHeader(com.hedera.hashgraph.sdk.proto.ResponseHeader) Response(com.hedera.hashgraph.sdk.proto.Response) Duration(java.time.Duration) Map(java.util.Map) TransactionReceipt(com.hedera.hashgraph.sdk.proto.TransactionReceipt) TransactionRecord(com.hedera.hashgraph.sdk.proto.TransactionRecord) Server(io.grpc.Server) ACCOUNT_DELETED(com.hedera.hashgraph.sdk.proto.ResponseCodeEnum.ACCOUNT_DELETED) SUCCESS(com.hedera.hashgraph.sdk.proto.ResponseCodeEnum.SUCCESS) Uninterruptibles(com.google.common.util.concurrent.Uninterruptibles) Set(java.util.Set) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) Instant(java.time.Instant) TransactionGetRecordResponse(com.hedera.hashgraph.sdk.proto.TransactionGetRecordResponse) Test(org.junit.jupiter.api.Test) TransactionGetReceiptResponse(com.hedera.hashgraph.sdk.proto.TransactionGetReceiptResponse) AfterEach(org.junit.jupiter.api.AfterEach) TransactionResponse(com.hedera.hashgraph.sdk.proto.TransactionResponse) Data(lombok.Data) Log4j2(lombok.extern.log4j.Log4j2) PrivateKey(com.hedera.hashgraph.sdk.PrivateKey) Queue(java.util.Queue) Timeout(org.junit.jupiter.api.Timeout) MonitorProperties(com.hedera.mirror.monitor.MonitorProperties) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) ResponseCodeEnum(com.hedera.hashgraph.sdk.proto.ResponseCodeEnum) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 4 with Response

use of com.hedera.hashgraph.sdk.proto.Response in project hedera-mirror-node by hashgraph.

the class TransactionPublisherTest method publishPreCheckError.

@Test
@Timeout(3)
void publishPreCheckError() {
    ResponseCodeEnum errorResponseCode = ResponseCodeEnum.INSUFFICIENT_ACCOUNT_BALANCE;
    cryptoServiceStub.addTransactions(Mono.just(response(errorResponseCode)));
    transactionPublisher.publish(request().build()).as(StepVerifier::create).expectErrorSatisfies(t -> assertThat(t).isInstanceOf(PublishException.class).hasMessageContaining(errorResponseCode.toString())).verify(Duration.ofSeconds(1L));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) CryptoServiceGrpc(com.hedera.hashgraph.sdk.proto.CryptoServiceGrpc) NodeProperties(com.hedera.mirror.monitor.NodeProperties) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) TransferTransaction(com.hedera.hashgraph.sdk.TransferTransaction) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) OperatorProperties(com.hedera.mirror.monitor.OperatorProperties) OK(com.hedera.hashgraph.sdk.proto.ResponseCodeEnum.OK) TransactionType(com.hedera.mirror.monitor.publish.transaction.TransactionType) TimeoutException(java.util.concurrent.TimeoutException) InProcessServerBuilder(io.grpc.inprocess.InProcessServerBuilder) ResponseCodeEnum(com.hedera.hashgraph.sdk.proto.ResponseCodeEnum) Query(com.hedera.hashgraph.sdk.proto.Query) Transaction(com.hedera.hashgraph.sdk.proto.Transaction) StreamObserver(io.grpc.stub.StreamObserver) ResponseHeader(com.hedera.hashgraph.sdk.proto.ResponseHeader) Response(com.hedera.hashgraph.sdk.proto.Response) Duration(java.time.Duration) Map(java.util.Map) TransactionReceipt(com.hedera.hashgraph.sdk.proto.TransactionReceipt) TransactionRecord(com.hedera.hashgraph.sdk.proto.TransactionRecord) Server(io.grpc.Server) ACCOUNT_DELETED(com.hedera.hashgraph.sdk.proto.ResponseCodeEnum.ACCOUNT_DELETED) SUCCESS(com.hedera.hashgraph.sdk.proto.ResponseCodeEnum.SUCCESS) Uninterruptibles(com.google.common.util.concurrent.Uninterruptibles) Set(java.util.Set) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) Instant(java.time.Instant) TransactionGetRecordResponse(com.hedera.hashgraph.sdk.proto.TransactionGetRecordResponse) Test(org.junit.jupiter.api.Test) TransactionGetReceiptResponse(com.hedera.hashgraph.sdk.proto.TransactionGetReceiptResponse) AfterEach(org.junit.jupiter.api.AfterEach) TransactionResponse(com.hedera.hashgraph.sdk.proto.TransactionResponse) Data(lombok.Data) Log4j2(lombok.extern.log4j.Log4j2) PrivateKey(com.hedera.hashgraph.sdk.PrivateKey) Queue(java.util.Queue) Timeout(org.junit.jupiter.api.Timeout) MonitorProperties(com.hedera.mirror.monitor.MonitorProperties) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) ResponseCodeEnum(com.hedera.hashgraph.sdk.proto.ResponseCodeEnum) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 5 with Response

use of com.hedera.hashgraph.sdk.proto.Response in project hedera-mirror-node by hashgraph.

the class TransactionPublisherTest method publishRetryError.

@Test
@Timeout(3)
void publishRetryError() {
    ResponseCodeEnum errorResponseCode = ResponseCodeEnum.PLATFORM_TRANSACTION_NOT_CREATED;
    cryptoServiceStub.addTransactions(Mono.just(response(errorResponseCode)), Mono.just(response(errorResponseCode)));
    transactionPublisher.publish(request().build()).as(StepVerifier::create).expectErrorSatisfies(t -> assertThat(t).isInstanceOf(PublishException.class).hasMessageContaining("exceeded maximum attempts for request with last exception being").getRootCause().hasMessageContaining(errorResponseCode.toString())).verify(Duration.ofSeconds(2L));
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) CryptoServiceGrpc(com.hedera.hashgraph.sdk.proto.CryptoServiceGrpc) NodeProperties(com.hedera.mirror.monitor.NodeProperties) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) TransferTransaction(com.hedera.hashgraph.sdk.TransferTransaction) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) OperatorProperties(com.hedera.mirror.monitor.OperatorProperties) OK(com.hedera.hashgraph.sdk.proto.ResponseCodeEnum.OK) TransactionType(com.hedera.mirror.monitor.publish.transaction.TransactionType) TimeoutException(java.util.concurrent.TimeoutException) InProcessServerBuilder(io.grpc.inprocess.InProcessServerBuilder) ResponseCodeEnum(com.hedera.hashgraph.sdk.proto.ResponseCodeEnum) Query(com.hedera.hashgraph.sdk.proto.Query) Transaction(com.hedera.hashgraph.sdk.proto.Transaction) StreamObserver(io.grpc.stub.StreamObserver) ResponseHeader(com.hedera.hashgraph.sdk.proto.ResponseHeader) Response(com.hedera.hashgraph.sdk.proto.Response) Duration(java.time.Duration) Map(java.util.Map) TransactionReceipt(com.hedera.hashgraph.sdk.proto.TransactionReceipt) TransactionRecord(com.hedera.hashgraph.sdk.proto.TransactionRecord) Server(io.grpc.Server) ACCOUNT_DELETED(com.hedera.hashgraph.sdk.proto.ResponseCodeEnum.ACCOUNT_DELETED) SUCCESS(com.hedera.hashgraph.sdk.proto.ResponseCodeEnum.SUCCESS) Uninterruptibles(com.google.common.util.concurrent.Uninterruptibles) Set(java.util.Set) IOException(java.io.IOException) Mono(reactor.core.publisher.Mono) Instant(java.time.Instant) TransactionGetRecordResponse(com.hedera.hashgraph.sdk.proto.TransactionGetRecordResponse) Test(org.junit.jupiter.api.Test) TransactionGetReceiptResponse(com.hedera.hashgraph.sdk.proto.TransactionGetReceiptResponse) AfterEach(org.junit.jupiter.api.AfterEach) TransactionResponse(com.hedera.hashgraph.sdk.proto.TransactionResponse) Data(lombok.Data) Log4j2(lombok.extern.log4j.Log4j2) PrivateKey(com.hedera.hashgraph.sdk.PrivateKey) Queue(java.util.Queue) Timeout(org.junit.jupiter.api.Timeout) MonitorProperties(com.hedera.mirror.monitor.MonitorProperties) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) ResponseCodeEnum(com.hedera.hashgraph.sdk.proto.ResponseCodeEnum) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Aggregations

Response (com.hedera.hashgraph.sdk.proto.Response)7 Test (org.junit.jupiter.api.Test)7 ResponseCodeEnum (com.hedera.hashgraph.sdk.proto.ResponseCodeEnum)5 ResponseHeader (com.hedera.hashgraph.sdk.proto.ResponseHeader)5 TransactionGetRecordResponse (com.hedera.hashgraph.sdk.proto.TransactionGetRecordResponse)5 Arrays (java.util.Arrays)5 TimeoutException (java.util.concurrent.TimeoutException)5 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)5 BeforeEach (org.junit.jupiter.api.BeforeEach)5 Uninterruptibles (com.google.common.util.concurrent.Uninterruptibles)4 PrivateKey (com.hedera.hashgraph.sdk.PrivateKey)4 TransferTransaction (com.hedera.hashgraph.sdk.TransferTransaction)4 CryptoServiceGrpc (com.hedera.hashgraph.sdk.proto.CryptoServiceGrpc)4 Query (com.hedera.hashgraph.sdk.proto.Query)4 ACCOUNT_DELETED (com.hedera.hashgraph.sdk.proto.ResponseCodeEnum.ACCOUNT_DELETED)4 OK (com.hedera.hashgraph.sdk.proto.ResponseCodeEnum.OK)4 SUCCESS (com.hedera.hashgraph.sdk.proto.ResponseCodeEnum.SUCCESS)4 Transaction (com.hedera.hashgraph.sdk.proto.Transaction)4 TransactionGetReceiptResponse (com.hedera.hashgraph.sdk.proto.TransactionGetReceiptResponse)4 TransactionReceipt (com.hedera.hashgraph.sdk.proto.TransactionReceipt)4