Skip to content
This repository has been archived by the owner on Aug 7, 2023. It is now read-only.

Commit

Permalink
Add unit test coverage fixes #262 (#263)
Browse files Browse the repository at this point in the history
Increase test coverage of TCPRegistry
  • Loading branch information
tgd committed Jul 24, 2023
1 parent 5bdc1ab commit faf872a
Showing 1 changed file with 87 additions and 2 deletions.
89 changes: 87 additions & 2 deletions src/test/java/net/openhft/chronicle/network/TCPRegistryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,18 @@
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.net.InetSocketAddress;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.fail;
import static org.junit.jupiter.api.Assertions.*;

public abstract class TCPRegistryTest extends NetworkTestCommon {

/**
* {@link TCPRegistry#lookup(String)} contains logic to look up an address from system properties where the key
* maps to a host:port pair. This key constant is used to map to various host:port combinations in tests below.
*/
public static final String SYSTEM_PROPERTY_HOST_DETAILS_KEY = "exampleHostKey";

@Test
void testResetClearsRegistry() throws IOException {
TCPRegistry.createServerSocketChannelFor("host1", "host2", "host3");
Expand All @@ -47,6 +53,85 @@ void testResetIsIdempotent() throws IOException {
TCPRegistry.reset();
}

@Test
void testAllServersStopped() throws IOException {
TCPRegistry.createServerSocketChannelFor("host1", "host2", "host3");
TCPRegistry.reset();
TCPRegistry.assertAllServersStopped();
}

@Test
void createServerSocketChannelFor_withHostAndInvalidPort() {
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class,
() -> TCPRegistry.createServerSocketChannelFor("host1:-1")
);
assertEquals("port out of range:-1", exception.getMessage());
}

@Test
public void lookup_lookupViaSystemProperty_empty() {
try {
System.setProperty(SYSTEM_PROPERTY_HOST_DETAILS_KEY, "");
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> TCPRegistry.lookup(SYSTEM_PROPERTY_HOST_DETAILS_KEY));
assertEquals("Alias " + SYSTEM_PROPERTY_HOST_DETAILS_KEY + " as malformed, expected hostname:port", exception.getMessage());
} finally {
System.clearProperty(SYSTEM_PROPERTY_HOST_DETAILS_KEY);
}
}

@Test
public void lookup_lookupViaSystemProperty_nullHostname() {
try {
System.setProperty(SYSTEM_PROPERTY_HOST_DETAILS_KEY, "null:");
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> TCPRegistry.lookup(SYSTEM_PROPERTY_HOST_DETAILS_KEY));
assertEquals("Invalid hostname \"null\"", exception.getMessage());
} finally {
System.clearProperty(SYSTEM_PROPERTY_HOST_DETAILS_KEY);
}
}

@Test
public void lookup_lookupViaSystemProperty_invalidPort() {
try {
System.setProperty(SYSTEM_PROPERTY_HOST_DETAILS_KEY, "a:z");
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> TCPRegistry.lookup(SYSTEM_PROPERTY_HOST_DETAILS_KEY));
assertEquals("Alias " + SYSTEM_PROPERTY_HOST_DETAILS_KEY + " as a:z malformed, expected hostname:port with port as a number", exception.getMessage());
} finally {
System.clearProperty(SYSTEM_PROPERTY_HOST_DETAILS_KEY);
}
}

@Test
public void lookup_lookupViaSystemProperty_wellFormed() {
try {
System.setProperty(SYSTEM_PROPERTY_HOST_DETAILS_KEY, "host:9999");
InetSocketAddress address = TCPRegistry.lookup(SYSTEM_PROPERTY_HOST_DETAILS_KEY);
assertEquals("host", address.getHostName());
assertEquals(9999, address.getPort());
} finally {
System.clearProperty(SYSTEM_PROPERTY_HOST_DETAILS_KEY);
}
}

@Test
public void lookup_fallback_wellFormed() {
InetSocketAddress address = TCPRegistry.lookup("host:9999");
assertEquals("host", address.getHostName());
assertEquals(9999, address.getPort());
}

@Test
public void lookup_fallback_malformedPort() {
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> TCPRegistry.lookup("host:a"));
assertEquals("Description host:a malformed, expected hostname:port with port as a number", exception.getMessage());
}

@Test
void dumpAll() throws IOException {
TCPRegistry.createServerSocketChannelFor("host1", "host2", "host3");
TCPRegistry.dumpAllSocketChannels();
}

private void assertNotMapped(String hostName) {
try {
TCPRegistry.lookup(hostName);
Expand Down

0 comments on commit faf872a

Please sign in to comment.