Fix flaky LifecycleAwareAsyncTaskTest
Fix: 385137513 Flag: TEST_ONLY Test: atest SettingsRoboTests:LifecycleAwareAsyncTaskTest --rerun-until-failure 500 Change-Id: I269d2fa1c63e0b805d7d1b0710919f60c2096ec0
This commit is contained in:
@@ -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,12 +51,15 @@ abstract class LifecycleAwareAsyncTask<Result>(private val lifecycle: Lifecycle?
|
||||
fun start() {
|
||||
execute() // expects main thread
|
||||
val lifecycle = lifecycle ?: return
|
||||
mainExecutor.execute {
|
||||
// Status is updated to FINISHED if onPoseExecute happened before. And task is cancelled
|
||||
// if lifecycle is stopped.
|
||||
if (status == Status.RUNNING && !isCancelled) {
|
||||
lifecycle.addObserver(this) // requires main thread
|
||||
}
|
||||
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) {
|
||||
lifecycle.addObserver(this) // requires main thread
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user