diff --git a/app/src/main/java/dev/wander/android/opentagviewer/MapsActivity.java b/app/src/main/java/dev/wander/android/opentagviewer/MapsActivity.java index 9417d93c..d47f5370 100644 --- a/app/src/main/java/dev/wander/android/opentagviewer/MapsActivity.java +++ b/app/src/main/java/dev/wander/android/opentagviewer/MapsActivity.java @@ -2144,13 +2144,17 @@ private void showDevicesAndFetch(final Observable asyncAppleService) { *

Accepting saves first and signs out only once the save has landed. The other order * looks the same right up until the write loses a race with the activity finishing, at * which point they sign in again and arrive back on the server they just left. + * + *

{@link RxFlows#onlyAfter} and not {@code andThen}, because {@code clearUser()} starts + * writing when it is called. Passed to {@code andThen} directly it signed the user out at + * once, and still did if the save then failed. */ private void offerLocalAnisetteUpgradeIfDue() { this.runOnUiThread(() -> AnisetteUpgradeDialog.offerIfDue(this, this.userSettings, accepted -> { var saved = this.userSettingsRepo.storeUserSettings(this.userSettings); - var async = (accepted ? saved.andThen(this.userAuthRepo.clearUser()) : saved) + var async = (accepted ? RxFlows.onlyAfter(saved, this.userAuthRepo::clearUser) : saved) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(() -> { diff --git a/app/src/main/java/dev/wander/android/opentagviewer/util/rx/RxFlows.java b/app/src/main/java/dev/wander/android/opentagviewer/util/rx/RxFlows.java index 322cff5e..b0d13f3c 100644 --- a/app/src/main/java/dev/wander/android/opentagviewer/util/rx/RxFlows.java +++ b/app/src/main/java/dev/wander/android/opentagviewer/util/rx/RxFlows.java @@ -6,11 +6,12 @@ import io.reactivex.rxjava3.core.Observable; import io.reactivex.rxjava3.functions.BiConsumer; import io.reactivex.rxjava3.functions.Function; +import io.reactivex.rxjava3.functions.Supplier; /** * Stream compositions that the app depends on and that are easy to get subtly wrong. *
- * Both methods here exist because the composition, not the work, was the bug. They are kept + * Each method here exists because the composition, not the work, was the bug. They are kept * free of Android types so they can be tested on the JVM: the originals lived inside * {@code MapsActivity}, where nothing could reach them, and the failure they produced was a * missing card rather than an exception - no crash, no log, no test. @@ -45,6 +46,26 @@ public static Completable allThen(final Completable then, final Observable... return Completable.mergeArray(parts).andThen(then); } + /** + * Runs {@code first}, and only once it has completed, creates and runs {@code then}. + *
+ * {@code then} is taken as a supplier because some Completables here have already started + * by the time they are returned. Every DataStore write ({@code UserAuthRepository.clearUser}, + * {@code UserSettingsRepository.storeUserSettings} and the rest) is built on + * {@code RxDataStore.updateDataAsync}, which launches its coroutine when called. + * {@code first.andThen(repo.clearUser())} therefore signs the user out while the chain is + * being built, and still does if {@code first} fails. Nothing is logged and the success path + * looks identical, so it went unnoticed. + *
+ * If {@code first} fails, {@code then} is never created. + * + * @param first run first + * @param then called only after {@code first} completes + */ + public static Completable onlyAfter(final Completable first, final Supplier then) { + return first.andThen(Completable.defer(then)); + } + /** * Runs {@code work} for each item, one at a time and in order, carrying on if one fails. *
diff --git a/app/src/test/java/dev/wander/android/opentagviewer/util/rx/RxFlowsTest.java b/app/src/test/java/dev/wander/android/opentagviewer/util/rx/RxFlowsTest.java index 8be51670..5e163c64 100644 --- a/app/src/test/java/dev/wander/android/opentagviewer/util/rx/RxFlowsTest.java +++ b/app/src/test/java/dev/wander/android/opentagviewer/util/rx/RxFlowsTest.java @@ -330,4 +330,57 @@ public void oneAtATime_completesImmediatelyForAnEmptyList() { assertEquals(List.of("0/0"), progress); } + + // ------------------------------------------------------------------------------------- + // onlyAfter - a DataStore write that must wait for something before it + // ------------------------------------------------------------------------------------- + + /** + * Stands in for a repository method built on {@code RxDataStore.updateDataAsync}: the write + * happens when the method is called, and the Completable it returns only reports on it. + */ + private static Completable eagerWrite(final AtomicInteger writes) { + writes.incrementAndGet(); + return Completable.complete(); + } + + @Test + public void onlyAfter_doesNotStartTheNextStepBeforeTheFirstCompletes() { + final AtomicInteger signOuts = new AtomicInteger(); + final PublishSubject save = PublishSubject.create(); + + final TestObserver result = RxFlows.onlyAfter( + save.ignoreElements(), () -> eagerWrite(signOuts)).test(); + + assertEquals("signed out before the save landed", 0, signOuts.get()); + + save.onComplete(); + + result.assertComplete(); + assertEquals(1, signOuts.get()); + } + + /** The bug the helper exists for: a failed save must leave the user signed in. */ + @Test + public void onlyAfter_neverStartsTheNextStepWhenTheFirstFails() { + final AtomicInteger signOuts = new AtomicInteger(); + + RxFlows.onlyAfter(Completable.error(new RuntimeException("save failed")), + () -> eagerWrite(signOuts)) + .test() + .assertError(RuntimeException.class); + + assertEquals(0, signOuts.get()); + } + + /** Building the chain without subscribing must not do the second step either. */ + @Test + public void onlyAfter_doesNothingUntilSubscribed() { + final AtomicInteger signOuts = new AtomicInteger(); + + final Completable unused = RxFlows.onlyAfter(Completable.complete(), + () -> eagerWrite(signOuts)); + + assertEquals(0, signOuts.get()); + } }