From e44dd967d0230f3509b8c8ee0e65e210bc6b2246 Mon Sep 17 00:00:00 2001 From: Christian Basler Date: Fri, 13 May 2016 12:24:16 +0200 Subject: [PATCH] Test for NodeRegistry --- core/build.gradle | 1 + .../bitmessage/ports/MemoryNodeRegistry.java | 14 +-- .../bitmessage/ports/NodeRegistryTest.java | 95 +++++++++++++++++++ 3 files changed, 101 insertions(+), 9 deletions(-) create mode 100644 core/src/test/java/ch/dissem/bitmessage/ports/NodeRegistryTest.java diff --git a/core/build.gradle b/core/build.gradle index 8754cb3..dd100e6 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -26,6 +26,7 @@ artifacts { dependencies { compile 'org.slf4j:slf4j-api:1.7.12' testCompile 'junit:junit:4.11' + testCompile 'org.hamcrest:hamcrest-library:1.3' testCompile 'org.mockito:mockito-core:1.10.19' testCompile project(':cryptography-bc') } diff --git a/core/src/main/java/ch/dissem/bitmessage/ports/MemoryNodeRegistry.java b/core/src/main/java/ch/dissem/bitmessage/ports/MemoryNodeRegistry.java index c68a474..ff2ad95 100644 --- a/core/src/main/java/ch/dissem/bitmessage/ports/MemoryNodeRegistry.java +++ b/core/src/main/java/ch/dissem/bitmessage/ports/MemoryNodeRegistry.java @@ -46,15 +46,11 @@ public class MemoryNodeRegistry implements NodeRegistry { while (scanner.hasNext()) { try { String line = scanner.nextLine().trim(); - if (line.startsWith("#") || line.isEmpty()) { - // Ignore - continue; - } if (line.startsWith("[stream")) { stream = Long.parseLong(line.substring(8, line.lastIndexOf(']'))); streamSet = new HashSet<>(); stableNodes.put(stream, streamSet); - } else if (streamSet != null) { + } else if (streamSet != null && !line.isEmpty() && !line.startsWith("#")) { int portIndex = line.lastIndexOf(':'); InetAddress[] inetAddresses = InetAddress.getAllByName(line.substring(0, portIndex)); int port = Integer.valueOf(line.substring(portIndex + 1)); @@ -89,12 +85,12 @@ public class MemoryNodeRegistry implements NodeRegistry { known.remove(node); } } - } else { - Set nodes = stableNodes.get(stream); - if (nodes == null || nodes.isEmpty()) { + } + if (result.isEmpty()) { + if (stableNodes.isEmpty()) { loadStableNodes(); - nodes = stableNodes.get(stream); } + Set nodes = stableNodes.get(stream); if (nodes != null && !nodes.isEmpty()) { // To reduce load on stable nodes, only return one result.add(selectRandom(nodes)); diff --git a/core/src/test/java/ch/dissem/bitmessage/ports/NodeRegistryTest.java b/core/src/test/java/ch/dissem/bitmessage/ports/NodeRegistryTest.java new file mode 100644 index 0000000..0e54446 --- /dev/null +++ b/core/src/test/java/ch/dissem/bitmessage/ports/NodeRegistryTest.java @@ -0,0 +1,95 @@ +/* + * Copyright 2016 Christian Basler + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ch.dissem.bitmessage.ports; + +import ch.dissem.bitmessage.entity.valueobject.NetworkAddress; +import ch.dissem.bitmessage.utils.UnixTime; +import org.junit.Test; + +import java.util.Arrays; + +import static ch.dissem.bitmessage.utils.UnixTime.HOUR; +import static org.hamcrest.Matchers.*; +import static org.junit.Assert.assertThat; + +public class NodeRegistryTest { + private NodeRegistry registry = new MemoryNodeRegistry(); + + @Test + public void ensureGetKnownNodesWithoutStreamsYieldsEmpty() { + assertThat(registry.getKnownAddresses(10), empty()); + } + + @Test + public void ensureGetKnownNodesForStream1YieldsResult() { + assertThat(registry.getKnownAddresses(10, 1), hasSize(1)); + } + + @Test + public void ensureNodeIsStored() { + registry.offerAddresses(Arrays.asList( + new NetworkAddress.Builder() + .ipv4(127, 0, 0, 1) + .port(42) + .stream(1) + .time(UnixTime.now()) + .build(), + new NetworkAddress.Builder() + .ipv4(127, 0, 0, 2) + .port(42) + .stream(1) + .time(UnixTime.now()) + .build(), + new NetworkAddress.Builder() + .ipv4(127, 0, 0, 2) + .port(42) + .stream(2) + .time(UnixTime.now()) + .build() + )); + assertThat(registry.getKnownAddresses(10, 1).size(), is(2)); + assertThat(registry.getKnownAddresses(10, 2).size(), is(1)); + assertThat(registry.getKnownAddresses(10, 1, 2).size(), is(3)); + } + + @Test + public void ensureOldNodesAreRemoved() { + registry.offerAddresses(Arrays.asList( + new NetworkAddress.Builder() + .ipv4(127, 0, 0, 1) + .port(42) + .stream(1) + .time(UnixTime.now()) + .build(), + new NetworkAddress.Builder() + .ipv4(127, 0, 0, 2) + .port(42) + .stream(1) + .time(UnixTime.now(-4 * HOUR)) + .build(), + new NetworkAddress.Builder() + .ipv4(127, 0, 0, 2) + .port(42) + .stream(2) + .time(UnixTime.now()) + .build() + )); + assertThat(registry.getKnownAddresses(10, 1).size(), is(1)); + assertThat(registry.getKnownAddresses(10, 2).size(), is(1)); + assertThat(registry.getKnownAddresses(10, 1, 2).size(), is(2)); + } +}