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