]> git.saurik.com Git - apple/libdispatch.git/blob - testing/dispatch_timer_set_time.c
libdispatch-84.5.5.tar.gz
[apple/libdispatch.git] / testing / dispatch_timer_set_time.c
1 #include <sys/time.h>
2 #include <assert.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <sys/time.h>
6
7 #include <dispatch/dispatch.h>
8
9 #include "dispatch_test.h"
10
11 int main(void)
12 {
13 test_start("Dispatch Update Timer");
14
15 dispatch_queue_t main_q = dispatch_get_main_queue();
16 test_ptr("dispatch_get_main_queue", main_q, dispatch_get_current_queue());
17
18 __block int i = 0;
19 struct timeval start_time;
20
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
28 // this.
29 test_long_less_than("total duration", end_time.tv_sec - start_time.tv_sec, 3);
30 test_ptr_notnull("finalizer ran", ds);
31 test_stop();
32 });
33
34 dispatch_source_t s = dispatch_source_timer_create(DISPATCH_TIMER_INTERVAL,
35 1000000000ull,
36 0,
37 attr,
38 main_q,
39 ^(dispatch_event_t ev) {
40 long err;
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));
44 } else {
45 fprintf(stderr, "%d\n", ++i);
46 if (i >= 7) {
47 dispatch_cancel(dispatch_event_get_source(ev));
48 } else if (i == 1) {
49 dispatch_source_timer_set_time(dispatch_event_get_source(ev), 100, 0);
50 }
51 }
52 });
53 test_ptr_notnull("dispatch_source_timer_create", s);
54
55 dispatch_release(attr);
56
57 dispatch_main();
58
59 return 0;
60 }