t-event.c revision 157001
1116743Ssam/*
2186904Ssam * Copyright (c) 2001-2002, 2004 Sendmail, Inc. and its suppliers.
3116743Ssam *      All rights reserved.
4116743Ssam *
5116743Ssam * By using this file, you agree to the terms and conditions set
6116743Ssam * forth in the LICENSE file which can be found at the top level of
7116743Ssam * the sendmail distribution.
8116743Ssam */
9116743Ssam
10116743Ssam#include <sm/gen.h>
11116743SsamSM_RCSID("@(#)$Id: t-event.c,v 1.13 2005/06/14 23:07:20 ca Exp $")
12116743Ssam
13116743Ssam#include <stdio.h>
14116743Ssam
15116743Ssam#include <stdlib.h>
16116743Ssam#include <unistd.h>
17116743Ssam# include <sys/wait.h>
18116743Ssam#if SM_CONF_SETITIMER
19116743Ssam# include <sm/time.h>
20116743Ssam#endif /* SM_CONF_SETITIMER */
21116743Ssam
22116743Ssam#include <sm/clock.h>
23116743Ssam#include <sm/test.h>
24116743Ssam
25116743Ssamstatic void	evcheck __P((int));
26116743Ssamstatic void	ev1 __P((int));
27116743Ssam
28116743Ssamstatic int check;
29116743Ssam
30116743Ssamstatic void
31116743Ssamevcheck(arg)
32116743Ssam	int arg;
33116743Ssam{
34116743Ssam	SM_TEST(arg == 3);
35116743Ssam	SM_TEST(check == 0);
36116743Ssam	check++;
37116743Ssam}
38116743Ssam
39116743Ssamstatic void
40116743Ssamev1(arg)
41116743Ssam	int arg;
42155492Ssam{
43138570Ssam	SM_TEST(arg == 1);
44116743Ssam}
45116743Ssam
46116743Ssam/* define as x if you want debug output */
47138570Ssam#define DBG_OUT(x)
48116743Ssam
49138570Ssamint
50116743Ssammain(argc, argv)
51116743Ssam	int argc;
52116743Ssam	char *argv[];
53116743Ssam{
54116743Ssam	SM_EVENT *ev;
55116743Ssam
56116743Ssam	sm_test_begin(argc, argv, "test event handling");
57116743Ssam	fprintf(stdout, "This test may hang. If there is no output within twelve seconds, abort it\nand recompile with -DSM_CONF_SETITIMER=%d\n",
58116743Ssam		SM_CONF_SETITIMER == 0 ? 1 : 0);
59116743Ssam	sleep(1);
60116743Ssam	SM_TEST(1 == 1);
61116743Ssam	DBG_OUT(fprintf(stdout, "We're back, test 1 seems to work.\n"));
62116743Ssam	ev = sm_seteventm(1000, ev1, 1);
63116743Ssam	sleep(1);
64116743Ssam	SM_TEST(2 == 2);
65116743Ssam	DBG_OUT(fprintf(stdout, "We're back, test 2 seems to work.\n"));
66116743Ssam
67116743Ssam	/* schedule an event in 9s */
68116743Ssam	ev = sm_seteventm(9000, ev1, 2);
69116743Ssam	sleep(1);
70127779Ssam
71127779Ssam	/* clear the event before it can fire */
72170530Ssam	sm_clrevent(ev);
73170530Ssam	SM_TEST(3 == 3);
74116743Ssam	DBG_OUT(fprintf(stdout, "We're back, test 3 seems to work.\n"));
75116743Ssam
76116743Ssam	/* schedule an event in 1s */
77116743Ssam	check = 0;
78116743Ssam	ev = sm_seteventm(1000, evcheck, 3);
79116743Ssam	sleep(2);
80138570Ssam
81116743Ssam	/* clear the event */
82218689Sadrian	sm_clrevent(ev);
83119147Ssam	SM_TEST(4 == 4);
84127779Ssam	SM_TEST(check == 1);
85138570Ssam	DBG_OUT(fprintf(stdout, "We're back, test 4 seems to work.\n"));
86138570Ssam
87119147Ssam	return sm_test_end();
88138570Ssam}
89138570Ssam