Merge "Do not immediately send new queries on cache hit"
This commit is contained in:
@@ -178,6 +178,7 @@ public class MdnsServiceTypeClient {
|
|||||||
@NonNull MdnsSearchOptions searchOptions) {
|
@NonNull MdnsSearchOptions searchOptions) {
|
||||||
synchronized (lock) {
|
synchronized (lock) {
|
||||||
this.searchOptions = searchOptions;
|
this.searchOptions = searchOptions;
|
||||||
|
boolean hadReply = false;
|
||||||
if (listeners.put(listener, searchOptions) == null) {
|
if (listeners.put(listener, searchOptions) == null) {
|
||||||
for (MdnsResponse existingResponse : instanceNameToResponse.values()) {
|
for (MdnsResponse existingResponse : instanceNameToResponse.values()) {
|
||||||
if (!responseMatchesOptions(existingResponse, searchOptions)) continue;
|
if (!responseMatchesOptions(existingResponse, searchOptions)) continue;
|
||||||
@@ -186,6 +187,7 @@ public class MdnsServiceTypeClient {
|
|||||||
listener.onServiceNameDiscovered(info);
|
listener.onServiceNameDiscovered(info);
|
||||||
if (existingResponse.isComplete()) {
|
if (existingResponse.isComplete()) {
|
||||||
listener.onServiceFound(info);
|
listener.onServiceFound(info);
|
||||||
|
hadReply = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -195,14 +197,16 @@ public class MdnsServiceTypeClient {
|
|||||||
}
|
}
|
||||||
// Keep tracking the ScheduledFuture for the task so we can cancel it if caller is not
|
// Keep tracking the ScheduledFuture for the task so we can cancel it if caller is not
|
||||||
// interested anymore.
|
// interested anymore.
|
||||||
requestTaskFuture =
|
final QueryTaskConfig taskConfig = new QueryTaskConfig(
|
||||||
executor.submit(
|
searchOptions.getSubtypes(),
|
||||||
new QueryTask(
|
searchOptions.isPassiveMode(),
|
||||||
new QueryTaskConfig(
|
++currentSessionId,
|
||||||
searchOptions.getSubtypes(),
|
searchOptions.getNetwork());
|
||||||
searchOptions.isPassiveMode(),
|
if (hadReply) {
|
||||||
++currentSessionId,
|
requestTaskFuture = scheduleNextRunLocked(taskConfig);
|
||||||
searchOptions.getNetwork())));
|
} else {
|
||||||
|
requestTaskFuture = executor.submit(new QueryTask(taskConfig));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -590,11 +594,14 @@ public class MdnsServiceTypeClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
QueryTaskConfig config = this.config.getConfigForNextRun();
|
requestTaskFuture = scheduleNextRunLocked(this.config);
|
||||||
requestTaskFuture =
|
|
||||||
executor.schedule(
|
|
||||||
new QueryTask(config), config.timeToRunNextTaskInMs, MILLISECONDS);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
private Future<?> scheduleNextRunLocked(@NonNull QueryTaskConfig lastRunConfig) {
|
||||||
|
QueryTaskConfig config = lastRunConfig.getConfigForNextRun();
|
||||||
|
return executor.schedule(new QueryTask(config), config.timeToRunNextTaskInMs, MILLISECONDS);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -21,6 +21,7 @@ import static com.android.testutils.DevSdkIgnoreRuleKt.SC_V2;
|
|||||||
import static org.junit.Assert.assertArrayEquals;
|
import static org.junit.Assert.assertArrayEquals;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertNotNull;
|
||||||
import static org.junit.Assert.assertNull;
|
import static org.junit.Assert.assertNull;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
@@ -424,6 +425,34 @@ public class MdnsServiceTypeClientTests {
|
|||||||
assertNull(currentThreadExecutor.getAndClearLastScheduledRunnable());
|
assertNull(currentThreadExecutor.getAndClearLastScheduledRunnable());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testQueryScheduledWhenAnsweredFromCache() {
|
||||||
|
final MdnsSearchOptions searchOptions = MdnsSearchOptions.getDefaultOptions();
|
||||||
|
client.startSendAndReceive(mockListenerOne, searchOptions);
|
||||||
|
assertNotNull(currentThreadExecutor.getAndClearSubmittedRunnable());
|
||||||
|
|
||||||
|
client.processResponse(createResponse(
|
||||||
|
"service-instance-1", "192.0.2.123", 5353,
|
||||||
|
SERVICE_TYPE_LABELS,
|
||||||
|
Collections.emptyMap(), TEST_TTL), /* interfaceIndex= */ 20, mockNetwork);
|
||||||
|
|
||||||
|
verify(mockListenerOne).onServiceNameDiscovered(any());
|
||||||
|
verify(mockListenerOne).onServiceFound(any());
|
||||||
|
|
||||||
|
// File another identical query
|
||||||
|
client.startSendAndReceive(mockListenerTwo, searchOptions);
|
||||||
|
|
||||||
|
verify(mockListenerTwo).onServiceNameDiscovered(any());
|
||||||
|
verify(mockListenerTwo).onServiceFound(any());
|
||||||
|
|
||||||
|
// This time no query is submitted, only scheduled
|
||||||
|
assertNull(currentThreadExecutor.getAndClearSubmittedRunnable());
|
||||||
|
assertNotNull(currentThreadExecutor.getAndClearLastScheduledRunnable());
|
||||||
|
// This just skips the first query of the first burst
|
||||||
|
assertEquals(MdnsConfigs.timeBetweenQueriesInBurstMs(),
|
||||||
|
currentThreadExecutor.getAndClearLastScheduledDelayInMs());
|
||||||
|
}
|
||||||
|
|
||||||
private static void verifyServiceInfo(MdnsServiceInfo serviceInfo, String serviceName,
|
private static void verifyServiceInfo(MdnsServiceInfo serviceInfo, String serviceName,
|
||||||
String[] serviceType, List<String> ipv4Addresses, List<String> ipv6Addresses, int port,
|
String[] serviceType, List<String> ipv4Addresses, List<String> ipv6Addresses, int port,
|
||||||
List<String> subTypes, Map<String, String> attributes, int interfaceIndex,
|
List<String> subTypes, Map<String, String> attributes, int interfaceIndex,
|
||||||
|
|||||||
Reference in New Issue
Block a user