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