- test for scheduler implemented
[strongswan.git] / Source / charon / tests / scheduler_test.c
1 /**
2 * @file scheduler_test.c
3 *
4 * @brief Tests to test the Scheduler (type scheduler_t)
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #include <string.h>
24 #include <unistd.h>
25
26 #include "scheduler_test.h"
27 #include "../globals.h"
28 #include "../scheduler.h"
29 #include "../event_queue.h"
30 #include "../job_queue.h"
31
32
33 /**
34 * @brief implementation of a scheduler test
35 *
36 * This one uses relative time events, which are not that exact.
37 * Test may fail on too slow machines.
38 */
39 void test_scheduler(tester_t *tester)
40 {
41 int job_count = 5;
42 int job_queue_size;
43 int event_queue_size;
44 job_t *jobs[job_count];
45 int current;
46 scheduler_t *scheduler = scheduler_create();
47
48 /* schedule 5 jobs */
49 for (current = 0; current < job_count; current++)
50 {
51 jobs[current] = job_create(INCOMING_PACKET, (void*)current);
52 global_event_queue->add_relative(global_event_queue, jobs[current], (current+1) * 500);
53 }
54
55
56 for (current = 0; current < job_count; current++)
57 {
58 jobs[current] = NULL;
59 }
60
61 usleep(50 * 1000);
62
63 /* check if times are correct */
64 for (current = 0; current < job_count; current++)
65 {
66 usleep(400 * 1000);
67 global_event_queue->get_count(global_event_queue, &event_queue_size);
68 global_job_queue->get_count(global_job_queue, &job_queue_size);
69 tester->assert_true(tester, (job_queue_size == current ), "job-queue size before event");
70 tester->assert_true(tester, (event_queue_size == job_count - current), "event-queue size before event");
71 usleep(100 * 1000);
72 global_event_queue->get_count(global_event_queue, &event_queue_size);
73 global_job_queue->get_count(global_job_queue, &job_queue_size);
74 tester->assert_true(tester, (job_queue_size == current + 1), "job-queue size after event");
75 tester->assert_true(tester, (event_queue_size == job_count - current - 1), "event-queue size after event");
76 }
77
78 /* check job order */
79 for (current = 0; current < job_count; current++)
80 {
81 global_job_queue->get(global_job_queue, &(jobs[current]));
82 tester->assert_true(tester, ((int)jobs[current]->assigned_data == current), "job order");
83 jobs[current]->destroy(jobs[current]);
84 }
85
86 /* destruction test */
87 tester->assert_true(tester, (scheduler->destroy(scheduler) == SUCCESS), "destroy call check");
88 }