Test for NodeRegistry
This commit is contained in:
parent
a67ac27921
commit
e44dd967d0
@ -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')
|
||||
}
|
||||
|
@ -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<NetworkAddress> nodes = stableNodes.get(stream);
|
||||
if (nodes == null || nodes.isEmpty()) {
|
||||
}
|
||||
if (result.isEmpty()) {
|
||||
if (stableNodes.isEmpty()) {
|
||||
loadStableNodes();
|
||||
nodes = stableNodes.get(stream);
|
||||
}
|
||||
Set<NetworkAddress> nodes = stableNodes.get(stream);
|
||||
if (nodes != null && !nodes.isEmpty()) {
|
||||
// To reduce load on stable nodes, only return one
|
||||
result.add(selectRandom(nodes));
|
||||
|
@ -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));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user