Fix the memory leak in nsd tests
The NsdManager constructor creates a new thread, which is not intended to be closed. However, the test in NsdMangerTest and NsdServiceTest create a new NsdManager instance, resulting in a large number of threads running during the tests. This can lead to an out-of-memory error for the tests. To resolve this issue, use the common singleton thread ConnectivityThread to handle tasks from the various instances. Fix: 308544734 Test: atest ConnectivityCoverageTests --test-filter Nsd* Change-Id: I1908a2bb1bd20da8e0498353bca26f7e89e16626
This commit is contained in:
@@ -34,10 +34,10 @@ import android.app.compat.CompatChanges;
|
||||
import android.content.Context;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.ConnectivityManager.NetworkCallback;
|
||||
import android.net.ConnectivityThread;
|
||||
import android.net.Network;
|
||||
import android.net.NetworkRequest;
|
||||
import android.os.Handler;
|
||||
import android.os.HandlerThread;
|
||||
import android.os.Looper;
|
||||
import android.os.Message;
|
||||
import android.os.RemoteException;
|
||||
@@ -632,10 +632,9 @@ public final class NsdManager {
|
||||
*/
|
||||
public NsdManager(Context context, INsdManager service) {
|
||||
mContext = context;
|
||||
|
||||
HandlerThread t = new HandlerThread("NsdManager");
|
||||
t.start();
|
||||
mHandler = new ServiceHandler(t.getLooper());
|
||||
// Use a common singleton thread ConnectivityThread to be shared among all nsd tasks.
|
||||
// Instead of launching separate threads to handle tasks from the various instances.
|
||||
mHandler = new ServiceHandler(ConnectivityThread.getInstanceLooper());
|
||||
|
||||
try {
|
||||
mService = service.connect(new NsdCallbackImpl(mHandler), CompatChanges.isChangeEnabled(
|
||||
|
||||
@@ -51,6 +51,7 @@ import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
@DevSdkIgnoreRunner.MonitorThreadLeak
|
||||
@RunWith(DevSdkIgnoreRunner.class)
|
||||
@SmallTest
|
||||
@DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.S_V2)
|
||||
|
||||
@@ -145,6 +145,7 @@ import java.util.Queue;
|
||||
// TODOs:
|
||||
// - test client can send requests and receive replies
|
||||
// - test NSD_ON ENABLE/DISABLED listening
|
||||
@DevSdkIgnoreRunner.MonitorThreadLeak
|
||||
@RunWith(DevSdkIgnoreRunner.class)
|
||||
@SmallTest
|
||||
@DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.S_V2)
|
||||
|
||||
Reference in New Issue
Block a user