Skip to content

Commit 277af51

Browse files
committed
Update datastore canary tests
1 parent 2da2d85 commit 277af51

File tree

3 files changed

+27
-154
lines changed

3 files changed

+27
-154
lines changed

aws-datastore/src/androidTest/java/com/amplifyframework/datastore/DatastoreCanaryTest.kt

Lines changed: 11 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,10 @@ import com.amplifyframework.testmodels.commentsblog.AmplifyModelProvider
2323
import com.amplifyframework.testmodels.commentsblog.Post
2424
import com.amplifyframework.testmodels.commentsblog.PostStatus
2525
import com.amplifyframework.testutils.HubAccumulator
26+
import com.amplifyframework.testutils.sync.SynchronousDataStore
2627
import java.util.UUID
27-
import java.util.concurrent.CountDownLatch
2828
import java.util.concurrent.TimeUnit
2929
import org.junit.After
30-
import org.junit.Assert.assertTrue
31-
import org.junit.Assert.fail
3230
import org.junit.BeforeClass
3331
import org.junit.Test
3432

@@ -53,45 +51,26 @@ class DatastoreCanaryTest {
5351
}
5452
}
5553

54+
val syncDatastore = SynchronousDataStore.delegatingTo(Amplify.DataStore)
55+
5656
@After
5757
fun teardown() {
58-
val latch = CountDownLatch(1)
59-
Amplify.DataStore.clear(
60-
{ latch.countDown() },
61-
{
62-
latch.countDown()
63-
Log.e(TAG, "Error clearing DataStore", it)
64-
}
65-
)
66-
latch.await(TIMEOUT_S, TimeUnit.SECONDS)
58+
syncDatastore.clear()
6759
}
6860

6961
@Test
7062
fun save() {
71-
val latch = CountDownLatch(1)
7263
val post = Post.builder()
7364
.title("Post" + UUID.randomUUID().toString())
7465
.status(PostStatus.ACTIVE)
7566
.rating(3)
7667
.build()
77-
78-
Amplify.DataStore.save(
79-
post,
80-
{ latch.countDown() },
81-
{ fail("Error creating post: $it") }
82-
)
83-
assertTrue(latch.await(TIMEOUT_S, TimeUnit.SECONDS))
68+
syncDatastore.save(post)
8469
}
8570

8671
@Test
8772
fun query() {
88-
val latch = CountDownLatch(1)
89-
Amplify.DataStore.query(
90-
Post::class.java,
91-
{ latch.countDown() },
92-
{ fail("Error retrieving posts: $it") }
93-
)
94-
assertTrue(latch.await(TIMEOUT_S, TimeUnit.SECONDS))
73+
syncDatastore.query(Post::class.java)
9574
}
9675

9776
@Test
@@ -103,75 +82,30 @@ class DatastoreCanaryTest {
10382
.id(UUID.randomUUID().toString())
10483
.build()
10584

106-
val saveLatch = CountDownLatch(1)
10785
val createHub = HubAccumulator.create(
10886
HubChannel.DATASTORE,
10987
DataStoreHubEventFilters.enqueueOf(Post::class.simpleName, post.id),
11088
1
11189
).start()
11290

113-
Amplify.DataStore.save(
114-
post,
115-
{ saveLatch.countDown() },
116-
{ fail("Error creating post: $it") }
117-
)
118-
saveLatch.await(TIMEOUT_S, TimeUnit.SECONDS)
91+
syncDatastore.save(post)
11992
createHub.await(TIMEOUT_S.toInt(), TimeUnit.SECONDS)
12093

121-
val deleteLatch = CountDownLatch(1)
122-
Amplify.DataStore.delete(
123-
post,
124-
{ deleteLatch.countDown() },
125-
{ fail("Failed to delete post: $it") }
126-
)
127-
// Temporarily prevent https://github.com/aws-amplify/amplify-android/issues/2617
128-
Thread.sleep(1000)
129-
assertTrue(deleteLatch.await(TIMEOUT_S, TimeUnit.SECONDS))
94+
syncDatastore.delete(post)
13095
}
13196

