Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 601573b7 authored by fionaxu's avatar fionaxu
Browse files

fix TelephonyNetworkFactory unit test failure

there might be a race condition: the main testing thread executes
testcase (after setup) before the looper of the handlerThread is ready,
or before the message has been successfully delivered to the handerThread.
As a result, the main thread is creating a TelephonyNetworkFactory
object with a null DCT object.
the fix is create DcT object when the looper is ready and wait till DCT is
created successfully.

Bug: 29579544
Test: runtest --path
Change-Id: I9d00c78fc3290d2a0ec2e493c5a13879598005af
parent 5f84fc93
Loading
Loading
Loading
Loading
+19 −9
Original line number Diff line number Diff line
@@ -22,7 +22,6 @@ import android.net.NetworkCapabilities;
import android.net.NetworkRequest;
import android.net.StringNetworkSpecifier;
import android.os.Binder;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
@@ -65,18 +64,29 @@ public class TelephonyNetworkFactoryTest extends AndroidTestCase {
        final Looper looper;
        DcTrackerMock dcTrackerMock;
        final Context contextMock;
        private Object mLock = new Object();
        private static final int MAX_INIT_WAIT_MS = 30000; // 30 seconds

        TestSetup(int numPhones) {
            handlerThread = new HandlerThread("TelephonyNetworkFactoryTest");
            handlerThread.start();
            looper = handlerThread.getLooper();

            Handler myHandler = new Handler(looper) {
                public void handleMessage(Message msg) {
            handlerThread = new HandlerThread("TelephonyNetworkFactoryTest") {
                @Override
                public void onLooperPrepared() {
                    synchronized (mLock) {
                        if (dcTrackerMock == null) dcTrackerMock = new DcTrackerMock();
                        mLock.notifyAll();
                    }
                }
            };
            myHandler.obtainMessage(0).sendToTarget();
            handlerThread.start();
            // wait until dct created
            synchronized (mLock) {
                try {
                    mLock.wait(MAX_INIT_WAIT_MS);
                } catch (InterruptedException ie) {
                }
                if (dcTrackerMock == null) fail("failed to initialize dct");
            }
            looper = handlerThread.getLooper();

            final ContextFixture contextFixture = new ContextFixture();
            String[] networkConfigString = getContext().getResources().getStringArray(