Groonga 3.0.9 Source Code Document
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
test-stress-hash.c
Go to the documentation of this file.
1 /* -*- c-basic-offset: 2; coding: utf-8 -*- */
2 /*
3  Copyright (C) 2008-2009 Kouhei Sutou <kou@clear-code.com>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Lesser General Public
7  License version 2.1 as published by the Free Software Foundation.
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  Lesser General Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public
15  License along with this library; if not, write to the Free Software
16  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 
19 #include <hash.h>
20 #include <gcutter.h>
21 #include "../lib/grn-assertions.h"
22 
23 void data_read_write(void);
24 void test_read_write(gconstpointer *data);
25 
26 #define N_THREADS 10
27 
28 static grn_ctx *contexts[N_THREADS];
29 static grn_hash *hashes[N_THREADS];
30 
31 void
33 {
34  int i;
35 
36  for (i = 0; i < N_THREADS; i++) {
37  contexts[i] = NULL;
38  }
39 }
40 
41 void
43 {
44  int i;
45 
46  for (i = 0; i < N_THREADS; i++) {
48 
49  context = contexts[i];
50  if (context) {
51  grn_hash *hash;
52 
53  hash = hashes[i];
54  if (hash)
55  grn_hash_close(context, hash);
56  grn_ctx_fin(context);
57  }
58  }
59 }
60 
61 
62 void
63 data_read_write(void)
64 {
65  gint i;
66  const gchar *n_processes, *process_number, *thread_type;
67 
68  n_processes = g_getenv(GRN_TEST_ENV_N_PROCESSES);
69  if (!n_processes)
70  n_processes = "?";
71 
72  process_number = g_getenv(GRN_TEST_ENV_PROCESS_NUMBER);
73  if (!process_number)
74  process_number = "?";
75 
76  if (g_getenv(GRN_TEST_ENV_MULTI_THREAD))
77  thread_type = "multi thread";
78  else
79  thread_type = "single thread";
80 
81  for (i = 0; i < N_THREADS; i++) {
82  cut_add_data(cut_take_printf("%s process(es)[%s] - %s[%d]",
83  n_processes, process_number, thread_type, i),
84  GINT_TO_POINTER(i), NULL);
85  }
86 }
87 
88 void
89 test_read_write(gconstpointer *data)
90 {
91  gint i, key;
92  int added;
94  grn_hash *hash;
95  const gchar *path;
96  const gchar *value_string;
97  gint process_number = 0;
98  const gchar *process_number_string;
99  void *value;
100  grn_id id = GRN_ID_NIL;
101  grn_rc rc;
102 
103  i = GPOINTER_TO_INT(data);
104  process_number_string = g_getenv(GRN_TEST_ENV_PROCESS_NUMBER);
105  if (process_number_string)
106  process_number = atoi(process_number_string);
107 
108  key = i + process_number * N_THREADS;
109 
110  rc = grn_ctx_init(contexts[i], GRN_CTX_USE_QL);
111  grn_test_assert(rc, cut_message("context: %d (%d)", i, process_number));
112  context = contexts[i];
113 
114  path = g_getenv(GRN_TEST_ENV_HASH_PATH);
115  cut_assert_not_null(path);
116  hashes[i] = grn_hash_open(context, path);
117  cut_assert_not_null(hashes[i],
118  cut_message("hash: %d (%d)", i, process_number));
119  hash = hashes[i];
120 
122  grn_hash_get(context, hash, &key, sizeof(key), &value),
123  cut_message("lookup - fail: %d (%d:%d)", key, i, process_number));
124 
125  value_string = cut_take_printf("value: %d (%d:%d)", key, i, process_number);
126  rc = grn_io_lock(context, hash->io, -1);
127  if (rc != GRN_SUCCESS)
128  grn_test_assert(rc);
129  id = grn_hash_add(context, hash, &key, sizeof(key), &value, &added);
130  grn_io_unlock(hash->io);
132  cut_assert_equal_int(1, added);
133  strcpy(value, value_string);
134 
135  value = NULL;
136  id = grn_hash_get(context, hash, &key, sizeof(key), &value);
138  id,
139  cut_message("lookup - success: %d (%d:%d)", key, i, process_number));
140  cut_assert_equal_string(value_string, value);
141 
142  hashes[i] = NULL;
143  grn_test_assert(grn_hash_close(context, hash));
144 
145  contexts[i] = NULL;
146  grn_test_assert(grn_ctx_fin(context));
147 }