Test SRV lookup against stubbed records, not the live DNS

The test resolved voice.teamspeak.com for real, so it failed whenever
that name, or the network, did not answer. It now checks the ordering
and parsing with a stubbed resolver.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-09-25 11:03:41 +00:00
co-authored by Claude Opus 5.5
parent c47bbdf96c
commit d89a02957a
@@ -1,27 +1,47 @@
package com.github.manevolent.ts3j; package com.github.manevolent.ts3j;
import com.github.manevolent.ts3j.protocol.TS3DNS; import com.github.manevolent.ts3j.protocol.TS3DNS;
import com.github.manevolent.ts3j.util.Ts3Debugging;
import junit.framework.TestCase; import junit.framework.TestCase;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.SocketAddress; import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List; import java.util.List;
/** SRV answers are stubbed: the test must not depend on what the network resolves today. */
public class TS3DNSTest extends TestCase { public class TS3DNSTest extends TestCase {
public static void main(String[] args) throws Exception { @Override
new TS3DNSTest().testParser(); protected void tearDown() {
TS3DNS.setSrvLookup(null);
} }
public void testParser() throws Exception { public void testOrdersRecordsByPriorityThenWeight() throws Exception {
Ts3Debugging.setEnabled(true); TS3DNS.setSrvLookup(name -> {
assertEquals("_ts3._udp.voice.example.com", name);
return Arrays.asList("20 0 9989 backup.example.com.", "10 5 9988 b.example.com.",
"10 1 9987 a.example.com.");
});
List<InetSocketAddress> soaddr = TS3DNS.lookup("voice.teamspeak.com"); List<InetSocketAddress> addresses = TS3DNS.lookup("voice.example.com");
assertTrue(soaddr.size() > 0); assertEquals(3, addresses.size());
assertEquals("a.example.com", addresses.get(0).getHostString());
assertEquals(9987, addresses.get(0).getPort());
assertEquals("b.example.com", addresses.get(1).getHostString());
assertEquals("backup.example.com", addresses.get(2).getHostString());
}
for (SocketAddress socketAddress : soaddr) public void testNoRecordMeansTheHostIsNotFound() {
Ts3Debugging.debug(socketAddress); TS3DNS.setSrvLookup(name -> Collections.emptyList());
try {
TS3DNS.lookup("voice.example.com");
fail("expected UnknownHostException");
} catch (UnknownHostException expected) {
// the caller then connects to the address itself
} catch (Exception e) {
fail("unexpected " + e);
}
} }
} }