3d0c15e3b5d0d57c6c9385476aabcd32d339d0e8
[idzebra-moved-to-github.git] / test / api / t15.c
1 /* $Id: t15.c,v 1.2 2006-04-05 02:03:33 adam Exp $
2    Copyright (C) 2004-2005
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 Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21 */
22
23 #include <sys/types.h>
24 #if HAVE_UNISTD_H
25 #include <unistd.h>
26 #endif
27 #if HAVE_SYS_WAIT_H
28 #include <sys/wait.h>
29 #endif
30
31 #include "testlib.h"
32
33 static void update_process(ZebraService zs, int iter)
34 {
35     int i;
36     for (i = 0; i<iter; i++)
37     {
38         const char *rec = "<gils><title>some</title></gils>";
39         ZebraHandle zh = zebra_open(zs, 0);
40         // printf("update_record i=%d\n", i);
41         zebra_add_record(zh, rec, strlen(rec));
42         if ((i % 30) == 0 || i == iter-1)
43             zebra_commit(zh);
44         zebra_close(zh);
45     }
46 }
47
48 static void search_process(ZebraService zs, int iter)
49 {
50     zint hits_max = 0;
51     int i;
52     for (i = 0; i<iter; i++)
53     {
54         ZebraHandle zh = zebra_open(zs, 0);
55         zint hits;          
56         ZEBRA_RES res = zebra_search_PQF(zh, "@attr 1=4 some", "default",
57                                          &hits);
58         YAZ_CHECK(res == ZEBRA_OK);
59         
60         YAZ_CHECK(hits >= hits_max);
61         if (hits < hits_max)
62             printf("hits=%lld hits_max=%lld\n", hits, hits_max);
63             hits_max = hits;
64         zebra_close(zh);
65     }
66 }
67
68 static pid_t fork_service(ZebraService zs, int iter,
69                            void (*f)(ZebraService zs, int iter))
70 {
71     pid_t pid = fork();
72
73     YAZ_CHECK(pid != -1);
74     if (pid)
75         return pid;
76     
77     (*f)(zs, iter);
78     YAZ_CHECK_TERM;
79 }
80
81 static void tst(int argc, char **argv)
82 {
83     ZebraService zs = tl_start_up("zebra15.cfg", argc, argv);
84
85     ZebraHandle zh = zebra_open(zs, 0);
86
87     YAZ_CHECK(zs);
88
89     YAZ_CHECK(zh);
90
91     YAZ_CHECK(zebra_select_database(zh, "Default") == ZEBRA_OK);
92
93     zebra_init(zh);
94
95     YAZ_CHECK(zebra_create_database (zh, "Default") == ZEBRA_OK);
96     YAZ_CHECK(zebra_select_database(zh, "Default") == ZEBRA_OK);
97     zebra_close(zh);
98
99     update_process(zs, 1);
100
101 #if HAVE_SYS_WAIT_H
102 #if HAVE_UNISTD_H
103     if (1)
104     {
105         int status[3];
106         pid_t pids[3];
107
108         pids[0] = fork_service(zs, 200, search_process);
109         pids[1] = fork_service(zs, 20, update_process);
110         pids[2] = fork_service(zs, 20, update_process);
111         waitpid(pids[0], &status[0], 0);
112         YAZ_CHECK(status[0] == 0);
113         waitpid(pids[1], &status[1], 0);
114         YAZ_CHECK(status[1] == 0);
115         waitpid(pids[2], &status[2], 0);
116         YAZ_CHECK(status[2] == 0);
117     }
118 #endif
119 #endif
120
121 }
122
123 TL_MAIN