-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Android] Ensure all events touching Android happen on the main threa…
…d. (#334) * [Android] Ensure main thread internally. * Test AndroidUpdateScheduler. * Add a bunch of tests. * Fix issue with updateScheduled being removed only once. * Make sure to clear pending update. * Move main thread scheduler to FragmentFlowStore.
- Loading branch information
Showing
14 changed files
with
345 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
...-tests/src/test/java/com/instacart/formula/android/internal/AndroidUpdateSchedulerTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.instacart.formula.android.internal | ||
|
||
import android.os.Looper | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import com.google.common.truth.Truth | ||
import com.google.common.truth.Truth.assertThat | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.robolectric.Shadows.shadowOf | ||
import java.util.LinkedList | ||
import java.util.concurrent.CountDownLatch | ||
import java.util.concurrent.Executors | ||
import java.util.concurrent.TimeUnit | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class AndroidUpdateSchedulerTest { | ||
|
||
@Test fun `when an update triggers another update, scheduler finishes first one before proceeding to the next`() { | ||
val computedValues = LinkedList<String>() | ||
val scheduler = AndroidUpdateScheduler<() -> String> { valueComputation -> | ||
val value = valueComputation() | ||
computedValues.addLast(value) | ||
} | ||
|
||
scheduler.emitUpdate { | ||
scheduler.emitUpdate { "next" } | ||
"first" | ||
} | ||
|
||
assertThat(computedValues).containsExactly("first", "next").inOrder() | ||
} | ||
|
||
@Test fun `when update arrives on bg thread, handle it on main thread`() { | ||
val computedValues = LinkedList<String>() | ||
val scheduler = AndroidUpdateScheduler<() -> String> { valueComputation -> | ||
val value = valueComputation() | ||
computedValues.addLast(value) | ||
} | ||
|
||
val latch = CountDownLatch(1) | ||
Executors.newSingleThreadExecutor().execute { | ||
scheduler.emitUpdate { "bg update" } | ||
latch.countDown() | ||
} | ||
|
||
if (!latch.await(100, TimeUnit.MILLISECONDS)) { | ||
throw IllegalStateException("timeout") | ||
} | ||
|
||
shadowOf(Looper.getMainLooper()).idle() | ||
assertThat(computedValues).containsExactly("bg update").inOrder() | ||
} | ||
|
||
@Test fun `when multiple updates arrive on bg thread before main thread is ready, we handle only last`() { | ||
val computedValues = LinkedList<String>() | ||
val scheduler = AndroidUpdateScheduler<() -> String> { valueComputation -> | ||
val value = valueComputation() | ||
computedValues.addLast(value) | ||
} | ||
|
||
val latch = CountDownLatch(1) | ||
Executors.newSingleThreadExecutor().execute { | ||
scheduler.emitUpdate { "bg update-1" } | ||
scheduler.emitUpdate { "bg update-2" } | ||
scheduler.emitUpdate { "bg update-3" } | ||
scheduler.emitUpdate { "bg update-4" } | ||
latch.countDown() | ||
} | ||
|
||
if (!latch.await(100, TimeUnit.MILLISECONDS)) { | ||
throw IllegalStateException("timeout") | ||
} | ||
|
||
shadowOf(Looper.getMainLooper()).idle() | ||
assertThat(computedValues).containsExactly("bg update-4").inOrder() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
...la-android/src/main/java/com/instacart/formula/android/internal/AndroidUpdateScheduler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.instacart.formula.android.internal | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean | ||
import java.util.concurrent.atomic.AtomicReference | ||
|
||
/** | ||
* Handles state update scheduling to the main thread. If update arrives on a background thread, | ||
* it will added it the main thread queue. It will throw away a pending update if a new update | ||
* arrives. | ||
*/ | ||
class AndroidUpdateScheduler<Value : Any>( | ||
private val update: (Value) -> Unit, | ||
) { | ||
/** | ||
* If not null, that means that we have an update pending. | ||
*/ | ||
private val pendingValue = AtomicReference<Value>() | ||
|
||
/** | ||
* Defines if an update is currently scheduled. | ||
*/ | ||
private val updateScheduled = AtomicBoolean(false) | ||
|
||
/** | ||
* To avoid re-entry, we track if [updateRunnable] is currently handling an update. | ||
*/ | ||
private var isUpdating = false | ||
|
||
private val updateRunnable = object : Runnable { | ||
override fun run() { | ||
updateScheduled.set(false) | ||
|
||
var localPending = pendingValue.getAndSet(null) | ||
while (localPending != null) { | ||
// Handle the update | ||
isUpdating = true | ||
update(localPending) | ||
isUpdating = false | ||
|
||
// Check if another update arrived while we were processing. | ||
localPending = pendingValue.getAndSet(null) | ||
|
||
if (localPending != null) { | ||
// We will perform the update, let's clear the values. | ||
updateScheduled.set(false) | ||
Utils.mainThreadHandler.removeCallbacks(this) | ||
} | ||
} | ||
} | ||
} | ||
|
||
fun emitUpdate(value: Value) { | ||
// Set pending value | ||
pendingValue.set(value) | ||
|
||
if (Utils.isMainThread()) { | ||
if (isUpdating) { | ||
// Let's exit and let the [updateRunnable] to pick up the change | ||
return | ||
} else { | ||
// Since we are on main thread, let's force run it | ||
updateRunnable.run() | ||
} | ||
} else { | ||
// If no update is scheduled, schedule one | ||
if (updateScheduled.compareAndSet(false, true)) { | ||
Utils.mainThreadHandler.post(updateRunnable) | ||
} | ||
} | ||
} | ||
} |
1 change: 0 additions & 1 deletion
1
formula-android/src/main/java/com/instacart/formula/android/internal/FeatureBinding.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.