@@ -222,30 +222,38 @@ class RealLocalizationProviderTests: XCTestCase {
222222 }
223223
224224 func testRaceCondition( ) {
225+ let expectation = self . expectation ( description: " Race condition test finished " )
226+
225227 let localization = " en "
226228 let localStorage = LocalLocalizationStorage ( localization: localization)
227229 let remoteStorage = CrowdinRemoteLocalizationStorage ( localization: localization, config: crowdinProviderConfig)
228230 localizationProvider = LocalizationProvider ( localization: localization, localStorage: localStorage, remoteStorage: remoteStorage)
229231
230232 remoteStorage. prepare {
231- let expectation = self . expectation ( description: " Race condition test finished " )
232-
233233 let dispatchGroup = DispatchGroup ( )
234234
235- // Simulate background updates
236- for _ in 0 ..< 100 {
235+ // Reduce the number of concurrent operations to avoid overwhelming the system
236+ // This is more realistic - apps don't typically have 100s of simultaneous refresh calls
237+ let refreshCount = 10
238+ let accessCount = 20
239+
240+ // Add a small delay between operations to make the test more stable
241+ let delayBetweenOperations : TimeInterval = 0.01
242+
243+ // Simulate background updates with controlled timing
244+ for i in 0 ..< refreshCount {
237245 dispatchGroup. enter ( )
238- DispatchQueue . global ( ) . async {
246+ DispatchQueue . global ( ) . asyncAfter ( deadline : . now ( ) + Double ( i ) * delayBetweenOperations ) {
239247 self . localizationProvider. refreshLocalization { _ in
240248 dispatchGroup. leave ( )
241249 }
242250 }
243251 }
244252
245- // Simulate main thread access
246- for i in 0 ..< 100 {
253+ // Simulate main thread access with controlled timing
254+ for i in 0 ..< accessCount {
247255 dispatchGroup. enter ( )
248- DispatchQueue . main. async {
256+ DispatchQueue . main. asyncAfter ( deadline : . now ( ) + Double ( i ) * delayBetweenOperations ) {
249257 _ = self . localizationProvider. key ( for: " some string \( i) " )
250258 dispatchGroup. leave ( )
251259 }
@@ -254,8 +262,8 @@ class RealLocalizationProviderTests: XCTestCase {
254262 dispatchGroup. notify ( queue: . main) {
255263 expectation. fulfill ( )
256264 }
257-
258- self . wait ( for: [ expectation] , timeout: 60.0 )
259265 }
266+
267+ wait ( for: [ expectation] , timeout: 30.0 )
260268 }
261269}
0 commit comments