Fixed file descriptor leak in telephony unit tests
Jack Yu [Tue, 23 May 2017 18:00:27 +0000 (11:00 -0700)]
The handler thread in SimulatedCommands was not closed after
each unit test run. Fixed by adding a dispose method to quit
the thread.

Also use quit() instead of quitSafely() for all thread quiting
because we don't care if the messages were dispatched or not
after the test. This can fix some messages sending to dead
threads non-harmful exceptions in the output debug messages.

Test: Telephony unit tests
bug: 29579544
Change-Id: I112e46b0147a1950046a35cd9ac2498560c6ee1b

29 files changed:
src/java/com/android/internal/telephony/test/SimulatedCommands.java
tests/telephonytests/src/com/android/internal/telephony/CallManagerTest.java
tests/telephonytests/src/com/android/internal/telephony/DeviceStateMonitorTest.java
tests/telephonytests/src/com/android/internal/telephony/GsmCdmaCallTrackerTest.java
tests/telephonytests/src/com/android/internal/telephony/GsmCdmaPhoneTest.java
tests/telephonytests/src/com/android/internal/telephony/ImsSMSDispatcherTest.java
tests/telephonytests/src/com/android/internal/telephony/PhoneStateListenerTest.java
tests/telephonytests/src/com/android/internal/telephony/PhoneSwitcherTest.java
tests/telephonytests/src/com/android/internal/telephony/ServiceStateTrackerTest.java
tests/telephonytests/src/com/android/internal/telephony/SmsStorageMonitorTest.java
tests/telephonytests/src/com/android/internal/telephony/SubscriptionInfoUpdaterTest.java
tests/telephonytests/src/com/android/internal/telephony/TelephonyTest.java
tests/telephonytests/src/com/android/internal/telephony/cdma/CdmaInboundSmsHandlerTest.java
tests/telephonytests/src/com/android/internal/telephony/cdma/CdmaSmsDispatcherTest.java
tests/telephonytests/src/com/android/internal/telephony/dataconnection/DataConnectionTest.java
tests/telephonytests/src/com/android/internal/telephony/dataconnection/DcControllerTest.java
tests/telephonytests/src/com/android/internal/telephony/dataconnection/DcTrackerTest.java
tests/telephonytests/src/com/android/internal/telephony/dataconnection/TelephonyNetworkFactoryTest.java
tests/telephonytests/src/com/android/internal/telephony/gsm/GsmCellBroadcastHandlerTest.java
tests/telephonytests/src/com/android/internal/telephony/gsm/GsmInboundSmsHandlerTest.java
tests/telephonytests/src/com/android/internal/telephony/gsm/GsmSmsDispatcherTest.java
tests/telephonytests/src/com/android/internal/telephony/imsphone/ImsPhoneCallTrackerTest.java
tests/telephonytests/src/com/android/internal/telephony/imsphone/ImsPhoneFactoryTest.java
tests/telephonytests/src/com/android/internal/telephony/imsphone/ImsPhoneTest.java
tests/telephonytests/src/com/android/internal/telephony/mocks/ConnectivityServiceMock.java
tests/telephonytests/src/com/android/internal/telephony/uicc/IccPhoneBookInterfaceManagerTest.java
tests/telephonytests/src/com/android/internal/telephony/uicc/UiccCardApplicationTest.java
tests/telephonytests/src/com/android/internal/telephony/uicc/UiccCardTest.java
tests/telephonytests/src/com/android/internal/telephony/uicc/UiccControllerTest.java

index 920b911..cd24760 100644 (file)
@@ -150,6 +150,12 @@ public class SimulatedCommands extends BaseCommands
         mPin2Code = DEFAULT_SIM_PIN2_CODE;
     }
 
+    public void dispose() {
+        if (mHandlerThread != null) {
+            mHandlerThread.quit();
+        }
+    }
+
     private void log(String str) {
         Rlog.d(LOG_TAG, str);
     }
