17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright (c) 2001-2002, 2004 Sendmail, Inc. and its suppliers.
37c478bd9Sstevel@tonic-gate  *      All rights reserved.
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  * By using this file, you agree to the terms and conditions set
67c478bd9Sstevel@tonic-gate  * forth in the LICENSE file which can be found at the top level of
77c478bd9Sstevel@tonic-gate  * the sendmail distribution.
87c478bd9Sstevel@tonic-gate  */
97c478bd9Sstevel@tonic-gate 
107c478bd9Sstevel@tonic-gate #include <sm/gen.h>
11*49218d4fSjbeck SM_RCSID("@(#)$Id: t-event.c,v 1.13 2005/06/14 23:07:20 ca Exp $")
127c478bd9Sstevel@tonic-gate 
137c478bd9Sstevel@tonic-gate #include <stdio.h>
147c478bd9Sstevel@tonic-gate 
157c478bd9Sstevel@tonic-gate #include <stdlib.h>
167c478bd9Sstevel@tonic-gate #include <unistd.h>
177c478bd9Sstevel@tonic-gate # include <sys/wait.h>
187c478bd9Sstevel@tonic-gate #if SM_CONF_SETITIMER
19*49218d4fSjbeck # include <sm/time.h>
207c478bd9Sstevel@tonic-gate #endif /* SM_CONF_SETITIMER */
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate #include <sm/clock.h>
237c478bd9Sstevel@tonic-gate #include <sm/test.h>
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate static void	evcheck __P((int));
267c478bd9Sstevel@tonic-gate static void	ev1 __P((int));
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate static int check;
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate static void
evcheck(arg)317c478bd9Sstevel@tonic-gate evcheck(arg)
327c478bd9Sstevel@tonic-gate 	int arg;
337c478bd9Sstevel@tonic-gate {
347c478bd9Sstevel@tonic-gate 	SM_TEST(arg == 3);
357c478bd9Sstevel@tonic-gate 	SM_TEST(check == 0);
367c478bd9Sstevel@tonic-gate 	check++;
377c478bd9Sstevel@tonic-gate }
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate static void
ev1(arg)407c478bd9Sstevel@tonic-gate ev1(arg)
417c478bd9Sstevel@tonic-gate 	int arg;
427c478bd9Sstevel@tonic-gate {
437c478bd9Sstevel@tonic-gate 	SM_TEST(arg == 1);
447c478bd9Sstevel@tonic-gate }
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /* define as x if you want debug output */
477c478bd9Sstevel@tonic-gate #define DBG_OUT(x)
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate int
main(argc,argv)507c478bd9Sstevel@tonic-gate main(argc, argv)
517c478bd9Sstevel@tonic-gate 	int argc;
527c478bd9Sstevel@tonic-gate 	char *argv[];
537c478bd9Sstevel@tonic-gate {
547c478bd9Sstevel@tonic-gate 	SM_EVENT *ev;
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate 	sm_test_begin(argc, argv, "test event handling");
577c478bd9Sstevel@tonic-gate 	fprintf(stdout, "This test may hang. If there is no output within twelve seconds, abort it\nand recompile with -DSM_CONF_SETITIMER=%d\n",
587c478bd9Sstevel@tonic-gate 		SM_CONF_SETITIMER == 0 ? 1 : 0);
597c478bd9Sstevel@tonic-gate 	sleep(1);
607c478bd9Sstevel@tonic-gate 	SM_TEST(1 == 1);
617c478bd9Sstevel@tonic-gate 	DBG_OUT(fprintf(stdout, "We're back, test 1 seems to work.\n"));
627c478bd9Sstevel@tonic-gate 	ev = sm_seteventm(1000, ev1, 1);
637c478bd9Sstevel@tonic-gate 	sleep(1);
647c478bd9Sstevel@tonic-gate 	SM_TEST(2 == 2);
657c478bd9Sstevel@tonic-gate 	DBG_OUT(fprintf(stdout, "We're back, test 2 seems to work.\n"));
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 	/* schedule an event in 9s */
687c478bd9Sstevel@tonic-gate 	ev = sm_seteventm(9000, ev1, 2);
697c478bd9Sstevel@tonic-gate 	sleep(1);
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 	/* clear the event before it can fire */
727c478bd9Sstevel@tonic-gate 	sm_clrevent(ev);
737c478bd9Sstevel@tonic-gate 	SM_TEST(3 == 3);
747c478bd9Sstevel@tonic-gate 	DBG_OUT(fprintf(stdout, "We're back, test 3 seems to work.\n"));
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	/* schedule an event in 1s */
777c478bd9Sstevel@tonic-gate 	check = 0;
787c478bd9Sstevel@tonic-gate 	ev = sm_seteventm(1000, evcheck, 3);
797c478bd9Sstevel@tonic-gate 	sleep(2);
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	/* clear the event */
827c478bd9Sstevel@tonic-gate 	sm_clrevent(ev);
837c478bd9Sstevel@tonic-gate 	SM_TEST(4 == 4);
847c478bd9Sstevel@tonic-gate 	SM_TEST(check == 1);
857c478bd9Sstevel@tonic-gate 	DBG_OUT(fprintf(stdout, "We're back, test 4 seems to work.\n"));
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	return sm_test_end();
887c478bd9Sstevel@tonic-gate }
89