Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(datastore): Temporary workaround to stabilize DataStore test. #2618

Merged
merged 6 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
tylerjroach marked this conversation as resolved.
Show resolved Hide resolved
}
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
Loading