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"
28 @interface CKKSNearFutureSchedulerTests : XCTestCase
32 @implementation CKKSNearFutureSchedulerTests
43 XCTestExpectation *expectation = [self expectationWithDescription:@"FutureScheduler fired"];
45 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" delay:50*NSEC_PER_MSEC keepProcessAlive:true block:^{
46 [expectation fulfill];
51 [self waitForExpectationsWithTimeout:1 handler:nil];
54 - (void)testOneShotDelay {
55 XCTestExpectation *toofastexpectation = [self expectationWithDescription:@"FutureScheduler fired (too soon)"];
56 toofastexpectation.inverted = YES;
58 XCTestExpectation *expectation = [self expectationWithDescription:@"FutureScheduler fired"];
60 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" delay: 200*NSEC_PER_MSEC keepProcessAlive:false block:^{
61 [toofastexpectation fulfill];
62 [expectation fulfill];
67 // Make sure it waits at least 0.1 seconds
68 [self waitForExpectations: @[toofastexpectation] timeout:0.1];
70 // But finishes within 1.1s (total)
71 [self waitForExpectations: @[expectation] timeout:1];
74 - (void)testOneShotManyTrigger {
75 XCTestExpectation *toofastexpectation = [self expectationWithDescription:@"FutureScheduler fired (too soon)"];
76 toofastexpectation.inverted = YES;
78 XCTestExpectation *expectation = [self expectationWithDescription:@"FutureScheduler fired"];
79 expectation.assertForOverFulfill = YES;
81 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" delay: 200*NSEC_PER_MSEC keepProcessAlive:true block:^{
82 [toofastexpectation fulfill];
83 [expectation fulfill];
95 // Make sure it waits at least 0.1 seconds
96 [self waitForExpectations: @[toofastexpectation] timeout:0.1];
98 // But finishes within .6s (total)
99 [self waitForExpectations: @[expectation] timeout:0.5];
101 // Ensure we don't get called again in the next 0.3 s
102 XCTestExpectation* waitmore = [self expectationWithDescription:@"waiting"];
103 waitmore.inverted = YES;
104 [self waitForExpectations: @[waitmore] timeout: 0.3];
108 - (void)testMultiShot {
109 XCTestExpectation *first = [self expectationWithDescription:@"FutureScheduler fired (one)"];
110 first.assertForOverFulfill = NO;
112 XCTestExpectation *second = [self expectationWithDescription:@"FutureScheduler fired (two)"];
113 second.expectedFulfillmentCount = 2;
114 second.assertForOverFulfill = YES;
116 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" delay: 100*NSEC_PER_MSEC keepProcessAlive:false block:^{
123 [self waitForExpectations: @[first] timeout:0.2];
129 [self waitForExpectations: @[second] timeout:0.2];
131 XCTestExpectation* waitmore = [self expectationWithDescription:@"waiting"];
132 waitmore.inverted = YES;
133 [self waitForExpectations: @[waitmore] timeout: 0.2];
136 - (void)testMultiShotDelays {
137 XCTestExpectation *first = [self expectationWithDescription:@"FutureScheduler fired (one)"];
138 first.assertForOverFulfill = NO;
140 XCTestExpectation *longdelay = [self expectationWithDescription:@"FutureScheduler fired (long delay expectation)"];
141 longdelay.inverted = YES;
142 longdelay.expectedFulfillmentCount = 2;
144 XCTestExpectation *second = [self expectationWithDescription:@"FutureScheduler fired (two)"];
145 second.expectedFulfillmentCount = 2;
146 second.assertForOverFulfill = YES;
148 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" initialDelay: 50*NSEC_PER_MSEC continuingDelay:300*NSEC_PER_MSEC keepProcessAlive:false block:^{
156 [self waitForExpectations: @[first] timeout:0.2];
162 // longdelay should NOT be fulfilled twice in the first 0.3 seconds
163 [self waitForExpectations: @[longdelay] timeout:0.2];
165 // But second should be fulfilled in the first 0.8 seconds
166 [self waitForExpectations: @[second] timeout:0.5];
168 XCTestExpectation* waitmore = [self expectationWithDescription:@"waiting"];
169 waitmore.inverted = YES;
170 [self waitForExpectations: @[waitmore] timeout: 0.2];
174 XCTestExpectation *cancelexpectation = [self expectationWithDescription:@"FutureScheduler fired (after cancel)"];
175 cancelexpectation.inverted = YES;
177 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" delay: 100*NSEC_PER_MSEC keepProcessAlive:true block:^{
178 [cancelexpectation fulfill];
184 // Make sure it does not fire in 0.5 s
185 [self waitForExpectations: @[cancelexpectation] timeout:0.2];
188 - (void)testDelayedNoShot {
189 XCTestExpectation *toofastexpectation = [self expectationWithDescription:@"FutureScheduler fired (too soon)"];
190 toofastexpectation.inverted = YES;
192 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" delay: 10*NSEC_PER_MSEC keepProcessAlive:false block:^{
193 [toofastexpectation fulfill];
196 // Tell the scheduler to wait, but don't trigger it. It shouldn't fire.
197 [scheduler waitUntil: 50*NSEC_PER_MSEC];
199 [self waitForExpectations: @[toofastexpectation] timeout:0.1];
202 - (void)testDelayedOneShot {
203 XCTestExpectation *first = [self expectationWithDescription:@"FutureScheduler fired (one)"];
204 first.assertForOverFulfill = NO;
206 XCTestExpectation *toofastexpectation = [self expectationWithDescription:@"FutureScheduler fired (too soon)"];
207 toofastexpectation.inverted = YES;
209 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" delay: 10*NSEC_PER_MSEC keepProcessAlive:false block:^{
211 [toofastexpectation fulfill];
214 [scheduler waitUntil: 150*NSEC_PER_MSEC];
217 [self waitForExpectations: @[toofastexpectation] timeout:0.1];
218 [self waitForExpectations: @[first] timeout:0.2];
221 - (void)testDelayedMultiShot {
222 XCTestExpectation *first = [self expectationWithDescription:@"FutureScheduler fired (one)"];
223 first.assertForOverFulfill = NO;
225 XCTestExpectation *toofastexpectation = [self expectationWithDescription:@"FutureScheduler fired (too soon)"];
226 toofastexpectation.expectedFulfillmentCount = 2;
227 toofastexpectation.inverted = YES;
229 XCTestExpectation *second = [self expectationWithDescription:@"FutureScheduler fired (two)"];
230 second.expectedFulfillmentCount = 2;
231 second.assertForOverFulfill = YES;
233 CKKSNearFutureScheduler* scheduler = [[CKKSNearFutureScheduler alloc] initWithName: @"test" delay: 10*NSEC_PER_MSEC keepProcessAlive:false block:^{
236 [toofastexpectation fulfill];
240 [self waitForExpectations: @[first] timeout:0.2];
242 [scheduler waitUntil: 150*NSEC_PER_MSEC];
245 [self waitForExpectations: @[toofastexpectation] timeout:0.1];
246 [self waitForExpectations: @[second] timeout:0.3];