Convert scheduler API entry order test to the test API.

Review: https://reviewboard.asterisk.org/r/470/


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@242185 65c4cc65-6c06-0410-ace0-fbb531ad65f3
This commit is contained in:
Russell Bryant 2010-01-22 04:50:38 +00:00
parent 8f4383d1fa
commit 2a6d62e94d
1 changed files with 39 additions and 37 deletions

View File

@ -34,122 +34,123 @@
ASTERISK_FILE_VERSION(__FILE__, "$Revision$") ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include "asterisk/module.h" #include "asterisk/module.h"
#include "asterisk/cli.h"
#include "asterisk/utils.h" #include "asterisk/utils.h"
#include "asterisk/sched.h" #include "asterisk/sched.h"
#include "asterisk/test.h"
static int sched_cb(const void *data) static int sched_cb(const void *data)
{ {
return 0; return 0;
} }
static char *handle_cli_sched_test(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a) AST_TEST_DEFINE(sched_test_order)
{ {
struct sched_context *con; struct sched_context *con;
char *res = CLI_FAILURE; enum ast_test_result_state res = AST_TEST_FAIL;
int id1, id2, id3, wait; int id1, id2, id3, wait;
switch (cmd) { switch (cmd) {
case CLI_INIT: case TEST_INIT:
e->command = "sched test"; info->name = "sched_test_order";
e->usage = "" info->category = "main/sched/";
"Usage: sched test\n" info->summary = "Test ordering of events in the scheduler API";
" Test scheduler entry ordering.\n" info->description =
""; "This test ensures that events are properly ordered by the "
return NULL; "time they are scheduled to execute in the scheduler API.";
case CLI_GENERATE: return AST_TEST_NOT_RUN;
return NULL; case TEST_EXECUTE:
break;
} }
if (a->argc != e->args) {
return CLI_SHOWUSAGE;
}
ast_cli(a->fd, "Testing scheduler entry ordering ...\n");
if (!(con = sched_context_create())) { if (!(con = sched_context_create())) {
ast_cli(a->fd, "Test failed - could not create scheduler context\n"); ast_str_set(&args->ast_test_error_str, 0,
return CLI_FAILURE; "Test failed - could not create scheduler context\n");
return AST_TEST_FAIL;
} }
/* Add 3 scheduler entries, and then remove them, ensuring that the result /* Add 3 scheduler entries, and then remove them, ensuring that the result
* of ast_sched_wait() looks appropriate at each step along the way. */ * of ast_sched_wait() looks appropriate at each step along the way. */
if ((wait = ast_sched_wait(con)) != -1) { if ((wait = ast_sched_wait(con)) != -1) {
ast_cli(a->fd, "ast_sched_wait() should have returned -1, returned '%d'\n", ast_str_set(&args->ast_test_error_str, 0,
"ast_sched_wait() should have returned -1, returned '%d'\n",
wait); wait);
goto return_cleanup; goto return_cleanup;
} }
if ((id1 = ast_sched_add(con, 100000, sched_cb, NULL)) == -1) { if ((id1 = ast_sched_add(con, 100000, sched_cb, NULL)) == -1) {
ast_cli(a->fd, "Failed to add scheduler entry\n"); ast_str_set(&args->ast_test_error_str, 0, "Failed to add scheduler entry\n");
goto return_cleanup; goto return_cleanup;
} }
if ((wait = ast_sched_wait(con)) > 100000) { if ((wait = ast_sched_wait(con)) > 100000) {
ast_cli(a->fd, "ast_sched_wait() should have returned <= 100000, returned '%d'\n", ast_str_set(&args->ast_test_error_str, 0,
"ast_sched_wait() should have returned <= 100000, returned '%d'\n",
wait); wait);
goto return_cleanup; goto return_cleanup;
} }
if ((id2 = ast_sched_add(con, 10000, sched_cb, NULL)) == -1) { if ((id2 = ast_sched_add(con, 10000, sched_cb, NULL)) == -1) {
ast_cli(a->fd, "Failed to add scheduler entry\n"); ast_str_set(&args->ast_test_error_str, 0, "Failed to add scheduler entry\n");
goto return_cleanup; goto return_cleanup;
} }
if ((wait = ast_sched_wait(con)) > 10000) { if ((wait = ast_sched_wait(con)) > 10000) {
ast_cli(a->fd, "ast_sched_wait() should have returned <= 10000, returned '%d'\n", ast_str_set(&args->ast_test_error_str, 0,
"ast_sched_wait() should have returned <= 10000, returned '%d'\n",
wait); wait);
goto return_cleanup; goto return_cleanup;
} }
if ((id3 = ast_sched_add(con, 1000, sched_cb, NULL)) == -1) { if ((id3 = ast_sched_add(con, 1000, sched_cb, NULL)) == -1) {
ast_cli(a->fd, "Failed to add scheduler entry\n"); ast_str_set(&args->ast_test_error_str, 0, "Failed to add scheduler entry\n");
goto return_cleanup; goto return_cleanup;
} }
if ((wait = ast_sched_wait(con)) > 1000) { if ((wait = ast_sched_wait(con)) > 1000) {
ast_cli(a->fd, "ast_sched_wait() should have returned <= 1000, returned '%d'\n", ast_str_set(&args->ast_test_error_str, 0,
"ast_sched_wait() should have returned <= 1000, returned '%d'\n",
wait); wait);
goto return_cleanup; goto return_cleanup;
} }
if (ast_sched_del(con, id3) == -1) { if (ast_sched_del(con, id3) == -1) {
ast_cli(a->fd, "Failed to remove scheduler entry\n"); ast_str_set(&args->ast_test_error_str, 0, "Failed to remove scheduler entry\n");
goto return_cleanup; goto return_cleanup;
} }
if ((wait = ast_sched_wait(con)) <= 1000) { if ((wait = ast_sched_wait(con)) <= 1000) {
ast_cli(a->fd, "ast_sched_wait() should have returned > 1000, returned '%d'\n", ast_str_set(&args->ast_test_error_str, 0,
"ast_sched_wait() should have returned > 1000, returned '%d'\n",
wait); wait);
goto return_cleanup; goto return_cleanup;
} }
if (ast_sched_del(con, id2) == -1) { if (ast_sched_del(con, id2) == -1) {
ast_cli(a->fd, "Failed to remove scheduler entry\n"); ast_str_set(&args->ast_test_error_str, 0, "Failed to remove scheduler entry\n");
goto return_cleanup; goto return_cleanup;
} }
if ((wait = ast_sched_wait(con)) <= 10000) { if ((wait = ast_sched_wait(con)) <= 10000) {
ast_cli(a->fd, "ast_sched_wait() should have returned > 10000, returned '%d'\n", ast_str_set(&args->ast_test_error_str, 0,
"ast_sched_wait() should have returned > 10000, returned '%d'\n",
wait); wait);
goto return_cleanup; goto return_cleanup;
} }
if (ast_sched_del(con, id1) == -1) { if (ast_sched_del(con, id1) == -1) {
ast_cli(a->fd, "Failed to remove scheduler entry\n"); ast_str_set(&args->ast_test_error_str, 0, "Failed to remove scheduler entry\n");
goto return_cleanup; goto return_cleanup;
} }
if ((wait = ast_sched_wait(con)) != -1) { if ((wait = ast_sched_wait(con)) != -1) {
ast_cli(a->fd, "ast_sched_wait() should have returned -1, returned '%d'\n", ast_str_set(&args->ast_test_error_str, 0,
"ast_sched_wait() should have returned -1, returned '%d'\n",
wait); wait);
goto return_cleanup; goto return_cleanup;
} }
res = CLI_SUCCESS; res = AST_TEST_PASS;
ast_cli(a->fd, "Test passed!\n");
return_cleanup: return_cleanup:
sched_context_destroy(con); sched_context_destroy(con);
@ -233,17 +234,18 @@ return_cleanup:
static struct ast_cli_entry cli_sched[] = { static struct ast_cli_entry cli_sched[] = {
AST_CLI_DEFINE(handle_cli_sched_bench, "Benchmark ast_sched add/del performance"), AST_CLI_DEFINE(handle_cli_sched_bench, "Benchmark ast_sched add/del performance"),
AST_CLI_DEFINE(handle_cli_sched_test, "Test scheduler entry ordering"),
}; };
static int unload_module(void) static int unload_module(void)
{ {
AST_TEST_UNREGISTER(sched_test_order);
ast_cli_unregister_multiple(cli_sched, ARRAY_LEN(cli_sched)); ast_cli_unregister_multiple(cli_sched, ARRAY_LEN(cli_sched));
return 0; return 0;
} }
static int load_module(void) static int load_module(void)
{ {
AST_TEST_REGISTER(sched_test_order);
ast_cli_register_multiple(cli_sched, ARRAY_LEN(cli_sched)); ast_cli_register_multiple(cli_sched, ARRAY_LEN(cli_sched));
return AST_MODULE_LOAD_SUCCESS; return AST_MODULE_LOAD_SUCCESS;
} }