Skip to content

Commit b67ec35

Browse files
authored
fix(datastore): Temporary workaround to stabilize DataStore test. (#2618)
1 parent 69132df commit b67ec35

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

aws-datastore/src/androidTest/java/com/amplifyframework/datastore/DataStoreHubEventFilters.java

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,57 @@ private DataStoreHubEventFilters() {}
3737
* @return A filter that watches for publication of the provided model.
3838
*/
3939
public static HubEventFilter publicationOf(String modelName, String modelId) {
40+
return outboxEventOf(
41+
DataStoreChannelEventName.OUTBOX_MUTATION_PROCESSED,
42+
modelName,
43+
modelId
44+
);
45+
}
46+
47+
/**
48+
* Watches for enqueue (out of mutation queue) of a given model.
49+
* Creates a filter that catches events from the mutation processor.
50+
* Events will pass if they mention the provided model by its name and ID,
51+
* and state that it has successfully been enqueued off of the mutation queue.
52+
* @param modelName Model name, e.g. "Post"
53+
* @param modelId The ID of a model instance that might be published
54+
* @return A filter that watches for publication of the provided model.
55+
*/
56+
public static HubEventFilter enqueueOf(String modelName, String modelId) {
57+
return outboxEventOf(
58+
DataStoreChannelEventName.OUTBOX_MUTATION_ENQUEUED,
59+
modelName,
60+
modelId
61+
);
62+
}
63+
64+
/**
65+
* Watches for the passed event (out of mutation queue) of a given model.
66+
* Creates a filter that catches events from the mutation processor.
67+
* Events will pass if they mention the provided model by its name and ID,
68+
* and state that it has successfully received passed event type off of the mutation queue.
69+
* @param eventType Either OUTBOX_MUTATION_ENQUEUED or OUTBOX_MUTATION_PROCESSED
70+
* @param modelName Model name, e.g. "Post"
71+
* @param modelId The ID of a model instance that might be published
72+
* @return A filter that watches for publication of the provided model.
73+
*/
74+
private static HubEventFilter outboxEventOf(
75+
DataStoreChannelEventName eventType,
76+
String modelName,
77+
String modelId
78+
) {
4079
return event -> {
41-
if (!DataStoreChannelEventName.OUTBOX_MUTATION_PROCESSED.toString().equals(event.getName())) {
80+
if (!eventType.toString().equals(event.getName())) {
4281
return false;
4382
}
4483
if (!(event.getData() instanceof OutboxMutationEvent)) {
4584
return false;
4685
}
4786
OutboxMutationEvent<? extends Model> outboxMutationEvent =
48-
(OutboxMutationEvent<? extends Model>) event.getData();
87+
(OutboxMutationEvent<? extends Model>) event.getData();
4988

5089
return modelId.equals(outboxMutationEvent.getElement().getModel().getPrimaryKeyString()) &&
51-
modelName.equals(outboxMutationEvent.getModelName());
90+
modelName.equals(outboxMutationEvent.getModelName());
5291
};
5392
}
5493

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ import android.util.Log
1818
import androidx.test.core.app.ApplicationProvider
1919
import com.amplifyframework.AmplifyException
2020
import com.amplifyframework.core.Amplify
21+
import com.amplifyframework.hub.HubChannel
2122
import com.amplifyframework.testmodels.commentsblog.AmplifyModelProvider
2223
import com.amplifyframework.testmodels.commentsblog.Post
2324
import com.amplifyframework.testmodels.commentsblog.PostStatus
25+
import com.amplifyframework.testutils.HubAccumulator
2426
import java.util.UUID
2527
import java.util.concurrent.CountDownLatch
2628
import java.util.concurrent.TimeUnit
@@ -98,21 +100,32 @@ class DatastoreCanaryTest {
98100
.title("Post" + UUID.randomUUID().toString())
99101
.status(PostStatus.ACTIVE)
100102
.rating(3)
103+
.id(UUID.randomUUID().toString())
101104
.build()
102105

103106
val saveLatch = CountDownLatch(1)
107+
val createHub = HubAccumulator.create(
108+
HubChannel.DATASTORE,
109+
DataStoreHubEventFilters.enqueueOf(Post::class.simpleName, post.id),
110+
1
111+
).start()
112+
104113
Amplify.DataStore.save(
105114
post,
106115
{ saveLatch.countDown() },
107116
{ fail("Error creating post: $it") }
108117
)
109118
saveLatch.await(TIMEOUT_S, TimeUnit.SECONDS)
119+
createHub.await(TIMEOUT_S.toInt(), TimeUnit.SECONDS)
120+
110121
val deleteLatch = CountDownLatch(1)
111122
Amplify.DataStore.delete(
112123
post,
113124
{ deleteLatch.countDown() },
114125
{ fail("Failed to delete post: $it") }
115126
)
127+
// Temporarily prevent https://github.com/aws-amplify/amplify-android/issues/2617
128+
Thread.sleep(1000)
116129
assertTrue(deleteLatch.await(TIMEOUT_S, TimeUnit.SECONDS))
117130
}
118131

0 commit comments

Comments
 (0)