mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-25 01:41:43 +00:00
[wpinet] Add mDNS discovery tests and fix mDNS JNI bugs (#8682)
In https://github.com/wpilibsuite/allwpilib/issues/8681 we discovered that multicast service types need to be valid (end with _tcp or _udp), or else errors are silently swallowed. Let's make our C++ unit test use a valid name and also check that it works. I think if we should/shouldn't do this is up for debate still. I also discovered two bugs in the JNI code that lead to incorrect results being returned - Return array index was always 0 - Use of JLocal for the return value seems to mean that the array will always be NULL in java
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
// Copyright (c) FIRST and other WPILib contributors.
|
||||
// Open Source Software; you can modify and/or share it under the terms of
|
||||
// the WPILib BSD license file in the root directory of this project.
|
||||
|
||||
package org.wpilib.net;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class MulticastServiceAnnouncerTest {
|
||||
@Test
|
||||
void emptyText() throws InterruptedException, UnknownHostException {
|
||||
final String serviceName = "Foaksdfjasklfj";
|
||||
final String serviceType = "_wpinotxt._tcp";
|
||||
final int port = 12345;
|
||||
|
||||
try (MulticastServiceAnnouncer announcer =
|
||||
new MulticastServiceAnnouncer(serviceName, serviceType, port);
|
||||
MulticastServiceResolver resolver = new MulticastServiceResolver(serviceType)) {
|
||||
assertTrue(announcer.hasImplementation() && resolver.hasImplementation());
|
||||
|
||||
announcer.start();
|
||||
resolver.start();
|
||||
|
||||
List<ServiceData> allData = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < 10; i++) {
|
||||
ServiceData[] data = resolver.getData();
|
||||
if (data == null) {
|
||||
continue;
|
||||
}
|
||||
|
||||
allData.addAll(List.of(data));
|
||||
|
||||
if (!allData.isEmpty()) {
|
||||
break;
|
||||
}
|
||||
|
||||
Thread.sleep(1000);
|
||||
}
|
||||
|
||||
assertFalse(allData.isEmpty(), "Expected at least one service entry, but got none");
|
||||
|
||||
resolver.stop();
|
||||
announcer.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user