Stop the created handler threads on the tests

The created handler thread will stay there for a while until
all the tests are finished. This is considered as a leakage
which will take the resource from the devices. In low end
devices, this may possibly impact the performance. The created
threads should be closed explicitly.

Test: atest ConnectivityCoverageTests ConntrackSocketTest
Change-Id: Ic1beb2a210e7c8c80c66fc9e0727c47599150672
This commit is contained in:
Paul Hu
2023-10-11 17:24:45 +08:00
parent f24beefe55
commit ebbbf8f470
3 changed files with 33 additions and 4 deletions

View File

@@ -44,6 +44,7 @@ import com.android.net.module.util.netlink.NetlinkMessage;
import com.android.net.module.util.netlink.NetlinkUtils; import com.android.net.module.util.netlink.NetlinkUtils;
import com.android.net.module.util.netlink.StructNlMsgHdr; import com.android.net.module.util.netlink.StructNlMsgHdr;
import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@@ -84,6 +85,14 @@ public class ConntrackSocketTest {
mOffloadHw = new OffloadHardwareInterface(mHandler, mLog, mDeps); mOffloadHw = new OffloadHardwareInterface(mHandler, mLog, mDeps);
} }
@After
public void tearDown() throws Exception {
if (mHandlerThread != null) {
mHandlerThread.quitSafely();
mHandlerThread.join();
}
}
void findConnectionOrThrow(FileDescriptor fd, InetSocketAddress local, InetSocketAddress remote) void findConnectionOrThrow(FileDescriptor fd, InetSocketAddress local, InetSocketAddress remote)
throws Exception { throws Exception {
Log.d(TAG, "Looking for socket " + local + " -> " + remote); Log.d(TAG, "Looking for socket " + local + " -> " + remote);

View File

@@ -42,6 +42,7 @@ import com.android.testutils.DevSdkIgnoreRule;
import com.android.testutils.DevSdkIgnoreRunner; import com.android.testutils.DevSdkIgnoreRunner;
import com.android.testutils.HandlerUtils; import com.android.testutils.HandlerUtils;
import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@@ -69,6 +70,7 @@ public class MdnsMultinetworkSocketClientTest {
@Mock private SocketCreationCallback mSocketCreationCallback; @Mock private SocketCreationCallback mSocketCreationCallback;
@Mock private SharedLog mSharedLog; @Mock private SharedLog mSharedLog;
private MdnsMultinetworkSocketClient mSocketClient; private MdnsMultinetworkSocketClient mSocketClient;
private HandlerThread mHandlerThread;
private Handler mHandler; private Handler mHandler;
private SocketKey mSocketKey; private SocketKey mSocketKey;
@@ -76,14 +78,23 @@ public class MdnsMultinetworkSocketClientTest {
public void setUp() throws SocketException { public void setUp() throws SocketException {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
final HandlerThread thread = new HandlerThread("MdnsMultinetworkSocketClientTest"); mHandlerThread = new HandlerThread("MdnsMultinetworkSocketClientTest");
thread.start(); mHandlerThread.start();
mHandler = new Handler(thread.getLooper()); mHandler = new Handler(mHandlerThread.getLooper());
mSocketKey = new SocketKey(1000 /* interfaceIndex */); mSocketKey = new SocketKey(1000 /* interfaceIndex */);
mSocketClient = new MdnsMultinetworkSocketClient(thread.getLooper(), mProvider, mSharedLog); mSocketClient = new MdnsMultinetworkSocketClient(
mHandlerThread.getLooper(), mProvider, mSharedLog);
mHandler.post(() -> mSocketClient.setCallback(mCallback)); mHandler.post(() -> mSocketClient.setCallback(mCallback));
} }
@After
public void tearDown() throws Exception {
if (mHandlerThread != null) {
mHandlerThread.quitSafely();
mHandlerThread.join();
}
}
private SocketCallback expectSocketCallback() { private SocketCallback expectSocketCallback() {
return expectSocketCallback(mListener, mNetwork); return expectSocketCallback(mListener, mNetwork);
} }

View File

@@ -59,6 +59,7 @@ import com.android.testutils.DevSdkIgnoreRule;
import com.android.testutils.DevSdkIgnoreRunner; import com.android.testutils.DevSdkIgnoreRunner;
import com.android.testutils.HandlerUtils; import com.android.testutils.HandlerUtils;
import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@@ -139,6 +140,14 @@ public class NetworkStatsObserversTest {
mUsageCallback = new TestableUsageCallback(mUsageCallbackBinder); mUsageCallback = new TestableUsageCallback(mUsageCallbackBinder);
} }
@After
public void tearDown() throws Exception {
if (mObserverHandlerThread != null) {
mObserverHandlerThread.quitSafely();
mObserverHandlerThread.join();
}
}
@Test @Test
public void testRegister_thresholdTooLow_setsDefaultThreshold() throws Exception { public void testRegister_thresholdTooLow_setsDefaultThreshold() throws Exception {
final long thresholdTooLowBytes = 1L; final long thresholdTooLowBytes = 1L;