xref: /freebsd/contrib/sendmail/libsm/t-event.c (revision 0b57cec536236d46e3dba9bd041533462f33dbb7)
1 /*
2  * Copyright (c) 2001-2002, 2004 Proofpoint, 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 #include <sm/gen.h>
11 SM_RCSID("@(#)$Id: t-event.c,v 1.14 2013-11-22 20:51:43 ca Exp $")
12 
13 #include <stdio.h>
14 
15 #include <stdlib.h>
16 #include <unistd.h>
17 # include <sys/wait.h>
18 #if SM_CONF_SETITIMER
19 # include <sm/time.h>
20 #endif /* SM_CONF_SETITIMER */
21 
22 #include <sm/clock.h>
23 #include <sm/test.h>
24 
25 static void	evcheck __P((int));
26 static void	ev1 __P((int));
27 
28 static int check;
29 
30 static void
31 evcheck(arg)
32 	int arg;
33 {
34 	SM_TEST(arg == 3);
35 	SM_TEST(check == 0);
36 	check++;
37 }
38 
39 static void
40 ev1(arg)
41 	int arg;
42 {
43 	SM_TEST(arg == 1);
44 }
45 
46 /* define as x if you want debug output */
47 #define DBG_OUT(x)
48 
49 int
50 main(argc, argv)
51 	int argc;
52 	char *argv[];
53 {
54 	SM_EVENT *ev;
55 
56 	sm_test_begin(argc, argv, "test event handling");
57 	fprintf(stdout, "This test may hang. If there is no output within twelve seconds, abort it\nand recompile with -DSM_CONF_SETITIMER=%d\n",
58 		SM_CONF_SETITIMER == 0 ? 1 : 0);
59 	sleep(1);
60 	SM_TEST(1 == 1);
61 	DBG_OUT(fprintf(stdout, "We're back, test 1 seems to work.\n"));
62 	ev = sm_seteventm(1000, ev1, 1);
63 	sleep(1);
64 	SM_TEST(2 == 2);
65 	DBG_OUT(fprintf(stdout, "We're back, test 2 seems to work.\n"));
66 
67 	/* schedule an event in 9s */
68 	ev = sm_seteventm(9000, ev1, 2);
69 	sleep(1);
70 
71 	/* clear the event before it can fire */
72 	sm_clrevent(ev);
73 	SM_TEST(3 == 3);
74 	DBG_OUT(fprintf(stdout, "We're back, test 3 seems to work.\n"));
75 
76 	/* schedule an event in 1s */
77 	check = 0;
78 	ev = sm_seteventm(1000, evcheck, 3);
79 	sleep(2);
80 
81 	/* clear the event */
82 	sm_clrevent(ev);
83 	SM_TEST(4 == 4);
84 	SM_TEST(check == 1);
85 	DBG_OUT(fprintf(stdout, "We're back, test 4 seems to work.\n"));
86 
87 	return sm_test_end();
88 }
89