Skip to content

Commit 7d2a98f

Browse files
authored
chore(auth): Signout before tests start (#2925)
1 parent 14c71f3 commit 7d2a98f

File tree

3 files changed

+85
-47
lines changed

3 files changed

+85
-47
lines changed

aws-auth-cognito/src/androidTest/java/com/amplifyframework/auth/cognito/AWSCognitoAuthPluginInstrumentationTests.kt

Lines changed: 50 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,15 @@ import com.amplifyframework.core.Amplify
2626
import com.amplifyframework.core.InitializationStatus
2727
import com.amplifyframework.hub.HubChannel
2828
import com.amplifyframework.testutils.HubAccumulator
29+
import com.amplifyframework.testutils.await
2930
import java.util.concurrent.CountDownLatch
3031
import java.util.concurrent.TimeUnit
31-
import org.junit.After
32+
import kotlin.time.Duration.Companion.seconds
3233
import org.junit.Assert.assertFalse
3334
import org.junit.Assert.assertNotNull
3435
import org.junit.Assert.assertNull
3536
import org.junit.Assert.assertTrue
37+
import org.junit.Before
3638
import org.junit.BeforeClass
3739
import org.junit.Test
3840
import org.junit.runner.RunWith
@@ -65,63 +67,51 @@ class AWSCognitoAuthPluginInstrumentationTests {
6567
}
6668
}
6769

68-
@After
69-
fun tearDown() {
70+
@Before
71+
fun setup() {
7072
signOut()
73+
Thread.sleep(1000) // ensure signout has time to complete
7174
}
7275

7376
@Test
7477
fun signed_In_Hub_Event_Is_Published_When_Signed_In() {
75-
val hubAccumulator = HubAccumulator.create(HubChannel.AUTH, AuthChannelEventName.SIGNED_IN, 1).start()
76-
77-
signInWithCognito()
78-
79-
hubAccumulator.await(10, TimeUnit.SECONDS)
80-
// if we made it this far without timeout, it means hub event was received
78+
withHubAccumulator(AuthChannelEventName.SIGNED_IN) { hubAccumulator ->
79+
signInWithCognito()
80+
hubAccumulator.await(10.seconds)
81+
}
8182
}
8283

8384
@Test
8485
fun signed_Out_Hub_Event_Is_Published_When_Signed_Out() {
85-
val hubAccumulator = HubAccumulator.create(HubChannel.AUTH, AuthChannelEventName.SIGNED_OUT, 1).start()
86-
87-
signInWithCognito()
88-
signOut()
89-
90-
hubAccumulator.await(10, TimeUnit.SECONDS)
91-
// if we made it this far without timeout, it means hub event was received
86+
withHubAccumulator(AuthChannelEventName.SIGNED_OUT) { hubAccumulator ->
87+
signInWithCognito()
88+
signOut()
89+
hubAccumulator.await(10.seconds)
90+
}
9291
}
9392

9493
@Test
9594
fun hub_Events_Are_Received_Only_Once_Per_Change() {
96-
val signInAccumulator = HubAccumulator
97-
.create(HubChannel.AUTH, AuthChannelEventName.SIGNED_IN, 2)
98-
.start()
99-
val signOutAccumulator = HubAccumulator
100-
.create(HubChannel.AUTH, AuthChannelEventName.SIGNED_OUT, 1)
101-
.start()
102-
103-
signInWithCognito()
104-
signOut()
105-
signInWithCognito()
106-
107-
signInAccumulator.await(10, TimeUnit.SECONDS)
108-
signOutAccumulator.await(10, TimeUnit.SECONDS)
109-
// if we made it this far without timeout, it means hub event was received
95+
withHubAccumulator(AuthChannelEventName.SIGNED_IN, quantity = 2) { signInAccumulator ->
96+
withHubAccumulator(AuthChannelEventName.SIGNED_OUT) { signOutAccumulator ->
97+
signInWithCognito()
98+
signOut()
99+
signInWithCognito()
100+
signInAccumulator.await(10.seconds)
101+
signOutAccumulator.await(10.seconds)
102+
}
103+
}
110104
}
111105

112106
// This compliments the hub_Events_Are_Received_Only_Once_Per_Change test
113107
@Test(expected = RuntimeException::class)
114108
fun hub_Events_Are_Received_Only_Once_Per_Change_2() {
115-
val signInAccumulatorExtra = HubAccumulator
116-
.create(HubChannel.AUTH, AuthChannelEventName.SIGNED_IN, 3)
117-
.start()
118-
119-
signInWithCognito()
120-
signOut()
121-
signInWithCognito()
122-
123-
signInAccumulatorExtra.await(10, TimeUnit.SECONDS)
124-
// Execution should not reach here
109+
withHubAccumulator(AuthChannelEventName.SIGNED_IN, quantity = 3) { signInAccumulatorExtra ->
110+
signInWithCognito()
111+
signOut()
112+
signInWithCognito()
113+
signInAccumulatorExtra.await(2.seconds)
114+
}
125115
}
126116

127117
@Test
@@ -140,7 +130,7 @@ class AWSCognitoAuthPluginInstrumentationTests {
140130
latch.countDown()
141131
}
142132
)
143-
latch.await(10, TimeUnit.SECONDS)
133+
latch.await(10.seconds)
144134

