- memory allocation tests removed
[strongswan.git] / Source / charon / testcases / 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
28 #include <globals.h>
29 #include <threads/scheduler.h>
30 #include <queues/event_queue.h>
31 #include <queues/job_queue.h>
32 #include <queues/jobs/incoming_packet_job.h>
33
34
35 /**
36 * @brief implementation of a scheduler test
37 *
38 * This one uses relative time events, which are not that exact.
39 * Test may fail on too slow machines.
40 */
41 void test_scheduler(tester_t *tester)
42 {
43 int job_count = 5;
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 /* misusing for testing only */
52 jobs[current] = (job_t *) incoming_packet_job_create((packet_t*)(current+1));
53 global_event_queue->add_relative(global_event_queue, jobs[current], (current+1) * 500);
54 }
55
56
57 for (current = 0; current < job_count; current++)
58 {
59 jobs[current] = NULL;
60 }
61
62 usleep(50 * 1000);
63
64 /* check if times are correct */
65 for (current = 0; current < job_count; current++)
66 {
67 usleep(400 * 1000);
68
69 tester->assert_true(tester, (global_job_queue->get_count(global_job_queue) == current ), "job-queue size before event");
70 tester->assert_true(tester, (global_event_queue->get_count(global_event_queue) == job_count - current), "event-queue size before event");
71 usleep(100 * 1000);
72
73 tester->assert_true(tester, (global_job_queue->get_count(global_job_queue) == current + 1), "job-queue size after event");
74 tester->assert_true(tester, (global_event_queue->get_count(global_event_queue) == job_count - current - 1), "event-queue size after event");
75 }
76
77 /* check job order */
78 for (current = 0; current < job_count; current++)
79 {
80 jobs[current] = global_job_queue->get(global_job_queue);
81 incoming_packet_job_t *current_job;
82 current_job = (incoming_packet_job_t*) jobs[current];
83 packet_t *packet;
84 current_job->get_packet(current_job,&packet);
85
86 tester->assert_true(tester, (((int)packet) == current+1), "job order");
87 jobs[current]->destroy(jobs[current]);
88 }
89
90 /* destruction test */
91 scheduler->destroy(scheduler);
92 }