Search in sources :

Example 1 with SubscribeRequest

use of org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.request.SubscribeRequest in project besu by hyperledger.

the class EthSubscribeTest method responseContainsSubscriptionId.

@Test
public void responseContainsSubscriptionId() {
    final WebSocketRpcRequest webSocketRequest = createWebSocketRpcRequest();
    final JsonRpcRequestContext jsonRpcrequestContext = new JsonRpcRequestContext(webSocketRequest);
    final SubscribeRequest subscribeRequest = new SubscribeRequest(SubscriptionType.SYNCING, null, null, webSocketRequest.getConnectionId());
    when(mapperMock.mapSubscribeRequest(eq(jsonRpcrequestContext))).thenReturn(subscribeRequest);
    when(subscriptionManagerMock.subscribe(eq(subscribeRequest))).thenReturn(1L);
    final JsonRpcSuccessResponse expectedResponse = new JsonRpcSuccessResponse(jsonRpcrequestContext.getRequest().getId(), Quantity.create((1L)));
    assertThat(ethSubscribe.response(jsonRpcrequestContext)).isEqualTo(expectedResponse);
}
Also used : JsonRpcRequestContext(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext) SubscribeRequest(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.request.SubscribeRequest) JsonRpcSuccessResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse) Test(org.junit.Test)

Example 2 with SubscribeRequest

use of org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.request.SubscribeRequest in project besu by hyperledger.

the class EthUnsubscribeIntegrationTest method shouldRemoveSubscriptionAndKeepConnection.

@Test
public void shouldRemoveSubscriptionAndKeepConnection(final TestContext context) {
    final Async async = context.async();
    // Add the subscriptions we'd like to remove
    final SubscribeRequest subscribeRequest = new SubscribeRequest(SubscriptionType.SYNCING, null, null, CONNECTION_ID);
    final Long subscriptionId1 = subscriptionManager.subscribe(subscribeRequest);
    final Long subscriptionId2 = subscriptionManager.subscribe(subscribeRequest);
    assertThat(subscriptionManager.getSubscriptionById(subscriptionId1)).isNotNull();
    assertThat(subscriptionManager.getSubscriptionById(subscriptionId2)).isNotNull();
    final JsonRpcRequest unsubscribeRequestBody = createEthUnsubscribeRequestBody(subscriptionId2, CONNECTION_ID);
    final JsonRpcSuccessResponse expectedResponse = new JsonRpcSuccessResponse(unsubscribeRequestBody.getId(), Boolean.TRUE);
    final ServerWebSocket websocketMock = mock(ServerWebSocket.class);
    when(websocketMock.textHandlerID()).thenReturn(CONNECTION_ID);
    when(websocketMock.writeFrame(argThat(this::isFinalFrame))).then(completeOnLastFrame(async));
    webSocketMessageHandler.handle(websocketMock, Json.encodeToBuffer(unsubscribeRequestBody), Optional.empty());
    async.awaitSuccess(ASYNC_TIMEOUT);
    assertThat(subscriptionManager.getSubscriptionById(subscriptionId1)).isNotNull();
    assertThat(subscriptionManager.getSubscriptionById(subscriptionId2)).isNull();
    verify(websocketMock).writeFrame(argThat(isFrameWithText(Json.encode(expectedResponse))));
    verify(websocketMock).writeFrame(argThat(this::isFinalFrame));
}
Also used : ServerWebSocket(io.vertx.core.http.ServerWebSocket) JsonRpcRequest(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest) SubscribeRequest(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.request.SubscribeRequest) Async(io.vertx.ext.unit.Async) JsonRpcSuccessResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse) Test(org.junit.Test)

Example 3 with SubscribeRequest

use of org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.request.SubscribeRequest in project besu by hyperledger.

the class EthUnsubscribeIntegrationTest method shouldRemoveConnectionWithSingleSubscriptionFromMap.

@Test
public void shouldRemoveConnectionWithSingleSubscriptionFromMap(final TestContext context) {
    final Async async = context.async();
    // Add the subscription we'd like to remove
    final SubscribeRequest subscribeRequest = new SubscribeRequest(SubscriptionType.SYNCING, null, null, CONNECTION_ID);
    final Long subscriptionId = subscriptionManager.subscribe(subscribeRequest);
    assertThat(subscriptionManager.getSubscriptionById(subscriptionId)).isNotNull();
    final JsonRpcRequest unsubscribeRequestBody = createEthUnsubscribeRequestBody(subscriptionId, CONNECTION_ID);
    final JsonRpcSuccessResponse expectedResponse = new JsonRpcSuccessResponse(unsubscribeRequestBody.getId(), Boolean.TRUE);
    final ServerWebSocket websocketMock = mock(ServerWebSocket.class);
    when(websocketMock.textHandlerID()).thenReturn(CONNECTION_ID);
    when(websocketMock.writeFrame(argThat(this::isFinalFrame))).then(completeOnLastFrame(async));
    webSocketMessageHandler.handle(websocketMock, Json.encodeToBuffer(unsubscribeRequestBody), Optional.empty());
    async.awaitSuccess(ASYNC_TIMEOUT);
    assertThat(subscriptionManager.getSubscriptionById(subscriptionId)).isNull();
    verify(websocketMock).writeFrame(argThat(isFrameWithText(Json.encode(expectedResponse))));
    verify(websocketMock).writeFrame(argThat(this::isFinalFrame));
}
Also used : ServerWebSocket(io.vertx.core.http.ServerWebSocket) JsonRpcRequest(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest) SubscribeRequest(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.request.SubscribeRequest) Async(io.vertx.ext.unit.Async) JsonRpcSuccessResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse) Test(org.junit.Test)

