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

Commit 6291dd79 authored by Jacky Wang's avatar Jacky Wang
Browse files

Fix flaky LifecycleAwareAsyncTaskTest

Fix: 385137513
Flag: TEST_ONLY
Test: atest SettingsRoboTests:LifecycleAwareAsyncTaskTest --rerun-until-failure 500
Change-Id: I269d2fa1c63e0b805d7d1b0710919f60c2096ec0
parent 6d9de784
Loading
Loading
Loading
Loading
+10 −6
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.settings.fuelgauge.batteryusage

import android.os.AsyncTask
import androidx.annotation.CallSuper
import androidx.annotation.OpenForTesting
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
@@ -50,7 +51,11 @@ abstract class LifecycleAwareAsyncTask<Result>(private val lifecycle: Lifecycle?
    fun start() {
        execute() // expects main thread
        val lifecycle = lifecycle ?: return
        mainExecutor.execute {
        mainExecutor.execute { maybeAddObserver(lifecycle) }
    }

    @OpenForTesting
    open fun maybeAddObserver(lifecycle: Lifecycle) {
        // Status is updated to FINISHED if onPoseExecute happened before. And task is cancelled
        // if lifecycle is stopped.
        if (status == Status.RUNNING && !isCancelled) {
@@ -58,4 +63,3 @@ abstract class LifecycleAwareAsyncTask<Result>(private val lifecycle: Lifecycle?
        }
    }
}
}
+12 −15
Original line number Diff line number Diff line
@@ -22,12 +22,13 @@ import androidx.lifecycle.LifecycleOwner
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.google.common.truth.Truth.assertThat
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit.MILLISECONDS
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.mock
import org.mockito.kotlin.never
import org.mockito.kotlin.verify
import java.util.concurrent.CountDownLatch

@RunWith(AndroidJUnit4::class)
class LifecycleAwareAsyncTaskTest {
@@ -85,6 +86,7 @@ class LifecycleAwareAsyncTaskTest {

    @Test
    fun onPostExecute_addObserver() {
        val countDownLatch = CountDownLatch(2)
        val observers = mutableListOf<LifecycleObserver>()
        val lifecycle =
            object : Lifecycle() {
@@ -97,32 +99,27 @@ class LifecycleAwareAsyncTaskTest {

                override fun removeObserver(observer: LifecycleObserver) {
                    observers.remove(observer)
                    countDownLatch.countDown()
                }
            }
        val asyncTask =
            object : LifecycleAwareAsyncTask<Void?>(lifecycle) {
                override fun doInBackground(vararg params: Void) = null

                override fun maybeAddObserver(lifecycle: Lifecycle) {
                    super.maybeAddObserver(lifecycle)
                    countDownLatch.countDown()
                }
            }

        Thread { asyncTask.start() }.start()
        idleAsyncTaskExecutor()
        do {
            instrumentation.waitForIdleSync()
        } while (!countDownLatch.await(100, MILLISECONDS))

        assertThat(observers).isEmpty()
    }

    private fun idleAsyncTaskExecutor() {
        val taskCountDownLatch = CountDownLatch(1)
        object : LifecycleAwareAsyncTask<Void?>(null) {
                override fun doInBackground(vararg params: Void): Void? {
                    taskCountDownLatch.countDown()
                    return null
                }
            }
            .start()
        taskCountDownLatch.await()
    }

    @Test
    fun onStop_addObserver() {
        val executorBlocker = CountDownLatch(1)