@Test public void netPeerCountOfZero() throws Exception { when(peerDiscoveryMock.getPeers()).thenReturn(Collections.emptyList()); final String id = "123"; final RequestBody body = RequestBody.create( JSON, "{\"jsonrpc\":\"2.0\",\"id\":" + Json.encode(id) + ",\"method\":\"net_peerCount\"}"); try (final Response resp = client.newCall(buildPostRequest(body)).execute()) { assertThat(resp.code()).isEqualTo(200); // Check general format of result final JsonObject json = new JsonObject(resp.body().string()); testHelper.assertValidJsonRpcResult(json, id); // Check result final String expectedResult = "0x0"; assertThat(json.getString("result")).isEqualTo(expectedResult); } }
@SuppressWarnings("StringConcatenationInsideStringBufferAppend") public void assertPeerCounts() { int errCnt = 0; final StringBuilder sb = new StringBuilder(); final int expectedCount = nodes.size() - 1; // Connections to every other node, but not itself for (final TestNode node : nodes) { final int actualCount = node.network.getPeers().size(); if (expectedCount != actualCount) { sb.append( "Node " + node.shortId() + " expected " + expectedCount + " peer connections, but actually has " + actualCount + " peer connections.\n"); errCnt++; } } final String header = "" + errCnt + " Nodes have missing peer connections.\n"; assertThat(errCnt).describedAs(header + sb).isEqualTo(0); }
@Test public void netPeerCountSuccessful() throws Exception { when(peerDiscoveryMock.getPeers()).thenReturn(Arrays.asList(null, null, null)); final String id = "123"; final RequestBody body = RequestBody.create( JSON, "{\"jsonrpc\":\"2.0\",\"id\":" + Json.encode(id) + ",\"method\":\"net_peerCount\"}"); try (final Response resp = client.newCall(buildPostRequest(body)).execute()) { assertThat(resp.code()).isEqualTo(200); // Check general format of result final JsonObject json = new JsonObject(resp.body().string()); testHelper.assertValidJsonRpcResult(json, id); // Check result final String expectedResult = "0x3"; assertThat(json.getString("result")).isEqualTo(expectedResult); } }
@Test public void shouldReturnExpectedPeerListWhenP2PNetworkHavePeers() { final Collection<PeerConnection> peerList = peerList(); final List<PeerResult> expectedPeerResults = peerList.stream().map(PeerResult::new).collect(Collectors.toList()); final JsonRpcRequest request = adminPeers(); final JsonRpcResponse expectedResponse = new JsonRpcSuccessResponse(null, expectedPeerResults); when(p2pNetwork.getPeers()).thenReturn(peerList); final JsonRpcResponse response = adminPeers.response(request); assertThat(response).isEqualToComparingFieldByFieldRecursively(expectedResponse); }
@Test public void shouldReturnEmptyPeersListWhenP2PNetworkDoesNotHavePeers() { final JsonRpcResponse expectedResponse = new JsonRpcSuccessResponse(null, Collections.emptyList()); final JsonRpcRequest request = adminPeers(); when(p2pNetwork.getPeers()).thenReturn(Collections.emptyList()); final JsonRpcResponse response = adminPeers.response(request); assertThat(response).isEqualToComparingFieldByField(expectedResponse); }
private boolean hasConnection(final TestNode node1, final TestNode node2) { for (final PeerConnection peer : node1.network.getPeers()) { if (node2.id().equals(peer.getPeer().getNodeId())) { return true; } } return false; }
@Test public void shouldFailIfP2pDisabled() { when(p2pNetwork.getPeers()).thenThrow(new P2pDisabledException("P2P disabled.")); final JsonRpcRequest request = adminPeers(); final JsonRpcResponse expectedResponse = new JsonRpcErrorResponse(request.getId(), JsonRpcError.P2P_DISABLED); assertThat(adminPeers.response(request)).isEqualToComparingFieldByField(expectedResponse); }
/** Logs the Peer connections for each node. */ public void logPeerConnections() { final List<String> connStr = new ArrayList<>(); for (final TestNode node : nodes) { for (final PeerConnection peer : node.network.getPeers()) { final String localString = node.shortId() + "@" + peer.getLocalAddress(); final String peerString = shortId(peer.getPeer().getNodeId()) + "@" + peer.getRemoteAddress(); connStr.add("Connection: " + localString + " to " + peerString); } } LOG.info("TestNodeList Connections:\n" + join(connStr, '\n')); }
peerList.add(new MockPeerConnection(info3, addr30301, addr60303)); when(peerDiscoveryMock.getPeers()).thenReturn(peerList);
final Predicate<PeerConnection> isPeerTwo = peerConnection -> peerConnection.getPeer().getNodeId().equals(two.getId()); Assertions.assertThat(network1.getPeers().stream().filter(isPeerTwo).findFirst()) .isNotPresent(); Assertions.assertThat(network2.getPeers().stream().filter(isPeerOne).findFirst()) .isNotPresent(); Assertions.assertThat(peer1Future.get().getPeer().getNodeId()).isEqualTo(one.getId()); Assertions.assertThat(peer2Future.get().getPeer().getNodeId()).isEqualTo(two.getId()); Assertions.assertThat(network1.getPeers().stream().filter(isPeerTwo).findFirst()).isPresent(); final Optional<PeerConnection> optionalConnection = network2.getPeers().stream().filter(isPeerOne).findFirst(); Assertions.assertThat(optionalConnection).isPresent(); Assertions.assertThat(peer2DisconnectFuture.get()) .isEqualTo(DisconnectReason.CLIENT_QUITTING); Assertions.assertThat(network1.getPeers().stream().filter(isPeerTwo).findFirst()) .isNotPresent(); Assertions.assertThat(network2.getPeers().stream().filter(isPeerOne).findFirst()) .isNotPresent();
/** * 2nd order test to verify the framework correctly fails if a disconnect occurs It could have a * more detailed exception check - more than just the class. */ @Test(expected = ComparisonFailure.class) public void disconnectShouldThrow() throws Exception { try (final TestNodeList txNodes = new TestNodeList()) { // Create & Start Nodes final TestNode node1 = txNodes.create(vertx, null, null, noDiscovery); txNodes.create(vertx, null, null, noDiscovery); txNodes.create(vertx, null, null, noDiscovery); initTest(txNodes); node1.network.getPeers().iterator().next().disconnect(DisconnectReason.BREACH_OF_PROTOCOL); wrapup(txNodes); } }