6f4ccf617b336730990f9d49fec630c064f95005
[idzebra-moved-to-github.git] / test / api / test_zebra_fork.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 1995-2008 Index Data
3
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20 #if HAVE_SYS_STAT_H
21 #include <sys/stat.h>
22 #endif
23 #if HAVE_SYS_TYPES_H
24 #include <sys/types.h>
25 #endif
26 #if HAVE_UNISTD_H
27 #include <unistd.h>
28 #endif
29 #if HAVE_SYS_WAIT_H
30 #include <sys/wait.h>
31 #endif
32 #if HAVE_SYS_UTSNAME_H
33 #include <sys/utsname.h>
34 #endif
35
36 #include <string.h>
37
38 #include "testlib.h"
39
40 static void update_process(ZebraService zs, int iter)
41 {
42     int i;
43     for (i = 0; i<iter; i++)
44     {
45         const char *rec = "<gils><title>some</title></gils>";
46         ZebraHandle zh = zebra_open(zs, 0);
47         zebra_add_record(zh, rec, strlen(rec));
48         if ((i % 30) == 0 || i == iter-1)
49             zebra_commit(zh);
50         zebra_close(zh);
51     }
52 }
53
54 static void search_process(ZebraService zs, int iter)
55 {
56     zint hits_max = 0;
57     int i;
58     for (i = 0; i<iter; i++)
59     {
60         ZebraHandle zh = zebra_open(zs, 0);
61         zint hits;          
62         ZEBRA_RES res = zebra_search_PQF(zh, "@attr 1=4 some", "default",
63                                          &hits);
64         YAZ_CHECK(res == ZEBRA_OK);
65         
66         YAZ_CHECK(hits >= hits_max);
67         if (hits < hits_max)
68             printf("i=%d hits=%lld hits_max=%lld\n", i, hits, hits_max);
69         hits_max = hits;
70         zebra_close(zh);
71     }
72 }
73
74 static pid_t fork_service(ZebraService zs, int iter,
75                            void (*f)(ZebraService zs, int iter))
76 {
77     pid_t pid = fork();
78
79     YAZ_CHECK(pid != -1);
80     if (pid)
81         return pid;
82     
83     (*f)(zs, iter);
84     YAZ_CHECK_TERM;
85 }
86
87 static void tst(int argc, char **argv)
88 {
89     ZebraService zs;
90     ZebraHandle zh;
91     
92     mkdir("register", 0775);
93     mkdir("shadow", 0775);
94
95     zs = tl_start_up("test_zebra_fork.cfg", argc, argv);
96     YAZ_CHECK(zs);
97
98     zh = zebra_open(zs, 0);
99     YAZ_CHECK(zh);
100
101     YAZ_CHECK(zebra_select_database(zh, "Default") == ZEBRA_OK);
102
103     zebra_init(zh);
104
105     YAZ_CHECK(zebra_create_database (zh, "Default") == ZEBRA_OK);
106     YAZ_CHECK(zebra_select_database(zh, "Default") == ZEBRA_OK);
107     zebra_close(zh);
108
109     update_process(zs, 1);
110
111 #if HAVE_SYS_WAIT_H
112 #if HAVE_UNISTD_H
113 #if HAVE_SYS_UTSNAME_H
114
115     if (1)
116     {
117         int tst_with_fork = 1;
118         int status[3];
119         pid_t pids[3];
120         struct utsname s;
121         uname(&s);
122         if (!strcmp(s.sysname, "FreeBSD"))
123             tst_with_fork = 0;
124
125         yaz_log(YLOG_LOG, "s.sysname=%s tst_with_fork=%d", s.sysname,
126             tst_with_fork);
127         if (tst_with_fork)
128         {
129             pids[0] = fork_service(zs, 200, search_process);
130             pids[1] = fork_service(zs, 20, update_process);
131             pids[2] = fork_service(zs, 20, update_process);
132             waitpid(pids[0], &status[0], 0);
133             YAZ_CHECK(status[0] == 0);
134             waitpid(pids[1], &status[1], 0);
135             YAZ_CHECK(status[1] == 0);
136             waitpid(pids[2], &status[2], 0);
137             YAZ_CHECK(status[2] == 0);
138         }
139     }
140 #endif
141 #endif
142 #endif
143     YAZ_CHECK(tl_close_down(0, zs));
144 }
145
146 TL_MAIN
147 /*
148  * Local variables:
149  * c-basic-offset: 4
150  * indent-tabs-mode: nil
151  * End:
152  * vim: shiftwidth=4 tabstop=8 expandtab
153  */
154