Commit bd3c351e authored by Ian Murdock's avatar Ian Murdock
Browse files

Added test cases for event library.

parent b30d583b
# Makefile for building event system tests
#
# $Id: Makefile,v 1.1 2001-11-02 04:47:14 imurdock Exp $
CC = gcc
CFLAGS = -g -I. -I../lib -Wall -DDEBUG
LDFLAGS =
LIBS = -L../lib -levent
CFLAGS += `elvin-config --cflags`
LIBS += `elvin-config --libs`
MV = mv -f
RM = rm -f
tests = test-consume \
test-produce \
test-attr-consume \
test-attr-produce
%: %.c
$(CC) $(LDFLAGS) -o $@ $(CFLAGS) $< $(LIBS) $(LIBS)
default: $(tests)
clean:
$(RM) $(tests) $(addsuffix .o,$(tests))
distclean: clean
$(tests): ../lib/libevent.a
/* test-consume.c: Test delivery of events, with attributes (consumer). */
static char rcsid[] = "$Id: test-attr-consume.c,v 1.1 2001-11-02 04:47:14 imurdock Exp $";
#include <event.h>
static void callback(event_handle_t handle, event_notification_t notification,
void *data);
int
main(int argc, char **argv)
{
event_handle_t handle;
/* Register with the event system: */
handle = event_register();
if (handle == NULL) {
ERROR("could not register with event system\n");
return 1;
}
/* Subscribe to the test event: */
if (event_subscribe(handle, callback, EVENT_TEST, "event received")
== NULL)
{
ERROR("could not subscribe to test event\n");
return 1;
}
/* Begin the event loop, waiting to receive event notifications: */
event_main(handle);
/* Unregister with the event system: */
if (event_unregister(handle) == 0) {
ERROR("could not unregister with event system\n");
return 1;
}
return 0;
}
static void
callback(event_handle_t handle, event_notification_t notification, void *data)
{
char *message = (char *) data;
char *host;
event_type_t type;
double attr_double;
int32_t attr_int32;
int64_t attr_int64;
char *attr_string;
TRACE("the message is: %s\n", message);
if (event_notification_attr_get(handle, notification,
EVENT_ATTR_STRING, "host",
(event_attr_value_t *) &host)
== 0)
{
ERROR("could not get host attribute\n");
return;
}
TRACE("host: %s\n", host);
if (event_notification_attr_get(handle, notification,
EVENT_ATTR_STRING, "type",
(event_attr_value_t *) &type)
== 0)
{
ERROR("could not get type attribute\n");
return;
}
TRACE("type: %d\n", type);
if (event_notification_attr_get(handle, notification,
EVENT_ATTR_DOUBLE, "double",
(event_attr_value_t *) &attr_double)
== 0)
{
ERROR("could not get double attribute\n");
return;
}
TRACE("double: %f\n", attr_double);
if (event_notification_attr_get(handle, notification,
EVENT_ATTR_DOUBLE, "int32",
(event_attr_value_t *) &attr_int32)
== 0)
{
ERROR("could not get int32 attribute\n");
return;
}
TRACE("int32: %d\n", attr_int32);
if (event_notification_attr_get(handle, notification,
EVENT_ATTR_DOUBLE, "int64",
(event_attr_value_t *) &attr_int64)
== 0)
{
ERROR("could not get int64 attribute\n");
return;
}
TRACE("int64: %lld\n", attr_int64);
if (event_notification_attr_get(handle, notification,
EVENT_ATTR_DOUBLE, "string",
(event_attr_value_t *) &attr_string)
== 0)
{
ERROR("could not get string attribute\n");
return;
}
TRACE("string: \"%s\"\n", attr_string);
}
/* test-attr-produce.c: Test delivery of events, with attributes (producer). */
static char rcsid[] = "$Id: test-attr-produce.c,v 1.1 2001-11-02 04:47:14 imurdock Exp $";
#include <event.h>
int
main(int argc, char **argv)
{
event_handle_t handle;
event_notification_t notification;
/* Register with the event system: */
handle = event_register();
if (handle == NULL) {
ERROR("could not register with event system\n");
return 1;
}
/* Generate a test event, with some attributes: */
notification = event_notification_alloc(handle, EVENT_HOST_ANY,
EVENT_TEST);
if (notification == NULL) {
ERROR("could not allocate notification\n");
return 1;
}
if (event_notification_attr_put(handle, notification,
EVENT_ATTR_DOUBLE, "double",
(event_attr_value_t) 1.23)
== 0)
{
ERROR("could not put double attribute\n");
return 1;
}
if (event_notification_attr_put(handle, notification,
EVENT_ATTR_INT32, "int32",
(event_attr_value_t) 123)
== 0)
{
ERROR("could not put int32 attribute\n");
return 1;
}
if (event_notification_attr_put(handle, notification,
EVENT_ATTR_INT64, "int64",
(event_attr_value_t) 100000000000)
== 0)
{
ERROR("could not put int64 attribute\n");
return 1;
}
if (event_notification_attr_put(handle, notification,
EVENT_ATTR_STRING, "string",
(event_attr_value_t) "foo")
== 0)
{
ERROR("could not put string attribute\n");
return 1;
}
if (event_notify(handle, notification) == 0) {
ERROR("could not send test event notification\n");
return 1;
}
event_notification_free(handle, notification);
/* Unregister with the event system: */
if (event_unregister(handle) == 0) {
ERROR("could not unregister with event system\n");
return 1;
}
return 0;
}
/* test-consume.c: Test delivery of events (consumer). */
static char rcsid[] = "$Id: test-consume.c,v 1.1 2001-11-02 04:47:14 imurdock Exp $";
#include <event.h>
static void callback(event_handle_t handle, event_notification_t notification,
void *data);
int
main(int argc, char **argv)
{
event_handle_t handle;
/* Register with the event system: */
handle = event_register();
if (handle == NULL) {
ERROR("could not register with event system\n");
return 1;
}
/* Subscribe to the test event: */
if (event_subscribe(handle, callback, EVENT_TEST, "event received")
== NULL)
{
ERROR("could not subscribe to test event\n");
return 1;
}
/* Begin the event loop, waiting to receive event notifications: */
event_main(handle);
/* Unregister with the event system: */
if (event_unregister(handle) == 0) {
ERROR("could not unregister with event system\n");
return 1;
}
return 0;
}
static void
callback(event_handle_t handle, event_notification_t notification, void *data)
{
char *message = (char *) data;
char *host;
event_type_t type;
TRACE("the message is: %s\n", message);
if (event_notification_attr_get(handle, notification,
EVENT_ATTR_STRING, "host",
(event_attr_value_t *) &host)
== 0)
{
ERROR("could not get host attribute\n");
return;
}
TRACE("host: %s\n", host);
if (event_notification_attr_get(handle, notification,
EVENT_ATTR_STRING, "type",
(event_attr_value_t *) &type)
== 0)
{
ERROR("could not get type attribute\n");
return;
}
TRACE("type: %d\n", type);
}
/* test-produce.c: Test delivery of events (producer). */
static char rcsid[] = "$Id: test-produce.c,v 1.1 2001-11-02 04:47:14 imurdock Exp $";
#include <event.h>
int
main(int argc, char **argv)
{
event_handle_t handle;
event_notification_t notification;
/* Register with the event system: */
handle = event_register();
if (handle == NULL) {
ERROR("could not register with event system\n");
return 1;
}
/* Generate a test event: */
notification = event_notification_alloc(handle, EVENT_HOST_ANY,
EVENT_TEST);
if (notification == NULL) {
ERROR("could not allocate notification\n");
return 1;
}
if (event_notify(handle, notification) == 0) {
ERROR("could not send test event notification\n");
return 1;
}
event_notification_free(handle, notification);
/* Unregister with the event system: */
if (event_unregister(handle) == 0) {
ERROR("could not unregister with event system\n");
return 1;
}
return 0;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment