Search in sources :

Example 1 with SyncingSubscription

use of org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.syncing.SyncingSubscription in project besu by hyperledger.

the class EthSubscribeIntegrationTest method shouldAddMultipleConnectionsToMap.

@Test
public void shouldAddMultipleConnectionsToMap(final TestContext context) {
    final Async async = context.async(2);
    final JsonRpcRequest subscribeRequestBody1 = createEthSubscribeRequestBody(CONNECTION_ID_1);
    final JsonRpcRequest subscribeRequestBody2 = createEthSubscribeRequestBody(CONNECTION_ID_2);
    final JsonRpcSuccessResponse expectedResponse1 = new JsonRpcSuccessResponse(subscribeRequestBody1.getId(), "0x1");
    final JsonRpcSuccessResponse expectedResponse2 = new JsonRpcSuccessResponse(subscribeRequestBody2.getId(), "0x2");
    final ServerWebSocket websocketMock1 = mock(ServerWebSocket.class);
    when(websocketMock1.textHandlerID()).thenReturn(CONNECTION_ID_1);
    when(websocketMock1.writeFrame(argThat(this::isFinalFrame))).then(countDownOnLastFrame(async));
    final ServerWebSocket websocketMock2 = mock(ServerWebSocket.class);
    when(websocketMock2.textHandlerID()).thenReturn(CONNECTION_ID_2);
    when(websocketMock2.writeFrame(argThat(this::isFinalFrame))).then(countDownOnLastFrame(async));
    webSocketMessageHandler.handle(websocketMock1, Json.encodeToBuffer(subscribeRequestBody1), Optional.empty());
    webSocketMessageHandler.handle(websocketMock2, Json.encodeToBuffer(subscribeRequestBody2), Optional.empty());
    async.awaitSuccess(ASYNC_TIMEOUT);
    final List<SyncingSubscription> updatedSubscriptions = getSubscriptions();
    assertThat(updatedSubscriptions).hasSize(2);
    final List<String> connectionIds = updatedSubscriptions.stream().map(Subscription::getConnectionId).collect(Collectors.toList());
    assertThat(connectionIds).containsExactlyInAnyOrder(CONNECTION_ID_1, CONNECTION_ID_2);
    verify(websocketMock1).writeFrame(argThat(isFrameWithAnyText(Json.encode(expectedResponse1), Json.encode(expectedResponse2))));
    verify(websocketMock1).writeFrame(argThat(this::isFinalFrame));
    verify(websocketMock2).writeFrame(argThat(isFrameWithAnyText(Json.encode(expectedResponse1), Json.encode(expectedResponse2))));
    verify(websocketMock2).writeFrame(argThat(this::isFinalFrame));
}
Also used : ServerWebSocket(io.vertx.core.http.ServerWebSocket) JsonRpcRequest(org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest) Async(io.vertx.ext.unit.Async) SyncingSubscription(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.syncing.SyncingSubscription) JsonRpcSuccessResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse) Test(org.junit.Test)

Example 2 with SyncingSubscription

use of org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.syncing.SyncingSubscription in project besu by hyperledger.

the class SubscriptionBuilderTest method shouldReturnSubscriptionWhenMappingSyncingSubscription.

@Test
public void shouldReturnSubscriptionWhenMappingSyncingSubscription() {
    final Function<Subscription, SyncingSubscription> function = subscriptionBuilder.mapToSubscriptionClass(SyncingSubscription.class);
    final Subscription subscription = new SyncingSubscription(1L, CONNECTION_ID, SubscriptionType.SYNCING);
    assertThat(function.apply(subscription)).isInstanceOf(SyncingSubscription.class);
}
Also used : SyncingSubscription(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.syncing.SyncingSubscription) 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 3 with SyncingSubscription

use of org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.syncing.SyncingSubscription in project besu by hyperledger.

the class SubscriptionBuilderTest method shouldBuildSubscriptionWhenSubscribeRequestTypeIsSyncing.

@Test
public void shouldBuildSubscriptionWhenSubscribeRequestTypeIsSyncing() {
    final SubscribeRequest subscribeRequest = new SubscribeRequest(SubscriptionType.SYNCING, null, null, CONNECTION_ID);
    final SyncingSubscription expectedSubscription = new SyncingSubscription(1L, CONNECTION_ID, SubscriptionType.SYNCING);
    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) 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 4 with SyncingSubscription

use of org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.syncing.SyncingSubscription in project besu by hyperledger.

the class SubscriptionManagerTest method subscribeShouldCreateSubscription.

@Test
public void subscribeShouldCreateSubscription() {
    final SubscribeRequest subscribeRequest = subscribeRequest(CONNECTION_ID);
    final Long subscriptionId = subscriptionManager.subscribe(subscribeRequest);
    final SyncingSubscription expectedSubscription = new SyncingSubscription(subscriptionId, CONNECTION_ID, subscribeRequest.getSubscriptionType());
    final Subscription createdSubscription = subscriptionManager.getSubscriptionById(subscriptionId);
    assertThat(subscriptionId).isEqualTo(1L);
    assertThat(createdSubscription).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) SyncingSubscription(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.syncing.SyncingSubscription) 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 SyncingSubscription

use of org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.syncing.SyncingSubscription in project besu by hyperledger.

the class EthSubscribeIntegrationTest method shouldAddConnectionToMap.

@Test
public void shouldAddConnectionToMap(final TestContext context) {
    final Async async = context.async();
    final JsonRpcRequest subscribeRequestBody = createEthSubscribeRequestBody(CONNECTION_ID_1);
    final JsonRpcSuccessResponse expectedResponse = new JsonRpcSuccessResponse(subscribeRequestBody.getId(), "0x1");
    final ServerWebSocket websocketMock = mock(ServerWebSocket.class);
    when(websocketMock.textHandlerID()).thenReturn(CONNECTION_ID_1);
    when(websocketMock.writeFrame(argThat(this::isFinalFrame))).then(completeOnLastFrame(async, websocketMock));
    webSocketMessageHandler.handle(websocketMock, Json.encodeToBuffer(subscribeRequestBody), Optional.empty());
    async.awaitSuccess(ASYNC_TIMEOUT);
    final List<SyncingSubscription> syncingSubscriptions = getSubscriptions();
    assertThat(syncingSubscriptions).hasSize(1);
    assertThat(syncingSubscriptions.get(0).getConnectionId()).isEqualTo(CONNECTION_ID_1);
    verify(websocketMock).writeFrame(argThat(isFrameWithAnyText(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) Async(io.vertx.ext.unit.Async) SyncingSubscription(org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.syncing.SyncingSubscription) JsonRpcSuccessResponse(org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse) Test(org.junit.Test)

Aggregations

SyncingSubscription (org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.syncing.SyncingSubscription)5 Test (org.junit.Test)5 NewBlockHeadersSubscription (org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.blockheaders.NewBlockHeadersSubscription)3 PrivateLogsSubscription (org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.logs.PrivateLogsSubscription)3 ServerWebSocket (io.vertx.core.http.ServerWebSocket)2 Async (io.vertx.ext.unit.Async)2 JsonRpcRequest (org.hyperledger.besu.ethereum.api.jsonrpc.internal.JsonRpcRequest)2 JsonRpcSuccessResponse (org.hyperledger.besu.ethereum.api.jsonrpc.internal.response.JsonRpcSuccessResponse)2 LogsSubscription (org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.logs.LogsSubscription)2 PrivateSubscribeRequest (org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.request.PrivateSubscribeRequest)2 SubscribeRequest (org.hyperledger.besu.ethereum.api.jsonrpc.websocket.subscription.request.SubscribeRequest)2