akaros/user/utest/alarm.c
<<
>>
Prefs
   1#include <utest/utest.h>
   2#include <parlib/alarm.h>
   3
   4TEST_SUITE("ALARMS");
   5
   6/* <--- Begin definition of test cases ---> */
   7
   8bool test_alarm(void) {
   9        const int INTERVAL = 10000;
  10        const int ITERATIONS = 100;
  11        void alarm_handler(struct alarm_waiter *waiter)
  12        {
  13                __sync_fetch_and_add((int*)waiter->data, 1);
  14                set_awaiter_inc(waiter, INTERVAL);
  15                set_alarm(waiter);
  16        }
  17
  18        int count = 0;
  19        uint64_t now, then;
  20        struct alarm_waiter waiter;
  21        init_awaiter(&waiter, alarm_handler);
  22        waiter.data = &count;
  23        set_awaiter_rel(&waiter, INTERVAL);
  24        now = tsc2usec(read_tsc());
  25        set_alarm(&waiter);
  26        while(count < ITERATIONS)
  27                cpu_relax();
  28        then = tsc2usec(read_tsc());
  29        unset_alarm(&waiter);
  30        UT_ASSERT_M("Alarms finished too soon", then >= (now + INTERVAL*count));
  31        UT_ASSERT_M("Alarms finished too late", then < (now + 2*INTERVAL*count));
  32        return true;
  33}
  34
  35/* <--- End definition of test cases ---> */
  36
  37struct utest utests[] = {
  38        UTEST_REG(alarm),
  39};
  40int num_utests = sizeof(utests) / sizeof(struct utest);
  41
  42int main(int argc, char *argv[]) {
  43        char **whitelist = &argv[1];
  44        int whitelist_len = argc - 1;
  45        RUN_TEST_SUITE(utests, num_utests, whitelist, whitelist_len);
  46}
  47
  48
  49