Add to git.
[pthrlib.git] / src / test_reactor.c
1 /* Test the reactor.
2  * Copyright (C) 2001 Richard W.M. Jones <rich@annexia.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the Free
16  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  *
18  * $Id: test_reactor.c,v 1.2 2002/08/21 10:42:21 rich Exp $
19  */
20
21 #include "config.h"
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <assert.h>
26
27 #ifdef HAVE_FCNTL_H
28 #include <fcntl.h>
29 #endif
30
31 #ifdef HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
34
35 #include <pool.h>
36
37 #include "pthr_reactor.h"
38
39 static void set_flag (void *data) { int *flag = (int *) data; *flag = 1; }
40 static void set_flag_h (int s, int e, void *data) { int *flag = (int *) data; *flag = 1; }
41
42 int
43 main ()
44 {
45   int p1[2], p2[2];
46   reactor_handle h1, h2;
47   int flag1 = 0, flag2 = 0, flag3 = 0;
48   char c = '\0';
49   reactor_timer t1;
50   reactor_prepoll pre1;
51
52   /* Create some pipes. */
53   if (pipe (p1) < 0) { perror ("pipe"); exit (1); }
54   if (pipe (p2) < 0) { perror ("pipe"); exit (1); }
55
56   if (fcntl (p1[0], F_SETFL, O_NONBLOCK) < 0) { perror ("fcntl"); exit (1); }
57   if (fcntl (p1[1], F_SETFL, O_NONBLOCK) < 0) { perror ("fcntl"); exit (1); }
58   if (fcntl (p2[0], F_SETFL, O_NONBLOCK) < 0) { perror ("fcntl"); exit (1); }
59   if (fcntl (p2[1], F_SETFL, O_NONBLOCK) < 0) { perror ("fcntl"); exit (1); }
60
61   /* Register read handlers. */
62   h1 = reactor_register (p1[0], REACTOR_READ, set_flag_h, &flag1);
63   h2 = reactor_register (p2[0], REACTOR_READ, set_flag_h, &flag2);
64
65   /* Register a prepoll handler. */
66   pre1 = reactor_register_prepoll (global_pool, set_flag, &flag3);
67
68   /* Write something and check the flags. */
69   write (p1[1], &c, 1);
70   reactor_invoke ();
71   assert (flag1 == 1);
72   assert (flag2 == 0);
73   assert (flag3 == 1);
74   flag1 = flag3 = 0;
75   read (p1[0], &c, 1);
76   write (p2[1], &c, 1);
77   reactor_invoke ();
78   assert (flag1 == 0);
79   assert (flag2 == 1);
80   assert (flag3 == 1);
81   flag2 = flag3 = 0;
82   read (p1[0], &c, 1);
83
84   reactor_unregister (h1);
85   reactor_unregister (h2);
86   reactor_unregister_prepoll (pre1);
87
88   /* Register a timer function. */
89   t1 = reactor_set_timer (global_pool, 1000, set_flag, &flag1);
90   sleep (2);
91   reactor_invoke ();
92   assert (flag1 == 1);
93   assert (flag3 == 0);
94   flag1 = 0;
95
96   exit (0);
97 }