@Test public void onConnectCallback_SubscriptionCallbackShouldCloseSessionOnFailure() throws Exception { when(subscriptionManager.subscribe(any(), any())).thenReturn(SubscriptionStatus.success()); final RemoteEndpoint remoteEndpoint = mock(RemoteEndpoint.class); doAnswer( invocation -> { final WriteCallback callback = invocation.getArgument(1); callback.writeFailed(new IOException()); return null; }) .when(remoteEndpoint) .sendString(any(), any()); when(wsSession.getRemote()).thenReturn(remoteEndpoint); remoteValidatorApi.subscribeValidator(wsContext); verify(subscriptionManager).subscribe(any(), subscriberCallbackArgCaptor.capture()); final Consumer<BeaconChainEvent> subscriberCallback = subscriberCallbackArgCaptor.getValue(); subscriberCallback.accept(new BeaconChainEvent("foo", UInt64.ONE)); verify(wsSession) .close(eq(StatusCode.SERVER_ERROR), eq("Unexpected error on Remote Validator server")); }
@VisibleForTesting void subscribeValidator(final WsConnectContext handler) { final SubscriptionStatus subscriptionStatus = subscriptionManager.subscribe( handler.getSessionId(), (msg) -> { try { final String json = jsonProvider.objectToJSON(msg); handler .session .getRemote() .sendString( json, new WriteCallback() { @Override public void writeFailed(final Throwable x) { LOG.error( "Error sending msg to validator {}", handler.getSessionId(), x); handler.session.close( StatusCode.SERVER_ERROR, "Unexpected error on Remote Validator server"); } @Override public void writeSuccess() {} }); } catch (JsonProcessingException e) { LOG.error("Error sending msg to validator {}", handler.getSessionId(), e); handler.session.close( StatusCode.SERVER_ERROR, "Unexpected error on Remote Validator server"); } }); if (!subscriptionStatus.hasSubscribed()) { handler.session.close(StatusCode.NORMAL, subscriptionStatus.getInfo()); } }
RemoteValidatorApi { @VisibleForTesting void subscribeValidator(final WsConnectContext handler) { final SubscriptionStatus subscriptionStatus = subscriptionManager.subscribe( handler.getSessionId(), (msg) -> { try { final String json = jsonProvider.objectToJSON(msg); handler .session .getRemote() .sendString( json, new WriteCallback() { @Override public void writeFailed(final Throwable x) { LOG.error( "Error sending msg to validator {}", handler.getSessionId(), x); handler.session.close( StatusCode.SERVER_ERROR, "Unexpected error on Remote Validator server"); } @Override public void writeSuccess() {} }); } catch (JsonProcessingException e) { LOG.error("Error sending msg to validator {}", handler.getSessionId(), e); handler.session.close( StatusCode.SERVER_ERROR, "Unexpected error on Remote Validator server"); } }); if (!subscriptionStatus.hasSubscribed()) { handler.session.close(StatusCode.NORMAL, subscriptionStatus.getInfo()); } } }
RemoteValidatorApi { @VisibleForTesting void subscribeValidator(final WsConnectContext handler) { final SubscriptionStatus subscriptionStatus = subscriptionManager.subscribe( handler.getSessionId(), (msg) -> { try { final String json = jsonProvider.objectToJSON(msg); handler .session .getRemote() .sendString( json, new WriteCallback() { @Override public void writeFailed(final Throwable x) { LOG.error( "Error sending msg to validator {}", handler.getSessionId(), x); handler.session.close( StatusCode.SERVER_ERROR, "Unexpected error on Remote Validator server"); } @Override public void writeSuccess() {} }); } catch (JsonProcessingException e) { LOG.error("Error sending msg to validator {}", handler.getSessionId(), e); handler.session.close( StatusCode.SERVER_ERROR, "Unexpected error on Remote Validator server"); } }); if (!subscriptionStatus.hasSubscribed()) { handler.session.close(StatusCode.NORMAL, subscriptionStatus.getInfo()); } } RemoteValidatorApi(
      final GlobalConfiguration configuration,
      final RemoteValidatorSubscriptions subscriptionManager); }
RemoteValidatorApi { @VisibleForTesting void subscribeValidator(final WsConnectContext handler) { final SubscriptionStatus subscriptionStatus = subscriptionManager.subscribe( handler.getSessionId(), (msg) -> { try { final String json = jsonProvider.objectToJSON(msg); handler .session .getRemote() .sendString( json, new WriteCallback() { @Override public void writeFailed(final Throwable x) { LOG.error( "Error sending msg to validator {}", handler.getSessionId(), x); handler.session.close( StatusCode.SERVER_ERROR, "Unexpected error on Remote Validator server"); } @Override public void writeSuccess() {} }); } catch (JsonProcessingException e) { LOG.error("Error sending msg to validator {}", handler.getSessionId(), e); handler.session.close( StatusCode.SERVER_ERROR, "Unexpected error on Remote Validator server"); } }); if (!subscriptionStatus.hasSubscribed()) { handler.session.close(StatusCode.NORMAL, subscriptionStatus.getInfo()); } } RemoteValidatorApi(
      final GlobalConfiguration configuration,
      final RemoteValidatorSubscriptions subscriptionManager);  }
RemoteValidatorApi { @VisibleForTesting void subscribeValidator(final WsConnectContext handler) { final SubscriptionStatus subscriptionStatus = subscriptionManager.subscribe( handler.getSessionId(), (msg) -> { try { final String json = jsonProvider.objectToJSON(msg); handler .session .getRemote() .sendString( json, new WriteCallback() { @Override public void writeFailed(final Throwable x) { LOG.error( "Error sending msg to validator {}", handler.getSessionId(), x); handler.session.close( StatusCode.SERVER_ERROR, "Unexpected error on Remote Validator server"); } @Override public void writeSuccess() {} }); } catch (JsonProcessingException e) { LOG.error("Error sending msg to validator {}", handler.getSessionId(), e); handler.session.close( StatusCode.SERVER_ERROR, "Unexpected error on Remote Validator server"); } }); if (!subscriptionStatus.hasSubscribed()) { handler.session.close(StatusCode.NORMAL, subscriptionStatus.getInfo()); } } RemoteValidatorApi(
      final GlobalConfiguration configuration,
      final RemoteValidatorSubscriptions subscriptionManager);   }