From bcb84a0bf72ebda61ae8a264bf7155b25fc339c8 Mon Sep 17 00:00:00 2001 From: Juraj Piar Date: Mon, 1 Apr 2024 10:31:16 +0100 Subject: [PATCH] squashme: makes safer test --- .../rsk/net/discovery/PeerExplorerTest.java | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/rskj-core/src/test/java/co/rsk/net/discovery/PeerExplorerTest.java b/rskj-core/src/test/java/co/rsk/net/discovery/PeerExplorerTest.java index b9e3bbf65eb..b1ea7965ba6 100644 --- a/rskj-core/src/test/java/co/rsk/net/discovery/PeerExplorerTest.java +++ b/rskj-core/src/test/java/co/rsk/net/discovery/PeerExplorerTest.java @@ -81,6 +81,18 @@ class PeerExplorerTest { private static final int NETWORK_ID1 = 1; private static final int NETWORK_ID2 = 2; + private static void assertNoHostnameFromDiscovery(ECKey key1, PeerExplorer peerExplorer) { + Node localhostNode = new Node(key1.getNodeId(), HOST_1, PORT_1); // more stable if loopback address overrides + String localhostIP = localhostNode.getAddress().getAddress().getHostAddress(); + Optional discoveredNode = peerExplorer.getNodes() + .stream().filter((Node aNode) -> Objects.equals( + aNode.getAddress().getAddress().getHostAddress(), + localhostIP)) + .findFirst(); + assertTrue(discoveredNode.isPresent()); + assertNotEquals(HOST_1, discoveredNode.get().getHost()); + } + @Test void sendInitialMessageToNodesNoNodes() { Node node = new Node(new ECKey().getNodeId(), HOST_2, PORT_2); @@ -265,9 +277,7 @@ void handlePongMessage() throws Exception { assertEquals(1, peerExplorer.getNodes().size()); // Verify that only IP is being sent, not hostname - for (Node discoveredNode : peerExplorer.getNodes()) { - assertNotEquals(HOST_1, discoveredNode.getHost()); - } + assertNoHostnameFromDiscovery(key1, peerExplorer); peerExplorer.dispose(); } @@ -432,9 +442,7 @@ void handleFindNodeMessage() throws Exception { assertEquals(1, neighborsPeerMessage.getNodes().size()); // Verify that only IP is being sent, not hostname - for (Node discoveredNode : neighborsPeerMessage.getNodes()) { - assertNotEquals(HOST_1, discoveredNode.getHost()); - } + assertNoHostnameFromDiscovery(key1, peerExplorer); peerExplorer.dispose(); }