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(auth): Signout before tests start #2925

Merged
merged 4 commits into from
Sep 23, 2024
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 @@ -26,13 +26,15 @@ import com.amplifyframework.core.Amplify
import com.amplifyframework.core.InitializationStatus
import com.amplifyframework.hub.HubChannel
import com.amplifyframework.testutils.HubAccumulator
import com.amplifyframework.testutils.await
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import org.junit.After
import kotlin.time.Duration.Companion.seconds
import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.BeforeClass
import org.junit.Test
import org.junit.runner.RunWith
Expand Down Expand Up @@ -65,63 +67,51 @@ class AWSCognitoAuthPluginInstrumentationTests {
}
}

@After
fun tearDown() {
@Before
fun setup() {
signOut()
Thread.sleep(1000) // ensure signout has time to complete
}

@Test
fun signed_In_Hub_Event_Is_Published_When_Signed_In() {
val hubAccumulator = HubAccumulator.create(HubChannel.AUTH, AuthChannelEventName.SIGNED_IN, 1).start()

signInWithCognito()

hubAccumulator.await(10, TimeUnit.SECONDS)
// if we made it this far without timeout, it means hub event was received
withHubAccumulator(AuthChannelEventName.SIGNED_IN) { hubAccumulator ->
signInWithCognito()
hubAccumulator.await(10.seconds)
}
}

@Test
fun signed_Out_Hub_Event_Is_Published_When_Signed_Out() {
val hubAccumulator = HubAccumulator.create(HubChannel.AUTH, AuthChannelEventName.SIGNED_OUT, 1).start()

signInWithCognito()
signOut()

hubAccumulator.await(10, TimeUnit.SECONDS)
// if we made it this far without timeout, it means hub event was received
withHubAccumulator(AuthChannelEventName.SIGNED_OUT) { hubAccumulator ->
signInWithCognito()
signOut()
hubAccumulator.await(10.seconds)
}
}

@Test
fun hub_Events_Are_Received_Only_Once_Per_Change() {
val signInAccumulator = HubAccumulator
.create(HubChannel.AUTH, AuthChannelEventName.SIGNED_IN, 2)
.start()
val signOutAccumulator = HubAccumulator
.create(HubChannel.AUTH, AuthChannelEventName.SIGNED_OUT, 1)
.start()

signInWithCognito()
signOut()
signInWithCognito()

signInAccumulator.await(10, TimeUnit.SECONDS)
signOutAccumulator.await(10, TimeUnit.SECONDS)
// if we made it this far without timeout, it means hub event was received
withHubAccumulator(AuthChannelEventName.SIGNED_IN, quantity = 2) { signInAccumulator ->
withHubAccumulator(AuthChannelEventName.SIGNED_OUT) { signOutAccumulator ->
signInWithCognito()
signOut()
signInWithCognito()
signInAccumulator.await(10.seconds)
signOutAccumulator.await(10.seconds)
}
}
}

// This compliments the hub_Events_Are_Received_Only_Once_Per_Change test
@Test(expected = RuntimeException::class)
fun hub_Events_Are_Received_Only_Once_Per_Change_2() {
val signInAccumulatorExtra = HubAccumulator
.create(HubChannel.AUTH, AuthChannelEventName.SIGNED_IN, 3)
.start()

signInWithCognito()
signOut()
signInWithCognito()

signInAccumulatorExtra.await(10, TimeUnit.SECONDS)
// Execution should not reach here
withHubAccumulator(AuthChannelEventName.SIGNED_IN, quantity = 3) { signInAccumulatorExtra ->
signInWithCognito()
signOut()
signInWithCognito()
signInAccumulatorExtra.await(2.seconds)
}
}

@Test
Expand All @@ -140,7 +130,7 @@ class AWSCognitoAuthPluginInstrumentationTests {
latch.countDown()
}
)
latch.await(10, TimeUnit.SECONDS)
latch.await(10.seconds)

assertTrue(session.isSignedIn)
with(session as AWSCognitoAuthSession) {
Expand Down Expand Up @@ -168,7 +158,7 @@ class AWSCognitoAuthPluginInstrumentationTests {
latch.countDown()
}
)
latch.await(10, TimeUnit.SECONDS)
latch.await(10.seconds)

assertFalse(session.isSignedIn)
with(session as AWSCognitoAuthSession) {
Expand All @@ -195,7 +185,7 @@ class AWSCognitoAuthPluginInstrumentationTests {
}
)

rememberLatch.await(10, TimeUnit.SECONDS)
rememberLatch.await(10.seconds)

val forgetLatch = CountDownLatch(1)

Expand All @@ -209,7 +199,7 @@ class AWSCognitoAuthPluginInstrumentationTests {
}
)

forgetLatch.await(10, TimeUnit.SECONDS)
forgetLatch.await(10.seconds)

signOut()
signInWithCognito()
Expand All @@ -226,7 +216,7 @@ class AWSCognitoAuthPluginInstrumentationTests {
}
)

rememberLatch2.await(10, TimeUnit.SECONDS)
rememberLatch2.await(10.seconds)

val forgetLatch2 = CountDownLatch(1)

Expand All @@ -240,7 +230,7 @@ class AWSCognitoAuthPluginInstrumentationTests {
}
)

forgetLatch2.await(10, TimeUnit.SECONDS)
forgetLatch2.await(10.seconds)
}

private fun signInWithCognito(synchronous: Boolean = true) {
Expand All @@ -258,4 +248,18 @@ class AWSCognitoAuthPluginInstrumentationTests {
auth.signOut { latch.countDown() }
if (synchronous) latch.await()
}

// Creates and starts a HubAccumulator, runs the supplied block, and then stops the accumulator
private fun withHubAccumulator(
eventName: AuthChannelEventName,
quantity: Int = 1,
block: (HubAccumulator) -> Unit
) {
val accumulator = HubAccumulator.create(HubChannel.AUTH, eventName, quantity).start()
try {
block(accumulator)
} finally {
accumulator.stop()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public HubAccumulator start() {
events.add(event);
latch.countDown();
if (latch.getCount() == 0) {
Amplify.Hub.unsubscribe(this.token.get());
stop();
}
}
}
Expand Down Expand Up @@ -196,4 +196,11 @@ public HubEvent<?> awaitFirst(int amount, TimeUnit unit) {
}
return events.isEmpty() ? null : events.get(0);
}

/**
* Unsubscribe from the Hub.
*/
public void stop() {
Amplify.Hub.unsubscribe(this.token.get());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package com.amplifyframework.testutils

import com.amplifyframework.hub.HubEvent
import java.util.concurrent.TimeUnit
import kotlin.time.Duration
import kotlin.time.Duration.Companion.seconds

/**
* Await using a [Duration]
*/
fun HubAccumulator.await(timeout: Duration = 2.seconds): List<HubEvent<*>> =
await(timeout.inWholeMilliseconds.toInt(), TimeUnit.MILLISECONDS)
Loading