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