Skip to content

Commit f7cd41c

Browse files
committed
Fix RxDataStore isDisposed
isDisposed was returning isActive instead of isComplete ¯\_(ツ)_/¯ I also tried to repro b/288539336. It is happening because shutdown hook has no way of waiting for the removal of the file lock. This test does not even care about it and fixing b/233402915 will fix that issue. Otoh, i was able to run it 100 times on FTL w/o failure so I'm not sure how prominant this error is. If it keeps happening, we might put in a delay for now (very sad). Fixes: 290668148 Bug: 288539336 Bug: 233402915 Test: RxdataStoreBuilderTest Change-Id: I1430e481b0906710a480544b9d5955dda025f1b2
1 parent 4fa8a24 commit f7cd41c

4 files changed

Lines changed: 51 additions & 21 deletions

File tree

‎datastore/datastore-rxjava2/src/androidTest/java/androidx/datastore/rxjava2/RxDataStoreBuilderTest.java‎

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
import androidx.datastore.core.handlers.ReplaceFileCorruptionHandler;
2424
import androidx.test.core.app.ApplicationProvider;
2525

26+
import io.reactivex.Completable;
27+
import io.reactivex.Scheduler;
28+
import io.reactivex.Single;
29+
import io.reactivex.schedulers.Schedulers;
30+
2631
import org.junit.Rule;
2732
import org.junit.Test;
2833
import org.junit.rules.TemporaryFolder;
@@ -31,11 +36,6 @@
3136
import java.util.concurrent.Executors;
3237
import java.util.concurrent.ThreadFactory;
3338

