darling-libkqueue/test/main.c
mheily 736b3ad6bc Make the Linux backend completely independent from the POSIX backend.
Optimize the Linux backend to remove the use of a per-filter pollfd. 
Each knote now adds a pollable descriptor to a per-kqueue epollfd.

Windows and Solaris backends are not yet refactored and are broken.



git-svn-id: svn://svn.code.sf.net/p/libkqueue/code/trunk@408 fb4e3144-bc1c-4b72-a658-5bcd248dd7f7
2011-02-01 02:40:59 +00:00

248 lines
5.7 KiB
C

/*
* Copyright (c) 2009 Mark Heily <mark@heily.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include "common.h"
struct unit_test {
const char *ut_name;
int ut_enabled;
void (*ut_func)(int);
};
void
test_kqueue_descriptor_is_pollable(void)
{
int kq, rv;
struct kevent kev;
fd_set fds;
struct timeval tv;
if ((kq = kqueue()) < 0)
die("kqueue()");
test_no_kevents(kq);
kevent_add(kq, &kev, 2, EVFILT_TIMER, EV_ADD | EV_ONESHOT, 0, 1000, NULL);
test_no_kevents(kq);
FD_ZERO(&fds);
FD_SET(kq, &fds);
tv.tv_sec = 5;
tv.tv_usec = 0;
rv = select(1, &fds, NULL, NULL, &tv);
if (rv < 0)
die("select() error");
if (rv == 0)
die("select() no events");
if (!FD_ISSET(kq, &fds)) {
die("descriptor is not ready for reading");
}
close(kq);
}
/*
* DEADWOOD: fails on Solaris
* Test if calling fstat() on a socketpair returns unique ino_t and dev_t
* values. This is something that kqueue_gc() relies on.
*/
void
test_fstat_on_socketpair(void)
{
#ifdef _WIN32
return;
#else
struct stat sb[4];
int a[2], b[2];
int unique = 1;
if (socketpair(AF_UNIX, SOCK_STREAM, 0, a) < 0)
die("socketpair");
if (fstat(a[0], &sb[0]) < 0)
die("fstat");
if (fstat(a[1], &sb[1]) < 0)
die("fstat");
if (sb[0].st_ino == sb[1].st_ino) {
printf(" inodes=%zu %zu\n", sb[0].st_ino, sb[1].st_ino);
die("NO, inode numbers are not unique");
}
close(a[0]);
close(a[1]);
if (socketpair(AF_UNIX, SOCK_STREAM, 0, b) < 0)
die("socketpair");
if (fstat(b[0], &sb[2]) < 0)
die("fstat");
if (fstat(b[1], &sb[3]) < 0)
die("fstat");
if (sb[0].st_ino == sb[2].st_ino || sb[0].st_ino == sb[3].st_ino)
unique = 0;
if (sb[1].st_ino == sb[2].st_ino || sb[1].st_ino == sb[3].st_ino)
unique = 0;
printf(" inodes=%zu %zu %zu %zu\n",
sb[0].st_ino, sb[1].st_ino, sb[2].st_ino, sb[3].st_ino);
if (!unique)
die("ERROR - inode numbers are not unique");
#endif
}
/*
* Test the method for detecting when one end of a socketpair
* has been closed. This technique is used in kqueue_validate()
*/
static void
test_peer_close_detection(void)
{
#ifdef _WIN32
return;
//FIXME
#else
int sockfd[2];
char buf[1];
struct pollfd pfd;
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sockfd) < 0)
die("socketpair");
pfd.fd = sockfd[0];
pfd.events = POLLIN | POLLHUP;
pfd.revents = 0;
if (poll(&pfd, 1, 0) > 0)
die("unexpected data");
if (close(sockfd[1]) < 0)
die("close");
if (poll(&pfd, 1, 0) > 0) {
if (recv(sockfd[0], buf, sizeof(buf), MSG_PEEK | MSG_DONTWAIT) != 0)
die("failed to detect peer shutdown");
}
#endif
}
void
test_kqueue(void)
{
int kqfd;
if ((kqfd = kqueue()) < 0)
die("kqueue()");
test_no_kevents(kqfd);
if (close(kqfd) < 0)
die("close()");
}
void
test_ev_receipt(void)
{
int kq;
struct kevent kev;
if ((kq = kqueue()) < 0)
die("kqueue()");
#if HAVE_EV_RECEIPT
EV_SET(&kev, SIGUSR2, EVFILT_SIGNAL, EV_ADD | EV_RECEIPT, 0, 0, NULL);
if (kevent(kq, &kev, 1, &kev, 1, NULL) < 0)
die("kevent");
/* TODO: check the receipt */
close(kq);
#else
memset(&kev, 0, sizeof(kev));
puts("Skipped -- EV_RECEIPT is not available");
#endif
}
int
main(int argc, char **argv)
{
struct unit_test tests[] = {
#ifndef _WIN32
{ "socket", 1, test_evfilt_read },
{ "signal", 1, test_evfilt_signal },
#endif
#if FIXME
{ "proc", 1, test_evfilt_proc },
#endif
{ "timer", 1, test_evfilt_timer },
{ "vnode", 1, test_evfilt_vnode },
#if HAVE_EVFILT_USER
{ "user", 1, test_evfilt_user },
#endif
{ NULL, 0, NULL },
};
struct unit_test *test;
char *arg;
int match, kqfd;
/* If specific tests are requested, disable all tests by default */
if (argc > 1) {
for (test = &tests[0]; test->ut_name != NULL; test++) {
test->ut_enabled = 0;
}
}
while (argc > 1) {
match = 0;
arg = argv[1];
for (test = &tests[0]; test->ut_name != NULL; test++) {
if (strcmp(arg, test->ut_name) == 0) {
test->ut_enabled = 1;
match = 1;
break;
}
}
if (!match) {
printf("ERROR: invalid option: %s\n", arg);
exit(1);
} else {
printf("enabled test: %s\n", arg);
}
argv++;
argc--;
}
testing_begin();
test(peer_close_detection);
//DEADWOOD:test(fstat_on_socketpair);
test(kqueue);
if ((kqfd = kqueue()) < 0)
die("kqueue()");
for (test = &tests[0]; test->ut_name != NULL; test++) {
if (test->ut_enabled)
test->ut_func(kqfd);
}
test(ev_receipt);
/* TODO: this fails now, but would be good later
test(kqueue_descriptor_is_pollable);
*/
testing_end();
return (0);
}