11import Foundation
2+ import Combine
23import Testing
34import WordPressData
45import WordPressAPI
@@ -168,15 +169,16 @@ class ApplicationPasswordsRepositoryTests {
168169 let host = " \( uuid) .example.com "
169170 let blog = try await createSelfHostedSite ( host: host)
170171
171- let monitor = Monitor ( delay: 0.1 )
172+ let monitor = Monitor ( delay: 0.5 )
172173 stubApiDiscovery ( siteHost: host)
173174 stubSelfHostedSiteWpV2GetUser ( )
174175 stubSelfHostedSiteCreateApplicationPassword ( host: host, password: uuid, monitor: monitor)
175176
176177 let repository = ApplicationPasswordRepository . forTesting ( coreDataStack: coreDataStack, keychain: keychain)
177178
178179 let first = Task { try await repository. createPasswordIfNeeded ( for: blog) }
179- try await Task . sleep ( for: . milliseconds( 10 ) )
180+ await monitor. hasReceivedRequest ( )
181+
180182 let second = Task { try await repository. createPasswordIfNeeded ( for: blog) }
181183
182184 first. cancel ( )
@@ -193,15 +195,15 @@ class ApplicationPasswordsRepositoryTests {
193195 #expect( password == uuid)
194196 }
195197
196- @Test ( arguments: [ 0 , 1 , 2 , 3 , 4 ] )
198+ @Test ( arguments: [ 1 , 2 , 3 , 4 ] )
197199 func cancelConcurrentCall( nthTaskToBeCancelled: Int ) async throws {
198200 defer { HTTPStubs . removeAllStubs ( ) }
199201
200202 let uuid = UUID ( ) . uuidString. lowercased ( )
201203 let host = " \( uuid) .example.com "
202204 let blog = try await createSelfHostedSite ( host: host)
203205
204- let monitor = Monitor ( delay: 0.1 )
206+ let monitor = Monitor ( delay: 0.5 )
205207 stubApiDiscovery ( siteHost: host)
206208 stubSelfHostedSiteWpV2GetUser ( )
207209 stubSelfHostedSiteCreateApplicationPassword ( host: host, password: uuid, monitor: monitor)
@@ -210,11 +212,21 @@ class ApplicationPasswordsRepositoryTests {
210212
211213 let numberOfTasks = 5
212214 var tasks : [ Task < Void , Error > ] = [ ]
215+ // Start a few tasks and ensure they all have started at the end of the for-loop
213216 for _ in 0 ..< numberOfTasks {
214- tasks. append ( Task { try await repository. createPasswordIfNeeded ( for: blog) } )
217+ let started = CurrentValueSubject < _ , Never > ( false )
218+ let task = Task {
219+ started. value = true
220+ try await repository. createPasswordIfNeeded ( for: blog)
221+ }
222+ tasks. append ( task)
223+
224+ _ = await started. values. first ( where: { $0 } )
215225 try await Task . sleep ( for: . milliseconds( 10 ) )
216226 }
217227
228+ _ = await monitor. hasReceivedRequest ( )
229+
218230 tasks [ nthTaskToBeCancelled] . cancel ( )
219231
220232 for (index, task) in tasks. enumerated ( ) {
@@ -559,7 +571,11 @@ private extension ApplicationPasswordsRepositoryTests {
559571
560572private class Monitor {
561573 let delay : TimeInterval ?
562- private( set) var numberOfRequests : Int = 0
574+ let numberOfRequestsSubject = CurrentValueSubject < _ , Never > ( 0 )
575+
576+ var numberOfRequests : Int {
577+ numberOfRequestsSubject. value
578+ }
563579
564580 private let lock = NSLock ( )
565581
@@ -571,7 +587,11 @@ private class Monitor {
571587 lock. lock ( )
572588 defer { lock. unlock ( ) }
573589
574- numberOfRequests += 1
590+ numberOfRequestsSubject. value += 1
591+ }
592+
593+ func hasReceivedRequest( ) async {
594+ _ = await numberOfRequestsSubject. values. first ( where: { $0 > 0 } )
575595 }
576596}
577597
0 commit comments