@@ -95,59 +95,39 @@ internal final class ObservableTests: XCTestCase {
9595
9696 // MARK: Pagination
9797
98- // swiftlint:disable line_length
9998 /// Test a paginated fetch request.
10099 func testPagination( ) {
101- let languages = [ " en " , " it " , " de " , " fr " ]
102- let expectations = [ languages. map ( XCTestExpectation . init) ,
103- [ XCTestExpectation ( description: " delay " ) , XCTestExpectation ( description: " completion " ) ] ]
104- . reduce ( into: [ ] ) { $0. append ( contentsOf: $1) }
105- let offset = Reference ( 0 )
106- let delayed = Reference ( false )
107- // Prepare the provider.
108- PagerProvider { pages in
109- // Actually paginate futures.
110- Pager ( pages) { offset in
111- LockProvider < String , LockProvider < Int , LockProvider < Int , LockProvider < Int , LockProvider < Int , AnyPublisher < Int , Never > > > > > > { value, _, _, _, _ in
112- Just ( value) . map { _ in offset } . eraseToAnyPublisher ( )
113- }
114- . unlock ( with: languages [ offset] )
115- . unlock ( with: 0 )
116- . unlock ( with: 0 )
117- . unlock ( with: 0 )
118- . unlock ( with: 0 )
119- . assertBackgroundThread ( )
120- . iterateFirst ( stoppingAt: offset) {
121- XCTAssert ( !Thread. isMainThread)
122- return . load( $0 + 1 )
100+ // swiftlint:disable line_length
101+ typealias Lock = LockProvider < Int , LockProvider < Void , LockProvider < Void , LockProvider < Void , LockProvider < Void , Just < Int > > > > > >
102+ // swiftlint:enable line_length
103+
104+ let date = Date ( )
105+ let expectations = ( ( 0 ... 4 ) . map ( String . init) + [ " completion " ] ) . map ( XCTestExpectation . init)
106+ PagerProvider {
107+ Pager ( $0) { offset in
108+ Lock { value, _, _, _, _ in
109+ Just ( value)
123110 }
111+ . unlock ( with: offset)
112+ . unlock ( )
113+ . unlock ( )
114+ . unlock ( )
115+ . unlock ( )
116+ . iterateFirst { . load( $0 + 1 ) }
124117 }
125118 }
126- . pages ( languages. count, offset: 0 , delay: . seconds( 2 ) )
127- . subscribe ( on: DispatchQueue . global ( qos: . userInitiated) )
128- . receive ( on: RunLoop . main)
129- . assertMainThread ( )
130- . sink (
131- receiveCompletion: {
132- if case . failure( let error) = $0 { XCTFail ( error. localizedDescription) }
133- XCTAssert ( delayed. value, " Pagination did not wait. " )
134- expectations. last? . fulfill ( )
135- } ,
136- receiveValue: {
137- XCTAssert ( offset. value == $0)
138- offset. value = $0 + 1
139- expectations [ $0] . fulfill ( )
140- }
141- )
142- . store ( in: & bin)
143- // Make sure you delay before it's completed.
144- DispatchQueue . main. asyncAfter ( deadline: . now( ) + 3 ) {
145- delayed. value = true
146- expectations [ 4 ] . fulfill ( )
119+ . pages ( 5 , offset: 0 , delay: . seconds( 1 ) )
120+ . sink {
121+ if case . failure( let error) = $0 { XCTFail ( error. localizedDescription) }
122+ expectations. last? . fulfill ( )
123+ } receiveValue: {
124+ expectations [ $0] . fulfill ( )
147125 }
148- wait ( for: expectations, timeout: 30 )
126+ . store ( in: & bin)
127+
128+ wait ( for: expectations, timeout: 10 )
129+ XCTAssertLessThan ( date. timeIntervalSinceNow, - 4 )
149130 }
150- // swiftlint:enable line_length
151131
152132 /// Test a pagination request using a ranked offset.
153133 func testRankedOffsetPagination( ) {
@@ -159,55 +139,19 @@ internal final class ObservableTests: XCTestCase {
159139 Pager ( pages) { Just ( pages. rank [ $0] ) . iterateFirst { . load( $0 + 1 ) } }
160140 }
161141 . pages ( values. count, offset: 0 , rank: values)
162- . sink (
163- receiveCompletion: {
164- if case . failure( let error) = $0 { XCTFail ( error. localizedDescription) }
165- expectations. last? . fulfill ( )
166- } ,
167- receiveValue: { value in
168- offset. sync {
169- XCTAssert ( value == $0)
170- expectations [ value] . fulfill ( )
171- $0 = value + 1
172- }
173- }
174- )
175- . store ( in: & bin)
176- wait ( for: expectations, timeout: 30 )
177- }
178-
179- /// Test a remote paginated fetch request.
180- func testRemotePagination( ) {
181- let languages = [ " en " , " it " , " de " , " fr " ]
182- let expectations = languages. map ( XCTestExpectation . init) + [ XCTestExpectation ( description: " completion " ) ]
183- let offset = Reference ( 0 )
184- // Prepare the provider.
185- LockSessionPagerProvider { url, session, pages in // Additional tests.
186- // Actually paginate futures.
187- Pager ( pages) { offset in
188- Request ( url)
189- . query ( appending: languages [ offset] , forKey: " l " )
190- . publish ( with: session)
191- . map { _ in offset }
192- . iterateLast { . load( ( $0 ?? - 1 ) + 1 ) }
193- }
142+ . sink {
143+ if case . failure( let error) = $0 { XCTFail ( error. localizedDescription) }
144+ expectations. last? . fulfill ( )
194145 }
195- . unlock ( with: url)
196- . session ( . shared)
197- . pages ( languages. count, offset: 0 )
198- . sink (
199- receiveCompletion: {
200- if case . failure( let error) = $0 { XCTFail ( error. localizedDescription) }
201- expectations. last? . fulfill ( )
202- } ,
203- receiveValue: {
204- XCTAssert ( offset. value == $0)
205- offset. value = $0 + 1
206- expectations [ $0] . fulfill ( )
146+ receiveValue: { value in
147+ offset. sync {
148+ XCTAssert ( value == $0)
149+ expectations [ value] . fulfill ( )
150+ $0 = value + 1
207151 }
208- )
152+ }
209153 . store ( in: & bin)
210- wait ( for: expectations, timeout: 30 * TimeInterval ( languages . count ) )
154+ wait ( for: expectations, timeout: 30 )
211155 }
212156
213157 // MARK: Cancellation
@@ -230,28 +174,4 @@ internal final class ObservableTests: XCTestCase {
230174 }
231175 wait ( for: expectations, timeout: 5 )
232176 }
233-
234- /// Test paginated cancellation.
235- func testPaginatedCancellation( ) {
236- let expectations = [ " output " , " completion " ] . map ( XCTestExpectation . init)
237- expectations [ 0 ] . assertForOverFulfill = true
238- expectations [ 0 ] . expectedFulfillmentCount = 1
239- Pager ( 3 ) {
240- Request ( url)
241- . publish ( session: . shared)
242- . map ( \. response)
243- }
244- . sink (
245- receiveCompletion: { _ in
246- XCTFail ( " This should not complete " )
247- } ,
248- receiveValue: { _ in
249- expectations. first? . fulfill ( )
250- self . bin. removeAll ( )
251- }
252- )
253- . store ( in: & bin)
254- DispatchQueue . main. asyncAfter ( deadline: . now( ) + 25 ) { expectations. last? . fulfill ( ) }
255- wait ( for: expectations, timeout: 30 )
256- }
257177}
0 commit comments