1 /*
2  * Copyright (c) 2001-2002, 2004 Sendmail, Inc. and its suppliers.
3  *      All rights reserved.
4  *
5  * By using this file, you agree to the terms and conditions set
6  * forth in the LICENSE file which can be found at the top level of
7  * the sendmail distribution.
8  */
9 
10 #pragma ident	"%Z%%M%	%I%	%E% SMI"
11 
12 #include <sm/gen.h>
13 SM_RCSID("@(#)$Id: t-event.c,v 1.13 2005/06/14 23:07:20 ca Exp $")
14 
15 #include <stdio.h>
16 
17 #include <stdlib.h>
18 #include <unistd.h>
19 # include <sys/wait.h>
20 #if SM_CONF_SETITIMER
21 # include <sm/time.h>
22 #endif /* SM_CONF_SETITIMER */
23 
24 #include <sm/clock.h>
25 #include <sm/test.h>
26 
27 static void	evcheck __P((int));
28 static void	ev1 __P((int));
29 
30 static int check;
31 
32 static void
evcheck(arg)33 evcheck(arg)
34 	int arg;
35 {
36 	SM_TEST(arg == 3);
37 	SM_TEST(check == 0);
38 	check++;
39 }
40 
41 static void
ev1(arg)42 ev1(arg)
43 	int arg;
44 {
45 	SM_TEST(arg == 1);
46 }
47 
48 /* define as x if you want debug output */
49 #define DBG_OUT(x)
50 
51 int
main(argc,argv)52 main(argc, argv)
53 	int argc;
54 	char *argv[];
55 {
56 	SM_EVENT *ev;
57 
58 	sm_test_begin(argc, argv, "test event handling");
59 	fprintf(stdout, "This test may hang. If there is no output within twelve seconds, abort it\nand recompile with -DSM_CONF_SETITIMER=%d\n",
60 		SM_CONF_SETITIMER == 0 ? 1 : 0);
61 	sleep(1);
62 	SM_TEST(1 == 1);
63 	DBG_OUT(fprintf(stdout, "We're back, test 1 seems to work.\n"));
64 	ev = sm_seteventm(1000, ev1, 1);
65 	sleep(1);
66 	SM_TEST(2 == 2);
67 	DBG_OUT(fprintf(stdout, "We're back, test 2 seems to work.\n"));
68 
69 	/* schedule an event in 9s */
70 	ev = sm_seteventm(9000, ev1, 2);
71 	sleep(1);
72 
73 	/* clear the event before it can fire */
74 	sm_clrevent(ev);
75 	SM_TEST(3 == 3);
76 	DBG_OUT(fprintf(stdout, "We're back, test 3 seems to work.\n"));
77 
78 	/* schedule an event in 1s */
79 	check = 0;
80 	ev = sm_seteventm(1000, evcheck, 3);
81 	sleep(2);
82 
83 	/* clear the event */
84 	sm_clrevent(ev);
85 	SM_TEST(4 == 4);
86 	SM_TEST(check == 1);
87 	DBG_OUT(fprintf(stdout, "We're back, test 4 seems to work.\n"));
88 
89 	return sm_test_end();
90 }
91