Skip to content

Commit

Permalink
tests: Add basic and speed timer list test
Browse files Browse the repository at this point in the history
Speed test is adding only 10000 items so it is reasonable
fast even with sorted linked list implementation.

Signed-off-by: Jan Friesse <jfriesse@redhat.com>
  • Loading branch information
jfriesse committed Mar 11, 2021
1 parent 404adbc commit 22e2ec1
Show file tree
Hide file tree
Showing 2 changed files with 194 additions and 1 deletion.
6 changes: 5 additions & 1 deletion tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ resources.log: rb.log log.log ipc.log

check_LTLIBRARIES =
check_PROGRAMS = array.test ipc.test list.test log.test loop.test \
map.test rb.test util.test \
map.test rb.test util.test tlist.test \
crash_test_dummy file_change_bytes
dist_check_SCRIPTS = start.test resources.test blackbox-segfault.sh

Expand Down Expand Up @@ -161,6 +161,10 @@ loop_test_SOURCES = check_loop.c
loop_test_CFLAGS = @CHECK_CFLAGS@
loop_test_LDADD = $(top_builddir)/lib/libqb.la @CHECK_LIBS@

tlist_test_SOURCES = check_tlist.c
tlist_test_CFLAGS = @CHECK_CFLAGS@
tlist_test_LDADD = $(top_builddir)/lib/libqb.la @CHECK_LIBS@

ipc_test_SOURCES = check_ipc.c
ipc_test_CFLAGS = @CHECK_CFLAGS@
ipc_test_LDADD = $(top_builddir)/lib/libqb.la @CHECK_LIBS@
Expand Down
189 changes: 189 additions & 0 deletions tests/check_tlist.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
/*
* Copyright (c) 2021 Red Hat, Inc.
*
* All rights reserved.
*
* Author: Jan Friesse <jfriesse@redhat.com>
*
* This file is part of libqb.
*
* libqb is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2.1 of the License, or
* (at your option) any later version.
*
* libqb 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with libqb. If not, see <http://www.gnu.org/licenses/>.
*/

#include "os_base.h"

#include "check_common.h"

#include "tlist.h"

#include <poll.h>

#include <qb/qbdefs.h>
#include <qb/qbutil.h>
#include <qb/qblog.h>

#define SHORT_TIMEOUT (100 * QB_TIME_NS_IN_MSEC)
#define LONG_TIMEOUT (60 * QB_TIME_NS_IN_SEC)

#define SPEED_TEST_NO_ITEMS 10000

static int timer_list_fn1_called = 0;

static void
timer_list_fn1(void *data)
{

ck_assert(data == &timer_list_fn1_called);

timer_list_fn1_called++;
}

static void
sleep_ns(long long int ns)
{

(void)poll(NULL, 0, (ns / QB_TIME_NS_IN_MSEC));
}

START_TEST(test_check_basic)
{
struct timerlist tlist;
timer_handle thandle;
int res;
uint64_t u64;

timerlist_init(&tlist);

/*
* Check adding short duration and calling callback
*/
res = timerlist_add_duration(&tlist, timer_list_fn1, &timer_list_fn1_called, SHORT_TIMEOUT / 2, &thandle);
ck_assert_int_eq(res, 0);

sleep_ns(SHORT_TIMEOUT);
u64 = timerlist_msec_duration_to_expire(&tlist);
ck_assert(u64 == 0);

timer_list_fn1_called = 0;
timerlist_expire(&tlist);
ck_assert_int_eq(timer_list_fn1_called, 1);

u64 = timerlist_msec_duration_to_expire(&tlist);
ck_assert(u64 == -1);

/*
* Check callback is not called (long timeout)
*/
res = timerlist_add_duration(&tlist, timer_list_fn1, &timer_list_fn1_called, LONG_TIMEOUT / 2, &thandle);
ck_assert_int_eq(res, 0);

sleep_ns(SHORT_TIMEOUT);
u64 = timerlist_msec_duration_to_expire(&tlist);
ck_assert(u64 > 0);

timer_list_fn1_called = 0;
timerlist_expire(&tlist);
ck_assert_int_eq(timer_list_fn1_called, 0);

u64 = timerlist_msec_duration_to_expire(&tlist);
ck_assert(u64 > 0);

/*
* Delete timer
*/
timerlist_del(&tlist, thandle);
u64 = timerlist_msec_duration_to_expire(&tlist);
ck_assert(u64 == -1);
}
END_TEST

START_TEST(test_check_speed)
{
struct timerlist tlist;
timer_handle thandle[SPEED_TEST_NO_ITEMS];
int res;
uint64_t u64;
int i;

timerlist_init(&tlist);

/*
* Check adding a lot of short duration and deleting
*/
for (i = 0; i < SPEED_TEST_NO_ITEMS; i++) {
res = timerlist_add_duration(&tlist, timer_list_fn1, &timer_list_fn1_called,
SHORT_TIMEOUT / 2, &thandle[i]);
ck_assert_int_eq(res, 0);
}

for (i = 0; i < SPEED_TEST_NO_ITEMS; i++) {
timerlist_del(&tlist, thandle[i]);
}

u64 = timerlist_msec_duration_to_expire(&tlist);
ck_assert(u64 == -1);

/*
* Check adding a lot of short duration and calling callback
*/
for (i = 0; i < SPEED_TEST_NO_ITEMS; i++) {
res = timerlist_add_duration(&tlist, timer_list_fn1, &timer_list_fn1_called,
SHORT_TIMEOUT / 2, &thandle[i]);
ck_assert_int_eq(res, 0);
}

u64 = timerlist_msec_duration_to_expire(&tlist);
ck_assert(u64 != -1);

sleep_ns(SHORT_TIMEOUT);

timer_list_fn1_called = 0;
timerlist_expire(&tlist);
ck_assert_int_eq(timer_list_fn1_called, SPEED_TEST_NO_ITEMS);

u64 = timerlist_msec_duration_to_expire(&tlist);
ck_assert(u64 == -1);
}
END_TEST

static Suite *tlist_suite(void)
{
TCase *tc;
Suite *s = suite_create("tlist");

add_tcase(s, tc, test_check_basic);
add_tcase(s, tc, test_check_speed, 30);

return s;
}

int32_t main(void)
{
int32_t number_failed;

Suite *s = tlist_suite();
SRunner *sr = srunner_create(s);

qb_log_init("check", LOG_USER, LOG_EMERG);
atexit(qb_log_fini);
qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_ENABLED, QB_FALSE);
qb_log_filter_ctl(QB_LOG_STDERR, QB_LOG_FILTER_ADD,
QB_LOG_FILTER_FILE, "*", LOG_INFO);
qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, QB_TRUE);

srunner_run_all(sr, CK_VERBOSE);
number_failed = srunner_ntests_failed(sr);
srunner_free(sr);
return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}

0 comments on commit 22e2ec1

Please sign in to comment.