145135
assertTrue(session.isSignedIn)
146136
with(session as AWSCognitoAuthSession) {
@@ -168,7 +158,7 @@ class AWSCognitoAuthPluginInstrumentationTests {
168158
latch.countDown()
169159
}
170160
)
171-
latch.await(10, TimeUnit.SECONDS)
161+
latch.await(10.seconds)
172162

173163
assertFalse(session.isSignedIn)
174164
with(session as AWSCognitoAuthSession) {
@@ -195,7 +185,7 @@ class AWSCognitoAuthPluginInstrumentationTests {
195185
}
196186
)
197187

198-
rememberLatch.await(10, TimeUnit.SECONDS)
188+
rememberLatch.await(10.seconds)
199189

200190
val forgetLatch = CountDownLatch(1)
201191

@@ -209,7 +199,7 @@ class AWSCognitoAuthPluginInstrumentationTests {
209199
}
210200
)
211201

212-
forgetLatch.await(10, TimeUnit.SECONDS)
202+
forgetLatch.await(10.seconds)
213203

214204
signOut()
215205
signInWithCognito()
@@ -226,7 +216,7 @@ class AWSCognitoAuthPluginInstrumentationTests {
226216
}
227217
)
228218

229-
rememberLatch2.await(10, TimeUnit.SECONDS)
219+
rememberLatch2.await(10.seconds)
230220

231221
val forgetLatch2 = CountDownLatch(1)
232222

@@ -240,7 +230,7 @@ class AWSCognitoAuthPluginInstrumentationTests {
240230
}
241231
)
242232

243-
forgetLatch2.await(10, TimeUnit.SECONDS)
233+
forgetLatch2.await(10.seconds)
244234
}
245235

246236
private fun signInWithCognito(synchronous: Boolean = true) {
@@ -258,4 +248,18 @@ class AWSCognitoAuthPluginInstrumentationTests {
258248
auth.signOut { latch.countDown() }
259249
if (synchronous) latch.await()
260250
}
251+
252+
// Creates and starts a HubAccumulator, runs the supplied block, and then stops the accumulator
253+
private fun withHubAccumulator(
254+
eventName: AuthChannelEventName,
255+
quantity: Int = 1,
256+
block: (HubAccumulator) -> Unit
257+
) {
258+
val accumulator = HubAccumulator.create(HubChannel.AUTH, eventName, quantity).start()
259+
try {
260+
block(accumulator)
261+
} finally {
262+
accumulator.stop()
263+
}
264+
}
261265
}

testutils/src/main/java/com/amplifyframework/testutils/HubAccumulator.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public HubAccumulator start() {
123123
events.add(event);
124124
latch.countDown();
125125
if (latch.getCount() == 0) {
126-
Amplify.Hub.unsubscribe(this.token.get());
126+
stop();
127127
}
128128
}
129129
}
@@ -196,4 +196,11 @@ public HubEvent<?> awaitFirst(int amount, TimeUnit unit) {
196196
}
197197
return events.isEmpty() ? null : events.get(0);
198198
}
199+
200+
/**
201+
* Unsubscribe from the Hub.
202+
*/
203+
public void stop() {
204+
Amplify.Hub.unsubscribe(this.token.get());
205+
}
199206
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package com.amplifyframework.testutils
17+
18+
import com.amplifyframework.hub.HubEvent
19+
import java.util.concurrent.TimeUnit
20+
import kotlin.time.Duration
21+
import kotlin.time.Duration.Companion.seconds
22+
23+
/**
24+
* Await using a [Duration]
25+
*/
26+
fun HubAccumulator.await(timeout: Duration = 2.seconds): List<HubEvent<*>> =
27+
await(timeout.inWholeMilliseconds.toInt(), TimeUnit.MILLISECONDS)

0 commit comments

Comments
 (0)