7 #include <dispatch/dispatch.h>
9 #include "dispatch_test.h"
13 test_start("Dispatch Update Timer");
15 dispatch_queue_t main_q
= dispatch_get_main_queue();
16 test_ptr("dispatch_get_main_queue", main_q
, dispatch_get_current_queue());
19 struct timeval start_time
;
21 gettimeofday(&start_time
, NULL
);
22 dispatch_source_attr_t attr
= dispatch_source_attr_create();
23 dispatch_source_attr_set_finalizer(attr
, ^(dispatch_source_t ds
) {
24 struct timeval end_time
;
25 gettimeofday(&end_time
, NULL
);
26 // Make sure we actually managed to adjust the interval
27 // duration. Seven one second ticks would blow past
29 test_long_less_than("total duration", end_time
.tv_sec
- start_time
.tv_sec
, 3);
30 test_ptr_notnull("finalizer ran", ds
);
34 dispatch_source_t s
= dispatch_source_timer_create(DISPATCH_TIMER_INTERVAL
,
39 ^(dispatch_event_t ev
) {
41 if (dispatch_event_get_error(ev
, &err
)) {
42 test_errno("dispatch_event_get_error", err
, ECANCELED
);
43 dispatch_release(dispatch_event_get_source(ev
));
45 fprintf(stderr
, "%d\n", ++i
);
47 dispatch_cancel(dispatch_event_get_source(ev
));
49 dispatch_source_timer_set_time(dispatch_event_get_source(ev
), 100, 0);
53 test_ptr_notnull("dispatch_source_timer_create", s
);
55 dispatch_release(attr
);