blob: eaaac88c1d435ea04ef3f5062ad66fab123fac6b [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{
Benny Prijonof1a47b82009-03-30 18:22:16 +000058 enum { COUNT = 10 };
Benny Prijono5dcb38d2005-11-21 01:55:47 +000059 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) {
Benny Prijonof1a47b82009-03-30 18:22:16 +000065 pj_time_val tv;
66 pj_parsed_time pt;
67
Benny Prijono5dcb38d2005-11-21 01:55:47 +000068 rc = pj_thread_sleep(1000);
69 if (rc != PJ_SUCCESS) {
70 app_perror("...error: pj_thread_sleep()", rc);
71 return -10;
72 }
Benny Prijonof1a47b82009-03-30 18:22:16 +000073
74 rc = pj_gettimeofday(&tv);
75 if (rc != PJ_SUCCESS) {
76 app_perror("...error: pj_gettimeofday()", rc);
77 return -11;
78 }
79
80 pj_time_decode(&tv, &pt);
81
82 PJ_LOG(3,(THIS_FILE,
83 "...%04d-%02d-%02d %02d:%02d:%02d.%03d",
84 pt.year, pt.mon, pt.day,
85 pt.hour, pt.min, pt.sec, pt.msec));
86
Benny Prijono5dcb38d2005-11-21 01:55:47 +000087 }
88
89 return 0;
90}
91
92static int sleep_duration_test(void)
93{
Benny Prijonof1a47b82009-03-30 18:22:16 +000094 enum { MIS = 20};
95 unsigned duration[] = { 2000, 1000, 500, 200, 100 };
96 unsigned i;
Benny Prijono5dcb38d2005-11-21 01:55:47 +000097 pj_status_t rc;
98
99 PJ_LOG(3,(THIS_FILE, "..running sleep duration test"));
100
101 /* Test pj_thread_sleep() and pj_gettimeofday() */
Benny Prijonof1a47b82009-03-30 18:22:16 +0000102 for (i=0; i<PJ_ARRAY_SIZE(duration); ++i) {
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000103 pj_time_val start, stop;
104 pj_uint32_t msec;
105
106 /* Mark start of test. */
107 rc = pj_gettimeofday(&start);
108 if (rc != PJ_SUCCESS) {
109 app_perror("...error: pj_gettimeofday()", rc);
110 return -10;
111 }
112
113 /* Sleep */
Benny Prijonof1a47b82009-03-30 18:22:16 +0000114 rc = pj_thread_sleep(duration[i]);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000115 if (rc != PJ_SUCCESS) {
116 app_perror("...error: pj_thread_sleep()", rc);
117 return -20;
118 }
119
120 /* Mark end of test. */
121 rc = pj_gettimeofday(&stop);
122
123 /* Calculate duration (store in stop). */
124 PJ_TIME_VAL_SUB(stop, start);
125
126 /* Convert to msec. */
127 msec = PJ_TIME_VAL_MSEC(stop);
128
129 /* Check if it's within range. */
Benny Prijonof1a47b82009-03-30 18:22:16 +0000130 if (msec < duration[i] * (100-MIS)/100 ||
131 msec > duration[i] * (100+MIS)/100)
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000132 {
133 PJ_LOG(3,(THIS_FILE,
134 "...error: slept for %d ms instead of %d ms "
135 "(outside %d%% err window)",
Benny Prijonof1a47b82009-03-30 18:22:16 +0000136 msec, duration[i], MIS));
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000137 return -30;
138 }
139 }
140
141
142 /* Test pj_thread_sleep() and pj_get_timestamp() and friends */
Benny Prijonof1a47b82009-03-30 18:22:16 +0000143 for (i=0; i<PJ_ARRAY_SIZE(duration); ++i) {
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000144 pj_time_val t1, t2;
145 pj_timestamp start, stop;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000146 pj_uint32_t msec;
147
Benny Prijono99683ae2005-11-21 16:59:47 +0000148 pj_thread_sleep(0);
149
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000150 /* Mark start of test. */
151 rc = pj_get_timestamp(&start);
152 if (rc != PJ_SUCCESS) {
153 app_perror("...error: pj_get_timestamp()", rc);
154 return -60;
155 }
156
157 /* ..also with gettimeofday() */
158 pj_gettimeofday(&t1);
159
160 /* Sleep */
Benny Prijonof1a47b82009-03-30 18:22:16 +0000161 rc = pj_thread_sleep(duration[i]);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000162 if (rc != PJ_SUCCESS) {
163 app_perror("...error: pj_thread_sleep()", rc);
164 return -70;
165 }
166
167 /* Mark end of test. */
168 pj_get_timestamp(&stop);
169
170 /* ..also with gettimeofday() */
171 pj_gettimeofday(&t2);
172
173 /* Compare t1 and t2. */
Benny Prijono37e8d332006-01-20 21:03:36 +0000174 if (PJ_TIME_VAL_LT(t2, t1)) {
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000175 PJ_LOG(3,(THIS_FILE, "...error: t2 is less than t1!!"));
176 return -75;
177 }
178
Benny Prijono99683ae2005-11-21 16:59:47 +0000179 /* Get elapsed time in msec */
180 msec = pj_elapsed_msec(&start, &stop);
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000181
182 /* Check if it's within range. */
Benny Prijonof1a47b82009-03-30 18:22:16 +0000183 if (msec < duration[i] * (100-MIS)/100 ||
184 msec > duration[i] * (100+MIS)/100)
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000185 {
186 PJ_LOG(3,(THIS_FILE,
187 "...error: slept for %d ms instead of %d ms "
188 "(outside %d%% err window)",
Benny Prijonof1a47b82009-03-30 18:22:16 +0000189 msec, duration[i], MIS));
Benny Prijono99683ae2005-11-21 16:59:47 +0000190 PJ_TIME_VAL_SUB(t2, t1);
191 PJ_LOG(3,(THIS_FILE,
192 "...info: gettimeofday() reported duration is "
193 "%d msec",
194 PJ_TIME_VAL_MSEC(t2)));
195
196 return -76;
Benny Prijono5dcb38d2005-11-21 01:55:47 +0000197 }
198 }
199
200 /* All done. */
201 return 0;
202}
203
204int sleep_test()
205{
206 int rc;
207
208 rc = simple_sleep_test();
209 if (rc != PJ_SUCCESS)
210 return rc;
211
212 rc = sleep_duration_test();
213 if (rc != PJ_SUCCESS)
214 return rc;
215
216 return 0;
217}
218
219#else
220/* To prevent warning about "translation unit is empty"
221 * when this test is disabled.
222 */
223int dummy_sleep_test;
224#endif /* INCLUDE_SLEEP_TEST */