@@ -26,13 +26,15 @@ import com.amplifyframework.core.Amplify
2626import com.amplifyframework.core.InitializationStatus
2727import com.amplifyframework.hub.HubChannel
2828import com.amplifyframework.testutils.HubAccumulator
29+ import com.amplifyframework.testutils.await
2930import java.util.concurrent.CountDownLatch
3031import java.util.concurrent.TimeUnit
31- import org.junit.After
32+ import kotlin.time.Duration.Companion.seconds
3233import org.junit.Assert.assertFalse
3334import org.junit.Assert.assertNotNull
3435import org.junit.Assert.assertNull
3536import org.junit.Assert.assertTrue
37+ import org.junit.Before
3638import org.junit.BeforeClass
3739import org.junit.Test
3840import 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}
0 commit comments