blob: 2c735a338d2608c175bfda9ad76e67a5d0a62d0c [file] [log] [blame]
Benny Prijono5dcb38d2005-11-21 01:55:47 +00001/* $Id$ */
2/*
3 * Copyright (C)2003-2006 Benny Prijono <benny@prijono.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#include "test.h"
20
21/**
22 * \page page_pjlib_sleep_test Test: Sleep, Time, and Timestamp
23 *
24 * This file provides implementation of \b sleep_test().
25 *
26 * \section sleep_test_sec Scope of the Test
27 *
28 * This tests:
29 * - whether pj_thread_sleep() works.
30 * - whether pj_gettimeofday() works.
31 * - whether pj_get_timestamp() and friends works.
32 *
33 * API tested:
34 * - pj_thread_sleep()
35 * - pj_gettimeofday()
36 * - PJ_TIME_VAL_SUB()
37 * - PJ_TIME_VAL_LTE()
38 * - pj_get_timestamp()
39 * - pj_get_timestamp_freq() (implicitly)
40 * - pj_elapsed_time()
41 * - pj_elapsed_usec()
42 *
43 *
44 * This file is <b>pjlib-test/sleep.c</b>
45 *
46 * \include pjlib-test/sleep.c
47 */
48
49#if INCLUDE_SLEEP_TEST
50
51#include <pjlib.h>
52
53#define THIS_FILE "sleep_test"
54
55static int simple_sleep_test(void)
56{
57 enum { COUNT = 5 };
58 int i;
59 pj_status_t rc;
60
61 PJ_LOG(3,(THIS_FILE, "..will write messages every 1 second:"));
62
63 for (i=0; i<COUNT; ++i) {
64 rc = pj_thread_sleep(1000);
65 if (rc != PJ_SUCCESS) {
66 app_perror("...error: pj_thread_sleep()", rc);
67 return -10;
68 }
69 PJ_LOG(3,(THIS_FILE, "...wake up.."));
70 }
71
72 return 0;
73}
74
75static int sleep_duration_test(void)
76{
77 enum { MIS = 20, DURATION = 1000, DURATION2 = 500 };
78 pj_status_t rc;
79
80 PJ_LOG(3,(THIS_FILE, "..running sleep duration test"));
81
82 /* Test pj_thread_sleep() and pj_gettimeofday() */
83 {
84 pj_time_val start, stop;
85 pj_uint32_t msec;
86
87 /* Mark start of test. */
88 rc = pj_gettimeofday(&start);
89 if (rc != PJ_SUCCESS) {
90 app_perror("...error: pj_gettimeofday()", rc);
91 return -10;
92 }
93
94 /* Sleep */
95 rc = pj_thread_sleep(DURATION);
96 if (rc != PJ_SUCCESS) {
97 app_perror("...error: pj_thread_sleep()", rc);
98 return -20;
99 }
100
101 /* Mark end of test. */
102 rc = pj_gettimeofday(&stop);
103
104 /* Calculate duration (store in stop). */
105 PJ_TIME_VAL_SUB(stop, start);
106
107 /* Convert to msec. */
108 msec = PJ_TIME_VAL_MSEC(stop);
109
110 /* Check if it's within range. */
111 if (msec < DURATION * (100-MIS)/100 ||
112 msec > DURATION * (100+MIS)/100)
113 {
114 PJ_LOG(3,(THIS_FILE,
115 "...error: slept for %d ms instead of %d ms "
116 "(outside %d%% err window)",
117 msec, DURATION, MIS));
118 return -30;
119 }
120 }
121
122
123 /* Test pj_thread_sleep() and pj_get_timestamp() and friends */
124 {
125 pj_time_val t1, t2;
126 pj_timestamp start, stop;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000127 pj_uint32_t msec;
128
Benny Prijono99683ae2005-11-21 16:59:47 +0000129 pj_thread_sleep(0);
130
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000131 /* Mark start of test. */
132 rc = pj_get_timestamp(&start);
133 if (rc != PJ_SUCCESS) {
134 app_perror("...error: pj_get_timestamp()", rc);
135 return -60;
136 }
137
138 /* ..also with gettimeofday() */
139 pj_gettimeofday(&t1);
140
141 /* Sleep */
142 rc = pj_thread_sleep(DURATION2);
143 if (rc != PJ_SUCCESS) {
144 app_perror("...error: pj_thread_sleep()", rc);
145 return -70;
146 }
147
148 /* Mark end of test. */
149 pj_get_timestamp(&stop);
150
151 /* ..also with gettimeofday() */
152 pj_gettimeofday(&t2);
153
154 /* Compare t1 and t2. */
155 if (PJ_TIME_VAL_LTE(t2, t1)) {
156 PJ_LOG(3,(THIS_FILE, "...error: t2 is less than t1!!"));
157 return -75;
158 }
159
Benny Prijono99683ae2005-11-21 16:59:47 +0000160 /* Get elapsed time in msec */
161 msec = pj_elapsed_msec(&start, &stop);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000162
163 /* Check if it's within range. */
164 if (msec < DURATION2 * (100-MIS)/100 ||
165 msec > DURATION2 * (100+MIS)/100)
166 {
167 PJ_LOG(3,(THIS_FILE,
168 "...error: slept for %d ms instead of %d ms "
169 "(outside %d%% err window)",
170 msec, DURATION2, MIS));
Benny Prijono99683ae2005-11-21 16:59:47 +0000171 PJ_TIME_VAL_SUB(t2, t1);
172 PJ_LOG(3,(THIS_FILE,
173 "...info: gettimeofday() reported duration is "
174 "%d msec",
175 PJ_TIME_VAL_MSEC(t2)));
176
177 return -76;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000178 }
179 }
180
181 /* All done. */
182 return 0;
183}
184
185int sleep_test()
186{
187 int rc;
188
189 rc = simple_sleep_test();
190 if (rc != PJ_SUCCESS)
191 return rc;
192
193 rc = sleep_duration_test();
194 if (rc != PJ_SUCCESS)
195 return rc;
196
197 return 0;
198}
199
200#else
201/* To prevent warning about "translation unit is empty"
202 * when this test is disabled.
203 */
204int dummy_sleep_test;
205#endif /* INCLUDE_SLEEP_TEST */