Merge "Ensure the ServiceTypeClient Executor threads can be shutdown" into main

This commit is contained in:
Yuyang Huang
2023-07-25 03:05:08 +00:00
committed by Gerrit Code Review
4 changed files with 39 additions and 0 deletions

View File

@@ -16,6 +16,7 @@
package com.android.server.connectivity.mdns;
import android.annotation.NonNull;
import android.util.ArraySet;
import java.util.Set;
@@ -47,5 +48,17 @@ public class ExecutorProvider {
}
executor.shutdownNow();
}
serviceTypeClientSchedulerExecutors.clear();
}
/**
* Shutdown one executor service and remove the executor service from the set.
* @param executorService the executorService to be shutdown
*/
public void shutdownExecutorService(@NonNull ScheduledExecutorService executorService) {
if (!executorService.isShutdown()) {
executorService.shutdownNow();
}
serviceTypeClientSchedulerExecutors.remove(executorService);
}
}

View File

@@ -204,6 +204,7 @@ public class MdnsDiscoveryManager implements MdnsSocketClientBase.Callback {
if (serviceTypeClient == null) return;
// Notify all listeners that all services are removed from this socket.
serviceTypeClient.notifySocketDestroyed();
executorProvider.shutdownExecutorService(serviceTypeClient.getExecutor());
perSocketServiceTypeClients.remove(serviceTypeClient);
}
});
@@ -238,6 +239,7 @@ public class MdnsDiscoveryManager implements MdnsSocketClientBase.Callback {
if (serviceTypeClient.stopSendAndReceive(listener)) {
// No listener is registered for the service type anymore, remove it from the list
// of the service type clients.
executorProvider.shutdownExecutorService(serviceTypeClient.getExecutor());
perSocketServiceTypeClients.remove(serviceTypeClient);
}
}

View File

@@ -330,6 +330,13 @@ public class MdnsServiceTypeClient {
}
}
/**
* Get the executor service.
*/
public ScheduledExecutorService getExecutor() {
return executor;
}
private void removeScheduledTask() {
dependencies.removeMessages(handler, EVENT_START_QUERYTASK);
sharedLog.log("Remove EVENT_START_QUERYTASK"

View File

@@ -53,6 +53,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ScheduledExecutorService;
/** Tests for {@link MdnsDiscoveryManager}. */
@RunWith(DevSdkIgnoreRunner.class)
@@ -80,6 +81,7 @@ public class MdnsDiscoveryManagerTests {
private static final Pair<String, SocketKey> PER_SOCKET_SERVICE_TYPE_2_NETWORK_2 =
Pair.create(SERVICE_TYPE_2, SOCKET_KEY_NETWORK_2);
@Mock private ExecutorProvider executorProvider;
@Mock private ScheduledExecutorService mockExecutorService;
@Mock private MdnsSocketClientBase socketClient;
@Mock private MdnsServiceTypeClient mockServiceTypeClientType1NullNetwork;
@Mock private MdnsServiceTypeClient mockServiceTypeClientType1Network1;
@@ -128,6 +130,7 @@ public class MdnsDiscoveryManagerTests {
return null;
}
};
doReturn(mockExecutorService).when(mockServiceTypeClientType1NullNetwork).getExecutor();
}
@After
@@ -165,10 +168,24 @@ public class MdnsDiscoveryManagerTests {
when(mockServiceTypeClientType1NullNetwork.stopSendAndReceive(mockListenerOne))
.thenReturn(true);
runOnHandler(() -> discoveryManager.unregisterListener(SERVICE_TYPE_1, mockListenerOne));
verify(executorProvider).shutdownExecutorService(mockExecutorService);
verify(mockServiceTypeClientType1NullNetwork).stopSendAndReceive(mockListenerOne);
verify(socketClient).stopDiscovery();
}
@Test
public void onSocketDestroy_shutdownExecutorService() throws IOException {
final MdnsSearchOptions options =
MdnsSearchOptions.newBuilder().setNetwork(null /* network */).build();
final SocketCreationCallback callback = expectSocketCreationCallback(
SERVICE_TYPE_1, mockListenerOne, options);
runOnHandler(() -> callback.onSocketCreated(SOCKET_KEY_NULL_NETWORK));
verify(mockServiceTypeClientType1NullNetwork).startSendAndReceive(mockListenerOne, options);
runOnHandler(() -> callback.onSocketDestroyed(SOCKET_KEY_NULL_NETWORK));
verify(executorProvider).shutdownExecutorService(mockExecutorService);
}
@Test
public void registerMultipleListeners() throws IOException {
final MdnsSearchOptions options =