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);
}
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));
}
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));
}
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);
}
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);
}
Aggregations