Merge "Address leftover comments" into main am: bbaacf4d38

Original change: https://android-review.googlesource.com/c/platform/packages/modules/Connectivity/+/2664158

Change-Id: I92ec30f849a1598672007111cf1253e94efc1651
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Paul Hu
2023-07-21 03:41:51 +00:00
committed by Automerger Merge Worker
4 changed files with 76 additions and 77 deletions

View File

@@ -16,6 +16,8 @@
package com.android.server.connectivity.mdns; package com.android.server.connectivity.mdns;
import static com.android.server.connectivity.mdns.MdnsServiceTypeClient.INVALID_TRANSACTION_ID;
import android.annotation.NonNull; import android.annotation.NonNull;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
@@ -102,6 +104,11 @@ public class EnqueueMdnsQueryCallable implements Callable<Pair<Integer, List<Str
this.clock = clock; this.clock = clock;
} }
/**
* Call to execute the mdns query.
*
* @return The pair of transaction id and the subtypes for the query.
*/
// Incompatible return type for override of Callable#call(). // Incompatible return type for override of Callable#call().
@SuppressWarnings("nullness:override.return.invalid") @SuppressWarnings("nullness:override.return.invalid")
@Override @Override
@@ -109,7 +116,7 @@ public class EnqueueMdnsQueryCallable implements Callable<Pair<Integer, List<Str
try { try {
MdnsSocketClientBase requestSender = weakRequestSender.get(); MdnsSocketClientBase requestSender = weakRequestSender.get();
if (requestSender == null) { if (requestSender == null) {
return Pair.create(-1, new ArrayList<>()); return Pair.create(INVALID_TRANSACTION_ID, new ArrayList<>());
} }
int numQuestions = 0; int numQuestions = 0;
@@ -156,7 +163,7 @@ public class EnqueueMdnsQueryCallable implements Callable<Pair<Integer, List<Str
if (numQuestions == 0) { if (numQuestions == 0) {
// No query to send // No query to send
return Pair.create(-1, new ArrayList<>()); return Pair.create(INVALID_TRANSACTION_ID, new ArrayList<>());
} }
// Header. // Header.
@@ -195,7 +202,7 @@ public class EnqueueMdnsQueryCallable implements Callable<Pair<Integer, List<Str
} catch (IOException e) { } catch (IOException e) {
LOGGER.e(String.format("Failed to create mDNS packet for subtype: %s.", LOGGER.e(String.format("Failed to create mDNS packet for subtype: %s.",
TextUtils.join(",", subtypes)), e); TextUtils.join(",", subtypes)), e);
return Pair.create(-1, new ArrayList<>()); return Pair.create(INVALID_TRANSACTION_ID, new ArrayList<>());
} }
} }

View File

@@ -50,10 +50,6 @@ public class MdnsConfigs {
return false; return false;
} }
public static boolean useSessionIdToScheduleMdnsTask() {
return true;
}
public static long sleepTimeForSocketThreadMs() { public static long sleepTimeForSocketThreadMs() {
return 20_000L; return 20_000L;
} }

View File