13297
@Test
13398
fun start() {
134-
val latch = CountDownLatch(1)
135-
Amplify.DataStore.start(
136-
{ latch.countDown() },
137-
{ fail("Error starting DataStore: $it") }
138-
)
139-
assertTrue(latch.await(TIMEOUT_S, TimeUnit.SECONDS))
140-
}
141-
142-
@Test
143-
fun observe() {
144-
val latch = CountDownLatch(1)
145-
Amplify.DataStore.observe(
146-
Post::class.java,
147-
{ latch.countDown() },
148-
{
149-
val post = it.item()
150-
Log.i(TAG, "Post: $post")
151-
},
152-
{ fail("Observation failed: $it") },
153-
{ Log.i(TAG, "Observation complete") }
154-
)
155-
assertTrue(latch.await(TIMEOUT_S, TimeUnit.SECONDS))
99+
syncDatastore.start()
156100
}
157101

158102
@Test
159103
fun stop() {
160-
val latch = CountDownLatch(1)
161-
Amplify.DataStore.stop(
162-
{ latch.countDown() },
163-
{ fail("Error stopping DataStore: $it") }
164-
)
165-
assertTrue(latch.await(TIMEOUT_S, TimeUnit.SECONDS))
104+
syncDatastore.stop()
166105
}
167106

168107
@Test
169108
fun clear() {
170-
val latch = CountDownLatch(1)
171-
Amplify.DataStore.clear(
172-
{ latch.countDown() },
173-
{ fail("Error clearing DataStore: $it") }
174-
)
175-
assertTrue(latch.await(TIMEOUT_S, TimeUnit.SECONDS))
109+
syncDatastore.clear()
176110
}
177111
}

aws-datastore/src/androidTest/java/com/amplifyframework/datastore/DatastoreCanaryTestGen2.kt

Lines changed: 11 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,10 @@ import com.amplifyframework.testmodels.commentsblog.AmplifyModelProvider
2525
import com.amplifyframework.testmodels.commentsblog.Post
2626
import com.amplifyframework.testmodels.commentsblog.PostStatus
2727
import com.amplifyframework.testutils.HubAccumulator
28+
import com.amplifyframework.testutils.sync.SynchronousDataStore
2829
import java.util.UUID
29-
import java.util.concurrent.CountDownLatch
3030
import java.util.concurrent.TimeUnit
3131
import org.junit.After
32-
import org.junit.Assert.assertTrue
33-
import org.junit.Assert.fail
3432
import org.junit.BeforeClass
3533
import org.junit.Test
3634

@@ -55,45 +53,26 @@ class DatastoreCanaryTestGen2 {
5553
}
5654
}
5755

56+
val syncDatastore = SynchronousDataStore.delegatingTo(Amplify.DataStore)
57+
5858
@After
5959
fun teardown() {
60-
val latch = CountDownLatch(1)
61-
Amplify.DataStore.clear(
62-
{ latch.countDown() },
63-
{
64-
latch.countDown()
65-
Log.e(TAG, "Error clearing DataStore", it)
66-
}
67-
)
68-
latch.await(TIMEOUT_S, TimeUnit.SECONDS)
60+
syncDatastore.clear()
6961
}
7062

7163
@Test
7264
fun save() {
73-
val latch = CountDownLatch(1)
7465
val post = Post.builder()
7566
.title("Post" + UUID.randomUUID().toString())
7667
.status(PostStatus.ACTIVE)
7768
.rating(3)
7869
.build()
79-
80-
Amplify.DataStore.save(
81-
post,
82-
{ latch.countDown() },
83-
{ fail("Error creating post: $it") }
84-
)
85-
assertTrue(latch.await(TIMEOUT_S, TimeUnit.SECONDS))
70+
syncDatastore.save(post)
8671
}
8772

