[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:
Matt Morley
2026-03-29 20:41:32 -07:00
committed by GitHub
parent ceb712b089
commit db42c6cbba
8 changed files with 92 additions and 11 deletions

View File

@@ -3,11 +3,10 @@
// the WPILib BSD license file in the root directory of this project.
#include <array>
#include <chrono>
#include <string>
#include <string_view>
#include <thread>
#include <utility>
#include <vector>
#include <gtest/gtest.h>
@@ -17,7 +16,7 @@
TEST(MulticastServiceAnnouncerTest, EmptyText) {
const std::string_view serviceName = "TestServiceNoText";
const std::string_view serviceType = "_wpinotxt";
const std::string_view serviceType = "_wpinotxt._tcp";
const int port = std::rand();
wpi::net::MulticastServiceAnnouncer announcer(serviceName, serviceType, port);
wpi::net::MulticastServiceResolver resolver(serviceType);
@@ -26,7 +25,20 @@ TEST(MulticastServiceAnnouncerTest, EmptyText) {
announcer.Start();
resolver.Start();
std::this_thread::sleep_for(std::chrono::seconds(1));
std::vector<wpi::net::MulticastServiceResolver::ServiceData> allData;
for (int i = 0; i < 15; i++) {
// GetData gives me new data since last time. This smoketest is just
// looking for -any- response
allData = resolver.GetData();
if (!allData.empty()) {
break;
}
std::this_thread::sleep_for(std::chrono::seconds(1));
}
ASSERT_GT(allData.size(), 0ul);
resolver.Stop();
announcer.Stop();