From 89be94d6abb36df2ec991161e7e056900f0d680b Mon Sep 17 00:00:00 2001 From: tjroach Date: Wed, 25 Oct 2023 12:56:00 -0400 Subject: [PATCH 1/4] Temporary workaround to stabilize DataStore test. --- .../datastore/DataStoreHubEventFilters.java | 25 +++++++++++++++++++ .../datastore/DatastoreCanaryTest.kt | 19 +++++++++++--- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/aws-datastore/src/androidTest/java/com/amplifyframework/datastore/DataStoreHubEventFilters.java b/aws-datastore/src/androidTest/java/com/amplifyframework/datastore/DataStoreHubEventFilters.java index c06fbb3521..ee3befe032 100644 --- a/aws-datastore/src/androidTest/java/com/amplifyframework/datastore/DataStoreHubEventFilters.java +++ b/aws-datastore/src/androidTest/java/com/amplifyframework/datastore/DataStoreHubEventFilters.java @@ -52,6 +52,31 @@ public static HubEventFilter publicationOf(String modelName, String modelId) { }; } + /** + * Watches for publication (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 published 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 event -> { + if (!DataStoreChannelEventName.OUTBOX_MUTATION_ENQUEUED.toString().equals(event.getName())) { + return false; + } + if (!(event.getData() instanceof OutboxMutationEvent)) { + return false; + } + OutboxMutationEvent outboxMutationEvent = + (OutboxMutationEvent) event.getData(); + + return modelId.equals(outboxMutationEvent.getElement().getModel().getPrimaryKeyString()) && + modelName.equals(outboxMutationEvent.getModelName()); + }; + } + /** * Watches for the receipt of a given model, from the cloud. * Creates a filter that catches events from the subscription processor. diff --git a/aws-datastore/src/androidTest/java/com/amplifyframework/datastore/DatastoreCanaryTest.kt b/aws-datastore/src/androidTest/java/com/amplifyframework/datastore/DatastoreCanaryTest.kt index ce42cf935e..caf59ad600 100644 --- a/aws-datastore/src/androidTest/java/com/amplifyframework/datastore/DatastoreCanaryTest.kt +++ b/aws-datastore/src/androidTest/java/com/amplifyframework/datastore/DatastoreCanaryTest.kt @@ -18,17 +18,19 @@ 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 java.util.UUID -import java.util.concurrent.CountDownLatch -import java.util.concurrent.TimeUnit +import com.amplifyframework.testutils.HubAccumulator import org.junit.After import org.junit.Assert.assertTrue import org.junit.Assert.fail import org.junit.BeforeClass import org.junit.Test +import java.util.UUID +import java.util.concurrent.CountDownLatch +import java.util.concurrent.TimeUnit class DatastoreCanaryTest { companion object { @@ -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)) } From c9449b3c9821be5ab3e1881ed4c3155cf656c4a3 Mon Sep 17 00:00:00 2001 From: tjroach Date: Wed, 25 Oct 2023 13:19:37 -0400 Subject: [PATCH 2/4] Temporary workaround to stabilize DataStore test. --- .../com/amplifyframework/datastore/DatastoreCanaryTest.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/aws-datastore/src/androidTest/java/com/amplifyframework/datastore/DatastoreCanaryTest.kt b/aws-datastore/src/androidTest/java/com/amplifyframework/datastore/DatastoreCanaryTest.kt index caf59ad600..4f6ff0666c 100644 --- a/aws-datastore/src/androidTest/java/com/amplifyframework/datastore/DatastoreCanaryTest.kt +++ b/aws-datastore/src/androidTest/java/com/amplifyframework/datastore/DatastoreCanaryTest.kt @@ -23,14 +23,14 @@ 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 import org.junit.After import org.junit.Assert.assertTrue import org.junit.Assert.fail import org.junit.BeforeClass import org.junit.Test -import java.util.UUID -import java.util.concurrent.CountDownLatch -import java.util.concurrent.TimeUnit class DatastoreCanaryTest { companion object { From f0c5479ec3107a4e4fa7afe389ce9c95b8f998f5 Mon Sep 17 00:00:00 2001 From: tjroach Date: Wed, 25 Oct 2023 13:56:40 -0400 Subject: [PATCH 3/4] Temporary workaround to stabilize DataStore test. --- .../datastore/DataStoreHubEventFilters.java | 46 ++++++++++++------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/aws-datastore/src/androidTest/java/com/amplifyframework/datastore/DataStoreHubEventFilters.java b/aws-datastore/src/androidTest/java/com/amplifyframework/datastore/DataStoreHubEventFilters.java index ee3befe032..5ea90a22cf 100644 --- a/aws-datastore/src/androidTest/java/com/amplifyframework/datastore/DataStoreHubEventFilters.java +++ b/aws-datastore/src/androidTest/java/com/amplifyframework/datastore/DataStoreHubEventFilters.java @@ -37,33 +37,47 @@ private DataStoreHubEventFilters() {} * @return A filter that watches for publication of the provided model. */ public static HubEventFilter publicationOf(String modelName, String modelId) { - return event -> { - if (!DataStoreChannelEventName.OUTBOX_MUTATION_PROCESSED.toString().equals(event.getName())) { - return false; - } - if (!(event.getData() instanceof OutboxMutationEvent)) { - return false; - } - OutboxMutationEvent outboxMutationEvent = - (OutboxMutationEvent) event.getData(); - - return modelId.equals(outboxMutationEvent.getElement().getModel().getPrimaryKeyString()) && - modelName.equals(outboxMutationEvent.getModelName()); - }; + return outboxEventOf( + DataStoreChannelEventName.OUTBOX_MUTATION_PROCESSED, + modelName, + modelId + ); } /** - * Watches for publication (out of mutation queue) of a given model. + * 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 published off of the mutation queue. + * 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_ENQUEUED.toString().equals(event.getName())) { + if (!eventType.name().equals(event.getName())) { return false; } if (!(event.getData() instanceof OutboxMutationEvent)) { From a0ddb0d49124983a6c5f121db35fab493384cf4b Mon Sep 17 00:00:00 2001 From: tjroach Date: Wed, 25 Oct 2023 14:04:07 -0400 Subject: [PATCH 4/4] Temporary workaround to stabilize DataStore test. --- .../amplifyframework/datastore/DataStoreHubEventFilters.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws-datastore/src/androidTest/java/com/amplifyframework/datastore/DataStoreHubEventFilters.java b/aws-datastore/src/androidTest/java/com/amplifyframework/datastore/DataStoreHubEventFilters.java index 5ea90a22cf..e8ba883051 100644 --- a/aws-datastore/src/androidTest/java/com/amplifyframework/datastore/DataStoreHubEventFilters.java +++ b/aws-datastore/src/androidTest/java/com/amplifyframework/datastore/DataStoreHubEventFilters.java @@ -77,7 +77,7 @@ private static HubEventFilter outboxEventOf( String modelId ) { return event -> { - if (!eventType.name().equals(event.getName())) { + if (!eventType.toString().equals(event.getName())) { return false; } if (!(event.getData() instanceof OutboxMutationEvent)) {