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

chore: Prevent Instrumentation Test device conflicts #2620

Merged
merged 3 commits into from
Oct 30, 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 @@ -28,6 +28,7 @@ import com.amplifyframework.datastore.generated.model.HasOneChild
import com.amplifyframework.datastore.generated.model.Parent
import com.amplifyframework.datastore.generated.model.ParentPath
import com.amplifyframework.kotlin.core.Amplify
import java.util.UUID
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import kotlinx.coroutines.CoroutineScope
Expand All @@ -44,6 +45,11 @@ import org.junit.Test
class GraphQLLazySubscribeInstrumentationTest {

companion object {

val instrumentationRunId by lazy {
UUID.randomUUID().toString()
}

@JvmStatic
@BeforeClass
fun setUp() {
Expand All @@ -54,14 +60,30 @@ class GraphQLLazySubscribeInstrumentationTest {
}
}

/**
* Some of our test suites run at the same time on multiple devices.
* This can result in failures if 1 device creates a model that the other receives in its subscription.
* This helps check that the model was created by this instrumentation test run and not another device.
*/
private fun modelCreatedByDevice(modelId: String) = modelId.startsWith(instrumentationRunId)

/**
* Use this method for any models that will be subscribed to for the tests in this class.
* This allows us to verify that the models in this test were created by a specific device
*/
private fun createRandomIdWithRunIdPrefix() = "$instrumentationRunId-${UUID.randomUUID()}"

@OptIn(ExperimentalCoroutinesApi::class, FlowPreview::class)
@Test
fun subscribe_with_no_includes_create() = runTest {
// GIVEN
val hasOneChild = HasOneChild.builder()
.content("Child1")
.build()
val parent = Parent.builder().parentChildId(hasOneChild.id).build()
val parent = Parent.builder()
.id(createRandomIdWithRunIdPrefix())
.parentChildId(hasOneChild.id)
.build()

val latch = CountDownLatch(1)
val collectRunningLatch = CountDownLatch(1)
Expand All @@ -71,10 +93,13 @@ class GraphQLLazySubscribeInstrumentationTest {
val subscription = Amplify.API.subscribe(ModelSubscription.onCreate(Parent::class.java))
CoroutineScope(Dispatchers.IO).launch {
subscription.collect {
assertEquals(parent.id, it.data.id)
capturedParent = it.data
capturedChild = (it.data.child as LazyModelReference).fetchModel()!!
latch.countDown()
val returnedParent = it.data
if (modelCreatedByDevice(returnedParent.id)) {
assertEquals(parent.id, returnedParent.id)
capturedParent = returnedParent
capturedChild = (returnedParent.child as LazyModelReference).fetchModel()!!
latch.countDown()
}
}
collectRunningLatch.countDown()
}
Expand Down Expand Up @@ -105,7 +130,10 @@ class GraphQLLazySubscribeInstrumentationTest {
val hasOneChild = HasOneChild.builder()
.content("Child1")
.build()
val parent = Parent.builder().parentChildId(hasOneChild.id).build()
val parent = Parent.builder()
.id(createRandomIdWithRunIdPrefix())
.parentChildId(hasOneChild.id)
.build()

val latch = CountDownLatch(1)
val collectRunningLatch = CountDownLatch(1)
Expand All @@ -119,10 +147,13 @@ class GraphQLLazySubscribeInstrumentationTest {
var capturedChild: HasOneChild? = null
CoroutineScope(Dispatchers.IO).launch {
subscription.collect {
assertEquals(parent.id, it.data.id)
capturedParent = it.data
capturedChild = (it.data.child as LoadedModelReference).value
latch.countDown()
val returnedParent = it.data
if (modelCreatedByDevice(returnedParent.id)) {
assertEquals(parent.id, returnedParent.id)
capturedParent = returnedParent
capturedChild = (returnedParent.child as LoadedModelReference).value
latch.countDown()
}
}
collectRunningLatch.countDown()
}
Expand Down Expand Up @@ -159,7 +190,10 @@ class GraphQLLazySubscribeInstrumentationTest {
val hasOneChild2 = HasOneChild.builder()
.content("Child2")
.build()
val parent = Parent.builder().parentChildId(hasOneChild.id).build()
val parent = Parent.builder()
.id(createRandomIdWithRunIdPrefix())
.parentChildId(hasOneChild.id)
.build()

val subscription = Amplify.API.subscribe(ModelSubscription.onUpdate(Parent::class.java))

Expand All @@ -168,10 +202,13 @@ class GraphQLLazySubscribeInstrumentationTest {
var capturedChild: HasOneChild? = null
CoroutineScope(Dispatchers.IO).launch {
subscription.collect {
assertEquals(parent.id, it.data.id)
capturedParent = it.data
capturedChild = (it.data.child as LazyModelReference).fetchModel()!!
latch.countDown()
val returnedParent = it.data
if (modelCreatedByDevice(returnedParent.id)) {
assertEquals(parent.id, returnedParent.id)
capturedParent = returnedParent
capturedChild = (returnedParent.child as LazyModelReference).fetchModel()!!
latch.countDown()
}
}
}

Expand Down Expand Up @@ -210,7 +247,10 @@ class GraphQLLazySubscribeInstrumentationTest {
val hasOneChild2 = HasOneChild.builder()
.content("Child2")
.build()
val parent = Parent.builder().parentChildId(hasOneChild.id).build()
val parent = Parent.builder()
.id(createRandomIdWithRunIdPrefix())
.parentChildId(hasOneChild.id)
.build()

val request = ModelSubscription.onUpdate<Parent, ParentPath>(Parent::class.java) {
includes(it.child)
Expand All @@ -221,12 +261,14 @@ class GraphQLLazySubscribeInstrumentationTest {
var capturedParent: Parent? = null
var capturedChild: HasOneChild? = null
CoroutineScope(Dispatchers.IO).launch {

subscription.collect {
assertEquals(parent.id, it.data.id)
capturedParent = it.data
capturedChild = (it.data.child as LoadedModelReference).value
latch.countDown()
val returnedParent = it.data
if (modelCreatedByDevice(returnedParent.id)) {
assertEquals(parent.id, returnedParent.id)
capturedParent = returnedParent
capturedChild = (returnedParent.child as LoadedModelReference).value
latch.countDown()
}
}
}

Expand Down Expand Up @@ -264,7 +306,10 @@ class GraphQLLazySubscribeInstrumentationTest {
val hasOneChild = HasOneChild.builder()
.content("Child1")
.build()
val parent = Parent.builder().parentChildId(hasOneChild.id).build()
val parent = Parent.builder()
.id(createRandomIdWithRunIdPrefix())
.parentChildId(hasOneChild.id)
.build()

val subscription = Amplify.API.subscribe(ModelSubscription.onDelete(Parent::class.java))

Expand All @@ -273,10 +318,13 @@ class GraphQLLazySubscribeInstrumentationTest {
var capturedChild: HasOneChild? = null
CoroutineScope(Dispatchers.IO).launch {
subscription.collect {
assertEquals(parent.id, it.data.id)
capturedParent = it.data
capturedChild = (it.data.child as LazyModelReference).fetchModel()!!
latch.countDown()
val returnedParent = it.data
if (modelCreatedByDevice(returnedParent.id)) {
assertEquals(parent.id, returnedParent.id)
capturedParent = returnedParent
capturedChild = (returnedParent.child as LazyModelReference).fetchModel()!!
latch.countDown()
}
}
}

Expand Down Expand Up @@ -307,7 +355,10 @@ class GraphQLLazySubscribeInstrumentationTest {
val hasOneChild = HasOneChild.builder()
.content("Child1")
.build()
val parent = Parent.builder().parentChildId(hasOneChild.id).build()
val parent = Parent.builder()
.id(createRandomIdWithRunIdPrefix())
.parentChildId(hasOneChild.id)
.build()

val request = ModelSubscription.onDelete<Parent, ParentPath>(Parent::class.java) {
includes(it.child)
Expand All @@ -319,10 +370,13 @@ class GraphQLLazySubscribeInstrumentationTest {
var capturedChild: HasOneChild? = null
CoroutineScope(Dispatchers.IO).launch {
subscription.collect {
assertEquals(parent.id, it.data.id)
capturedParent = it.data
capturedChild = (it.data.child as LoadedModelReference).value
latch.countDown()
val returnedParent = it.data
if (modelCreatedByDevice(returnedParent.id)) {
assertEquals(parent.id, returnedParent.id)
capturedParent = returnedParent
capturedChild = (returnedParent.child as LoadedModelReference).value
latch.countDown()
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class StorageCanaryTest {
fun uploadFile() {
val latch = CountDownLatch(1)
val file = createFile(1)
val fileKey = "ExampleKey"
val fileKey = UUID.randomUUID().toString()
Amplify.Storage.uploadFile(
fileKey,
file,
Expand Down
Loading