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@
24 #include <dispatch/dispatch.h>
25 #import <XCTest/XCTest.h>
26 #import "keychain/ckks/CKKSNearFutureScheduler.h"
27 #import "keychain/ckks/CKKSResultOperation.h"
29 @interface CKKSNearFutureSchedulerTests : XCTestCase
30 @property NSOperationQueue* operationQueue;
33 @implementation CKKSNearFutureSchedulerTests
38 self.operationQueue = [[NSOperationQueue alloc] init];
45 #pragma mark - Block-based tests
47 - (void)testBlockOneShot {
48 XCTestExpectation *expectation = [self expectationWithDescription:@"FutureScheduler fired"];
50 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" delay:50*NSEC_PER_MSEC keepProcessAlive:true block:^{
51 [expectation fulfill];
56 [self waitForExpectationsWithTimeout:1 handler:nil];
59 - (void)testBlockOneShotDelay {
60 XCTestExpectation *toofastexpectation = [self expectationWithDescription:@"FutureScheduler fired (too soon)"];
61 toofastexpectation.inverted = YES;
63 XCTestExpectation *expectation = [self expectationWithDescription:@"FutureScheduler fired"];
65 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" delay: 200*NSEC_PER_MSEC keepProcessAlive:false block:^{
66 [toofastexpectation fulfill];
67 [expectation fulfill];
72 // Make sure it waits at least 0.1 seconds
73 [self waitForExpectations: @[toofastexpectation] timeout:0.1];
75 // But finishes within 1.1s (total)
76 [self waitForExpectations: @[expectation] timeout:1];
79 - (void)testBlockOneShotManyTrigger {
80 XCTestExpectation *toofastexpectation = [self expectationWithDescription:@"FutureScheduler fired (too soon)"];
81 toofastexpectation.inverted = YES;
83 XCTestExpectation *expectation = [self expectationWithDescription:@"FutureScheduler fired"];
84 expectation.assertForOverFulfill = YES;
86 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" delay: 200*NSEC_PER_MSEC keepProcessAlive:true block:^{
87 [toofastexpectation fulfill];
88 [expectation fulfill];
100 // Make sure it waits at least 0.1 seconds
101 [self waitForExpectations: @[toofastexpectation] timeout:0.1];
103 // But finishes within .6s (total)
104 [self waitForExpectations: @[expectation] timeout:0.5];
106 // Ensure we don't get called again in the next 0.3 s
107 XCTestExpectation* waitmore = [self expectationWithDescription:@"waiting"];
108 waitmore.inverted = YES;
109 [self waitForExpectations: @[waitmore] timeout: 0.3];
113 - (void)testBlockMultiShot {
114 XCTestExpectation *first = [self expectationWithDescription:@"FutureScheduler fired (one)"];
115 first.assertForOverFulfill = NO;
117 XCTestExpectation *second = [self expectationWithDescription:@"FutureScheduler fired (two)"];
118 second.expectedFulfillmentCount = 2;
119 second.assertForOverFulfill = YES;
121 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" delay: 100*NSEC_PER_MSEC keepProcessAlive:false block:^{
128 [self waitForExpectations: @[first] timeout:0.4];
134 [self waitForExpectations: @[second] timeout:0.4];
136 XCTestExpectation* waitmore = [self expectationWithDescription:@"waiting"];
137 waitmore.inverted = YES;
138 [self waitForExpectations: @[waitmore] timeout: 0.4];
141 - (void)testBlockMultiShotDelays {
142 XCTestExpectation *first = [self expectationWithDescription:@"FutureScheduler fired (one)"];
143 first.assertForOverFulfill = NO;
145 XCTestExpectation *longdelay = [self expectationWithDescription:@"FutureScheduler fired (long delay expectation)"];
146 longdelay.inverted = YES;
147 longdelay.expectedFulfillmentCount = 2;
149 XCTestExpectation *second = [self expectationWithDescription:@"FutureScheduler fired (two)"];
150 second.expectedFulfillmentCount = 2;
151 second.assertForOverFulfill = YES;
153 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" initialDelay: 50*NSEC_PER_MSEC continuingDelay:600*NSEC_PER_MSEC keepProcessAlive:false block:^{
161 [self waitForExpectations: @[first] timeout:0.5];
167 // longdelay should NOT be fulfilled twice in the first 0.9 seconds
168 [self waitForExpectations: @[longdelay] timeout:0.4];
170 // But second should be fulfilled in the first 1.4 seconds
171 [self waitForExpectations: @[second] timeout:0.5];
173 XCTestExpectation* waitmore = [self expectationWithDescription:@"waiting"];
174 waitmore.inverted = YES;
175 [self waitForExpectations: @[waitmore] timeout: 0.2];
178 - (void)testBlockCancel {
179 XCTestExpectation *cancelexpectation = [self expectationWithDescription:@"FutureScheduler fired (after cancel)"];
180 cancelexpectation.inverted = YES;
182 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" delay: 100*NSEC_PER_MSEC keepProcessAlive:true block:^{
183 [cancelexpectation fulfill];
189 // Make sure it does not fire in 0.5 s
190 [self waitForExpectations: @[cancelexpectation] timeout:0.2];
193 - (void)testBlockDelayedNoShot {
194 XCTestExpectation *toofastexpectation = [self expectationWithDescription:@"FutureScheduler fired (too soon)"];
195 toofastexpectation.inverted = YES;
197 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" delay: 10*NSEC_PER_MSEC keepProcessAlive:false block:^{
198 [toofastexpectation fulfill];
201 // Tell the scheduler to wait, but don't trigger it. It shouldn't fire.
202 [scheduler waitUntil: 50*NSEC_PER_MSEC];
204 [self waitForExpectations: @[toofastexpectation] timeout:0.1];
207 - (void)testBlockDelayedOneShot {
208 XCTestExpectation *first = [self expectationWithDescription:@"FutureScheduler fired (one)"];
209 first.assertForOverFulfill = NO;
211 XCTestExpectation *toofastexpectation = [self expectationWithDescription:@"FutureScheduler fired (too soon)"];
212 toofastexpectation.inverted = YES;
214 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" delay: 10*NSEC_PER_MSEC keepProcessAlive:false block:^{
216 [toofastexpectation fulfill];
219 [scheduler waitUntil: 150*NSEC_PER_MSEC];
222 [self waitForExpectations: @[toofastexpectation] timeout:0.1];
223 [self waitForExpectations: @[first] timeout:0.5];
226 - (void)testBlockWaitedMultiShot {
227 XCTestExpectation *first = [self expectationWithDescription:@"FutureScheduler fired (one)"];
228 first.assertForOverFulfill = NO;
230 XCTestExpectation *toofastexpectation = [self expectationWithDescription:@"FutureScheduler fired (too soon)"];
231 toofastexpectation.expectedFulfillmentCount = 2;
232 toofastexpectation.inverted = YES;
234 XCTestExpectation *second = [self expectationWithDescription:@"FutureScheduler fired (two)"];
235 second.expectedFulfillmentCount = 2;
236 second.assertForOverFulfill = YES;
238 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" delay: 10*NSEC_PER_MSEC keepProcessAlive:false block:^{
241 [toofastexpectation fulfill];
245 [self waitForExpectations: @[first] timeout:0.5];
247 [scheduler waitUntil: 150*NSEC_PER_MSEC];
250 [self waitForExpectations: @[toofastexpectation] timeout:0.1];
251 [self waitForExpectations: @[second] timeout:0.3];
254 #pragma mark - Operation-based tests
256 - (NSOperation*)operationFulfillingExpectations:(NSArray<XCTestExpectation*>*)expectations {
257 return [NSBlockOperation named:@"test" withBlock:^{
258 for(XCTestExpectation* e in expectations) {
264 - (void)addOperationFulfillingExpectations:(NSArray<XCTestExpectation*>*)expectations scheduler:(CKKSNearFutureScheduler*)scheduler {
265 NSOperation* op = [self operationFulfillingExpectations:expectations];
266 XCTAssertNotNil(scheduler.operationDependency, "Should be an operation dependency");
267 XCTAssertTrue([scheduler.operationDependency isPending], "operation dependency shouldn't have run yet");
268 [op addDependency:scheduler.operationDependency];
269 [self.operationQueue addOperation:op];
272 - (void)testOperationOneShot {
273 XCTestExpectation *expectation = [self expectationWithDescription:@"FutureScheduler fired"];
275 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" delay:50*NSEC_PER_MSEC keepProcessAlive:true block:^{}];
276 [self addOperationFulfillingExpectations:@[expectation] scheduler:scheduler];
280 [self waitForExpectationsWithTimeout:1 handler:nil];
283 - (void)testOperationOneShotDelay {
284 XCTestExpectation *toofastexpectation = [self expectationWithDescription:@"FutureScheduler fired (too soon)"];
285 toofastexpectation.inverted = YES;
287 XCTestExpectation *expectation = [self expectationWithDescription:@"FutureScheduler fired"];
289 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" delay: 200*NSEC_PER_MSEC keepProcessAlive:false block:^{}];
290 [self addOperationFulfillingExpectations:@[expectation,toofastexpectation] scheduler:scheduler];
294 // Make sure it waits at least 0.1 seconds
295 [self waitForExpectations: @[toofastexpectation] timeout:0.1];
297 // But finishes within 1.1s (total)
298 [self waitForExpectations: @[expectation] timeout:1];
301 - (void)testOperationOneShotManyTrigger {
302 XCTestExpectation *toofastexpectation = [self expectationWithDescription:@"FutureScheduler fired (too soon)"];
303 toofastexpectation.inverted = YES;
305 XCTestExpectation *expectation = [self expectationWithDescription:@"FutureScheduler fired"];
306 expectation.assertForOverFulfill = YES;
308 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" delay: 200*NSEC_PER_MSEC keepProcessAlive:true block:^{}];
309 [self addOperationFulfillingExpectations:@[expectation,toofastexpectation] scheduler:scheduler];
320 // Make sure it waits at least 0.1 seconds
321 [self waitForExpectations: @[toofastexpectation] timeout:0.1];
323 // But finishes within .6s (total)
324 [self waitForExpectations: @[expectation] timeout:0.5];
326 // Ensure we don't get called again in the next 0.3 s
327 XCTestExpectation* waitmore = [self expectationWithDescription:@"waiting"];
328 waitmore.inverted = YES;
329 [self waitForExpectations: @[waitmore] timeout: 0.3];
333 - (void)testOperationMultiShot {
334 XCTestExpectation *first = [self expectationWithDescription:@"FutureScheduler fired (one)"];
336 XCTestExpectation *second = [self expectationWithDescription:@"FutureScheduler fired (two)"];
338 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" delay: 100*NSEC_PER_MSEC keepProcessAlive:false block:^{}];
340 [self addOperationFulfillingExpectations:@[first] scheduler:scheduler];
344 [self waitForExpectations: @[first] timeout:0.2];
346 [self addOperationFulfillingExpectations:@[second] scheduler:scheduler];
352 [self waitForExpectations: @[second] timeout:0.2];
354 XCTestExpectation* waitmore = [self expectationWithDescription:@"waiting"];
355 waitmore.inverted = YES;
356 [self waitForExpectations: @[waitmore] timeout: 0.2];
359 - (void)testOperationMultiShotDelays {
360 XCTestExpectation *first = [self expectationWithDescription:@"FutureScheduler fired (one)"];
362 XCTestExpectation *longdelay = [self expectationWithDescription:@"FutureScheduler fired (long delay expectation)"];
363 longdelay.inverted = YES;
364 XCTestExpectation *second = [self expectationWithDescription:@"FutureScheduler fired (two)"];
366 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" initialDelay: 50*NSEC_PER_MSEC continuingDelay:300*NSEC_PER_MSEC keepProcessAlive:false block:^{}];
368 [self addOperationFulfillingExpectations:@[first] scheduler:scheduler];
372 [self waitForExpectations: @[first] timeout:0.2];
374 [self addOperationFulfillingExpectations:@[second,longdelay] scheduler:scheduler];
380 // longdelay shouldn't be fulfilled in the first 0.2 seconds
381 [self waitForExpectations: @[longdelay] timeout:0.2];
383 // But second should be fulfilled in the next 0.5 seconds
384 [self waitForExpectations: @[second] timeout:0.5];
386 XCTestExpectation* waitmore = [self expectationWithDescription:@"waiting"];
387 waitmore.inverted = YES;
388 [self waitForExpectations: @[waitmore] timeout: 0.2];
391 - (void)testOperationCancel {
392 XCTestExpectation *cancelexpectation = [self expectationWithDescription:@"FutureScheduler fired (after cancel)"];
393 cancelexpectation.inverted = YES;
395 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" delay: 100*NSEC_PER_MSEC keepProcessAlive:true block:^{}];
397 [self addOperationFulfillingExpectations:@[cancelexpectation] scheduler:scheduler];
402 // Make sure it does not fire in 0.5 s
403 [self waitForExpectations: @[cancelexpectation] timeout:0.2];
406 - (void)testOperationDelayedNoShot {
407 XCTestExpectation *toofastexpectation = [self expectationWithDescription:@"FutureScheduler fired (too soon)"];
408 toofastexpectation.inverted = YES;
410 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" delay: 10*NSEC_PER_MSEC keepProcessAlive:false block:^{}];
411 [self addOperationFulfillingExpectations:@[toofastexpectation] scheduler:scheduler];
413 // Tell the scheduler to wait, but don't trigger it. It shouldn't fire.
414 [scheduler waitUntil: 50*NSEC_PER_MSEC];
416 [self waitForExpectations: @[toofastexpectation] timeout:0.1];
419 - (void)testOperationDelayedOneShot {
420 XCTestExpectation *first = [self expectationWithDescription:@"FutureScheduler fired (one)"];
421 first.assertForOverFulfill = NO;
423 XCTestExpectation *toofastexpectation = [self expectationWithDescription:@"FutureScheduler fired (too soon)"];
424 toofastexpectation.inverted = YES;
426 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" delay: 10*NSEC_PER_MSEC keepProcessAlive:false block:^{}];
427 [self addOperationFulfillingExpectations:@[first,toofastexpectation] scheduler:scheduler];
429 [scheduler waitUntil: 150*NSEC_PER_MSEC];
432 [self waitForExpectations: @[toofastexpectation] timeout:0.1];
433 [self waitForExpectations: @[first] timeout:0.5];