示例#1
0
void init_timeout_(struct timeout *t, unsigned int interval,
		   void (*cb)(void *), void *arg)
{
	timer_init(&t->timer);
	t->interval = time_from_sec(interval);
	t->cb = cb;
	t->arg = arg;
}
int main(void)
{
	struct io_listener *l;
	int fd;
	struct timers timers;
	struct timer timer, *expired;
	struct addrinfo *addrinfo;

	/* This is how many tests you plan to run */
	plan_tests(7);

	fake_time = time_now();

	timers_init(&timers, fake_time);
	timer_init(&timer);
	timer_add(&timers, &timer,
		  timeabs_add(fake_time, time_from_sec(1000)));

	fd = make_listen_fd(PORT, &addrinfo);
	freeaddrinfo(addrinfo);
	ok1(fd >= 0);
	l = io_new_listener(NULL, fd, init_conn, NULL);
	ok1(l);

	fake_time.ts.tv_sec += 1000;
	ok1(io_time_override(get_fake_time) == time_now);
	ok1(io_loop(&timers, &expired) == NULL);

	ok1(expired == &timer);
	ok1(!timers_expire(&timers, fake_time));
	ok1(io_time_override(time_now) == get_fake_time);
	io_close_listener(l);

	timers_cleanup(&timers);

	/* This exits depending on whether all tests passed */
	return exit_status();
}
示例#3
0
文件: run.c 项目: cantora/ccan
int main(void)
{
	struct timespec t1, t2, t3, zero = { 0, 0 };

	plan_tests(61);

	/* Test time_now */
	t1 = time_now();
	t2 = time_now();

	/* Test time_sub. */
	t3 = time_sub(t2, t1);
	ok1(t3.tv_sec > 0 || t3.tv_nsec >= 0);
	t3 = time_sub(t2, t2);
	ok1(t3.tv_sec == 0 && t3.tv_nsec == 0);
	t3 = time_sub(t1, t1);
	ok1(t3.tv_sec == 0 && t3.tv_nsec == 0);

	/* Test time_eq */
	ok1(time_eq(t1, t1));
	ok1(time_eq(t2, t2));
	ok1(!time_eq(t1, t3));
	ok1(!time_eq(t2, t3));

	/* Make sure t2 > t1. */
	t3.tv_sec = 0;
	t3.tv_nsec = 1;
	t2 = time_add(t2, t3);

	/* Test time_less and time_greater. */
	ok1(!time_eq(t1, t2));
	ok1(!time_greater(t1, t2));
	ok1(time_less(t1, t2));
	ok1(time_greater(t2, t1));
	ok1(!time_less(t2, t1));
	t3.tv_sec = 0;
	t3.tv_nsec = 999999999;
	t2 = time_add(t2, t3);
	ok1(!time_eq(t1, t2));
	ok1(!time_greater(t1, t2));
	ok1(time_less(t1, t2));
	ok1(time_greater(t2, t1));
	ok1(!time_less(t2, t1));

	t3 = time_sub(t2, zero);
	ok1(time_eq(t3, t2));
	t3 = time_sub(t2, t2);
	ok1(time_eq(t3, zero));

	/* time_from_sec / time_to_sec */
	t3 = time_from_sec(500);
	ok1(t3.tv_sec == 500);
	ok1(t3.tv_nsec == 0);
	ok1(time_to_sec(t3) == 500);

	/* time_from_msec / time_to_msec */
	t3 = time_from_msec(500);
	ok1(t3.tv_sec == 0);
	ok1(t3.tv_nsec == 500000000);
	ok1(time_to_msec(t3) == 500);

	t3 = time_from_msec(1000);
	ok1(t3.tv_sec == 1);
	ok1(t3.tv_nsec == 0);
	ok1(time_to_msec(t3) == 1000);

	t3 = time_from_msec(1500);
	ok1(t3.tv_sec == 1);
	ok1(t3.tv_nsec == 500000000);
	ok1(time_to_msec(t3) == 1500);

	/* time_from_usec */
	t3 = time_from_usec(500000);
	ok1(t3.tv_sec == 0);
	ok1(t3.tv_nsec == 500000000);
	ok1(time_to_usec(t3) == 500000);

	t3 = time_from_usec(1000000);
	ok1(t3.tv_sec == 1);
	ok1(t3.tv_nsec == 0);
	ok1(time_to_usec(t3) == 1000000);

	t3 = time_from_usec(1500000);
	ok1(t3.tv_sec == 1);
	ok1(t3.tv_nsec == 500000000);
	ok1(time_to_usec(t3) == 1500000);

	/* time_from_nsec */
	t3 = time_from_nsec(500000000);
	ok1(t3.tv_sec == 0);
	ok1(t3.tv_nsec == 500000000);
	ok1(time_to_nsec(t3) == 500000000);

	t3 = time_from_nsec(1000000000);
	ok1(t3.tv_sec == 1);
	ok1(t3.tv_nsec == 0);
	ok1(time_to_nsec(t3) == 1000000000);

	t3 = time_from_nsec(1500000000);
	ok1(t3.tv_sec == 1);
	ok1(t3.tv_nsec == 500000000);
	ok1(time_to_nsec(t3) == 1500000000);

	/* Test wrapunder */
	t3 = time_sub(time_sub(t2, time_from_msec(500)), time_from_msec(500));
	ok1(t3.tv_sec == t2.tv_sec - 1);
	ok1(t3.tv_nsec == t2.tv_nsec);

	/* time_divide and time_multiply */
	t1.tv_nsec = 100;
	t1.tv_sec = 100;

	t3 = time_divide(t1, 2);
	ok1(t3.tv_sec == 50);
	ok1(t3.tv_nsec == 50);

	t3 = time_divide(t1, 100);
	ok1(t3.tv_sec == 1);
	ok1(t3.tv_nsec == 1);

	t3 = time_multiply(t3, 100);
	ok1(time_eq(t3, t1));

	t3 = time_divide(t1, 200);
	ok1(t3.tv_sec == 0);
	ok1(t3.tv_nsec == 500000000);

	/* Divide by huge number. */
	t1.tv_sec = (1U << 31) - 1;
	t1.tv_nsec = 999999999;
	t2 = time_divide(t1, 1 << 30);
	/* Allow us to round either way. */
	ok1((t2.tv_sec == 2 && t2.tv_nsec == 0)
	    || (t2.tv_sec == 1 && t2.tv_nsec == 999999999));

	/* Multiply by huge number. */
	t1.tv_sec = 0;
	t1.tv_nsec = 1;
	t2 = time_multiply(t1, 1UL << 31);
	ok1(t2.tv_sec == 2);
	ok1(t2.tv_nsec == 147483648);

	return exit_status();
}