1 /*
2 * sqlite-test.c -- test the stream functions
3 *
4 * ====================================================================
5 * Licensed to the Apache Software Foundation (ASF) under one
6 * or more contributor license agreements. See the NOTICE file
7 * distributed with this work for additional information
8 * regarding copyright ownership. The ASF licenses this file
9 * to you under the Apache License, Version 2.0 (the
10 * "License"); you may not use this file except in compliance
11 * with the License. You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing,
16 * software distributed under the License is distributed on an
17 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18 * KIND, either express or implied. See the License for the
19 * specific language governing permissions and limitations
20 * under the License.
21 * ====================================================================
22 */
23
24 #include "private/svn_sqlite.h"
25 #include "../svn_test.h"
26
27 static svn_error_t *
open_db(svn_sqlite__db_t ** sdb,const char ** db_abspath_p,const char * db_name,const char * const * statements,apr_int32_t timeout,apr_pool_t * pool)28 open_db(svn_sqlite__db_t **sdb,
29 const char **db_abspath_p,
30 const char *db_name,
31 const char *const *statements,
32 apr_int32_t timeout,
33 apr_pool_t *pool)
34 {
35 const char *db_dir, *db_abspath;
36
37 SVN_ERR(svn_dirent_get_absolute(&db_dir, "sqlite-test-tmp", pool));
38 SVN_ERR(svn_io_remove_dir2(db_dir, TRUE, NULL, NULL, pool));
39 SVN_ERR(svn_io_make_dir_recursively(db_dir, pool));
40 svn_test_add_dir_cleanup(db_dir);
41
42 db_abspath = svn_dirent_join(db_dir, db_name, pool);
43
44 SVN_ERR(svn_sqlite__open(sdb, db_abspath, svn_sqlite__mode_rwcreate,
45 statements, 0, NULL, timeout, pool, pool));
46
47 if (db_abspath_p)
48 *db_abspath_p = db_abspath;
49 return SVN_NO_ERROR;
50 }
51
52 static svn_error_t *
error_second(svn_sqlite__context_t * sctx,int argc,svn_sqlite__value_t * values[],void * baton)53 error_second(svn_sqlite__context_t *sctx,
54 int argc,
55 svn_sqlite__value_t *values[],
56 void *baton)
57 {
58 static int i = 0;
59
60 if (++i == 2)
61 svn_sqlite__result_error(sctx, "fake error", 0);
62 else
63 svn_sqlite__result_int64(sctx, 1);
64
65 return SVN_NO_ERROR;
66 }
67
68 static svn_error_t *
test_sqlite_reset(apr_pool_t * pool)69 test_sqlite_reset(apr_pool_t *pool)
70 {
71 svn_sqlite__db_t *sdb;
72 svn_sqlite__stmt_t *stmt;
73 svn_boolean_t have_row;
74 const char *value;
75
76 static const char *const statements[] = {
77 "CREATE TABLE reset ("
78 " one TEXT NOT NULL PRIMARY KEY,"
79 " two TEXT"
80 ");"
81 "INSERT INTO reset(one, two) VALUES ('foo', 'bar');"
82 "INSERT INTO reset(one, two) VALUES ('zig', 'zag')",
83
84 "SELECT one FROM reset WHERE two IS NOT NULL AND error_second(one) "
85 "ORDER BY one",
86
87 NULL
88 };
89
90 SVN_ERR(open_db(&sdb, NULL, "reset", statements, 0, pool));
91 SVN_ERR(svn_sqlite__create_scalar_function(sdb, "error_second",
92 1, FALSE /* deterministic */,
93 error_second, NULL));
94 SVN_ERR(svn_sqlite__exec_statements(sdb, 0));
95 SVN_ERR(svn_sqlite__get_statement(&stmt, sdb, 1));
96
97 /* First step is OK. */
98 SVN_ERR(svn_sqlite__step(&have_row, stmt));
99 SVN_TEST_ASSERT(have_row);
100 value = svn_sqlite__column_text(stmt, 0, NULL);
101 SVN_TEST_ASSERT(value && !strcmp(value, "foo"));
102
103 /* Second step fails. */
104 SVN_TEST_ASSERT_ERROR(svn_sqlite__step(&have_row, stmt),
105 SVN_ERR_SQLITE_ERROR);
106
107 /* The svn_sqlite__step wrapper calls svn_sqlite__reset when step
108 fails so the reset call here is a no-op. The first step can be
109 repeated. */
110 SVN_ERR(svn_sqlite__reset(stmt));
111 SVN_ERR(svn_sqlite__step(&have_row, stmt));
112 SVN_TEST_ASSERT(have_row);
113 value = svn_sqlite__column_text(stmt, 0, NULL);
114 SVN_TEST_ASSERT(value && !strcmp(value, "foo"));
115 SVN_ERR(svn_sqlite__reset(stmt));
116
117 return SVN_NO_ERROR;
118 }
119
120 static svn_error_t *
test_sqlite_txn_commit_busy(apr_pool_t * pool)121 test_sqlite_txn_commit_busy(apr_pool_t *pool)
122 {
123 svn_sqlite__db_t *sdb1;
124 svn_sqlite__db_t *sdb2;
125 const char *db_abspath;
126 svn_error_t *err;
127
128 static const char *const statements[] = {
129 "CREATE TABLE test (one TEXT NOT NULL PRIMARY KEY)",
130
131 "INSERT INTO test(one) VALUES ('foo')",
132
133 "SELECT one from test",
134
135 NULL
136 };
137
138 /* Open two db connections.
139
140 Use a small busy_timeout of 250ms, since we're about to receive an
141 SVN_ERR_SQLITE_BUSY error, and retrying for the default 10 seconds
142 would be a waste of time. */
143 SVN_ERR(open_db(&sdb1, &db_abspath, "txn_commit_busy",
144 statements, 250, pool));
145 SVN_ERR(svn_sqlite__open(&sdb2, db_abspath, svn_sqlite__mode_readwrite,
146 statements, 0, NULL, 250, pool, pool));
147 SVN_ERR(svn_sqlite__exec_statements(sdb1, 0));
148
149 /* Begin two deferred transactions. */
150 SVN_ERR(svn_sqlite__begin_transaction(sdb1));
151 SVN_ERR(svn_sqlite__exec_statements(sdb1, 1 /* INSERT */));
152 SVN_ERR(svn_sqlite__begin_transaction(sdb2));
153 SVN_ERR(svn_sqlite__exec_statements(sdb2, 2 /* SELECT */));
154
155 /* Try to COMMIT the first write transaction; this should fail due to
156 the concurrent read transaction that holds a shared lock on the db. */
157 err = svn_sqlite__finish_transaction(sdb1, SVN_NO_ERROR);
158 SVN_TEST_ASSERT_ERROR(err, SVN_ERR_SQLITE_BUSY);
159
160 /* We failed to COMMIT the first transaction, but COMMIT-ting the
161 second transaction through a different db connection should succeed.
162 Upgrade it to a write transaction by executing the INSERT statement,
163 and then commit. */
164 SVN_ERR(svn_sqlite__exec_statements(sdb2, 1 /* INSERT */));
165 SVN_ERR(svn_sqlite__finish_transaction(sdb2, SVN_NO_ERROR));
166
167 SVN_ERR(svn_sqlite__close(sdb2));
168 SVN_ERR(svn_sqlite__close(sdb1));
169
170 return SVN_NO_ERROR;
171 }
172
173
174 static int max_threads = 1;
175
176 static struct svn_test_descriptor_t test_funcs[] =
177 {
178 SVN_TEST_NULL,
179 SVN_TEST_PASS2(test_sqlite_reset,
180 "sqlite reset"),
181 SVN_TEST_PASS2(test_sqlite_txn_commit_busy,
182 "sqlite busy on transaction commit"),
183 SVN_TEST_NULL
184 };
185
186 SVN_TEST_MAIN
187