2 * Copyright (c) 2008-2011 Apple Inc. All rights reserved.
4 * @APPLE_APACHE_LICENSE_HEADER_START@
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * @APPLE_APACHE_LICENSE_HEADER_END@
27 #include <dispatch/dispatch.h>
30 #include "dispatch_test.h"
35 dispatch_test_start("Dispatch Update Timer");
37 dispatch_queue_t main_q = dispatch_get_main_queue();
38 //test_ptr("dispatch_get_main_queue", main_q, dispatch_get_current_queue());
41 struct timeval start_time;
43 gettimeofday(&start_time, NULL);
45 dispatch_source_t s = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, main_q);
46 test_ptr_notnull("dispatch_source_create", s);
48 dispatch_source_set_timer(s, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC), NSEC_PER_SEC, 0);
50 dispatch_source_set_cancel_handler(s, ^{
51 struct timeval end_time;
52 gettimeofday(&end_time, NULL);
53 // Make sure we actually managed to adjust the interval
54 // duration. Seven one second ticks would blow past
56 test_long_less_than("total duration", end_time.tv_sec - start_time.tv_sec, 3);
62 dispatch_source_set_event_handler(s, ^{
63 fprintf(stderr, "%d\n", ++i);
65 dispatch_source_cancel(s);
67 dispatch_source_set_timer(s, dispatch_time(DISPATCH_TIME_NOW, 0), NSEC_PER_SEC / 10, 0);
70 test_ptr_notnull("dispatch_source_timer_create", s);