Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2144,13 +2144,17 @@ private void showDevicesAndFetch(final Observable<Object> asyncAppleService) {
* <p>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.
*
* <p>{@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(() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <br>
* 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.
Expand Down Expand Up @@ -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}.
* <br>
* {@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.
* <br>
* 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<Completable> 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.
* <br>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object> save = PublishSubject.create();

final TestObserver<Void> 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());
}
}