@@ -65,87 +65,37 @@ extension FeedUIIntegrationTests {
6565
6666 // MARK: - FeedImageDataLoader
6767
68- private var imageRequests = [ (
69- url: URL ,
70- publisher: AsyncThrowingStream < Data , Error > ,
71- continuation: AsyncThrowingStream < Data , Error > . Continuation ,
72- result: AsyncResult ?
73- ) ] ( )
74-
75- enum AsyncResult {
76- case success
77- case failure
78- case cancelled
79- }
68+ private var imageLoader = EssentialAppTests . LoaderSpy < URL , Data > ( )
8069
8170 var loadedImageURLs : [ URL ] {
82- return imageRequests . map { $0. url }
71+ return imageLoader . requests . map { $0. param }
8372 }
8473
85- private( set) var cancelledImageURLs = [ URL] ( )
74+ var cancelledImageURLs : [ URL ] {
75+ return imageLoader. requests. filter ( { $0. result == . cancelled } ) . map { $0. param }
76+ }
8677
8778 private struct NoResponse : Error { }
8879 private struct Timeout : Error { }
8980
9081 func loadImageData( from url: URL ) async throws -> Data {
91- let ( stream, continuation) = AsyncThrowingStream < Data , Error > . makeStream ( )
92- let index = imageRequests. count
93- imageRequests. append ( ( url, stream, continuation, nil ) )
94-
95- do {
96- for try await result in stream {
97- try Task . checkCancellation ( )
98- imageRequests [ index] . result = . success
99- return result
100- }
101-
102- try Task . checkCancellation ( )
103-
104- throw NoResponse ( )
105- } catch {
106- if Task . isCancelled {
107- cancelledImageURLs. append ( url)
108- imageRequests [ index] . result = . cancelled
109- } else {
110- imageRequests [ index] . result = . failure
111- }
112- throw error
113- }
82+ try await imageLoader. load ( url)
11483 }
11584
11685 func completeImageLoading( with imageData: Data = Data ( ) , at index: Int = 0 ) {
117- imageRequests [ index] . continuation. yield ( imageData)
118- imageRequests [ index] . continuation. finish ( )
119-
120- while imageRequests [ index] . result == nil { RunLoop . current. run ( until: Date ( ) ) }
86+ imageLoader. complete ( with: imageData, at: index)
12187 }
12288
12389 func completeImageLoadingWithError( at index: Int = 0 ) {
124- imageRequests [ index] . continuation. finish ( throwing: anyNSError ( ) )
125-
126- while imageRequests [ index] . result == nil { RunLoop . current. run ( until: Date ( ) ) }
90+ imageLoader. fail ( with: anyNSError ( ) , at: index)
12791 }
12892
12993 func imageResult( at index: Int , timeout: TimeInterval = 1 ) async throws -> AsyncResult {
130- let maxDate = Date ( ) + timeout
131-
132- while Date ( ) <= maxDate {
133- if let result = imageRequests [ index] . result {
134- return result
135- }
136-
137- await Task . yield ( )
138- }
139-
140- throw Timeout ( )
94+ try await imageLoader. result ( at: index, timeout: timeout)
14195 }
14296
14397 func cancelPendingRequests( ) async throws {
144- for (index, request) in imageRequests. enumerated ( ) where request. result == nil {
145- request. continuation. finish ( throwing: CancellationError ( ) )
146-
147- while imageRequests [ index] . result == nil { await Task . yield ( ) }
148- }
98+ try await imageLoader. cancelPendingRequests ( )
14999 }
150100 }
151101
0 commit comments