Tristan Matthews | 0a329cc | 2013-07-17 13:20:14 -0400 | [diff] [blame] | 1 | /* $Id: sleep.c 3553 2011-05-05 06:14:19Z nanang $ */ |
| 2 | /* |
| 3 | * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com) |
| 4 | * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> |
| 5 | * |
| 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 | |
| 56 | static int simple_sleep_test(void) |
| 57 | { |
| 58 | enum { COUNT = 10 }; |
| 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 | pj_time_val tv; |
| 66 | pj_parsed_time pt; |
| 67 | |
| 68 | rc = pj_thread_sleep(1000); |
| 69 | if (rc != PJ_SUCCESS) { |
| 70 | app_perror("...error: pj_thread_sleep()", rc); |
| 71 | return -10; |
| 72 | } |
| 73 | |
| 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 | |
| 87 | } |
| 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | static int sleep_duration_test(void) |
| 93 | { |
| 94 | enum { MIS = 20}; |
| 95 | unsigned duration[] = { 2000, 1000, 500, 200, 100 }; |
| 96 | unsigned i; |
| 97 | pj_status_t rc; |
| 98 | |
| 99 | PJ_LOG(3,(THIS_FILE, "..running sleep duration test")); |
| 100 | |
| 101 | /* Test pj_thread_sleep() and pj_gettimeofday() */ |
| 102 | for (i=0; i<PJ_ARRAY_SIZE(duration); ++i) { |
| 103 | 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 */ |
| 114 | rc = pj_thread_sleep(duration[i]); |
| 115 | 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. */ |
| 130 | if (msec < duration[i] * (100-MIS)/100 || |
| 131 | msec > duration[i] * (100+MIS)/100) |
| 132 | { |
| 133 | PJ_LOG(3,(THIS_FILE, |
| 134 | "...error: slept for %d ms instead of %d ms " |
| 135 | "(outside %d%% err window)", |
| 136 | msec, duration[i], MIS)); |
| 137 | return -30; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | |
| 142 | /* Test pj_thread_sleep() and pj_get_timestamp() and friends */ |
| 143 | for (i=0; i<PJ_ARRAY_SIZE(duration); ++i) { |
| 144 | pj_time_val t1, t2; |
| 145 | pj_timestamp start, stop; |
| 146 | pj_uint32_t msec; |
| 147 | |
| 148 | pj_thread_sleep(0); |
| 149 | |
| 150 | /* 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 */ |
| 161 | rc = pj_thread_sleep(duration[i]); |
| 162 | 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. */ |
| 174 | if (PJ_TIME_VAL_LT(t2, t1)) { |
| 175 | PJ_LOG(3,(THIS_FILE, "...error: t2 is less than t1!!")); |
| 176 | return -75; |
| 177 | } |
| 178 | |
| 179 | /* Get elapsed time in msec */ |
| 180 | msec = pj_elapsed_msec(&start, &stop); |
| 181 | |
| 182 | /* Check if it's within range. */ |
| 183 | if (msec < duration[i] * (100-MIS)/100 || |
| 184 | msec > duration[i] * (100+MIS)/100) |
| 185 | { |
| 186 | PJ_LOG(3,(THIS_FILE, |
| 187 | "...error: slept for %d ms instead of %d ms " |
| 188 | "(outside %d%% err window)", |
| 189 | msec, duration[i], MIS)); |
| 190 | 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; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | /* All done. */ |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | int 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 | */ |
| 223 | int dummy_sleep_test; |
| 224 | #endif /* INCLUDE_SLEEP_TEST */ |