asterisk/tests/test_poll.c

249 lines
7.1 KiB
C
Raw Normal View History

/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 2010, Digium, Inc.
*
* Tilghman Lesher <tlesher AT digium DOT com>
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*!
* \file
* \brief Poll Tests
*
* \author\verbatim Tilghman Lesher <tlesher AT digium DOT com> \endverbatim
*
* Verify that the various poll implementations work as desired (ast_poll, ast_poll2)
* \ingroup tests
*/
/*** MODULEINFO
<depend>TEST_FRAMEWORK</depend>
<support_level>core</support_level>
***/
#include "asterisk.h"
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
git migration: Refactor the ASTERISK_FILE_VERSION macro Git does not support the ability to replace a token with a version string during check-in. While it does have support for replacing a token on clone, this is somewhat sub-optimal: the token is replaced with the object hash, which is not particularly easy for human consumption. What's more, in practice, the source file version was often not terribly useful. Generally, when triaging bugs, the overall version of Asterisk is far more useful than an individual SVN version of a file. As a result, this patch removes Asterisk's support for showing source file versions. Specifically, it does the following: * Rename ASTERISK_FILE_VERSION macro to ASTERISK_REGISTER_FILE, and remove passing the version in with the macro. Other facilities than 'core show file version' make use of the file names, such as setting a debug level only on a specific file. As such, the act of registering source files with the Asterisk core still has use. The macro rename now reflects the new macro purpose. * main/asterisk: - Refactor the file_version structure to reflect that it no longer tracks a version field. - Remove the "core show file version" CLI command. Without the file version, it is no longer useful. - Remove the ast_file_version_find function. The file version is no longer tracked. - Rename ast_register_file_version/ast_unregister_file_version to ast_register_file/ast_unregister_file, respectively. * main/manager: Remove value from the Version key of the ModuleCheck Action. The actual key itself has not been removed, as doing so would absolutely constitute a backwards incompatible change. However, since the file version is no longer tracked, there is no need to attempt to include it in the Version key. * UPGRADE: Add notes for: - Modification to the ModuleCheck AMI Action - Removal of the "core show file version" CLI command Change-Id: I6cf0ff280e1668bf4957dc21f32a5ff43444a40e
2015-04-12 02:38:22 +00:00
ASTERISK_REGISTER_FILE()
#include "asterisk/utils.h"
#include "asterisk/module.h"
#include "asterisk/test.h"
#include "asterisk/poll-compat.h"
static void *failsafe_cancel(void *vparent)
{
pthread_t parent = (pthread_t) (long) vparent;
sleep(1);
pthread_testcancel();
pthread_kill(parent, SIGURG);
sleep(1);
pthread_testcancel();
pthread_kill(parent, SIGURG);
sleep(1);
pthread_testcancel();
pthread_kill(parent, SIGURG);
pthread_exit(NULL);
}
#define RESET for (i = 0; i < 4; i++) { pfd[i].revents = 0; }
AST_TEST_DEFINE(poll_test)
{
#define FDNO 3
int fd[2], res = AST_TEST_PASS, i, res2;
int rdblocker[2];
#if FDNO > 3
int wrblocker[2], consec_interrupt = 0;
#endif
struct pollfd pfd[4] = { { .events = POLLOUT, }, { .events = POLLIN, }, { .events = POLLIN }, { .events = POLLOUT } };
pthread_t failsafe_tid;
struct timeval tv = { 0, 0 };
#if FDNO > 3
char garbage[256] =
"abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ@/"
"abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ@/"
"abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ@/"
"abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ@/";
#endif
switch (cmd) {
case TEST_INIT:
info->name = "poll_test";
info->category = "/main/poll/";
info->summary = "unit test for the ast_poll() API";
info->description =
"Verifies behavior for the ast_poll() API call\n";
return AST_TEST_NOT_RUN;
case TEST_EXECUTE:
break;
}
ast_test_status_update(test, "Creating handle that should NEVER block on write\n");
if ((fd[0] = open("/dev/null", O_WRONLY)) < 0) {
ast_test_status_update(test, "Unable to open a writable handle to /dev/null: %s\n", strerror(errno));
return AST_TEST_FAIL;
}
ast_test_status_update(test, "Creating handle that should NEVER block on read\n");
if ((fd[1] = open("/dev/zero", O_RDONLY)) < 0) {
ast_test_status_update(test, "Unable to open a readable handle to /dev/zero: %s\n", strerror(errno));
close(fd[0]);
return AST_TEST_FAIL;
}
ast_test_status_update(test, "Creating handle that should block on read\n");
if (pipe(rdblocker) < 0) {
ast_test_status_update(test, "Unable to open a pipe: %s\n", strerror(errno));
close(fd[0]);
close(fd[1]);
return AST_TEST_FAIL;
}
#if FDNO > 3
ast_test_status_update(test, "Creating handle that should block on write\n");
if (pipe(wrblocker) < 0) {
ast_test_status_update(test, "Unable to open a pipe: %s\n", strerror(errno));
close(fd[0]);
close(fd[1]);
close(rdblocker[0]);
close(rdblocker[1]);
return AST_TEST_FAIL;
}
ast_test_status_update(test, "Starting thread to ensure we don't block forever\n");
if (ast_pthread_create_background(&failsafe_tid, NULL, failsafe_cancel, (void *) (long) pthread_self())) {
ast_test_status_update(test, "Unable to start failsafe thread\n");
close(fd[0]);
close(fd[1]);
close(fd[2]);
close(rdblocker[0]);
close(rdblocker[1]);
close(wrblocker[0]);
close(wrblocker[1]);
return AST_TEST_FAIL;
}
/* Fill the pipe full of data */
ast_test_status_update(test, "Making pipe block on write\n");
for (i = 0; i < 4096; i++) { /* 1MB of data should be more than enough for any pipe */
errno = 0;
if (write(wrblocker[1], garbage, sizeof(garbage)) < sizeof(garbage)) {
ast_test_status_update(test, "Got %d\n", errno);
if (errno == EINTR && ++consec_interrupt > 1) {
break;
}
} else {
consec_interrupt = 0;
}
}
ast_test_status_update(test, "Cancelling failsafe thread.\n");
pthread_cancel(failsafe_tid);
pthread_kill(failsafe_tid, SIGURG);
pthread_join(failsafe_tid, NULL);
#endif
pfd[0].fd = fd[0];
pfd[1].fd = fd[1];
pfd[2].fd = rdblocker[0];
#if FDNO > 3
pfd[3].fd = wrblocker[1];
#endif
/* Need to ensure the infinite timeout doesn't stall the process */
ast_test_status_update(test, "Starting thread to ensure we don't block forever\n");
if (ast_pthread_create_background(&failsafe_tid, NULL, failsafe_cancel, (void *) (long) pthread_self())) {
ast_test_status_update(test, "Unable to start failsafe thread\n");
close(fd[0]);
close(fd[1]);
close(rdblocker[0]);
close(rdblocker[1]);
#if FDNO > 3
close(wrblocker[0]);
close(wrblocker[1]);
#endif
return AST_TEST_FAIL;
}
RESET;
if ((res2 = ast_poll(pfd, FDNO, -1)) != 2) {
ast_test_status_update(test, "ast_poll does not return that only two handles are available (inf timeout): %d, %s\n", res2, res2 == -1 ? strerror(errno) : "");
res = AST_TEST_FAIL;
}
RESET;
if ((res2 = ast_poll2(pfd, FDNO, NULL)) != 2) {
ast_test_status_update(test, "ast_poll2 does not return that only two handles are available (inf timeout): %d %s\n", res2, res2 == -1 ? strerror(errno) : "");
res = AST_TEST_FAIL;
}
ast_test_status_update(test, "Cancelling failsafe thread.\n");
pthread_cancel(failsafe_tid);
pthread_kill(failsafe_tid, SIGURG);
pthread_join(failsafe_tid, NULL);
RESET;
if (ast_poll(pfd, FDNO, 0) != 2) {
ast_test_status_update(test, "ast_poll does not return that only two handles are available (0 timeout): %d, %s\n", res2, res2 == -1 ? strerror(errno) : "");
res = AST_TEST_FAIL;
}
RESET;
if (ast_poll2(pfd, FDNO, &tv) != 2) {
ast_test_status_update(test, "ast_poll2 does not return that only two handles are available (0 timeout): %d, %s\n", res2, res2 == -1 ? strerror(errno) : "");
res = AST_TEST_FAIL;
}
RESET;
if (ast_poll(pfd, FDNO, 1) != 2) {
ast_test_status_update(test, "ast_poll does not return that only two handles are available (1ms timeout): %d, %s\n", res2, res2 == -1 ? strerror(errno) : "");
res = AST_TEST_FAIL;
}
tv.tv_usec = 1000;
if (ast_poll2(pfd, FDNO, &tv) != 2) {
ast_test_status_update(test, "ast_poll2 does not return that only two handles are available (1ms timeout): %d, %s\n", res2, res2 == -1 ? strerror(errno) : "");
res = AST_TEST_FAIL;
}
close(fd[0]);
close(fd[1]);
close(rdblocker[0]);
close(rdblocker[1]);
#if FDNO > 3
close(wrblocker[0]);
close(wrblocker[1]);
#endif
return res;
}
static int unload_module(void)
{
AST_TEST_UNREGISTER(poll_test);
return 0;
}
static int load_module(void)
{
AST_TEST_REGISTER(poll_test);
return AST_MODULE_LOAD_SUCCESS;
}
AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Poll test");