Merge "Report resolution stop" into main

This commit is contained in:
Treehugger Robot
2023-08-18 09:30:26 +00:00
committed by Gerrit Code Review
4 changed files with 56 additions and 6 deletions

View File

@@ -206,4 +206,19 @@ public class NetworkNsdReportedMetrics {
builder.setEventDurationMillisec(durationMs);
mDependencies.statsWrite(builder.build());
}
/**
* Report service resolution stop metric data.
*
* @param transactionId The transaction id of service resolution.
* @param durationMs The duration before stop resolving the service.
*/
public void reportServiceResolutionStop(int transactionId, long durationMs) {
final Builder builder = makeReportedBuilder();
builder.setTransactionId(transactionId);
builder.setType(NsdEventType.NET_RESOLVE);
builder.setQueryResult(MdnsQueryResult.MQR_SERVICE_RESOLUTION_STOP);
builder.setEventDurationMillisec(durationMs);
mDependencies.statsWrite(builder.build());
}
}

View File

@@ -1062,12 +1062,12 @@ public class NsdService extends INsdManager.Stub {
if (request instanceof DiscoveryManagerRequest) {
stopDiscoveryManagerRequest(
request, clientRequestId, transactionId, clientInfo);
clientInfo.onStopResolutionSucceeded(clientRequestId);
clientInfo.onStopResolutionSucceeded(clientRequestId, request);
clientInfo.log("Unregister the ResolutionListener " + transactionId);
} else {
removeRequestMap(clientRequestId, transactionId, clientInfo);
if (stopResolveService(transactionId)) {
clientInfo.onStopResolutionSucceeded(clientRequestId);
clientInfo.onStopResolutionSucceeded(clientRequestId, request);
} else {
clientInfo.onStopResolutionFailed(
clientRequestId, NsdManager.FAILURE_OPERATION_NOT_RUNNING);
@@ -2393,6 +2393,9 @@ public class NsdService extends INsdManager.Stub {
request.getFoundServiceCount(),
request.getLostServiceCount(),
request.getServicesCount());
} else if (listener instanceof ResolutionListener) {
mMetrics.reportServiceResolutionStop(transactionId,
request.calculateRequestDurationMs(mClock.elapsedRealtime()));
}
continue;
}
@@ -2419,6 +2422,8 @@ public class NsdService extends INsdManager.Stub {
break;
case NsdManager.RESOLVE_SERVICE:
stopResolveService(transactionId);
mMetrics.reportServiceResolutionStop(transactionId,
request.calculateRequestDurationMs(mClock.elapsedRealtime()));
break;
case NsdManager.REGISTER_SERVICE:
unregisterService(transactionId);
@@ -2605,7 +2610,10 @@ public class NsdService extends INsdManager.Stub {
}
}
void onStopResolutionSucceeded(int listenerKey) {
void onStopResolutionSucceeded(int listenerKey, ClientRequest request) {
mMetrics.reportServiceResolutionStop(
request.mTransactionId,
request.calculateRequestDurationMs(mClock.elapsedRealtime()));
try {
mCb.onStopResolutionSucceeded(listenerKey);
} catch (RemoteException e) {

View File

@@ -201,4 +201,24 @@ class NetworkNsdReportedMetricsTest {
assertEquals(durationMs, it.eventDurationMillisec)
}
}
@Test
fun testReportServiceResolutionStop() {
val clientId = 99
val transactionId = 100
val durationMs = 10L
val metrics = NetworkNsdReportedMetrics(true /* isLegacy */, clientId, deps)
metrics.reportServiceResolutionStop(transactionId, durationMs)
val eventCaptor = ArgumentCaptor.forClass(NetworkNsdReported::class.java)
verify(deps).statsWrite(eventCaptor.capture())
eventCaptor.value.let {
assertTrue(it.isLegacy)
assertEquals(clientId, it.clientId)
assertEquals(transactionId, it.transactionId)
assertEquals(NsdEventType.NET_RESOLVE, it.type)
assertEquals(MdnsQueryResult.MQR_SERVICE_RESOLUTION_STOP, it.queryResult)
assertEquals(durationMs, it.eventDurationMillisec)
}
}
}

View File

@@ -717,6 +717,7 @@ public class NsdServiceTest {
eq("local.") /* domain */, eq(IFACE_IDX_ANY));
final int resolveId = resolvIdCaptor.getValue();
doReturn(TEST_TIME_MS + 10L).when(mClock).elapsedRealtime();
client.stopServiceResolution(resolveListener);
waitForIdle();
@@ -724,6 +725,7 @@ public class NsdServiceTest {
verify(resolveListener, timeout(TIMEOUT_MS)).onResolutionStopped(argThat(ns ->
request.getServiceName().equals(ns.getServiceName())
&& request.getServiceType().equals(ns.getServiceType())));
verify(mMetrics).reportServiceResolutionStop(resolveId, 10L /* durationMs */);
}
@Test
@@ -786,6 +788,7 @@ public class NsdServiceTest {
eq(IFACE_IDX_ANY));
final int getAddrId = getAddrIdCaptor.getValue();
doReturn(TEST_TIME_MS + 10L).when(mClock).elapsedRealtime();
client.stopServiceResolution(resolveListener);
waitForIdle();
@@ -793,6 +796,7 @@ public class NsdServiceTest {
verify(resolveListener, timeout(TIMEOUT_MS)).onResolutionStopped(argThat(ns ->
request.getServiceName().equals(ns.getServiceName())
&& request.getServiceType().equals(ns.getServiceType())));
verify(mMetrics).reportServiceResolutionStop(getAddrId, 10L /* durationMs */);
}
private void verifyUpdatedServiceInfo(NsdServiceInfo info, String serviceName,
@@ -1355,8 +1359,8 @@ public class NsdServiceTest {
final Network network = new Network(999);
final String serviceType = "_nsd._service._tcp";
final String constructedServiceType = "_service._tcp.local";
final ArgumentCaptor<MdnsServiceBrowserListener> listenerCaptor =
ArgumentCaptor.forClass(MdnsServiceBrowserListener.class);
final ArgumentCaptor<MdnsListener> listenerCaptor =
ArgumentCaptor.forClass(MdnsListener.class);
final NsdServiceInfo request = new NsdServiceInfo(SERVICE_NAME, serviceType);
request.setNetwork(network);
client.resolveService(request, resolveListener);
@@ -1371,16 +1375,19 @@ public class NsdServiceTest {
// Subtypes are not used for resolution, only for discovery
assertEquals(Collections.emptyList(), optionsCaptor.getValue().getSubtypes());
doReturn(TEST_TIME_MS + 10L).when(mClock).elapsedRealtime();
client.stopServiceResolution(resolveListener);
waitForIdle();
// Verify the listener has been unregistered.
final MdnsListener listener = listenerCaptor.getValue();
verify(mDiscoveryManager, timeout(TIMEOUT_MS))
.unregisterListener(eq(constructedServiceType), eq(listenerCaptor.getValue()));
.unregisterListener(eq(constructedServiceType), eq(listener));
verify(resolveListener, timeout(TIMEOUT_MS)).onResolutionStopped(argThat(ns ->
request.getServiceName().equals(ns.getServiceName())
&& request.getServiceType().equals(ns.getServiceType())));
verify(mSocketProvider, timeout(CLEANUP_DELAY_MS + TIMEOUT_MS)).requestStopWhenInactive();
verify(mMetrics).reportServiceResolutionStop(listener.mTransactionId, 10L /* durationMs */);
}
@Test