Tizen 2.1 base
[platform/upstream/gcd.git] / dispatch-1.0 / testing / dispatch_timer_set_time.c
1 #include "config/config.h"
2
3 #include <sys/time.h>
4 #include <assert.h>
5 #include <stdio.h>
6 #include <string.h>
7
8 #define __DISPATCH_INDIRECT__
9 #include "src/internal.h"
10
11 #include "dispatch_test.h"
12
13 int main(void)
14 {
15         test_start("Dispatch Update Timer");
16
17         dispatch_queue_t main_q = dispatch_get_main_queue();
18         test_ptr("dispatch_get_main_queue", main_q, dispatch_get_current_queue());
19
20         __block int i = 0;
21         struct timeval start_time;
22
23         gettimeofday(&start_time, NULL);
24         dispatch_source_attr_t attr = dispatch_source_attr_create();
25         dispatch_source_attr_set_finalizer(attr, ^(dispatch_source_t ds) {
26                 struct timeval end_time;
27                 gettimeofday(&end_time, NULL);
28                 // Make sure we actually managed to adjust the interval
29                 // duration.  Seven one second ticks would blow past
30                 // this.
31                 test_long_less_than("total duration", end_time.tv_sec - start_time.tv_sec, 3);
32                 test_ptr_notnull("finalizer ran", ds);
33                 test_stop();
34         });
35
36         dispatch_source_t s = dispatch_source_timer_create(DISPATCH_TIMER_INTERVAL,
37                 1000000000ull,
38                 0,
39                 attr,
40                 main_q,
41                 ^(dispatch_event_t ev) {
42                         long err;
43                         if (dispatch_event_get_error(ev, &err)) {
44                                 test_errno("dispatch_event_get_error", err, ECANCELED);
45                                 dispatch_release(dispatch_event_get_source(ev));
46                         } else {
47                                 fprintf(stderr, "%d\n", ++i);
48                                 if (i >= 7) {
49                                         dispatch_cancel(dispatch_event_get_source(ev));
50                                 } else if (i == 1) {
51                                         dispatch_source_timer_set_time(dispatch_event_get_source(ev), 100, 0);
52                                 }
53                         }
54                 });
55         test_ptr_notnull("dispatch_source_timer_create", s);
56
57         dispatch_release(attr);
58
59         dispatch_main();
60
61         return 0;
62 }