Example 4 with SubscribeRequest

use of org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.request.SubscribeRequest in project besu by hyperledger.

the class SubscriptionBuilderTest method shouldBuildSubscriptionWhenSubscribeRequestTypeIsNewPendingTransactions.

@Test
public void shouldBuildSubscriptionWhenSubscribeRequestTypeIsNewPendingTransactions() {
    final SubscribeRequest subscribeRequest = new SubscribeRequest(SubscriptionType.NEW_PENDING_TRANSACTIONS, null, null, CONNECTION_ID);
    final Subscription expectedSubscription = new Subscription(1L, CONNECTION_ID, SubscriptionType.NEW_PENDING_TRANSACTIONS, null);
    final Subscription builtSubscription = subscriptionBuilder.build(1L, CONNECTION_ID, subscribeRequest);
    assertThat(builtSubscription).usingRecursiveComparison().isEqualTo(expectedSubscription);
}
Also used : SubscribeRequest(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.request.SubscribeRequest) PrivateSubscribeRequest(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.request.PrivateSubscribeRequest) SyncingSubscription(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.syncing.SyncingSubscription) LogsSubscription(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.logs.LogsSubscription) NewBlockHeadersSubscription(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.blockheaders.NewBlockHeadersSubscription) PrivateLogsSubscription(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.logs.PrivateLogsSubscription) Test(org.junit.Test)

Example 5 with SubscribeRequest

use of org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.request.SubscribeRequest in project besu by hyperledger.

the class SubscriptionManagerSendMessageTest method shouldSendMessageOnTheConnectionIdEventBusAddressForExistingSubscription.

@Test
@Ignore
public void shouldSendMessageOnTheConnectionIdEventBusAddressForExistingSubscription(final TestContext context) {
    final String connectionId = UUID.randomUUID().toString();
    final SubscribeRequest subscribeRequest = new SubscribeRequest(SubscriptionType.SYNCING, null, null, connectionId);
    final JsonRpcResult expectedResult = mock(JsonRpcResult.class);
    final Subscription subscription = new Subscription(1L, connectionId, SubscriptionType.SYNCING, false);
    final SubscriptionResponse expectedResponse = new SubscriptionResponse(subscription, expectedResult);
    final Long subscriptionId = subscriptionManager.subscribe(subscribeRequest);
    final Async async = context.async();
    vertx.eventBus().consumer(connectionId).handler(msg -> {
        context.assertEquals(Json.encode(expectedResponse), msg.body());
        async.complete();
    }).completionHandler(v -> subscriptionManager.sendMessage(subscriptionId, expectedResult));
    async.awaitSuccess(VERTX_AWAIT_TIMEOUT_MILLIS);
}
Also used : TestContext(io.vertx.ext.unit.TestContext) SubscriptionType(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.request.SubscriptionType) Async(io.vertx.ext.unit.Async) Json(io.vertx.core.json.Json) TestCase.fail(junit.framework.TestCase.fail) Vertx(io.vertx.core.Vertx) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) SubscriptionResponse(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.response.SubscriptionResponse) NoOpMetricsSystem(org.hyperledger.besu.metrics.noop.NoOpMetricsSystem) UUID(java.util.UUID) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) SubscribeRequest(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.request.SubscribeRequest) Ignore(org.junit.Ignore) JsonRpcResult(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.JsonRpcResult) Mockito.mock(org.mockito.Mockito.mock) Before(org.junit.Before) SubscribeRequest(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.request.SubscribeRequest) Async(io.vertx.ext.unit.Async) JsonRpcResult(org.hyperledger.besu.ethereum.api.jsonrpc.internal.results.JsonRpcResult) SubscriptionResponse(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.response.SubscriptionResponse) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

SubscribeRequest (org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.request.SubscribeRequest)19 Test (org.junit.Test)18 PrivateSubscribeRequest (org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.request.PrivateSubscribeRequest)14 NewBlockHeadersSubscription (org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.blockheaders.NewBlockHeadersSubscription)8 PrivateLogsSubscription (org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.logs.PrivateLogsSubscription)6 SyncingSubscription (org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.syncing.SyncingSubscription)6 UnsubscribeRequest (org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.request.UnsubscribeRequest)5 JsonRpcSuccessResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse)4 LogsSubscription (org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.logs.LogsSubscription)4 Async (io.vertx.ext.unit.Async)3 ServerWebSocket (io.vertx.core.http.ServerWebSocket)2 Assertions.catchThrowable (org.assertj.core.api.Assertions.catchThrowable)2 JsonRpcRequest (org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest)2 Vertx (io.vertx.core.Vertx)1 Json (io.vertx.core.json.Json)1 TestContext (io.vertx.ext.unit.TestContext)1 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)1 UUID (java.util.UUID)1 TestCase.fail (junit.framework.TestCase.fail)1 JsonRpcRequestContext (org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequestContext)1