Skip to content

Commit 00adb06

Browse files
authored
fix(test): data race on reseting in test case tearDown (#2710)
* fix(test): data race on reseting in tear down * fix(test): fix flaky auth test cases
1 parent 8e6f0a2 commit 00adb06

File tree

5 files changed

+34
-6
lines changed

5 files changed

+34
-6
lines changed

AmplifyPlugins/API/AWSAPICategoryPluginTests/AWSAPICategoryPluginTestBase.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class AWSAPICategoryPluginTestBase: XCTestCase {
3131

3232
override func setUp() {
3333
Amplify.reset()
34+
wait(for: 1)
3435
apiPlugin = AWSAPIPlugin()
3536

3637
let authService = MockAWSAuthService()
@@ -71,7 +72,12 @@ class AWSAPICategoryPluginTestBase: XCTestCase {
7172
}
7273
}
7374

74-
override func tearDown() {
75-
Amplify.reset()
75+
private func wait(for duration: TimeInterval) {
76+
let expectation = expectation(description: "Sleep for \(duration) seconds")
77+
DispatchQueue.global(qos: .userInitiated).asyncAfter(deadline: DispatchTime.now() + duration) {
78+
expectation.fulfill()
79+
}
80+
81+
wait(for: [expectation], timeout: duration + 0.5)
7682
}
7783
}

AmplifyPlugins/Auth/AWSCognitoAuthPlugin.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
5C33248D27723D0800F2C47B /* AuthenticationProviderAdapter+DeleteUser.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5C33248C27723D0800F2C47B /* AuthenticationProviderAdapter+DeleteUser.swift */; };
2727
5CD3C21A277D275D007DB926 /* AuthDeleteUserTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5CD3C219277D275D007DB926 /* AuthDeleteUserTests.swift */; };
2828
5CF59BBB277BCD5300E1912D /* AuthenticationProviderDeleteUserTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5CF59BBA277BCD5300E1912D /* AuthenticationProviderDeleteUserTests.swift */; };
29+
6043243329836376007F5C35 /* XCTestCase+Amplify.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6043243229836375007F5C35 /* XCTestCase+Amplify.swift */; };
2930
70E42920C1C9027C533FE097 /* Pods_HostApp.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 079163684273F9FB27298F8F /* Pods_HostApp.framework */; };
3031
7B2955116F0489917FC30BE3 /* Pods_AWSCognitoAuthPlugin.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CB0C6F042059ACEDBC3306C2 /* Pods_AWSCognitoAuthPlugin.framework */; };
3132
9B3E7ADD46949E5B8F2CF1BC /* Pods_HostApp_AWSCognitoAuthPluginIntegrationTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 20977603F769B87802DEAEB7 /* Pods_HostApp_AWSCognitoAuthPluginIntegrationTests.framework */; };
@@ -222,6 +223,7 @@
222223
5C33248C27723D0800F2C47B /* AuthenticationProviderAdapter+DeleteUser.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "AuthenticationProviderAdapter+DeleteUser.swift"; sourceTree = "<group>"; };
223224
5CD3C219277D275D007DB926 /* AuthDeleteUserTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthDeleteUserTests.swift; sourceTree = "<group>"; };
224225
5CF59BBA277BCD5300E1912D /* AuthenticationProviderDeleteUserTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthenticationProviderDeleteUserTests.swift; sourceTree = "<group>"; };
226+
6043243229836375007F5C35 /* XCTestCase+Amplify.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "XCTestCase+Amplify.swift"; sourceTree = "<group>"; };
225227
68CA57EB40AAAB3A1A6756EA /* Pods-HostApp-AWSCognitoAuthPluginIntegrationTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-HostApp-AWSCognitoAuthPluginIntegrationTests.release.xcconfig"; path = "Target Support Files/Pods-HostApp-AWSCognitoAuthPluginIntegrationTests/Pods-HostApp-AWSCognitoAuthPluginIntegrationTests.release.xcconfig"; sourceTree = "<group>"; };
226228
8EAD42CD0F2BB4A5A4B497F6 /* Pods-AWSCognitoAuthPlugin-AWSCognitoAuthPluginTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-AWSCognitoAuthPlugin-AWSCognitoAuthPluginTests.release.xcconfig"; path = "Target Support Files/Pods-AWSCognitoAuthPlugin-AWSCognitoAuthPluginTests/Pods-AWSCognitoAuthPlugin-AWSCognitoAuthPluginTests.release.xcconfig"; sourceTree = "<group>"; };
227229
B402C915257700610020B83B /* BaseAuthenticationProviderTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BaseAuthenticationProviderTest.swift; sourceTree = "<group>"; };
@@ -807,6 +809,7 @@
807809
B41D0FDC2475A3A10049D08D /* Utils */ = {
808810
isa = PBXGroup;
809811
children = (
812+
6043243229836375007F5C35 /* XCTestCase+Amplify.swift */,
810813
B41D0FDD2475A3A10049D08D /* AuthUserAttributeKeyTests.swift */,
811814
5653A9F9297B157900AC6D82 /* AWSCognitoAuthPluginUserDefaultsTests.swift */,
812815
);
@@ -1566,6 +1569,7 @@
15661569
5653A9F8297AE40200AC6D82 /* AWSCognitoAuthPluginTests.swift in Sources */,
15671570
B4D5411B256F2C8A00436E5C /* BaseAuthorizationProviderTest.swift in Sources */,
15681571
B43B4E102565EA64008F345D /* AWSCognitoAuthClientBehaviorTests.swift in Sources */,
1572+
6043243329836376007F5C35 /* XCTestCase+Amplify.swift in Sources */,
15691573
B4136E2C256D764B0011210B /* AuthenticationProviderResendSignupCodeTests.swift in Sources */,
15701574
B41D0FE32475A3A10049D08D /* AuthHubEventHandlerTests.swift in Sources */,
15711575
B4136E38256D76790011210B /* AuthenticationProviderSigninWithSocialWebUITests.swift in Sources */,

AmplifyPlugins/Auth/AWSCognitoAuthPluginTests/ClientBehaviorAPITests/AWSCognitoAuthDeviceBehaviorTests.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ class AWSCognitoAuthDeviceBehaviorTests: XCTestCase {
1414
var plugin: AWSCognitoAuthPlugin!
1515

1616
override func setUp() {
17+
Amplify.reset()
18+
wait(for: 1)
19+
1720
plugin = AWSCognitoAuthPlugin()
1821
plugin.configure(authenticationProvider: MockAuthenticationProviderBehavior(),
1922
authorizationProvider: MockAuthorizationProviderBehavior(),
@@ -22,10 +25,6 @@ class AWSCognitoAuthDeviceBehaviorTests: XCTestCase {
2225
hubEventHandler: MockAuthHubEventBehavior())
2326
}
2427

25-
override func tearDown() {
26-
Amplify.reset()
27-
}
28-
2928
/// Test fetchDevices operation can be invoked
3029
///
3130
/// - Given: Given a configured auth plugin
100 Bytes
Binary file not shown.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// Copyright Amazon.com Inc. or its affiliates.
3+
// All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
8+
import XCTest
9+
10+
extension XCTestCase {
11+
func wait(for duration: TimeInterval) {
12+
let expectation = expectation(description: "Sleep for \(duration) seconds")
13+
DispatchQueue.global(qos: .userInitiated).asyncAfter(deadline: DispatchTime.now() + duration) {
14+
expectation.fulfill()
15+
}
16+
17+
wait(for: [expectation], timeout: duration + 0.5)
18+
}
19+
}

0 commit comments

Comments
 (0)