@@ -56,6 +56,7 @@ public class MdnsServiceTypeClient {
@VisibleForTesting @VisibleForTesting
static final int EVENT_START_QUERYTASK = 1; static final int EVENT_START_QUERYTASK = 1;
static final int EVENT_QUERY_RESULT = 2; static final int EVENT_QUERY_RESULT = 2;
static final int INVALID_TRANSACTION_ID = -1;
private final String serviceType; private final String serviceType;
private final String[] serviceTypeLabels; private final String[] serviceTypeLabels;
@@ -109,16 +110,15 @@ public class MdnsServiceTypeClient {
break; break;
} }
case EVENT_QUERY_RESULT: { case EVENT_QUERY_RESULT: {
final QuerySentResult sentResult = (QuerySentResult) msg.obj; final QuerySentArguments sentResult = (QuerySentArguments) msg.obj;
if (MdnsConfigs.useSessionIdToScheduleMdnsTask()) { // If a task is cancelled while the Executor is running it, EVENT_QUERY_RESULT
// In case that the task is not canceled successfully, use session ID to // will still be sent when it ends. So use session ID to check if this task
// check if this task should continue to schedule more. // should continue to schedule more.
if (sentResult.taskArgs.sessionId != currentSessionId) { if (sentResult.taskArgs.sessionId != currentSessionId) {
break; break;
}
} }
if ((sentResult.transactionId != -1)) { if ((sentResult.transactionId != INVALID_TRANSACTION_ID)) {
for (int i = 0; i < listeners.size(); i++) { for (int i = 0; i < listeners.size(); i++) {
listeners.keyAt(i).onDiscoveryQuerySent( listeners.keyAt(i).onDiscoveryQuerySent(
sentResult.subTypes, sentResult.transactionId); sentResult.subTypes, sentResult.transactionId);
@@ -747,12 +747,12 @@ public class MdnsServiceTypeClient {
} }
} }
private static class QuerySentResult { private static class QuerySentArguments {
private final int transactionId; private final int transactionId;
private final List<String> subTypes = new ArrayList<>(); private final List<String> subTypes = new ArrayList<>();
private final ScheduledQueryTaskArgs taskArgs; private final ScheduledQueryTaskArgs taskArgs;
QuerySentResult(int transactionId, @NonNull List<String> subTypes, QuerySentArguments(int transactionId, @NonNull List<String> subTypes,
@NonNull ScheduledQueryTaskArgs taskArgs) { @NonNull ScheduledQueryTaskArgs taskArgs) {
this.transactionId = transactionId; this.transactionId = transactionId;
this.subTypes.addAll(subTypes); this.subTypes.addAll(subTypes);
@@ -795,11 +795,11 @@ public class MdnsServiceTypeClient {
} catch (RuntimeException e) { } catch (RuntimeException e) {
sharedLog.e(String.format("Failed to run EnqueueMdnsQueryCallable for subtype: %s", sharedLog.e(String.format("Failed to run EnqueueMdnsQueryCallable for subtype: %s",
TextUtils.join(",", taskArgs.config.subtypes)), e); TextUtils.join(",", taskArgs.config.subtypes)), e);
result = Pair.create(-1, new ArrayList<>()); result = Pair.create(INVALID_TRANSACTION_ID, new ArrayList<>());
} }
dependencies.sendMessage( dependencies.sendMessage(
handler, handler.obtainMessage(EVENT_QUERY_RESULT, handler, handler.obtainMessage(EVENT_QUERY_RESULT,
new QuerySentResult(result.first, result.second, taskArgs))); new QuerySentArguments(result.first, result.second, taskArgs)));
} }
} }

View File

@@ -93,6 +93,7 @@ public class MdnsServiceTypeClientTests {
private static final int INTERFACE_INDEX = 999; private static final int INTERFACE_INDEX = 999;
private static final long DEFAULT_TIMEOUT = 2000L; private static final long DEFAULT_TIMEOUT = 2000L;
private static final String SERVICE_TYPE = "_googlecast._tcp.local"; private static final String SERVICE_TYPE = "_googlecast._tcp.local";
private static final String SUBTYPE = "_subtype";
private static final String[] SERVICE_TYPE_LABELS = TextUtils.split(SERVICE_TYPE, "\\."); private static final String[] SERVICE_TYPE_LABELS = TextUtils.split(SERVICE_TYPE, "\\.");
private static final InetSocketAddress IPV4_ADDRESS = new InetSocketAddress( private static final InetSocketAddress IPV4_ADDRESS = new InetSocketAddress(
MdnsConstants.getMdnsIPv4Address(), MdnsConstants.MDNS_PORT); MdnsConstants.getMdnsIPv4Address(), MdnsConstants.MDNS_PORT);
@@ -262,7 +263,7 @@ public class MdnsServiceTypeClientTests {
@Test @Test
public void sendQueries_activeScanMode() { public void sendQueries_activeScanMode() {
MdnsSearchOptions searchOptions = MdnsSearchOptions searchOptions =
MdnsSearchOptions.newBuilder().addSubtype("12345").setIsPassiveMode(false).build(); MdnsSearchOptions.newBuilder().addSubtype(SUBTYPE).setIsPassiveMode(false).build();
startSendAndReceive(mockListenerOne, searchOptions); startSendAndReceive(mockListenerOne, searchOptions);
// Always try to remove the task. // Always try to remove the task.
verify(mockDeps, times(1)).removeMessages(any(), eq(EVENT_START_QUERYTASK)); verify(mockDeps, times(1)).removeMessages(any(), eq(EVENT_START_QUERYTASK));
@@ -314,7 +315,7 @@ public class MdnsServiceTypeClientTests {
@Test @Test
public void sendQueries_reentry_activeScanMode() { public void sendQueries_reentry_activeScanMode() {
MdnsSearchOptions searchOptions = MdnsSearchOptions searchOptions =
MdnsSearchOptions.newBuilder().addSubtype("12345").setIsPassiveMode(false).build(); MdnsSearchOptions.newBuilder().addSubtype(SUBTYPE).setIsPassiveMode(false).build();
startSendAndReceive(mockListenerOne, searchOptions); startSendAndReceive(mockListenerOne, searchOptions);
// Always try to remove the task. // Always try to remove the task.
verify(mockDeps, times(1)).removeMessages(any(), eq(EVENT_START_QUERYTASK)); verify(mockDeps, times(1)).removeMessages(any(), eq(EVENT_START_QUERYTASK));
@@ -325,8 +326,8 @@ public class MdnsServiceTypeClientTests {
// After the first query is sent, change the subtypes, and restart. // After the first query is sent, change the subtypes, and restart.
searchOptions = searchOptions =
MdnsSearchOptions.newBuilder() MdnsSearchOptions.newBuilder()
.addSubtype("12345") .addSubtype(SUBTYPE)
.addSubtype("abcde") .addSubtype("_subtype2")
.setIsPassiveMode(false) .setIsPassiveMode(false)
.build(); .build();
startSendAndReceive(mockListenerOne, searchOptions); startSendAndReceive(mockListenerOne, searchOptions);
@@ -348,7 +349,7 @@ public class MdnsServiceTypeClientTests {
@Test @Test
public void sendQueries_passiveScanMode() { public void sendQueries_passiveScanMode() {
MdnsSearchOptions searchOptions = MdnsSearchOptions searchOptions =
MdnsSearchOptions.newBuilder().addSubtype("12345").setIsPassiveMode(true).build(); MdnsSearchOptions.newBuilder().addSubtype(SUBTYPE).setIsPassiveMode(true).build();
startSendAndReceive(mockListenerOne, searchOptions); startSendAndReceive(mockListenerOne, searchOptions);
// Always try to remove the task. // Always try to remove the task.
verify(mockDeps, times(1)).removeMessages(any(), eq(EVENT_START_QUERYTASK)); verify(mockDeps, times(1)).removeMessages(any(), eq(EVENT_START_QUERYTASK));
@@ -374,7 +375,7 @@ public class MdnsServiceTypeClientTests {
@Test @Test
public void sendQueries_activeScanWithQueryBackoff() { public void sendQueries_activeScanWithQueryBackoff() {
MdnsSearchOptions searchOptions = MdnsSearchOptions searchOptions =
MdnsSearchOptions.newBuilder().addSubtype("12345").setIsPassiveMode( MdnsSearchOptions.newBuilder().addSubtype(SUBTYPE).setIsPassiveMode(
false).setNumOfQueriesBeforeBackoff(11).build(); false).setNumOfQueriesBeforeBackoff(11).build();
startSendAndReceive(mockListenerOne, searchOptions); startSendAndReceive(mockListenerOne, searchOptions);
// Always try to remove the task. // Always try to remove the task.
@@ -433,7 +434,7 @@ public class MdnsServiceTypeClientTests {
@Test @Test
public void sendQueries_passiveScanWithQueryBackoff() { public void sendQueries_passiveScanWithQueryBackoff() {
MdnsSearchOptions searchOptions = MdnsSearchOptions searchOptions =
MdnsSearchOptions.newBuilder().addSubtype("12345").setIsPassiveMode( MdnsSearchOptions.newBuilder().addSubtype(SUBTYPE).setIsPassiveMode(
true).setNumOfQueriesBeforeBackoff(3).build(); true).setNumOfQueriesBeforeBackoff(3).build();
startSendAndReceive(mockListenerOne, searchOptions); startSendAndReceive(mockListenerOne, searchOptions);
// Always try to remove the task. // Always try to remove the task.
@@ -492,7 +493,7 @@ public class MdnsServiceTypeClientTests {
@Test @Test
public void sendQueries_reentry_passiveScanMode() { public void sendQueries_reentry_passiveScanMode() {
MdnsSearchOptions searchOptions = MdnsSearchOptions searchOptions =
MdnsSearchOptions.newBuilder().addSubtype("12345").setIsPassiveMode(true).build(); MdnsSearchOptions.newBuilder().addSubtype(SUBTYPE).setIsPassiveMode(true).build();
startSendAndReceive(mockListenerOne, searchOptions); startSendAndReceive(mockListenerOne, searchOptions);
// Always try to remove the task. // Always try to remove the task.
verify(mockDeps, times(1)).removeMessages(any(), eq(EVENT_START_QUERYTASK)); verify(mockDeps, times(1)).removeMessages(any(), eq(EVENT_START_QUERYTASK));
@@ -503,8 +504,8 @@ public class MdnsServiceTypeClientTests {
// After the first query is sent, change the subtypes, and restart. // After the first query is sent, change the subtypes, and restart.
searchOptions = searchOptions =
MdnsSearchOptions.newBuilder() MdnsSearchOptions.newBuilder()
.addSubtype("12345") .addSubtype(SUBTYPE)
.addSubtype("abcde") .addSubtype("_subtype2")
.setIsPassiveMode(true) .setIsPassiveMode(true)
.build(); .build();
startSendAndReceive(mockListenerOne, searchOptions); startSendAndReceive(mockListenerOne, searchOptions);
@@ -528,7 +529,7 @@ public class MdnsServiceTypeClientTests {
public void testQueryTaskConfig_alwaysAskForUnicastResponse() { public void testQueryTaskConfig_alwaysAskForUnicastResponse() {
//MdnsConfigsFlagsImpl.alwaysAskForUnicastResponseInEachBurst.override(true); //MdnsConfigsFlagsImpl.alwaysAskForUnicastResponseInEachBurst.override(true);
MdnsSearchOptions searchOptions = MdnsSearchOptions searchOptions =
MdnsSearchOptions.newBuilder().addSubtype("12345").setIsPassiveMode(false).build(); MdnsSearchOptions.newBuilder().addSubtype(SUBTYPE).setIsPassiveMode(false).build();
QueryTaskConfig config = new QueryTaskConfig( QueryTaskConfig config = new QueryTaskConfig(
searchOptions.getSubtypes(), searchOptions.isPassiveMode(), searchOptions.getSubtypes(), searchOptions.isPassiveMode(),
false /* onlyUseIpv6OnIpv6OnlyNetworks */, 3 /* numOfQueriesBeforeBackoff */, false /* onlyUseIpv6OnIpv6OnlyNetworks */, 3 /* numOfQueriesBeforeBackoff */,
@@ -559,7 +560,7 @@ public class MdnsServiceTypeClientTests {
@Test @Test
public void testQueryTaskConfig_askForUnicastInFirstQuery() { public void testQueryTaskConfig_askForUnicastInFirstQuery() {
MdnsSearchOptions searchOptions = MdnsSearchOptions searchOptions =
MdnsSearchOptions.newBuilder().addSubtype("12345").setIsPassiveMode(false).build(); MdnsSearchOptions.newBuilder().addSubtype(SUBTYPE).setIsPassiveMode(false).build();
QueryTaskConfig config = new QueryTaskConfig( QueryTaskConfig config = new QueryTaskConfig(
searchOptions.getSubtypes(), searchOptions.isPassiveMode(), searchOptions.getSubtypes(), searchOptions.isPassiveMode(),
false /* onlyUseIpv6OnIpv6OnlyNetworks */, 3 /* numOfQueriesBeforeBackoff */, false /* onlyUseIpv6OnIpv6OnlyNetworks */, 3 /* numOfQueriesBeforeBackoff */,
@@ -590,15 +591,15 @@ public class MdnsServiceTypeClientTests {
@Test @Test
public void testIfPreviousTaskIsCanceledWhenNewSessionStarts() { public void testIfPreviousTaskIsCanceledWhenNewSessionStarts() {
MdnsSearchOptions searchOptions = MdnsSearchOptions searchOptions =
MdnsSearchOptions.newBuilder().addSubtype("12345").setIsPassiveMode(true).build(); MdnsSearchOptions.newBuilder().addSubtype(SUBTYPE).setIsPassiveMode(true).build();
startSendAndReceive(mockListenerOne, searchOptions); startSendAndReceive(mockListenerOne, searchOptions);
Runnable firstMdnsTask = currentThreadExecutor.getAndClearSubmittedRunnable(); Runnable firstMdnsTask = currentThreadExecutor.getAndClearSubmittedRunnable();
// Change the sutypes and start a new session. // Change the sutypes and start a new session.
searchOptions = searchOptions =
MdnsSearchOptions.newBuilder() MdnsSearchOptions.newBuilder()
.addSubtype("12345") .addSubtype(SUBTYPE)
.addSubtype("abcde") .addSubtype("_subtype2")
.setIsPassiveMode(true) .setIsPassiveMode(true)
.build(); .build();
startSendAndReceive(mockListenerOne, searchOptions); startSendAndReceive(mockListenerOne, searchOptions);
@@ -619,7 +620,7 @@ public class MdnsServiceTypeClientTests {
public void testIfPreviousTaskIsCanceledWhenSessionStops() { public void testIfPreviousTaskIsCanceledWhenSessionStops() {
//MdnsConfigsFlagsImpl.shouldCancelScanTaskWhenFutureIsNull.override(true); //MdnsConfigsFlagsImpl.shouldCancelScanTaskWhenFutureIsNull.override(true);
MdnsSearchOptions searchOptions = MdnsSearchOptions searchOptions =
MdnsSearchOptions.newBuilder().addSubtype("12345").setIsPassiveMode(true).build(); MdnsSearchOptions.newBuilder().addSubtype(SUBTYPE).setIsPassiveMode(true).build();
startSendAndReceive(mockListenerOne, searchOptions); startSendAndReceive(mockListenerOne, searchOptions);
// Change the sutypes and start a new session. // Change the sutypes and start a new session.
stopSendAndReceive(mockListenerOne); stopSendAndReceive(mockListenerOne);
@@ -708,14 +709,12 @@ public class MdnsServiceTypeClientTests {
// Process the initial response. // Process the initial response.
processResponse(createResponse( processResponse(createResponse(
"service-instance-1", ipV4Address, 5353, "service-instance-1", ipV4Address, 5353, SUBTYPE,
/* subtype= */ "ABCDE",
Collections.emptyMap(), TEST_TTL), socketKey); Collections.emptyMap(), TEST_TTL), socketKey);
// Process a second response with a different port and updated text attributes. // Process a second response with a different port and updated text attributes.
processResponse(createResponse( processResponse(createResponse(
"service-instance-1", ipV4Address, 5354, "service-instance-1", ipV4Address, 5354, SUBTYPE,
/* subtype= */ "ABCDE",
Collections.singletonMap("key", "value"), TEST_TTL), Collections.singletonMap("key", "value"), TEST_TTL),
socketKey); socketKey);
@@ -727,7 +726,7 @@ public class MdnsServiceTypeClientTests {
List.of(ipV4Address) /* ipv4Address */, List.of(ipV4Address) /* ipv4Address */,
List.of() /* ipv6Address */, List.of() /* ipv6Address */,
5353 /* port */, 5353 /* port */,
Collections.singletonList("ABCDE") /* subTypes */, Collections.singletonList(SUBTYPE) /* subTypes */,
Collections.singletonMap("key", null) /* attributes */, Collections.singletonMap("key", null) /* attributes */,
socketKey); socketKey);
@@ -737,7 +736,7 @@ public class MdnsServiceTypeClientTests {
assertEquals(initialServiceInfo.getServiceInstanceName(), "service-instance-1"); assertEquals(initialServiceInfo.getServiceInstanceName(), "service-instance-1");
assertEquals(initialServiceInfo.getIpv4Address(), ipV4Address); assertEquals(initialServiceInfo.getIpv4Address(), ipV4Address);
assertEquals(initialServiceInfo.getPort(), 5353); assertEquals(initialServiceInfo.getPort(), 5353);
assertEquals(initialServiceInfo.getSubtypes(), Collections.singletonList("ABCDE")); assertEquals(initialServiceInfo.getSubtypes(), Collections.singletonList(SUBTYPE));
assertNull(initialServiceInfo.getAttributeByKey("key")); assertNull(initialServiceInfo.getAttributeByKey("key"));
assertEquals(socketKey.getInterfaceIndex(), initialServiceInfo.getInterfaceIndex()); assertEquals(socketKey.getInterfaceIndex(), initialServiceInfo.getInterfaceIndex());
assertEquals(socketKey.getNetwork(), initialServiceInfo.getNetwork()); assertEquals(socketKey.getNetwork(), initialServiceInfo.getNetwork());
@@ -749,7 +748,7 @@ public class MdnsServiceTypeClientTests {
assertEquals(updatedServiceInfo.getIpv4Address(), ipV4Address); assertEquals(updatedServiceInfo.getIpv4Address(), ipV4Address);
assertEquals(updatedServiceInfo.getPort(), 5354); assertEquals(updatedServiceInfo.getPort(), 5354);
assertTrue(updatedServiceInfo.hasSubtypes()); assertTrue(updatedServiceInfo.hasSubtypes());
assertEquals(updatedServiceInfo.getSubtypes(), Collections.singletonList("ABCDE")); assertEquals(updatedServiceInfo.getSubtypes(), Collections.singletonList(SUBTYPE));
assertEquals(updatedServiceInfo.getAttributeByKey("key"), "value"); assertEquals(updatedServiceInfo.getAttributeByKey("key"), "value");
assertEquals(socketKey.getInterfaceIndex(), updatedServiceInfo.getInterfaceIndex()); assertEquals(socketKey.getInterfaceIndex(), updatedServiceInfo.getInterfaceIndex());
assertEquals(socketKey.getNetwork(), updatedServiceInfo.getNetwork()); assertEquals(socketKey.getNetwork(), updatedServiceInfo.getNetwork());
@@ -762,14 +761,12 @@ public class MdnsServiceTypeClientTests {
// Process the initial response. // Process the initial response.
processResponse(createResponse( processResponse(createResponse(
"service-instance-1", ipV6Address, 5353, "service-instance-1", ipV6Address, 5353, SUBTYPE,
/* subtype= */ "ABCDE",
Collections.emptyMap(), TEST_TTL), socketKey); Collections.emptyMap(), TEST_TTL), socketKey);
// Process a second response with a different port and updated text attributes. // Process a second response with a different port and updated text attributes.
processResponse(createResponse( processResponse(createResponse(
"service-instance-1", ipV6Address, 5354, "service-instance-1", ipV6Address, 5354, SUBTYPE,
/* subtype= */ "ABCDE",
Collections.singletonMap("key", "value"), TEST_TTL), Collections.singletonMap("key", "value"), TEST_TTL),
socketKey); socketKey);
@@ -781,7 +778,7 @@ public class MdnsServiceTypeClientTests {
List.of() /* ipv4Address */, List.of() /* ipv4Address */,
List.of(ipV6Address) /* ipv6Address */, List.of(ipV6Address) /* ipv6Address */,
5353 /* port */, 5353 /* port */,
Collections.singletonList("ABCDE") /* subTypes */, Collections.singletonList(SUBTYPE) /* subTypes */,
Collections.singletonMap("key", null) /* attributes */, Collections.singletonMap("key", null) /* attributes */,
socketKey); socketKey);
@@ -791,7 +788,7 @@ public class MdnsServiceTypeClientTests {
assertEquals(initialServiceInfo.getServiceInstanceName(), "service-instance-1"); assertEquals(initialServiceInfo.getServiceInstanceName(), "service-instance-1");
assertEquals(initialServiceInfo.getIpv6Address(), ipV6Address); assertEquals(initialServiceInfo.getIpv6Address(), ipV6Address);
assertEquals(initialServiceInfo.getPort(), 5353); assertEquals(initialServiceInfo.getPort(), 5353);
assertEquals(initialServiceInfo.getSubtypes(), Collections.singletonList("ABCDE")); assertEquals(initialServiceInfo.getSubtypes(), Collections.singletonList(SUBTYPE));
assertNull(initialServiceInfo.getAttributeByKey("key")); assertNull(initialServiceInfo.getAttributeByKey("key"));
assertEquals(socketKey.getInterfaceIndex(), initialServiceInfo.getInterfaceIndex()); assertEquals(socketKey.getInterfaceIndex(), initialServiceInfo.getInterfaceIndex());
assertEquals(socketKey.getNetwork(), initialServiceInfo.getNetwork()); assertEquals(socketKey.getNetwork(), initialServiceInfo.getNetwork());
@@ -803,7 +800,7 @@ public class MdnsServiceTypeClientTests {
assertEquals(updatedServiceInfo.getIpv6Address(), ipV6Address); assertEquals(updatedServiceInfo.getIpv6Address(), ipV6Address);
assertEquals(updatedServiceInfo.getPort(), 5354); assertEquals(updatedServiceInfo.getPort(), 5354);
assertTrue(updatedServiceInfo.hasSubtypes()); assertTrue(updatedServiceInfo.hasSubtypes());
assertEquals(updatedServiceInfo.getSubtypes(), Collections.singletonList("ABCDE")); assertEquals(updatedServiceInfo.getSubtypes(), Collections.singletonList(SUBTYPE));
assertEquals(updatedServiceInfo.getAttributeByKey("key"), "value"); assertEquals(updatedServiceInfo.getAttributeByKey("key"), "value");
assertEquals(socketKey.getInterfaceIndex(), updatedServiceInfo.getInterfaceIndex()); assertEquals(socketKey.getInterfaceIndex(), updatedServiceInfo.getInterfaceIndex());
assertEquals(socketKey.getNetwork(), updatedServiceInfo.getNetwork()); assertEquals(socketKey.getNetwork(), updatedServiceInfo.getNetwork());
@@ -865,8 +862,7 @@ public class MdnsServiceTypeClientTests {
public void reportExistingServiceToNewlyRegisteredListeners() throws Exception { public void reportExistingServiceToNewlyRegisteredListeners() throws Exception {
// Process the initial response. // Process the initial response.
processResponse(createResponse( processResponse(createResponse(
"service-instance-1", "192.168.1.1", 5353, "service-instance-1", "192.168.1.1", 5353, SUBTYPE,
/* subtype= */ "ABCDE",
Collections.emptyMap(), TEST_TTL), socketKey); Collections.emptyMap(), TEST_TTL), socketKey);
startSendAndReceive(mockListenerOne, MdnsSearchOptions.getDefaultOptions()); startSendAndReceive(mockListenerOne, MdnsSearchOptions.getDefaultOptions());
@@ -879,7 +875,7 @@ public class MdnsServiceTypeClientTests {
List.of("192.168.1.1") /* ipv4Address */, List.of("192.168.1.1") /* ipv4Address */,
List.of() /* ipv6Address */, List.of() /* ipv6Address */,
5353 /* port */, 5353 /* port */,
Collections.singletonList("ABCDE") /* subTypes */, Collections.singletonList(SUBTYPE) /* subTypes */,
Collections.singletonMap("key", null) /* attributes */, Collections.singletonMap("key", null) /* attributes */,
socketKey); socketKey);
@@ -889,7 +885,7 @@ public class MdnsServiceTypeClientTests {
assertEquals(existingServiceInfo.getServiceInstanceName(), "service-instance-1"); assertEquals(existingServiceInfo.getServiceInstanceName(), "service-instance-1");
assertEquals(existingServiceInfo.getIpv4Address(), "192.168.1.1"); assertEquals(existingServiceInfo.getIpv4Address(), "192.168.1.1");
assertEquals(existingServiceInfo.getPort(), 5353); assertEquals(existingServiceInfo.getPort(), 5353);
assertEquals(existingServiceInfo.getSubtypes(), Collections.singletonList("ABCDE")); assertEquals(existingServiceInfo.getSubtypes(), Collections.singletonList(SUBTYPE));
assertNull(existingServiceInfo.getAttributeByKey("key")); assertNull(existingServiceInfo.getAttributeByKey("key"));
// Process a goodbye message for the existing response. // Process a goodbye message for the existing response.
@@ -928,7 +924,7 @@ public class MdnsServiceTypeClientTests {
// Process the initial response. // Process the initial response.
processResponse(createResponse( processResponse(createResponse(
serviceInstanceName, "192.168.1.1", 5353, /* subtype= */ "ABCDE", serviceInstanceName, "192.168.1.1", 5353, SUBTYPE,
Collections.emptyMap(), TEST_TTL), socketKey); Collections.emptyMap(), TEST_TTL), socketKey);
// Clear the scheduled runnable. // Clear the scheduled runnable.
@@ -970,7 +966,7 @@ public class MdnsServiceTypeClientTests {
// Process the initial response. // Process the initial response.
processResponse(createResponse( processResponse(createResponse(
serviceInstanceName, "192.168.1.1", 5353, /* subtype= */ "ABCDE", serviceInstanceName, "192.168.1.1", 5353, SUBTYPE,
Collections.emptyMap(), TEST_TTL), socketKey); Collections.emptyMap(), TEST_TTL), socketKey);
// Clear the scheduled runnable. // Clear the scheduled runnable.
@@ -1004,7 +1000,7 @@ public class MdnsServiceTypeClientTests {
// Process the initial response. // Process the initial response.
processResponse(createResponse( processResponse(createResponse(
serviceInstanceName, "192.168.1.1", 5353, /* subtype= */ "ABCDE", serviceInstanceName, "192.168.1.1", 5353, SUBTYPE,
Collections.emptyMap(), TEST_TTL), socketKey); Collections.emptyMap(), TEST_TTL), socketKey);
// Clear the scheduled runnable. // Clear the scheduled runnable.
@@ -1028,19 +1024,18 @@ public class MdnsServiceTypeClientTests {
InOrder inOrder = inOrder(mockListenerOne); InOrder inOrder = inOrder(mockListenerOne);
// Process the initial response which is incomplete. // Process the initial response which is incomplete.
final String subtype = "ABCDE";
processResponse(createResponse( processResponse(createResponse(
serviceName, null, 5353, subtype, serviceName, null, 5353, SUBTYPE,
Collections.emptyMap(), TEST_TTL), socketKey); Collections.emptyMap(), TEST_TTL), socketKey);
// Process a second response which has ip address to make response become complete. // Process a second response which has ip address to make response become complete.
processResponse(createResponse( processResponse(createResponse(
serviceName, ipV4Address, 5353, subtype, serviceName, ipV4Address, 5353, SUBTYPE,
Collections.emptyMap(), TEST_TTL), socketKey); Collections.emptyMap(), TEST_TTL), socketKey);
// Process a third response with a different ip address, port and updated text attributes. // Process a third response with a different ip address, port and updated text attributes.
processResponse(createResponse( processResponse(createResponse(
serviceName, ipV6Address, 5354, subtype, serviceName, ipV6Address, 5354, SUBTYPE,
Collections.singletonMap("key", "value"), TEST_TTL), socketKey); Collections.singletonMap("key", "value"), TEST_TTL), socketKey);
// Process the last response which is goodbye message (with the main type, not subtype). // Process the last response which is goodbye message (with the main type, not subtype).
@@ -1057,7 +1052,7 @@ public class MdnsServiceTypeClientTests {
List.of() /* ipv4Address */, List.of() /* ipv4Address */,
List.of() /* ipv6Address */, List.of() /* ipv6Address */,
5353 /* port */, 5353 /* port */,
Collections.singletonList(subtype) /* subTypes */, Collections.singletonList(SUBTYPE) /* subTypes */,
Collections.singletonMap("key", null) /* attributes */, Collections.singletonMap("key", null) /* attributes */,
socketKey); socketKey);
@@ -1069,7 +1064,7 @@ public class MdnsServiceTypeClientTests {
List.of(ipV4Address) /* ipv4Address */, List.of(ipV4Address) /* ipv4Address */,
List.of() /* ipv6Address */, List.of() /* ipv6Address */,
5353 /* port */, 5353 /* port */,
Collections.singletonList(subtype) /* subTypes */, Collections.singletonList(SUBTYPE) /* subTypes */,
Collections.singletonMap("key", null) /* attributes */, Collections.singletonMap("key", null) /* attributes */,
socketKey); socketKey);
@@ -1081,7 +1076,7 @@ public class MdnsServiceTypeClientTests {
List.of(ipV4Address) /* ipv4Address */, List.of(ipV4Address) /* ipv4Address */,
List.of(ipV6Address) /* ipv6Address */, List.of(ipV6Address) /* ipv6Address */,
5354 /* port */, 5354 /* port */,
Collections.singletonList(subtype) /* subTypes */, Collections.singletonList(SUBTYPE) /* subTypes */,
Collections.singletonMap("key", "value") /* attributes */, Collections.singletonMap("key", "value") /* attributes */,
socketKey); socketKey);
@@ -1093,7 +1088,7 @@ public class MdnsServiceTypeClientTests {
List.of(ipV4Address) /* ipv4Address */, List.of(ipV4Address) /* ipv4Address */,
List.of(ipV6Address) /* ipv6Address */, List.of(ipV6Address) /* ipv6Address */,
5354 /* port */, 5354 /* port */,
Collections.singletonList("ABCDE") /* subTypes */, Collections.singletonList(SUBTYPE) /* subTypes */,
Collections.singletonMap("key", "value") /* attributes */, Collections.singletonMap("key", "value") /* attributes */,
socketKey); socketKey);
@@ -1105,7 +1100,7 @@ public class MdnsServiceTypeClientTests {
List.of(ipV4Address) /* ipv4Address */, List.of(ipV4Address) /* ipv4Address */,
List.of(ipV6Address) /* ipv6Address */, List.of(ipV6Address) /* ipv6Address */,
5354 /* port */, 5354 /* port */,
Collections.singletonList("ABCDE") /* subTypes */, Collections.singletonList(SUBTYPE) /* subTypes */,
Collections.singletonMap("key", "value") /* attributes */, Collections.singletonMap("key", "value") /* attributes */,
socketKey); socketKey);
} }
@@ -1524,15 +1519,16 @@ public class MdnsServiceTypeClientTests {
verify(mockListenerOne, never()).onServiceNameRemoved(matchServiceName(otherInstance)); verify(mockListenerOne, never()).onServiceNameRemoved(matchServiceName(otherInstance));
// mockListenerTwo gets notified for both though // mockListenerTwo gets notified for both though
verify(mockListenerTwo).onServiceNameDiscovered( final InOrder inOrder2 = inOrder(mockListenerTwo);
inOrder2.verify(mockListenerTwo).onServiceNameDiscovered(
matchServiceName(requestedInstance)); matchServiceName(requestedInstance));
verify(mockListenerTwo).onServiceFound(matchServiceName(requestedInstance)); inOrder2.verify(mockListenerTwo).onServiceFound(matchServiceName(requestedInstance));
inOrder2.verify(mockListenerTwo).onServiceRemoved(matchServiceName(requestedInstance));
inOrder2.verify(mockListenerTwo).onServiceNameRemoved(matchServiceName(requestedInstance));
verify(mockListenerTwo).onServiceNameDiscovered(matchServiceName(otherInstance)); verify(mockListenerTwo).onServiceNameDiscovered(matchServiceName(otherInstance));
verify(mockListenerTwo).onServiceFound(matchServiceName(otherInstance)); verify(mockListenerTwo).onServiceFound(matchServiceName(otherInstance));
verify(mockListenerTwo).onServiceRemoved(matchServiceName(otherInstance)); verify(mockListenerTwo).onServiceRemoved(matchServiceName(otherInstance));
verify(mockListenerTwo).onServiceNameRemoved(matchServiceName(otherInstance)); verify(mockListenerTwo).onServiceNameRemoved(matchServiceName(otherInstance));
verify(mockListenerTwo).onServiceRemoved(matchServiceName(requestedInstance));
verify(mockListenerTwo).onServiceNameRemoved(matchServiceName(requestedInstance));
} }
@Test @Test
@@ -1545,9 +1541,9 @@ public class MdnsServiceTypeClientTests {
InOrder inOrder = inOrder(mockListenerOne); InOrder inOrder = inOrder(mockListenerOne);
// Process a response which has ip address to make response become complete. // Process a response which has ip address to make response become complete.
final String subtype = "ABCDE";
processResponse(createResponse( processResponse(createResponse(
serviceName, ipV4Address, 5353, subtype, serviceName, ipV4Address, 5353, SUBTYPE,
Collections.emptyMap(), TEST_TTL), Collections.emptyMap(), TEST_TTL),
socketKey); socketKey);
@@ -1559,7 +1555,7 @@ public class MdnsServiceTypeClientTests {
List.of(ipV4Address) /* ipv4Address */, List.of(ipV4Address) /* ipv4Address */,
List.of() /* ipv6Address */, List.of() /* ipv6Address */,
5353 /* port */, 5353 /* port */,
Collections.singletonList(subtype) /* subTypes */, Collections.singletonList(SUBTYPE) /* subTypes */,
Collections.singletonMap("key", null) /* attributes */, Collections.singletonMap("key", null) /* attributes */,
socketKey); socketKey);
@@ -1571,7 +1567,7 @@ public class MdnsServiceTypeClientTests {
List.of(ipV4Address) /* ipv4Address */, List.of(ipV4Address) /* ipv4Address */,
List.of() /* ipv6Address */, List.of() /* ipv6Address */,
5353 /* port */, 5353 /* port */,
Collections.singletonList(subtype) /* subTypes */, Collections.singletonList(SUBTYPE) /* subTypes */,
Collections.singletonMap("key", null) /* attributes */, Collections.singletonMap("key", null) /* attributes */,
socketKey); socketKey);
@@ -1593,7 +1589,7 @@ public class MdnsServiceTypeClientTests {
List.of(ipV4Address) /* ipv4Address */, List.of(ipV4Address) /* ipv4Address */,
List.of() /* ipv6Address */, List.of() /* ipv6Address */,
5353 /* port */, 5353 /* port */,
Collections.singletonList(subtype) /* subTypes */, Collections.singletonList(SUBTYPE) /* subTypes */,
Collections.singletonMap("key", null) /* attributes */, Collections.singletonMap("key", null) /* attributes */,
socketKey); socketKey);
@@ -1606,14 +1602,14 @@ public class MdnsServiceTypeClientTests {
List.of(ipV4Address) /* ipv4Address */, List.of(ipV4Address) /* ipv4Address */,
List.of() /* ipv6Address */, List.of() /* ipv6Address */,
5353 /* port */, 5353 /* port */,
Collections.singletonList(subtype) /* subTypes */, Collections.singletonList(SUBTYPE) /* subTypes */,
Collections.singletonMap("key", null) /* attributes */, Collections.singletonMap("key", null) /* attributes */,
socketKey); socketKey);
// Process a response with a different ip address, port and updated text attributes. // Process a response with a different ip address, port and updated text attributes.
final String ipV6Address = "2001:db8::"; final String ipV6Address = "2001:db8::";
processResponse(createResponse( processResponse(createResponse(
serviceName, ipV6Address, 5354, subtype, serviceName, ipV6Address, 5354, SUBTYPE,
Collections.singletonMap("key", "value"), TEST_TTL), socketKey); Collections.singletonMap("key", "value"), TEST_TTL), socketKey);
// Verify the onServiceUpdated is called. // Verify the onServiceUpdated is called.
@@ -1624,7 +1620,7 @@ public class MdnsServiceTypeClientTests {
List.of(ipV4Address) /* ipv4Address */, List.of(ipV4Address) /* ipv4Address */,
List.of(ipV6Address) /* ipv6Address */, List.of(ipV6Address) /* ipv6Address */,
5354 /* port */, 5354 /* port */,
Collections.singletonList(subtype) /* subTypes */, Collections.singletonList(SUBTYPE) /* subTypes */,
Collections.singletonMap("key", "value") /* attributes */, Collections.singletonMap("key", "value") /* attributes */,
socketKey); socketKey);
} }