Skip to content

Commit

Permalink
fix(datastore): Temporary workaround to stabilize DataStore test. (#2618
Browse files Browse the repository at this point in the history
)
  • Loading branch information
tylerjroach authored Oct 25, 2023
1 parent 69132df commit b67ec35
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,57 @@ private DataStoreHubEventFilters() {}
* @return A filter that watches for publication of the provided model.
*/
public static HubEventFilter publicationOf(String modelName, String modelId) {
return outboxEventOf(
DataStoreChannelEventName.OUTBOX_MUTATION_PROCESSED,
modelName,
modelId
);
}

/**
* Watches for enqueue (out of mutation queue) of a given model.
* Creates a filter that catches events from the mutation processor.
* Events will pass if they mention the provided model by its name and ID,
* and state that it has successfully been enqueued off of the mutation queue.
* @param modelName Model name, e.g. "Post"
* @param modelId The ID of a model instance that might be published
* @return A filter that watches for publication of the provided model.
*/
public static HubEventFilter enqueueOf(String modelName, String modelId) {
return outboxEventOf(
DataStoreChannelEventName.OUTBOX_MUTATION_ENQUEUED,
modelName,
modelId
);
}

/**
* Watches for the passed event (out of mutation queue) of a given model.
* Creates a filter that catches events from the mutation processor.
* Events will pass if they mention the provided model by its name and ID,
* and state that it has successfully received passed event type off of the mutation queue.
* @param eventType Either OUTBOX_MUTATION_ENQUEUED or OUTBOX_MUTATION_PROCESSED
* @param modelName Model name, e.g. "Post"
* @param modelId The ID of a model instance that might be published
* @return A filter that watches for publication of the provided model.
*/
private static HubEventFilter outboxEventOf(
DataStoreChannelEventName eventType,
String modelName,
String modelId
) {
return event -> {
if (!DataStoreChannelEventName.OUTBOX_MUTATION_PROCESSED.toString().equals(event.getName())) {
if (!eventType.toString().equals(event.getName())) {
return false;
}
if (!(event.getData() instanceof OutboxMutationEvent)) {
return false;
}
OutboxMutationEvent<? extends Model> outboxMutationEvent =
(OutboxMutationEvent<? extends Model>) event.getData();
(OutboxMutationEvent<? extends Model>) event.getData();

return modelId.equals(outboxMutationEvent.getElement().getModel().getPrimaryKeyString()) &&
modelName.equals(outboxMutationEvent.getModelName());
modelName.equals(outboxMutationEvent.getModelName());
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ import android.util.Log
import androidx.test.core.app.ApplicationProvider
import com.amplifyframework.AmplifyException
import com.amplifyframework.core.Amplify
import com.amplifyframework.hub.HubChannel
import com.amplifyframework.testmodels.commentsblog.AmplifyModelProvider
import com.amplifyframework.testmodels.commentsblog.Post
import com.amplifyframework.testmodels.commentsblog.PostStatus
import com.amplifyframework.testutils.HubAccumulator
import java.util.UUID
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
Expand Down Expand Up @@ -98,21 +100,32 @@ class DatastoreCanaryTest {
.title("Post" + UUID.randomUUID().toString())
.status(PostStatus.ACTIVE)
.rating(3)
.id(UUID.randomUUID().toString())
.build()

val saveLatch = CountDownLatch(1)
val createHub = HubAccumulator.create(
HubChannel.DATASTORE,
DataStoreHubEventFilters.enqueueOf(Post::class.simpleName, post.id),
1
).start()

Amplify.DataStore.save(
post,
{ saveLatch.countDown() },
{ fail("Error creating post: $it") }
)
saveLatch.await(TIMEOUT_S, TimeUnit.SECONDS)
createHub.await(TIMEOUT_S.toInt(), TimeUnit.SECONDS)

val deleteLatch = CountDownLatch(1)
Amplify.DataStore.delete(
post,
{ deleteLatch.countDown() },
{ fail("Failed to delete post: $it") }
)
// Temporarily prevent https://github.com/aws-amplify/amplify-android/issues/2617
Thread.sleep(1000)
assertTrue(deleteLatch.await(TIMEOUT_S, TimeUnit.SECONDS))
}

Expand Down

0 comments on commit b67ec35

Please sign in to comment.