pjsip logo pjsip.org
Open source SIP stack and media stack for presence, im/instant messaging, and multimedia communication

HOME

SIP/media Features
High Performance SIP
Small Footprint SIP
Symbian Port

FAQ

Documentation

Licensing

Download

Development (Trac)

Projects using pjsip

Mailing List

Open Source Links


About: PJLIB, PJLIB-UTIL, PJSIP, and PJMEDIA are created by: Benny Prijono
<bennylp@pjsip.org>


 

Home --> Documentations --> PJLIB Reference

Test: Sleep, Time, and Timestamp

This file provides implementation of sleep_test().

Scope of the Test

This tests:

API tested:

This file is pjlib-test/sleep.c

/* $Id: sleep.c 2039 2008-06-20 22:44:47Z bennylp $ */
/* 
 * Copyright (C)2003-2008 Benny Prijono <benny@prijono.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 */
#include "test.h"

#if INCLUDE_SLEEP_TEST

#include <pjlib.h>

#define THIS_FILE   "sleep_test"

static int simple_sleep_test(void)
{
    enum { COUNT = 5 };
    int i;
    pj_status_t rc;
    
    PJ_LOG(3,(THIS_FILE, "..will write messages every 1 second:"));
    
    for (i=0; i<COUNT; ++i) {
        rc = pj_thread_sleep(1000);
        if (rc != PJ_SUCCESS) {
            app_perror("...error: pj_thread_sleep()", rc);
            return -10;
        }
        PJ_LOG(3,(THIS_FILE, "...wake up.."));
    }

    return 0;
}

static int sleep_duration_test(void)
{
    enum { MIS = 20, DURATION = 1000, DURATION2 = 500 };
    pj_status_t rc;

    PJ_LOG(3,(THIS_FILE, "..running sleep duration test"));

    /* Test pj_thread_sleep() and pj_gettimeofday() */
    {
        pj_time_val start, stop;
        pj_uint32_t msec;

        /* Mark start of test. */
        rc = pj_gettimeofday(&start);
        if (rc != PJ_SUCCESS) {
            app_perror("...error: pj_gettimeofday()", rc);
            return -10;
        }

        /* Sleep */
        rc = pj_thread_sleep(DURATION);
        if (rc != PJ_SUCCESS) {
            app_perror("...error: pj_thread_sleep()", rc);
            return -20;
        }

        /* Mark end of test. */
        rc = pj_gettimeofday(&stop);

        /* Calculate duration (store in stop). */
        PJ_TIME_VAL_SUB(stop, start);

        /* Convert to msec. */
        msec = PJ_TIME_VAL_MSEC(stop);

        /* Check if it's within range. */
        if (msec < DURATION * (100-MIS)/100 ||
            msec > DURATION * (100+MIS)/100)
        {
            PJ_LOG(3,(THIS_FILE, 
                      "...error: slept for %d ms instead of %d ms "
                      "(outside %d%% err window)",
                      msec, DURATION, MIS));
            return -30;
        }
    }


    /* Test pj_thread_sleep() and pj_get_timestamp() and friends */
    {
        pj_time_val t1, t2;
        pj_timestamp start, stop;
        pj_uint32_t msec;

        pj_thread_sleep(0);

        /* Mark start of test. */
        rc = pj_get_timestamp(&start);
        if (rc != PJ_SUCCESS) {
            app_perror("...error: pj_get_timestamp()", rc);
            return -60;
        }

        /* ..also with gettimeofday() */
        pj_gettimeofday(&t1);

        /* Sleep */
        rc = pj_thread_sleep(DURATION2);
        if (rc != PJ_SUCCESS) {
            app_perror("...error: pj_thread_sleep()", rc);
            return -70;
        }

        /* Mark end of test. */
        pj_get_timestamp(&stop);

        /* ..also with gettimeofday() */
        pj_gettimeofday(&t2);

        /* Compare t1 and t2. */
        if (PJ_TIME_VAL_LT(t2, t1)) {
            PJ_LOG(3,(THIS_FILE, "...error: t2 is less than t1!!"));
            return -75;
        }

        /* Get elapsed time in msec */
        msec = pj_elapsed_msec(&start, &stop);

        /* Check if it's within range. */
        if (msec < DURATION2 * (100-MIS)/100 ||
            msec > DURATION2 * (100+MIS)/100)
        {
            PJ_LOG(3,(THIS_FILE, 
                      "...error: slept for %d ms instead of %d ms "
                      "(outside %d%% err window)",
                      msec, DURATION2, MIS));
            PJ_TIME_VAL_SUB(t2, t1);
            PJ_LOG(3,(THIS_FILE, 
                      "...info: gettimeofday() reported duration is "
                      "%d msec",
                      PJ_TIME_VAL_MSEC(t2)));

            return -76;
        }
    }

    /* All done. */
    return 0;
}

int sleep_test()
{
    int rc;

    rc = simple_sleep_test();
    if (rc != PJ_SUCCESS)
        return rc;

    rc = sleep_duration_test();
    if (rc != PJ_SUCCESS)
        return rc;

    return 0;
}

#else
/* To prevent warning about "translation unit is empty"
 * when this test is disabled. 
 */
int dummy_sleep_test;
#endif  /* INCLUDE_SLEEP_TEST */

 


PJLIB Open Source, high performance, small footprint, and very very portable framework
(C)2001-2008 Benny Prijono