34-
import io.reactivex.Completable;
35-
import io.reactivex.Scheduler;
36-
import io.reactivex.Single;
37-
import io.reactivex.schedulers.Schedulers;
38-
3939
public class RxDataStoreBuilderTest {
4040
@Rule
4141
public TemporaryFolder tempFolder = new TemporaryFolder();
@@ -67,6 +67,8 @@ public void testConstructWithProduceFile() throws Exception {
6767
public void testConstructWithContextAndName() throws Exception {
6868
Context context = ApplicationProvider.getApplicationContext();
6969
String name = "my_data_store";
70+
// make sure we cleanup before starting the test, in case a previous test left it dirty.
71+
new File(context.getFilesDir(), "datastore/" + name).delete();
7072
RxDataStore<Byte> dataStore =
7173
new RxDataStoreBuilder<Byte>(context, name, new TestingSerializer())
7274
.build();
@@ -91,6 +93,8 @@ public void testConstructWithContextAndName() throws Exception {
9193
)
9294
.build();
9395
assertThat(dataStore.data().blockingFirst()).isEqualTo(1);
96+
dataStore.dispose();
97+
dataStore.shutdownComplete().blockingAwait();
9498
}
9599

96100
@Test
@@ -167,4 +171,17 @@ public void testCorruptionHandlerIsUser() {
167171
.build();
168172
assertThat(dataStore.data().blockingFirst()).isEqualTo(99);
169173
}
174+
175+
@Test
176+
public void isDisposed() {
177+
TestingSerializer testingSerializer = new TestingSerializer();
178+
testingSerializer.setFailReadWithCorruptionException(true);
179+
RxDataStore<Byte> dataStore = new RxDataStoreBuilder<Byte>(
180+
() -> tempFolder.newFile(),
181+
testingSerializer)
182+
.build();
183+
assertThat(dataStore.isDisposed()).isFalse();
184+
dataStore.dispose();
185+
assertThat(dataStore.isDisposed()).isTrue();
186+
}
170187
}

‎datastore/datastore-rxjava2/src/main/java/androidx/datastore/rxjava2/RxDataStore.kt‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ import kotlinx.coroutines.Job
2828
import kotlinx.coroutines.SupervisorJob
2929
import kotlinx.coroutines.async
3030
import kotlinx.coroutines.job
31+
import kotlinx.coroutines.rx2.asCompletable
3132
import kotlinx.coroutines.rx2.asFlowable
3233
import kotlinx.coroutines.rx2.asSingle
3334
import kotlinx.coroutines.rx2.await
34-
import kotlinx.coroutines.rx2.rxCompletable
3535

3636
/**
3737
* A DataStore that supports RxJava operations on DataStore.
@@ -55,7 +55,7 @@ public class RxDataStore<T : Any> private constructor(
5555
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
5656
public fun <T : Any> create(delegateDs: DataStore<T>, scope: CoroutineScope):
5757
RxDataStore<T> {
58-
return RxDataStore<T>(delegateDs, scope)
58+
return RxDataStore(delegateDs, scope)
5959
}
6060
}
6161

@@ -68,16 +68,16 @@ public class RxDataStore<T : Any> private constructor(
6868
/**
6969
* Returns whether this DataStore is closed
7070
*/
71-
override fun isDisposed(): Boolean = scope.coroutineContext.job.isActive
71+
override fun isDisposed(): Boolean = !scope.coroutineContext.job.isActive
7272

7373
/**
7474
* Returns a completable that completes when the DataStore is completed. It is not safe to
7575
* create a new DataStore with the same file name until this has completed.
7676
*/
7777
public fun shutdownComplete(): Completable =
78-
rxCompletable(scope.coroutineContext.minusKey(Job)) {
79-
scope.coroutineContext.job.join()
80-
}
78+
scope.coroutineContext.job.asCompletable(
79+
scope.coroutineContext.minusKey(Job)
80+
)
8181

8282
/**
8383
* Gets a reactivex.Flowable of the data from DataStore. See [DataStore.data] for more information.

‎datastore/datastore-rxjava3/src/androidTest/java/androidx/datastore/rxjava3/RxDataStoreBuilderTest.java‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@
2323
import androidx.datastore.core.handlers.ReplaceFileCorruptionHandler;
2424
import androidx.test.core.app.ApplicationProvider;
2525

26+
import io.reactivex.rxjava3.core.Completable;
27+
import io.reactivex.rxjava3.core.Scheduler;
28+
import io.reactivex.rxjava3.core.Single;
29+
import io.reactivex.rxjava3.schedulers.Schedulers;
30+
2631
import org.junit.Rule;
2732
import org.junit.Test;
2833
import org.junit.rules.TemporaryFolder;
@@ -31,11 +36,6 @@
3136
import java.util.concurrent.Executors;
3237
import java.util.concurrent.ThreadFactory;
3338

34-
import io.reactivex.rxjava3.core.Completable;
35-
import io.reactivex.rxjava3.core.Scheduler;
36-
import io.reactivex.rxjava3.core.Single;
37-
import io.reactivex.rxjava3.schedulers.Schedulers;
38-
3939
public class RxDataStoreBuilderTest {
4040
@Rule
4141
public TemporaryFolder tempFolder = new TemporaryFolder();
@@ -167,4 +167,17 @@ public void testCorruptionHandlerIsUser() {
167167
.build();
168168
assertThat(dataStore.data().blockingFirst()).isEqualTo(99);
169169
}
170+
171+
@Test
172+
public void isDisposed() {
173+
TestingSerializer testingSerializer = new TestingSerializer();
174+
testingSerializer.setFailReadWithCorruptionException(true);
175+
RxDataStore<Byte> dataStore = new RxDataStoreBuilder<Byte>(
176+
() -> tempFolder.newFile(),
177+
testingSerializer)
178+
.build();
179+
assertThat(dataStore.isDisposed()).isFalse();
180+
dataStore.dispose();
181+
assertThat(dataStore.isDisposed()).isTrue();
182+
}
170183
}

‎datastore/datastore-rxjava3/src/main/java/androidx/datastore/rxjava3/RxDataStore.kt‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ import kotlinx.coroutines.Job
3030
import kotlinx.coroutines.SupervisorJob
3131
import kotlinx.coroutines.async
3232
import kotlinx.coroutines.job
33+
import kotlinx.coroutines.rx3.asCompletable
3334
import kotlinx.coroutines.rx3.asFlowable
3435
import kotlinx.coroutines.rx3.asSingle
3536
import kotlinx.coroutines.rx3.await
36-
import kotlinx.coroutines.rx3.rxCompletable
3737

3838
/**
3939
* A DataStore that supports RxJava operations on DataStore.
@@ -70,16 +70,16 @@ public class RxDataStore<T : Any> private constructor(
7070
/**
7171
* Returns whether this DataStore is closed
7272
*/
73-
override fun isDisposed(): Boolean = scope.coroutineContext.job.isActive
73+
override fun isDisposed(): Boolean = !scope.coroutineContext.job.isActive
7474

7575
/**
7676
* Returns a completable that completes when the DataStore is completed. It is not safe to
7777
* create a new DataStore with the same file name until this has completed.
7878
*/
7979
public fun shutdownComplete(): Completable =
80-
rxCompletable(scope.coroutineContext.minusKey(Job)) {
81-
scope.coroutineContext.job.join()
82-
}
80+
scope.coroutineContext.job.asCompletable(
81+
scope.coroutineContext.minusKey(Job)
82+
)
8383

8484
/**
8585
* Gets a reactivex.Flowable of the data from DataStore. See [DataStore.data] for more information.

0 commit comments

Comments
 (0)