8873
@Test
8974
fun query() {
90-
val latch = CountDownLatch(1)
91-
Amplify.DataStore.query(
92-
Post::class.java,
93-
{ latch.countDown() },
94-
{ fail("Error retrieving posts: $it") }
95-
)
96-
assertTrue(latch.await(TIMEOUT_S, TimeUnit.SECONDS))
75+
syncDatastore.query(Post::class.java)
9776
}
9877

9978
@Test
@@ -105,75 +84,30 @@ class DatastoreCanaryTestGen2 {
10584
.id(UUID.randomUUID().toString())
10685
.build()
10786

108-
val saveLatch = CountDownLatch(1)
10987
val createHub = HubAccumulator.create(
11088
HubChannel.DATASTORE,
11189
DataStoreHubEventFilters.enqueueOf(Post::class.simpleName, post.id),
11290
1
11391
).start()
11492

115-
Amplify.DataStore.save(
116-
post,
117-
{ saveLatch.countDown() },
118-
{ fail("Error creating post: $it") }
119-
)
120-
saveLatch.await(TIMEOUT_S, TimeUnit.SECONDS)
93+
syncDatastore.save(post)
12194
createHub.await(TIMEOUT_S.toInt(), TimeUnit.SECONDS)
12295

123-
val deleteLatch = CountDownLatch(1)
124-
Amplify.DataStore.delete(
125-
post,
126-
{ deleteLatch.countDown() },
127-
{ fail("Failed to delete post: $it") }
128-
)
129-
// Temporarily prevent https://github.com/aws-amplify/amplify-android/issues/2617
130-
Thread.sleep(1000)
131-
assertTrue(deleteLatch.await(TIMEOUT_S, TimeUnit.SECONDS))
96+
syncDatastore.delete(post)
13297
}
13398

13499
@Test
135100
fun start() {
136-
val latch = CountDownLatch(1)
137-
Amplify.DataStore.start(
138-
{ latch.countDown() },
139-
{ fail("Error starting DataStore: $it") }
140-
)
141-
assertTrue(latch.await(TIMEOUT_S, TimeUnit.SECONDS))
142-
}
143-
144-
@Test
145-
fun observe() {
146-
val latch = CountDownLatch(1)
147-
Amplify.DataStore.observe(
148-
Post::class.java,
149-
{ latch.countDown() },
150-
{
151-
val post = it.item()
152-
Log.i(TAG, "Post: $post")
153-
},
154-
{ fail("Observation failed: $it") },
155-
{ Log.i(TAG, "Observation complete") }
156-
)
157-
assertTrue(latch.await(TIMEOUT_S, TimeUnit.SECONDS))
101+
syncDatastore.start()
158102
}
159103

160104
@Test
161105
fun stop() {
162-
val latch = CountDownLatch(1)
163-
Amplify.DataStore.stop(
164-
{ latch.countDown() },
165-
{ fail("Error stopping DataStore: $it") }
166-
)
167-
assertTrue(latch.await(TIMEOUT_S, TimeUnit.SECONDS))
106+
syncDatastore.stop()
168107
}
169108

170109
@Test
171110
fun clear() {
172-
val latch = CountDownLatch(1)
173-
Amplify.DataStore.clear(
174-
{ latch.countDown() },
175-
{ fail("Error clearing DataStore: $it") }
176-
)
177-
assertTrue(latch.await(TIMEOUT_S, TimeUnit.SECONDS))
111+
syncDatastore.clear()
178112
}
179113
}

testutils/src/main/java/com/amplifyframework/testutils/sync/SynchronousDataStore.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,11 @@ public <T extends Model> List<T> list(@NonNull Class<T> clazz) throws DataStoreE
128128
return Immutable.of(items);
129129
}
130130

131+
@NonNull
132+
public <T extends Model> Iterator<T> query(@NonNull Class<T> clazz) throws DataStoreException {
133+
return awaitIterator((onResult, onError) -> asyncDelegate.query(clazz, onResult, onError));
134+
}
135+
131136
/**
132137
* Calls the start method of the underlying DataStore implementation.
133138
* @throws DataStoreException On failure to start data store.

0 commit comments

Comments
 (0)