-
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.
Use main dispatcher for fragment flow store. (#367)
* Use main dispatcher for fragment flow store. * Fix tests.
- Loading branch information
Showing
6 changed files
with
105 additions
and
6 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
...droid-tests/src/test/java/com/instacart/formula/android/utils/MainThreadDispatcherTest.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,64 @@ | ||
package com.instacart.formula.android.utils | ||
|
||
import android.os.Looper | ||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import com.google.common.truth.Truth | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.robolectric.Shadows | ||
import java.util.concurrent.CountDownLatch | ||
import java.util.concurrent.Executors | ||
import java.util.concurrent.TimeUnit | ||
import java.util.concurrent.atomic.AtomicBoolean | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class MainThreadDispatcherTest { | ||
|
||
@Test fun `isDispatchNeeded returns false when on main thread`() { | ||
val result = MainThreadDispatcher().isDispatchNeeded() | ||
Truth.assertThat(result).isFalse() | ||
} | ||
|
||
@Test fun `isDispatchNeeded returns true when not on main thread`() { | ||
val latch = CountDownLatch(1) | ||
val result = AtomicBoolean() | ||
Executors.newSingleThreadExecutor().execute { | ||
result.set(MainThreadDispatcher().isDispatchNeeded()) | ||
latch.countDown() | ||
} | ||
|
||
if (latch.await(1, TimeUnit.SECONDS)) { | ||
Truth.assertThat(result.get()).isTrue() | ||
} else { | ||
error("Latch timed out!") | ||
} | ||
} | ||
|
||
@Test fun `if dispatch is called from main thread, executable is executed immediately`() { | ||
val dispatcher = MainThreadDispatcher() | ||
val loopers = mutableSetOf<Looper?>() | ||
dispatcher.dispatch { loopers.add(Looper.myLooper()) } | ||
Truth.assertThat(loopers).containsExactly(Looper.getMainLooper()) | ||
} | ||
|
||
@Test fun `if dispatch is called from background thread, executable is dispatched to main thread`() { | ||
val dispatcher = MainThreadDispatcher() | ||
val latch = CountDownLatch(1) | ||
|
||
val loopers = mutableSetOf<Looper?>() | ||
Executors.newSingleThreadExecutor().execute { | ||
dispatcher.dispatch { | ||
loopers.add(Looper.myLooper()) | ||
} | ||
latch.countDown() | ||
} | ||
|
||
if (latch.await(1, TimeUnit.SECONDS)) { | ||
Shadows.shadowOf(Looper.getMainLooper()).idle() | ||
Truth.assertThat(loopers).containsExactly(Looper.getMainLooper()) | ||
} else { | ||
error("Latch timed out!") | ||
} | ||
} | ||
|
||
} |
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
30 changes: 30 additions & 0 deletions
30
formula-android/src/main/java/com/instacart/formula/android/utils/MainThreadDispatcher.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,30 @@ | ||
package com.instacart.formula.android.utils | ||
|
||
import android.os.Build | ||
import android.os.Handler | ||
import android.os.Looper | ||
import android.os.Message | ||
import com.instacart.formula.plugin.Dispatcher | ||
|
||
/** | ||
* Android main thread formula dispatcher. | ||
*/ | ||
class MainThreadDispatcher : Dispatcher { | ||
private val handler = Handler(Looper.getMainLooper()) | ||
|
||
override fun dispatch(executable: () -> Unit) { | ||
if (isDispatchNeeded()) { | ||
val message = Message.obtain(handler, executable) | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) { | ||
message.isAsynchronous = true | ||
} | ||
handler.sendMessage(message) | ||
} else { | ||
executable() | ||
} | ||
} | ||
|
||
override fun isDispatchNeeded(): Boolean { | ||
return Looper.getMainLooper().thread != Thread.currentThread() | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
formula-android/src/test/java/com/instacart/formula/android/ActivityStoreFactoryTest.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
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