2 * Copyright (c) 2017 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
26 #include <dispatch/dispatch.h>
27 #import <XCTest/XCTest.h>
28 #import "keychain/ckks/CKKSNearFutureScheduler.h"
29 #import "keychain/ckks/CKKSResultOperation.h"
30 #import "keychain/ckks/CKKS.h"
32 @interface CKKSNearFutureSchedulerTests : XCTestCase
33 @property NSOperationQueue* operationQueue;
36 @implementation CKKSNearFutureSchedulerTests
41 self.operationQueue = [[NSOperationQueue alloc] init];
48 #pragma mark - Block-based tests
50 - (void)testBlockOneShot {
51 XCTestExpectation *expectation = [self expectationWithDescription:@"FutureScheduler fired"];
53 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" delay:50*NSEC_PER_MSEC keepProcessAlive:true
54 dependencyDescriptionCode:CKKSResultDescriptionNone
56 [expectation fulfill];
61 [self waitForExpectationsWithTimeout:1 handler:nil];
64 - (void)testBlockOneShotDelay {
65 XCTestExpectation *toofastexpectation = [self expectationWithDescription:@"FutureScheduler fired (too soon)"];
66 toofastexpectation.inverted = YES;
68 XCTestExpectation *expectation = [self expectationWithDescription:@"FutureScheduler fired"];
70 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" delay: 200*NSEC_PER_MSEC keepProcessAlive:false
71 dependencyDescriptionCode:CKKSResultDescriptionNone
73 [toofastexpectation fulfill];
74 [expectation fulfill];
79 // Make sure it waits at least 0.1 seconds
80 [self waitForExpectations: @[toofastexpectation] timeout:0.1];
82 // But finishes within 1.1s (total)
83 [self waitForExpectations: @[expectation] timeout:1];
86 - (void)testBlockOneShotManyTrigger {
87 XCTestExpectation *toofastexpectation = [self expectationWithDescription:@"FutureScheduler fired (too soon)"];
88 toofastexpectation.inverted = YES;
90 XCTestExpectation *expectation = [self expectationWithDescription:@"FutureScheduler fired"];
91 expectation.assertForOverFulfill = YES;
93 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" delay: 200*NSEC_PER_MSEC keepProcessAlive:true
94 dependencyDescriptionCode:CKKSResultDescriptionNone
96 [toofastexpectation fulfill];
97 [expectation fulfill];
109 // Make sure it waits at least 0.1 seconds
110 [self waitForExpectations: @[toofastexpectation] timeout:0.1];
112 // But finishes within .6s (total)
113 [self waitForExpectations: @[expectation] timeout:0.5];
115 // Ensure we don't get called again in the next 0.3 s
116 XCTestExpectation* waitmore = [self expectationWithDescription:@"waiting"];
117 waitmore.inverted = YES;
118 [self waitForExpectations: @[waitmore] timeout: 0.3];
122 - (void)testBlockMultiShot {
123 XCTestExpectation *first = [self expectationWithDescription:@"FutureScheduler fired (one)"];
124 first.assertForOverFulfill = NO;
126 XCTestExpectation *second = [self expectationWithDescription:@"FutureScheduler fired (two)"];
127 second.expectedFulfillmentCount = 2;
128 second.assertForOverFulfill = YES;
130 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" delay: 100*NSEC_PER_MSEC keepProcessAlive:false
131 dependencyDescriptionCode:CKKSResultDescriptionNone
139 [self waitForExpectations: @[first] timeout:0.4];
145 [self waitForExpectations: @[second] timeout:0.4];
147 XCTestExpectation* waitmore = [self expectationWithDescription:@"waiting"];
148 waitmore.inverted = YES;
149 [self waitForExpectations: @[waitmore] timeout: 0.4];
152 - (void)testBlockMultiShotDelays {
153 XCTestExpectation *first = [self expectationWithDescription:@"FutureScheduler fired (one)"];
154 first.assertForOverFulfill = NO;
156 XCTestExpectation *longdelay = [self expectationWithDescription:@"FutureScheduler fired (long delay expectation)"];
157 longdelay.inverted = YES;
158 longdelay.expectedFulfillmentCount = 2;
160 XCTestExpectation *second = [self expectationWithDescription:@"FutureScheduler fired (two)"];
161 second.expectedFulfillmentCount = 2;
162 second.assertForOverFulfill = YES;
164 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" initialDelay: 50*NSEC_PER_MSEC continuingDelay:600*NSEC_PER_MSEC keepProcessAlive:false
165 dependencyDescriptionCode:CKKSResultDescriptionNone
174 [self waitForExpectations: @[first] timeout:0.5];
180 // longdelay should NOT be fulfilled twice in the first 0.9 seconds
181 [self waitForExpectations: @[longdelay] timeout:0.4];
183 // But second should be fulfilled in the first 1.4 seconds
184 [self waitForExpectations: @[second] timeout:0.5];
186 XCTestExpectation* waitmore = [self expectationWithDescription:@"waiting"];
187 waitmore.inverted = YES;
188 [self waitForExpectations: @[waitmore] timeout: 0.2];
191 - (void)testBlockCancel {
192 XCTestExpectation *cancelexpectation = [self expectationWithDescription:@"FutureScheduler fired (after cancel)"];
193 cancelexpectation.inverted = YES;
195 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" delay: 100*NSEC_PER_MSEC keepProcessAlive:true
196 dependencyDescriptionCode:CKKSResultDescriptionNone
198 [cancelexpectation fulfill];
204 // Make sure it does not fire in 0.5 s
205 [self waitForExpectations: @[cancelexpectation] timeout:0.2];
208 - (void)testBlockDelayedNoShot {
209 XCTestExpectation *toofastexpectation = [self expectationWithDescription:@"FutureScheduler fired (too soon)"];
210 toofastexpectation.inverted = YES;
212 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" delay: 10*NSEC_PER_MSEC keepProcessAlive:false
213 dependencyDescriptionCode:CKKSResultDescriptionNone
215 [toofastexpectation fulfill];
218 // Tell the scheduler to wait, but don't trigger it. It shouldn't fire.
219 [scheduler waitUntil: 50*NSEC_PER_MSEC];
221 [self waitForExpectations: @[toofastexpectation] timeout:0.1];
224 - (void)testBlockDelayedOneShot {
225 XCTestExpectation *first = [self expectationWithDescription:@"FutureScheduler fired (one)"];
226 first.assertForOverFulfill = NO;
228 XCTestExpectation *toofastexpectation = [self expectationWithDescription:@"FutureScheduler fired (too soon)"];
229 toofastexpectation.inverted = YES;
231 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" delay: 10*NSEC_PER_MSEC keepProcessAlive:false
232 dependencyDescriptionCode:CKKSResultDescriptionNone
235 [toofastexpectation fulfill];
238 [scheduler waitUntil: 150*NSEC_PER_MSEC];
241 [self waitForExpectations: @[toofastexpectation] timeout:0.1];
242 [self waitForExpectations: @[first] timeout:0.5];
245 - (void)testBlockWaitedMultiShot {
246 XCTestExpectation *first = [self expectationWithDescription:@"FutureScheduler fired (one)"];
247 first.assertForOverFulfill = NO;
249 XCTestExpectation *toofastexpectation = [self expectationWithDescription:@"FutureScheduler fired (too soon)"];
250 toofastexpectation.expectedFulfillmentCount = 2;
251 toofastexpectation.inverted = YES;
253 XCTestExpectation *second = [self expectationWithDescription:@"FutureScheduler fired (two)"];
254 second.expectedFulfillmentCount = 2;
255 second.assertForOverFulfill = YES;
257 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" delay: 10*NSEC_PER_MSEC keepProcessAlive:false
258 dependencyDescriptionCode:CKKSResultDescriptionNone
262 [toofastexpectation fulfill];
266 [self waitForExpectations: @[first] timeout:0.5];
268 [scheduler waitUntil: 150*NSEC_PER_MSEC];
271 [self waitForExpectations: @[toofastexpectation] timeout:0.1];
272 [self waitForExpectations: @[second] timeout:0.3];
275 #pragma mark - Operation-based tests
277 - (NSOperation*)operationFulfillingExpectations:(NSArray<XCTestExpectation*>*)expectations {
278 return [NSBlockOperation named:@"test" withBlock:^{
279 for(XCTestExpectation* e in expectations) {
285 - (void)addOperationFulfillingExpectations:(NSArray<XCTestExpectation*>*)expectations scheduler:(CKKSNearFutureScheduler*)scheduler {
286 NSOperation* op = [self operationFulfillingExpectations:expectations];
287 XCTAssertNotNil(scheduler.operationDependency, "Should be an operation dependency");
288 XCTAssertTrue([scheduler.operationDependency isPending], "operation dependency shouldn't have run yet");
289 [op addDependency:scheduler.operationDependency];
290 [self.operationQueue addOperation:op];
293 - (void)testOperationOneShot {
294 XCTestExpectation *expectation = [self expectationWithDescription:@"FutureScheduler fired"];
296 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" delay:50*NSEC_PER_MSEC keepProcessAlive:true
297 dependencyDescriptionCode:CKKSResultDescriptionNone
299 [self addOperationFulfillingExpectations:@[expectation] scheduler:scheduler];
303 [self waitForExpectationsWithTimeout:1 handler:nil];
306 - (void)testOperationOneShotDelay {
307 XCTestExpectation *toofastexpectation = [self expectationWithDescription:@"FutureScheduler fired (too soon)"];
308 toofastexpectation.inverted = YES;
310 XCTestExpectation *expectation = [self expectationWithDescription:@"FutureScheduler fired"];
312 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" delay: 200*NSEC_PER_MSEC keepProcessAlive:false
313 dependencyDescriptionCode:CKKSResultDescriptionNone
315 [self addOperationFulfillingExpectations:@[expectation,toofastexpectation] scheduler:scheduler];
319 // Make sure it waits at least 0.1 seconds
320 [self waitForExpectations: @[toofastexpectation] timeout:0.1];
322 // But finishes within 1.1s (total)
323 [self waitForExpectations: @[expectation] timeout:1];
326 - (void)testOperationOneShotManyTrigger {
327 XCTestExpectation *toofastexpectation = [self expectationWithDescription:@"FutureScheduler fired (too soon)"];
328 toofastexpectation.inverted = YES;
330 XCTestExpectation *expectation = [self expectationWithDescription:@"FutureScheduler fired"];
331 expectation.assertForOverFulfill = YES;
333 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" delay: 200*NSEC_PER_MSEC keepProcessAlive:true
334 dependencyDescriptionCode:CKKSResultDescriptionNone
336 [self addOperationFulfillingExpectations:@[expectation,toofastexpectation] scheduler:scheduler];
347 // Make sure it waits at least 0.1 seconds
348 [self waitForExpectations: @[toofastexpectation] timeout:0.1];
350 // But finishes within .6s (total)
351 [self waitForExpectations: @[expectation] timeout:0.5];
353 // Ensure we don't get called again in the next 0.3 s
354 XCTestExpectation* waitmore = [self expectationWithDescription:@"waiting"];
355 waitmore.inverted = YES;
356 [self waitForExpectations: @[waitmore] timeout: 0.3];
360 - (void)testOperationMultiShot {
361 XCTestExpectation *first = [self expectationWithDescription:@"FutureScheduler fired (one)"];
363 XCTestExpectation *second = [self expectationWithDescription:@"FutureScheduler fired (two)"];
365 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" delay: 100*NSEC_PER_MSEC keepProcessAlive:false
366 dependencyDescriptionCode:CKKSResultDescriptionNone
369 [self addOperationFulfillingExpectations:@[first] scheduler:scheduler];
373 [self waitForExpectations: @[first] timeout:0.2];
375 [self addOperationFulfillingExpectations:@[second] scheduler:scheduler];
381 [self waitForExpectations: @[second] timeout:0.2];
383 XCTestExpectation* waitmore = [self expectationWithDescription:@"waiting"];
384 waitmore.inverted = YES;
385 [self waitForExpectations: @[waitmore] timeout: 0.2];
388 - (void)testOperationMultiShotDelays {
389 XCTestExpectation *first = [self expectationWithDescription:@"FutureScheduler fired (one)"];
391 XCTestExpectation *longdelay = [self expectationWithDescription:@"FutureScheduler fired (long delay expectation)"];
392 longdelay.inverted = YES;
393 XCTestExpectation *second = [self expectationWithDescription:@"FutureScheduler fired (two)"];
395 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" initialDelay: 50*NSEC_PER_MSEC continuingDelay:300*NSEC_PER_MSEC keepProcessAlive:false
396 dependencyDescriptionCode:CKKSResultDescriptionNone
399 [self addOperationFulfillingExpectations:@[first] scheduler:scheduler];
403 [self waitForExpectations: @[first] timeout:0.2];
405 [self addOperationFulfillingExpectations:@[second,longdelay] scheduler:scheduler];
411 // longdelay shouldn't be fulfilled in the first 0.2 seconds
412 [self waitForExpectations: @[longdelay] timeout:0.2];
414 // But second should be fulfilled in the next 0.5 seconds
415 [self waitForExpectations: @[second] timeout:0.5];
417 XCTestExpectation* waitmore = [self expectationWithDescription:@"waiting"];
418 waitmore.inverted = YES;
419 [self waitForExpectations: @[waitmore] timeout: 0.2];
422 - (void)testOperationCancel {
423 XCTestExpectation *cancelexpectation = [self expectationWithDescription:@"FutureScheduler fired (after cancel)"];
424 cancelexpectation.inverted = YES;
426 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" delay: 100*NSEC_PER_MSEC keepProcessAlive:true
427 dependencyDescriptionCode:CKKSResultDescriptionNone
430 [self addOperationFulfillingExpectations:@[cancelexpectation] scheduler:scheduler];
435 // Make sure it does not fire in 0.5 s
436 [self waitForExpectations: @[cancelexpectation] timeout:0.2];
439 - (void)testOperationDelayedNoShot {
440 XCTestExpectation *toofastexpectation = [self expectationWithDescription:@"FutureScheduler fired (too soon)"];
441 toofastexpectation.inverted = YES;
443 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" delay: 10*NSEC_PER_MSEC keepProcessAlive:false
444 dependencyDescriptionCode:CKKSResultDescriptionNone
446 [self addOperationFulfillingExpectations:@[toofastexpectation] scheduler:scheduler];
448 // Tell the scheduler to wait, but don't trigger it. It shouldn't fire.
449 [scheduler waitUntil: 50*NSEC_PER_MSEC];
451 [self waitForExpectations: @[toofastexpectation] timeout:0.1];
454 - (void)testOperationDelayedOneShot {
455 XCTestExpectation *first = [self expectationWithDescription:@"FutureScheduler fired (one)"];
456 first.assertForOverFulfill = NO;
458 XCTestExpectation *toofastexpectation = [self expectationWithDescription:@"FutureScheduler fired (too soon)"];
459 toofastexpectation.inverted = YES;
461 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" delay: 10*NSEC_PER_MSEC keepProcessAlive:false
462 dependencyDescriptionCode:CKKSResultDescriptionNone
464 [self addOperationFulfillingExpectations:@[first,toofastexpectation] scheduler:scheduler];
466 [scheduler waitUntil: 150*NSEC_PER_MSEC];
469 [self waitForExpectations: @[toofastexpectation] timeout:0.1];
470 [self waitForExpectations: @[first] timeout:0.5];
473 - (void)testChangeDelay {
474 XCTestExpectation *first = [self expectationWithDescription:@"FutureScheduler fired (one)"];
475 first.assertForOverFulfill = NO;
477 XCTestExpectation *second = [self expectationWithDescription:@"FutureScheduler fired (two)"];
478 second.expectedFulfillmentCount = 2;
479 second.assertForOverFulfill = YES;
481 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" delay: 10*NSEC_PER_SEC keepProcessAlive:false
482 dependencyDescriptionCode:CKKSResultDescriptionNone
488 [scheduler changeDelays:100*NSEC_PER_MSEC continuingDelay:100*NSEC_PER_MSEC];
491 [self waitForExpectations: @[first] timeout:0.4];
497 [self waitForExpectations: @[second] timeout:0.4];