index a255f26..8231d06 100644 (file)
@@ -122,7 +122,7 @@ public class CallManagerTest extends TelephonyTest {
     @After
     public void tearDown() throws Exception {
         CallManager.getInstance().unregisterPhone(mPhone);
-        mCallManagerHandlerThread.quitSafely();
+        mCallManagerHandlerThread.quit();
         super.tearDown();
     }
 
index 26a243c..591b111 100644 (file)
@@ -71,7 +71,7 @@ public class DeviceStateMonitorTest extends TelephonyTest {
     @After
     public void tearDown() throws Exception {
         mDeviceStateMonitor = null;
-        mDeviceStateMonitorTestHandler.quitSafely();
+        mDeviceStateMonitorTestHandler.quit();
         super.tearDown();
     }
 
index 3f12594..fc86602 100644 (file)
@@ -89,7 +89,7 @@ public class GsmCdmaCallTrackerTest extends TelephonyTest {
     @After
     public void tearDown() throws Exception {
         mCTUT = null;
-        mGsmCdmaCTHandlerThread.quitSafely();
+        mGsmCdmaCTHandlerThread.quit();
         super.tearDown();
     }
 
index c9fcd26..3a80fe0 100644 (file)
@@ -134,7 +134,7 @@ public class GsmCdmaPhoneTest extends TelephonyTest {
     public void tearDown() throws Exception {
         mPhoneUT.removeCallbacksAndMessages(null);
         mPhoneUT = null;
-        mGsmCdmaPhoneTestHandler.quitSafely();
+        mGsmCdmaPhoneTestHandler.quit();
         super.tearDown();
     }
 
index 1a66bb5..105e0f8 100644 (file)
@@ -97,7 +97,7 @@ public class ImsSMSDispatcherTest extends TelephonyTest {
     @After
     public void tearDown() throws Exception {
         mImsSmsDispatcher = null;
-        mImsSmsDispatcherTestHandler.quitSafely();
+        mImsSmsDispatcherTestHandler.quit();
         super.tearDown();
     }
 
index 4a17199..fdda80f 100644 (file)
@@ -60,7 +60,7 @@ public class PhoneStateListenerTest extends TelephonyTest {
 
     @After
     public void tearDown() throws Exception {
-        mPhoneStateListenerHandler.quitSafely();
+        mPhoneStateListenerHandler.quit();
         super.tearDown();
     }
 
index e68262f..b5ec320 100644 (file)
@@ -388,6 +388,12 @@ public class PhoneSwitcherTest extends AndroidTestCase {
 //        }
 //        if (commandsInterfaces[0].isDataAllowed()) fail("data allowed");
 //        if (commandsInterfaces[1].isDataAllowed()) fail("data allowed");
+
+        for (int i = 0; i < numPhones; i++) {
+            commandsInterfaces[i].dispose();
+        }
+
+        connectivityServiceMock.die();
         testHandler.die();
         handlerThread.quit();
     }
@@ -468,6 +474,11 @@ public class PhoneSwitcherTest extends AndroidTestCase {
         if (commandsInterfaces[0].isDataAllowed()) fail("data allowed");
         if (commandsInterfaces[1].isDataAllowed() == false) fail("data not allowed");
 
+        for (int i = 0; i < numPhones; i++) {
+            commandsInterfaces[i].dispose();
+        }
+
+        connectivityServiceMock.die();
         testHandler.die();
         handlerThread.quit();
     }
@@ -537,6 +548,11 @@ public class PhoneSwitcherTest extends AndroidTestCase {
         if (commandsInterfaces[0].isDataAllowed() == false) fail("data not allowed");
         if (commandsInterfaces[1].isDataAllowed()) fail("data allowed");
 
+        for (int i = 0; i < numPhones; i++) {
+            commandsInterfaces[i].dispose();
+        }
+
+        connectivityServiceMock.die();
         testHandler.die();
         handlerThread.quit();
     }
index c9fba0c..5c5c8d4 100644 (file)
@@ -146,7 +146,7 @@ public class ServiceStateTrackerTest extends TelephonyTest {
     @After
     public void tearDown() throws Exception {
         sst = null;
-        mSSTTestHandler.quitSafely();
+        mSSTTestHandler.quit();
         super.tearDown();
     }
 
index 7c8c15c..cbc56ae 100644 (file)
@@ -67,7 +67,7 @@ public class SmsStorageMonitorTest extends TelephonyTest {
     @After
     public void tearDown() throws Exception {
         mSmsStorageMonitor = null;
-        mSmsStorageMonitorTestHandler.quitSafely();
+        mSmsStorageMonitorTestHandler.quit();
         super.tearDown();
     }
 
index f49aa33..53304ae 100644 (file)
@@ -131,7 +131,7 @@ public class SubscriptionInfoUpdaterTest extends TelephonyTest {
 
     @After
     public void tearDown() throws Exception {
-        mSubscriptionInfoUpdaterHandlerThread.quitSafely();
+        mSubscriptionInfoUpdaterHandlerThread.quit();
         super.tearDown();
     }
 
index b38659c..94e5905 100644 (file)
@@ -446,6 +446,8 @@ public abstract class TelephonyTest {
 
     protected void tearDown() throws Exception {
 
+        mSimulatedCommands.dispose();
+
         SharedPreferences sharedPreferences = mContext.getSharedPreferences((String) null, 0);
         sharedPreferences.edit().clear().commit();
 
index 82169d4..a26741f 100644 (file)
@@ -159,7 +159,7 @@ public class CdmaInboundSmsHandlerTest extends TelephonyTest {
         assertFalse(mCdmaInboundSmsHandler.getWakeLock().isHeld());
         mCdmaInboundSmsHandler = null;
         mContentProvider.shutdown();
-        mCdmaInboundSmsHandlerTestHandler.quitSafely();
+        mCdmaInboundSmsHandlerTestHandler.quit();
         super.tearDown();
     }
 
index e3ba4ff..a308762 100644 (file)
@@ -72,7 +72,7 @@ public class CdmaSmsDispatcherTest extends TelephonyTest {
     @After
     public void tearDown() throws Exception {
         mCdmaSmsDispatcher = null;
-        mCdmaSmsDispatcherTestHandler.quitSafely();
+        mCdmaSmsDispatcherTestHandler.quit();
         super.tearDown();
     }
 
index b56ef1c..1e34910 100644 (file)
@@ -164,7 +164,7 @@ public class DataConnectionTest extends TelephonyTest {
         logd("tearDown");
         mDc = null;
         mDcc = null;
-        mDataConnectionTestHandler.quitSafely();
+        mDataConnectionTestHandler.quit();
         super.tearDown();
     }
 
index f2661f6..d209639 100644 (file)
@@ -114,7 +114,7 @@ public class DcControllerTest extends TelephonyTest {
 
     @After
     public void tearDown() throws Exception {
-        mDcControllerTestHandler.quitSafely();
+        mDcControllerTestHandler.quit();
         super.tearDown();
     }
 
index 99c09c5..56edb7f 100644 (file)
@@ -346,7 +346,7 @@ public class DcTrackerTest extends TelephonyTest {
         logd("DcTrackerTest -tearDown");
         mDct.removeCallbacksAndMessages(null);
         mDct = null;
-        mDcTrackerTestHandler.quitSafely();
+        mDcTrackerTestHandler.quit();
         super.tearDown();
     }
 
index e377600..a60b502 100644 (file)
@@ -98,6 +98,7 @@ public class TelephonyNetworkFactoryTest extends AndroidTestCase {
         }
 
         void die() {
+            connectivityServiceMock.die();
             looper.quit();
             handlerThread.quit();
         }
@@ -139,7 +140,7 @@ public class TelephonyNetworkFactoryTest extends AndroidTestCase {
 
         TestSetup ts = new TestSetup(numberOfPhones);
 
-        TelephonyNetworkFactory tnf = makeTnf(phoneId, ts);
+        makeTnf(phoneId, ts);
 
         ts.subscriptionControllerMock.setDefaultDataSubId(subId);
         ts.subscriptionControllerMock.setSlotSubId(phoneId, subId);
@@ -234,7 +235,7 @@ public class TelephonyNetworkFactoryTest extends AndroidTestCase {
 
         TestSetup ts = new TestSetup(numberOfPhones);
 
-        TelephonyNetworkFactory tnf = makeTnf(phoneId, ts);
+        makeTnf(phoneId, ts);
 
         ts.subscriptionControllerMock.setDefaultDataSubId(subId);
         ts.subscriptionControllerMock.setSlotSubId(phoneId, subId);
@@ -272,7 +273,7 @@ public class TelephonyNetworkFactoryTest extends AndroidTestCase {
             fail("test 5, LiveRequests != 0");
         }
 
-        NetworkRequest subSpecificMms = makeSubSpecificMmsRequest(ts, subId);
+        makeSubSpecificMmsRequest(ts, subId);
         waitABit();
         if (ts.dcTrackerMock.getNumberOfLiveRequests() != 1) {
             fail("test 6,  LiveRequests != 1");
@@ -285,7 +286,7 @@ public class TelephonyNetworkFactoryTest extends AndroidTestCase {
             fail("test 7,  LiveRequests != 0");
         }
 
-        NetworkRequest subSpecificDefault = makeSubSpecificDefaultRequest(ts, subId);
+        makeSubSpecificDefaultRequest(ts, subId);
         waitABit();
         if (ts.dcTrackerMock.getNumberOfLiveRequests() != 0) {
             fail("test 8, LiveRequests != 0");
index e2e42cf..b117eb1 100644 (file)
@@ -95,7 +95,7 @@ public class GsmCellBroadcastHandlerTest extends TelephonyTest {
     @After
     public void tearDown() throws Exception {
         mGsmCellBroadcastHandler = null;
-        mGsmCellBroadcastHandlerTestHandler.quitSafely();
+        mGsmCellBroadcastHandlerTestHandler.quit();
         super.tearDown();
     }
 
index 340ff35..92f2bf6 100644 (file)
@@ -187,7 +187,7 @@ public class GsmInboundSmsHandlerTest extends TelephonyTest {
         assertFalse(mGsmInboundSmsHandler.getWakeLock().isHeld());
         mGsmInboundSmsHandler = null;
         mContentProvider.shutdown();
-        mGsmInboundSmsHandlerTestHandler.quitSafely();
+        mGsmInboundSmsHandlerTestHandler.quit();
         super.tearDown();
     }
 
index fc06962..6da7ca9 100644 (file)
@@ -91,7 +91,7 @@ public class GsmSmsDispatcherTest extends TelephonyTest {
     @After
     public void tearDown() throws Exception {
         mGsmSmsDispatcher = null;
-        mGsmSmsDispatcherTestHandler.quitSafely();
+        mGsmSmsDispatcherTestHandler.quit();
         super.tearDown();
     }
 
index d2238d7..db1dcf1 100644 (file)
@@ -207,7 +207,7 @@ public class ImsPhoneCallTrackerTest extends TelephonyTest {
     @After
     public void tearDown() throws Exception {
         mCTUT = null;
-        mImsCTHandlerThread.quitSafely();
+        mImsCTHandlerThread.quit();
         super.tearDown();
     }
 
index 8a7c53a..c31541e 100644 (file)
@@ -60,7 +60,7 @@ public class ImsPhoneFactoryTest extends TelephonyTest {
 
     @After
     public void tearDown() throws Exception {
-        mImsPhoneFactoryHandler.quitSafely();
+        mImsPhoneFactoryHandler.quit();
         super.tearDown();
     }
 
index bd0f149..e820165 100644 (file)
@@ -142,7 +142,7 @@ public class ImsPhoneTest extends TelephonyTest {
     @After
     public void tearDown() throws Exception {
         mImsPhoneUT = null;
-        mImsPhoneTestHandler.quitSafely();
+        mImsPhoneTestHandler.quit();
         super.tearDown();
     }
 
index 5b35e70..d7549a4 100644 (file)
@@ -127,6 +127,9 @@ public class ConnectivityServiceMock extends IConnectivityManager.Stub
 
     public void die() {
         // clean up threads/handlers
+        if (mHandlerThread != null) {
+            mHandlerThread.quit();
+        }
     }
 
     private class InternalHandler extends Handler {
index c99bd75..9510619 100644 (file)
@@ -89,7 +89,7 @@ public class IccPhoneBookInterfaceManagerTest extends TelephonyTest {
 
     @After
     public void tearDown() throws Exception {
-        mIccPhoneBookInterfaceManagerHandler.quitSafely();
+        mIccPhoneBookInterfaceManagerHandler.quit();
         super.tearDown();
     }
     @Test
index 1a5daeb..60f5f6f 100644 (file)
@@ -110,7 +110,7 @@ public class UiccCardApplicationTest extends TelephonyTest {
 
     @After
     public void tearDown() throws Exception {
-        mTestHandlerThread.quitSafely();
+        mTestHandlerThread.quit();
         super.tearDown();
     }
 
index 9a24a86..e090c25 100644 (file)
@@ -134,7 +134,7 @@ public class UiccCardTest extends TelephonyTest {
 
     @After
     public void tearDown() throws Exception {
-        mTestHandlerThread.quitSafely();
+        mTestHandlerThread.quit();
         super.tearDown();
     }
 
index 12c2690..e62700a 100644 (file)
@@ -96,7 +96,7 @@ public class UiccControllerTest extends TelephonyTest {
 
     @After
     public void tearDown() throws Exception {
-        mUiccControllerHandlerThread.quitSafely();
+        mUiccControllerHandlerThread.quit();
         super.tearDown();
     }