Rename test functions a bit
[pazpar2-moved-to-github.git] / src / test_sel_thread.c
1 /* $Id: test_sel_thread.c,v 1.4 2007-04-23 07:29:34 adam Exp $
2    Copyright (c) 2006-2007, Index Data.
3
4 This file is part of Pazpar2.
5
6 Pazpar2 is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
10
11 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Pazpar2; see the file LICENSE.  If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.
20  */
21
22 #if HAVE_CONFIG_H
23 #include "cconfig.h"
24 #endif
25
26 #include "sel_thread.h"
27 #include "eventl.h"
28 #include <yaz/test.h>
29 #include <yaz/xmalloc.h>
30
31 /** \brief stuff we work on in separate thread */
32 struct my_work_data {
33     int x;
34     int y;
35 };
36
37 /** \brief work to be carried out in separate thrad */
38 static void work_handler(void *vp)
39 {
40     struct my_work_data *p = vp;
41     p->y = p->x * 2;
42 }
43
44 /** \brief see if we can create and destroy without problems */
45 static void test_create_destroy(void)
46 {
47     int fd;
48     sel_thread_t p = sel_thread_create(work_handler, &fd);
49     YAZ_CHECK(p);
50     if (!p)
51         return;
52
53     sel_thread_destroy(p);
54 }
55
56
57 void iochan_handler(struct iochan *i, int event)
58 {
59     static int number = 0;
60     sel_thread_t p = iochan_getdata(i);
61
62     if (event & EVENT_INPUT)
63     {
64         struct my_work_data *work;
65
66         work = sel_thread_result(p);
67
68         YAZ_CHECK(work);
69         if (work)
70         {
71             YAZ_CHECK_EQ(work->x * 2, work->y);
72             /* stop work after a couple of iterations */
73             if (work->x > 10)
74                 iochan_destroy(i);
75
76             xfree(work);
77         }
78
79     }
80     if (event & EVENT_TIMEOUT)
81     {
82         struct my_work_data *work;
83
84         work = xmalloc(sizeof(*work));
85         work->x = number;
86         sel_thread_add(p, work);
87
88         work = xmalloc(sizeof(*work));
89         work->x = number+1;
90         sel_thread_add(p, work);
91
92         number += 10;
93     }
94 }
95
96 /** brief use the fd for something */
97 static void test_for_real_work(void)
98 {
99     int thread_fd;
100     sel_thread_t p = sel_thread_create(work_handler, &thread_fd);
101     YAZ_CHECK(p);
102     if (p)
103     {
104         IOCHAN chan = iochan_create(thread_fd, iochan_handler,
105                                     EVENT_INPUT|EVENT_TIMEOUT);
106         iochan_settimeout(chan, 1);
107         iochan_setdata(chan, p);
108
109         event_loop(&chan);
110         sel_thread_destroy(p);
111     }
112 }
113
114 int main(int argc, char **argv)
115 {
116     YAZ_CHECK_INIT(argc, argv); 
117     YAZ_CHECK_LOG(); 
118
119     test_create_destroy();
120     test_for_real_work();
121
122     YAZ_CHECK_TERM;
123 }
124
125
126
127
128 /*
129  * Local variables:
130  * c-basic-offset: 4
131  * indent-tabs-mode: nil
132  * End:
133  * vim: shiftwidth=4 tabstop=8 expandtab
134  */