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