@@ -193,9 +193,83 @@ class DatafileHandlerTests: XCTestCase {
193
193
194
194
XCTAssert ( count == 10 )
195
195
XCTAssert ( seconds == 10 )
196
+ }
197
+
198
+ func testPeriodicDownload_PollingShouldNotBeAccumulatedWhileInBackground( ) {
199
+ class FakeDatafileHandler : DefaultDatafileHandler {
200
+ let data = Data ( )
201
+ override func downloadDatafile( sdkKey: String ,
202
+ returnCacheIfNoChange: Bool ,
203
+ resourceTimeoutInterval: Double ? ,
204
+ completionHandler: @escaping DatafileDownloadCompletionHandler ) {
205
+ completionHandler ( . success( data) )
206
+ }
207
+ }
208
+
209
+ let expectation = XCTestExpectation ( description: " polling " )
210
+ let handler = FakeDatafileHandler ( )
211
+ let now = Date ( )
196
212
213
+ let updateInterval = 1
214
+ let idleTime = 5
215
+ var count = 0
216
+ var seconds = 0
217
+ handler. startPeriodicUpdates ( sdkKey: " notrealkey " , updateInterval: updateInterval) { _ in
218
+ // simulate going to background and coming back to foreground after 5secs
219
+ if count == 0 {
220
+ sleep ( UInt32 ( idleTime) )
221
+ }
222
+
223
+ count += 1
224
+
225
+ // check if delayed polling not accumulated and completed back-to-back
226
+ if count == 5 {
227
+ handler. stopPeriodicUpdates ( )
228
+ expectation. fulfill ( )
229
+ seconds = Int ( abs ( now. timeIntervalSinceNow) )
230
+ }
231
+ }
232
+
233
+ wait ( for: [ expectation] , timeout: 30 )
234
+
235
+ XCTAssert ( seconds >= idleTime + 3 ) // 3 instead of 5 for tolerating timer inaccuracy
197
236
}
198
237
238
+ func testPeriodicDownload_PollingPeriodAdjustedByDelay( ) {
239
+ class FakeDatafileHandler : DefaultDatafileHandler {
240
+ let data = Data ( )
241
+ override func downloadDatafile( sdkKey: String ,
242
+ returnCacheIfNoChange: Bool ,
243
+ resourceTimeoutInterval: Double ? ,
244
+ completionHandler: @escaping DatafileDownloadCompletionHandler ) {
245
+ sleep ( 1 )
246
+ completionHandler ( . success( data) )
247
+ }
248
+ }
249
+
250
+ let expectation = XCTestExpectation ( description: " polling " )
251
+ let handler = FakeDatafileHandler ( )
252
+ let now = Date ( )
253
+
254
+ let updateInterval = 2
255
+ let maxCount = 5
256
+ var count = 0
257
+ var seconds = 0
258
+ handler. startPeriodicUpdates ( sdkKey: " notrealkey " , updateInterval: updateInterval) { _ in
259
+ count += 1
260
+
261
+ if count == maxCount {
262
+ handler. stopPeriodicUpdates ( )
263
+ expectation. fulfill ( )
264
+ seconds = Int ( abs ( now. timeIntervalSinceNow) )
265
+ }
266
+ }
267
+
268
+ wait ( for: [ expectation] , timeout: 30 )
269
+ XCTAssert ( seconds <= updateInterval * ( maxCount + 1 ) )
270
+ }
271
+
272
+
199
273
func testPeriodicDownloadWithOptimizlyClient( ) {
200
274
class FakeDatafileHandler : DefaultDatafileHandler {
201
275
let data = OTUtils . loadJSONDatafile ( " typed_audience_datafile " )
@@ -228,7 +302,6 @@ class DatafileHandlerTests: XCTestCase {
228
302
wait ( for: [ expection] , timeout: 10 )
229
303
230
304
XCTAssert ( count == 9 )
231
-
232
305
}
233
306
234
307
func testDownloadTimeout( ) {
0 commit comments