@@ -17,6 +17,9 @@ class UnleashThreadSafetyTest: XCTestCase {
1717 // This test demonstrates the thread safety of UnleashClient
1818 // by simulating concurrent access from multiple threads to the same client instance
1919
20+ // Create an expectation to ensure the test doesn't hang
21+ let expectation = XCTestExpectation ( description: " Thread safety test completed " )
22+
2023 // Create a shared client instance
2124 let client = UnleashClientBase (
2225 unleashUrl: " https://sandbox.getunleash.io/enterprise/api/frontend " ,
@@ -28,7 +31,7 @@ class UnleashThreadSafetyTest: XCTestCase {
2831 client. start ( )
2932
3033 // Run the test with high iteration count to increase chances of detecting race conditions
31- for _ in 0 ..< 100 {
34+ for _ in 0 ..< 10000 {
3235 // Update context with userId and isAuthenticated properties
3336 client. updateContext ( context: [ " userId " : " 1 " ] , properties: [ " isAuthenticated " : " true " ] ) { _ in }
3437
@@ -48,10 +51,19 @@ class UnleashThreadSafetyTest: XCTestCase {
4851 }
4952 }
5053
51- // Give background operations a chance to complete
52- Thread . sleep ( forTimeInterval: 2.0 )
54+ // Use a background queue to avoid blocking the main thread
55+ DispatchQueue . global ( ) . async {
56+ // Give background operations a chance to complete
57+ Thread . sleep ( forTimeInterval: 2.0 )
58+
59+ // Clean up
60+ client. stop ( )
61+
62+ // Mark the expectation as fulfilled
63+ expectation. fulfill ( )
64+ }
5365
54- // Clean up
55- client . stop ( )
66+ // Wait for the expectation to be fulfilled with a timeout
67+ wait ( for : [ expectation ] , timeout : 10.0 ) // 10 second timeout
5668 }
5769}
0 commit comments