blob: 25caeb89f9fb413829528cf5962f3df008c95f54 [file] [log] [blame]
dan4be02b92010-07-22 15:44:06 +00001/*
drh9486c1b2014-12-30 19:26:07 +00002** 2010-07-22
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11*************************************************************************
12**
dan4be02b92010-07-22 15:44:06 +000013** The code in this file runs a few multi-threaded test cases using the
14** SQLite library. It can be compiled to an executable on unix using the
15** following command:
16**
17** gcc -O2 threadtest3.c sqlite3.c -ldl -lpthread -lm
18**
drh9486c1b2014-12-30 19:26:07 +000019** Even though threadtest3.c is the only C source code file mentioned on
20** the compiler command-line, #include macros are used to pull in additional
21** C code files named "tt3_*.c".
dan4be02b92010-07-22 15:44:06 +000022**
drh9486c1b2014-12-30 19:26:07 +000023** After compiling, run this program with an optional argument telling
24** which test to run. All tests are run if no argument is given. The
25** argument can be a glob pattern to match multiple tests. Examples:
dan4be02b92010-07-22 15:44:06 +000026**
drh9486c1b2014-12-30 19:26:07 +000027** ./a.out -- Run all tests
28** ./a.out walthread3 -- Run the "walthread3" test
29** ./a.out 'wal*' -- Run all of the wal* tests
30** ./a.out --help -- List all available tests
dan4be02b92010-07-22 15:44:06 +000031**
drh9486c1b2014-12-30 19:26:07 +000032** The exit status is non-zero if any test fails.
dan4be02b92010-07-22 15:44:06 +000033*/
34
drh9486c1b2014-12-30 19:26:07 +000035/*
36** The "Set Error Line" macro.
dan4be02b92010-07-22 15:44:06 +000037*/
drh9486c1b2014-12-30 19:26:07 +000038#define SEL(e) ((e)->iLine = ((e)->rc ? (e)->iLine : __LINE__))
dan4be02b92010-07-22 15:44:06 +000039
40/* Database functions */
41#define opendb(w,x,y,z) (SEL(w), opendb_x(w,x,y,z))
42#define closedb(y,z) (SEL(y), closedb_x(y,z))
43
44/* Functions to execute SQL */
45#define sql_script(x,y,z) (SEL(x), sql_script_x(x,y,z))
46#define integrity_check(x,y) (SEL(x), integrity_check_x(x,y))
47#define execsql_i64(x,y,...) (SEL(x), execsql_i64_x(x,y,__VA_ARGS__))
48#define execsql_text(x,y,z,...) (SEL(x), execsql_text_x(x,y,z,__VA_ARGS__))
49#define execsql(x,y,...) (SEL(x), (void)execsql_i64_x(x,y,__VA_ARGS__))
dan053542d2014-12-13 17:41:48 +000050#define sql_script_printf(x,y,z,...) ( \
51 SEL(x), sql_script_printf_x(x,y,z,__VA_ARGS__) \
52)
dan4be02b92010-07-22 15:44:06 +000053
54/* Thread functions */
dan053542d2014-12-13 17:41:48 +000055#define launch_thread(w,x,y,z) (SEL(w), launch_thread_x(w,x,y,z))
56#define join_all_threads(y,z) (SEL(y), join_all_threads_x(y,z))
dan4be02b92010-07-22 15:44:06 +000057
58/* Timer functions */
59#define setstoptime(y,z) (SEL(y), setstoptime_x(y,z))
60#define timetostop(z) (SEL(z), timetostop_x(z))
61
62/* Report/clear errors. */
63#define test_error(z, ...) test_error_x(z, sqlite3_mprintf(__VA_ARGS__))
64#define clear_error(y,z) clear_error_x(y, z)
65
66/* File-system operations */
67#define filesize(y,z) (SEL(y), filesize_x(y,z))
68#define filecopy(x,y,z) (SEL(x), filecopy_x(x,y,z))
69
dan053542d2014-12-13 17:41:48 +000070#define PTR2INT(x) ((int)((intptr_t)x))
71#define INT2PTR(x) ((void*)((intptr_t)x))
72
dan4be02b92010-07-22 15:44:06 +000073/*
74** End of test code/infrastructure interface macros.
75*************************************************************************/
76
77
78
79
80#include <sqlite3.h>
81#include <unistd.h>
82#include <stdio.h>
83#include <pthread.h>
84#include <assert.h>
85#include <sys/types.h>
86#include <sys/stat.h>
87#include <string.h>
88#include <fcntl.h>
89#include <errno.h>
90
91/*
92 * This code implements the MD5 message-digest algorithm.
93 * The algorithm is due to Ron Rivest. This code was
94 * written by Colin Plumb in 1993, no copyright is claimed.
95 * This code is in the public domain; do with it what you wish.
96 *
97 * Equivalent code is available from RSA Data Security, Inc.
98 * This code has been tested against that, and is equivalent,
99 * except that you don't need to include two pages of legalese
100 * with every copy.
101 *
102 * To compute the message digest of a chunk of bytes, declare an
103 * MD5Context structure, pass it to MD5Init, call MD5Update as
104 * needed on buffers full of bytes, and then call MD5Final, which
105 * will fill a supplied 16-byte array with the digest.
106 */
107
108/*
109 * If compiled on a machine that doesn't have a 32-bit integer,
110 * you just set "uint32" to the appropriate datatype for an
111 * unsigned 32-bit integer. For example:
112 *
113 * cc -Duint32='unsigned long' md5.c
114 *
115 */
116#ifndef uint32
117# define uint32 unsigned int
118#endif
119
120struct MD5Context {
121 int isInit;
122 uint32 buf[4];
123 uint32 bits[2];
dan1ee46c02014-12-15 20:49:26 +0000124 union {
125 unsigned char in[64];
126 uint32 in32[16];
127 } u;
dan4be02b92010-07-22 15:44:06 +0000128};
129typedef struct MD5Context MD5Context;
130
131/*
132 * Note: this code is harmless on little-endian machines.
133 */
134static void byteReverse (unsigned char *buf, unsigned longs){
135 uint32 t;
136 do {
137 t = (uint32)((unsigned)buf[3]<<8 | buf[2]) << 16 |
138 ((unsigned)buf[1]<<8 | buf[0]);
139 *(uint32 *)buf = t;
140 buf += 4;
141 } while (--longs);
142}
143/* The four core functions - F1 is optimized somewhat */
144
145/* #define F1(x, y, z) (x & y | ~x & z) */
146#define F1(x, y, z) (z ^ (x & (y ^ z)))
147#define F2(x, y, z) F1(z, x, y)
148#define F3(x, y, z) (x ^ y ^ z)
149#define F4(x, y, z) (y ^ (x | ~z))
150
151/* This is the central step in the MD5 algorithm. */
152#define MD5STEP(f, w, x, y, z, data, s) \
153 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
154
155/*
156 * The core of the MD5 algorithm, this alters an existing MD5 hash to
157 * reflect the addition of 16 longwords of new data. MD5Update blocks
158 * the data and converts bytes into longwords for this routine.
159 */
160static void MD5Transform(uint32 buf[4], const uint32 in[16]){
161 register uint32 a, b, c, d;
162
163 a = buf[0];
164 b = buf[1];
165 c = buf[2];
166 d = buf[3];
167
168 MD5STEP(F1, a, b, c, d, in[ 0]+0xd76aa478, 7);
169 MD5STEP(F1, d, a, b, c, in[ 1]+0xe8c7b756, 12);
170 MD5STEP(F1, c, d, a, b, in[ 2]+0x242070db, 17);
171 MD5STEP(F1, b, c, d, a, in[ 3]+0xc1bdceee, 22);
172 MD5STEP(F1, a, b, c, d, in[ 4]+0xf57c0faf, 7);
173 MD5STEP(F1, d, a, b, c, in[ 5]+0x4787c62a, 12);
174 MD5STEP(F1, c, d, a, b, in[ 6]+0xa8304613, 17);
175 MD5STEP(F1, b, c, d, a, in[ 7]+0xfd469501, 22);
176 MD5STEP(F1, a, b, c, d, in[ 8]+0x698098d8, 7);
177 MD5STEP(F1, d, a, b, c, in[ 9]+0x8b44f7af, 12);
178 MD5STEP(F1, c, d, a, b, in[10]+0xffff5bb1, 17);
179 MD5STEP(F1, b, c, d, a, in[11]+0x895cd7be, 22);
180 MD5STEP(F1, a, b, c, d, in[12]+0x6b901122, 7);
181 MD5STEP(F1, d, a, b, c, in[13]+0xfd987193, 12);
182 MD5STEP(F1, c, d, a, b, in[14]+0xa679438e, 17);
183 MD5STEP(F1, b, c, d, a, in[15]+0x49b40821, 22);
184
185 MD5STEP(F2, a, b, c, d, in[ 1]+0xf61e2562, 5);
186 MD5STEP(F2, d, a, b, c, in[ 6]+0xc040b340, 9);
187 MD5STEP(F2, c, d, a, b, in[11]+0x265e5a51, 14);
188 MD5STEP(F2, b, c, d, a, in[ 0]+0xe9b6c7aa, 20);
189 MD5STEP(F2, a, b, c, d, in[ 5]+0xd62f105d, 5);
190 MD5STEP(F2, d, a, b, c, in[10]+0x02441453, 9);
191 MD5STEP(F2, c, d, a, b, in[15]+0xd8a1e681, 14);
192 MD5STEP(F2, b, c, d, a, in[ 4]+0xe7d3fbc8, 20);
193 MD5STEP(F2, a, b, c, d, in[ 9]+0x21e1cde6, 5);
194 MD5STEP(F2, d, a, b, c, in[14]+0xc33707d6, 9);
195 MD5STEP(F2, c, d, a, b, in[ 3]+0xf4d50d87, 14);
196 MD5STEP(F2, b, c, d, a, in[ 8]+0x455a14ed, 20);
197 MD5STEP(F2, a, b, c, d, in[13]+0xa9e3e905, 5);
198 MD5STEP(F2, d, a, b, c, in[ 2]+0xfcefa3f8, 9);
199 MD5STEP(F2, c, d, a, b, in[ 7]+0x676f02d9, 14);
200 MD5STEP(F2, b, c, d, a, in[12]+0x8d2a4c8a, 20);
201
202 MD5STEP(F3, a, b, c, d, in[ 5]+0xfffa3942, 4);
203 MD5STEP(F3, d, a, b, c, in[ 8]+0x8771f681, 11);
204 MD5STEP(F3, c, d, a, b, in[11]+0x6d9d6122, 16);
205 MD5STEP(F3, b, c, d, a, in[14]+0xfde5380c, 23);
206 MD5STEP(F3, a, b, c, d, in[ 1]+0xa4beea44, 4);
207 MD5STEP(F3, d, a, b, c, in[ 4]+0x4bdecfa9, 11);
208 MD5STEP(F3, c, d, a, b, in[ 7]+0xf6bb4b60, 16);
209 MD5STEP(F3, b, c, d, a, in[10]+0xbebfbc70, 23);
210 MD5STEP(F3, a, b, c, d, in[13]+0x289b7ec6, 4);
211 MD5STEP(F3, d, a, b, c, in[ 0]+0xeaa127fa, 11);
212 MD5STEP(F3, c, d, a, b, in[ 3]+0xd4ef3085, 16);
213 MD5STEP(F3, b, c, d, a, in[ 6]+0x04881d05, 23);
214 MD5STEP(F3, a, b, c, d, in[ 9]+0xd9d4d039, 4);
215 MD5STEP(F3, d, a, b, c, in[12]+0xe6db99e5, 11);
216 MD5STEP(F3, c, d, a, b, in[15]+0x1fa27cf8, 16);
217 MD5STEP(F3, b, c, d, a, in[ 2]+0xc4ac5665, 23);
218
219 MD5STEP(F4, a, b, c, d, in[ 0]+0xf4292244, 6);
220 MD5STEP(F4, d, a, b, c, in[ 7]+0x432aff97, 10);
221 MD5STEP(F4, c, d, a, b, in[14]+0xab9423a7, 15);
222 MD5STEP(F4, b, c, d, a, in[ 5]+0xfc93a039, 21);
223 MD5STEP(F4, a, b, c, d, in[12]+0x655b59c3, 6);
224 MD5STEP(F4, d, a, b, c, in[ 3]+0x8f0ccc92, 10);
225 MD5STEP(F4, c, d, a, b, in[10]+0xffeff47d, 15);
226 MD5STEP(F4, b, c, d, a, in[ 1]+0x85845dd1, 21);
227 MD5STEP(F4, a, b, c, d, in[ 8]+0x6fa87e4f, 6);
228 MD5STEP(F4, d, a, b, c, in[15]+0xfe2ce6e0, 10);
229 MD5STEP(F4, c, d, a, b, in[ 6]+0xa3014314, 15);
230 MD5STEP(F4, b, c, d, a, in[13]+0x4e0811a1, 21);
231 MD5STEP(F4, a, b, c, d, in[ 4]+0xf7537e82, 6);
232 MD5STEP(F4, d, a, b, c, in[11]+0xbd3af235, 10);
233 MD5STEP(F4, c, d, a, b, in[ 2]+0x2ad7d2bb, 15);
234 MD5STEP(F4, b, c, d, a, in[ 9]+0xeb86d391, 21);
235
236 buf[0] += a;
237 buf[1] += b;
238 buf[2] += c;
239 buf[3] += d;
240}
241
242/*
243 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
244 * initialization constants.
245 */
246static void MD5Init(MD5Context *ctx){
247 ctx->isInit = 1;
248 ctx->buf[0] = 0x67452301;
249 ctx->buf[1] = 0xefcdab89;
250 ctx->buf[2] = 0x98badcfe;
251 ctx->buf[3] = 0x10325476;
252 ctx->bits[0] = 0;
253 ctx->bits[1] = 0;
254}
255
256/*
257 * Update context to reflect the concatenation of another buffer full
258 * of bytes.
259 */
260static
261void MD5Update(MD5Context *ctx, const unsigned char *buf, unsigned int len){
262 uint32 t;
263
264 /* Update bitcount */
265
266 t = ctx->bits[0];
267 if ((ctx->bits[0] = t + ((uint32)len << 3)) < t)
268 ctx->bits[1]++; /* Carry from low to high */
269 ctx->bits[1] += len >> 29;
270
271 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
272
273 /* Handle any leading odd-sized chunks */
274
275 if ( t ) {
dan1ee46c02014-12-15 20:49:26 +0000276 unsigned char *p = (unsigned char *)ctx->u.in + t;
dan4be02b92010-07-22 15:44:06 +0000277
278 t = 64-t;
279 if (len < t) {
280 memcpy(p, buf, len);
281 return;
282 }
283 memcpy(p, buf, t);
dan1ee46c02014-12-15 20:49:26 +0000284 byteReverse(ctx->u.in, 16);
285 MD5Transform(ctx->buf, (uint32 *)ctx->u.in);
dan4be02b92010-07-22 15:44:06 +0000286 buf += t;
287 len -= t;
288 }
289
290 /* Process data in 64-byte chunks */
291
292 while (len >= 64) {
dan1ee46c02014-12-15 20:49:26 +0000293 memcpy(ctx->u.in, buf, 64);
294 byteReverse(ctx->u.in, 16);
295 MD5Transform(ctx->buf, (uint32 *)ctx->u.in);
dan4be02b92010-07-22 15:44:06 +0000296 buf += 64;
297 len -= 64;
298 }
299
300 /* Handle any remaining bytes of data. */
301
dan1ee46c02014-12-15 20:49:26 +0000302 memcpy(ctx->u.in, buf, len);
dan4be02b92010-07-22 15:44:06 +0000303}
304
305/*
306 * Final wrapup - pad to 64-byte boundary with the bit pattern
307 * 1 0* (64-bit count of bits processed, MSB-first)
308 */
309static void MD5Final(unsigned char digest[16], MD5Context *ctx){
310 unsigned count;
311 unsigned char *p;
312
313 /* Compute number of bytes mod 64 */
314 count = (ctx->bits[0] >> 3) & 0x3F;
315
316 /* Set the first char of padding to 0x80. This is safe since there is
317 always at least one byte free */
dan1ee46c02014-12-15 20:49:26 +0000318 p = ctx->u.in + count;
dan4be02b92010-07-22 15:44:06 +0000319 *p++ = 0x80;
320
321 /* Bytes of padding needed to make 64 bytes */
322 count = 64 - 1 - count;
323
324 /* Pad out to 56 mod 64 */
325 if (count < 8) {
326 /* Two lots of padding: Pad the first block to 64 bytes */
327 memset(p, 0, count);
dan1ee46c02014-12-15 20:49:26 +0000328 byteReverse(ctx->u.in, 16);
329 MD5Transform(ctx->buf, (uint32 *)ctx->u.in);
dan4be02b92010-07-22 15:44:06 +0000330
331 /* Now fill the next block with 56 bytes */
dan1ee46c02014-12-15 20:49:26 +0000332 memset(ctx->u.in, 0, 56);
dan4be02b92010-07-22 15:44:06 +0000333 } else {
334 /* Pad block to 56 bytes */
335 memset(p, 0, count-8);
336 }
dan1ee46c02014-12-15 20:49:26 +0000337 byteReverse(ctx->u.in, 14);
dan4be02b92010-07-22 15:44:06 +0000338
339 /* Append length in bits and transform */
dan1ee46c02014-12-15 20:49:26 +0000340 ctx->u.in32[14] = ctx->bits[0];
341 ctx->u.in32[15] = ctx->bits[1];
dan4be02b92010-07-22 15:44:06 +0000342
dan1ee46c02014-12-15 20:49:26 +0000343 MD5Transform(ctx->buf, (uint32 *)ctx->u.in);
dan4be02b92010-07-22 15:44:06 +0000344 byteReverse((unsigned char *)ctx->buf, 4);
345 memcpy(digest, ctx->buf, 16);
dan053542d2014-12-13 17:41:48 +0000346 memset(ctx, 0, sizeof(*ctx)); /* In case it is sensitive */
dan4be02b92010-07-22 15:44:06 +0000347}
348
349/*
350** Convert a 128-bit MD5 digest into a 32-digit base-16 number.
351*/
352static void MD5DigestToBase16(unsigned char *digest, char *zBuf){
353 static char const zEncode[] = "0123456789abcdef";
354 int i, j;
355
356 for(j=i=0; i<16; i++){
357 int a = digest[i];
358 zBuf[j++] = zEncode[(a>>4)&0xf];
359 zBuf[j++] = zEncode[a & 0xf];
360 }
361 zBuf[j] = 0;
362}
363
364/*
365** During testing, the special md5sum() aggregate function is available.
366** inside SQLite. The following routines implement that function.
367*/
368static void md5step(sqlite3_context *context, int argc, sqlite3_value **argv){
369 MD5Context *p;
370 int i;
371 if( argc<1 ) return;
372 p = sqlite3_aggregate_context(context, sizeof(*p));
373 if( p==0 ) return;
374 if( !p->isInit ){
375 MD5Init(p);
376 }
377 for(i=0; i<argc; i++){
378 const char *zData = (char*)sqlite3_value_text(argv[i]);
379 if( zData ){
380 MD5Update(p, (unsigned char*)zData, strlen(zData));
381 }
382 }
383}
384static void md5finalize(sqlite3_context *context){
385 MD5Context *p;
386 unsigned char digest[16];
387 char zBuf[33];
388 p = sqlite3_aggregate_context(context, sizeof(*p));
389 MD5Final(digest,p);
390 MD5DigestToBase16(digest, zBuf);
391 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
392}
393
drh9486c1b2014-12-30 19:26:07 +0000394/*
dan4be02b92010-07-22 15:44:06 +0000395** End of copied md5sum() code.
drh9486c1b2014-12-30 19:26:07 +0000396**************************************************************************/
dan4be02b92010-07-22 15:44:06 +0000397
398typedef sqlite3_int64 i64;
399
400typedef struct Error Error;
401typedef struct Sqlite Sqlite;
402typedef struct Statement Statement;
403
404typedef struct Threadset Threadset;
405typedef struct Thread Thread;
406
407/* Total number of errors in this process so far. */
408static int nGlobalErr = 0;
409
dan4be02b92010-07-22 15:44:06 +0000410struct Error {
411 int rc;
412 int iLine;
413 char *zErr;
414};
415
416struct Sqlite {
417 sqlite3 *db; /* Database handle */
418 Statement *pCache; /* Linked list of cached statements */
419 int nText; /* Size of array at aText[] */
420 char **aText; /* Stored text results */
421};
422
423struct Statement {
424 sqlite3_stmt *pStmt; /* Pre-compiled statement handle */
425 Statement *pNext; /* Next statement in linked-list */
426};
427
428struct Thread {
429 int iTid; /* Thread number within test */
dan053542d2014-12-13 17:41:48 +0000430 void* pArg; /* Pointer argument passed by caller */
dan4be02b92010-07-22 15:44:06 +0000431
432 pthread_t tid; /* Thread id */
dan053542d2014-12-13 17:41:48 +0000433 char *(*xProc)(int, void*); /* Thread main proc */
dan4be02b92010-07-22 15:44:06 +0000434 Thread *pNext; /* Next in this list of threads */
435};
436
437struct Threadset {
438 int iMaxTid; /* Largest iTid value allocated so far */
439 Thread *pThread; /* Linked list of threads */
440};
441
442static void free_err(Error *p){
443 sqlite3_free(p->zErr);
444 p->zErr = 0;
445 p->rc = 0;
446}
447
448static void print_err(Error *p){
449 if( p->rc!=SQLITE_OK ){
drhbcbac682014-12-31 18:55:09 +0000450 int isWarn = 0;
451 if( p->rc==SQLITE_SCHEMA ) isWarn = 1;
452 if( sqlite3_strglob("* - no such table: *",p->zErr)==0 ) isWarn = 1;
453 printf("%s: (%d) \"%s\" at line %d\n", isWarn ? "Warning" : "Error",
454 p->rc, p->zErr, p->iLine);
455 if( !isWarn ) nGlobalErr++;
drh9486c1b2014-12-30 19:26:07 +0000456 fflush(stdout);
dan4be02b92010-07-22 15:44:06 +0000457 }
458}
459
460static void print_and_free_err(Error *p){
461 print_err(p);
462 free_err(p);
463}
464
465static void system_error(Error *pErr, int iSys){
466 pErr->rc = iSys;
467 pErr->zErr = (char *)sqlite3_malloc(512);
468 strerror_r(iSys, pErr->zErr, 512);
469 pErr->zErr[511] = '\0';
470}
471
472static void sqlite_error(
473 Error *pErr,
474 Sqlite *pDb,
475 const char *zFunc
476){
477 pErr->rc = sqlite3_errcode(pDb->db);
478 pErr->zErr = sqlite3_mprintf(
479 "sqlite3_%s() - %s (%d)", zFunc, sqlite3_errmsg(pDb->db),
480 sqlite3_extended_errcode(pDb->db)
481 );
482}
483
484static void test_error_x(
485 Error *pErr,
486 char *zErr
487){
488 if( pErr->rc==SQLITE_OK ){
489 pErr->rc = 1;
490 pErr->zErr = zErr;
491 }else{
492 sqlite3_free(zErr);
493 }
494}
495
496static void clear_error_x(
497 Error *pErr,
498 int rc
499){
500 if( pErr->rc==rc ){
501 pErr->rc = SQLITE_OK;
502 sqlite3_free(pErr->zErr);
503 pErr->zErr = 0;
504 }
505}
506
507static int busyhandler(void *pArg, int n){
508 usleep(10*1000);
509 return 1;
510}
511
512static void opendb_x(
513 Error *pErr, /* IN/OUT: Error code */
514 Sqlite *pDb, /* OUT: Database handle */
515 const char *zFile, /* Database file name */
516 int bDelete /* True to delete db file before opening */
517){
518 if( pErr->rc==SQLITE_OK ){
519 int rc;
dan053542d2014-12-13 17:41:48 +0000520 int flags = SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE | SQLITE_OPEN_URI;
dan4be02b92010-07-22 15:44:06 +0000521 if( bDelete ) unlink(zFile);
dan053542d2014-12-13 17:41:48 +0000522 rc = sqlite3_open_v2(zFile, &pDb->db, flags, 0);
dan4be02b92010-07-22 15:44:06 +0000523 if( rc ){
524 sqlite_error(pErr, pDb, "open");
525 sqlite3_close(pDb->db);
526 pDb->db = 0;
527 }else{
528 sqlite3_create_function(
529 pDb->db, "md5sum", -1, SQLITE_UTF8, 0, 0, md5step, md5finalize
530 );
531 sqlite3_busy_handler(pDb->db, busyhandler, 0);
dan16f77202010-08-07 05:15:22 +0000532 sqlite3_exec(pDb->db, "PRAGMA synchronous=OFF", 0, 0, 0);
dan4be02b92010-07-22 15:44:06 +0000533 }
534 }
535}
536
537static void closedb_x(
538 Error *pErr, /* IN/OUT: Error code */
539 Sqlite *pDb /* OUT: Database handle */
540){
541 int rc;
542 int i;
543 Statement *pIter;
544 Statement *pNext;
545 for(pIter=pDb->pCache; pIter; pIter=pNext){
546 pNext = pIter->pNext;
547 sqlite3_finalize(pIter->pStmt);
548 sqlite3_free(pIter);
549 }
550 for(i=0; i<pDb->nText; i++){
551 sqlite3_free(pDb->aText[i]);
552 }
553 sqlite3_free(pDb->aText);
554 rc = sqlite3_close(pDb->db);
555 if( rc && pErr->rc==SQLITE_OK ){
556 pErr->zErr = sqlite3_mprintf("%s", sqlite3_errmsg(pDb->db));
557 }
558 memset(pDb, 0, sizeof(Sqlite));
559}
560
561static void sql_script_x(
562 Error *pErr, /* IN/OUT: Error code */
563 Sqlite *pDb, /* Database handle */
564 const char *zSql /* SQL script to execute */
565){
566 if( pErr->rc==SQLITE_OK ){
567 pErr->rc = sqlite3_exec(pDb->db, zSql, 0, 0, &pErr->zErr);
568 }
569}
570
dan053542d2014-12-13 17:41:48 +0000571static void sql_script_printf_x(
572 Error *pErr, /* IN/OUT: Error code */
573 Sqlite *pDb, /* Database handle */
574 const char *zFormat, /* SQL printf format string */
575 ... /* Printf args */
576){
577 va_list ap; /* ... printf arguments */
578 va_start(ap, zFormat);
579 if( pErr->rc==SQLITE_OK ){
580 char *zSql = sqlite3_vmprintf(zFormat, ap);
581 pErr->rc = sqlite3_exec(pDb->db, zSql, 0, 0, &pErr->zErr);
582 sqlite3_free(zSql);
583 }
584 va_end(ap);
585}
586
dan4be02b92010-07-22 15:44:06 +0000587static Statement *getSqlStatement(
588 Error *pErr, /* IN/OUT: Error code */
589 Sqlite *pDb, /* Database handle */
590 const char *zSql /* SQL statement */
591){
592 Statement *pRet;
593 int rc;
594
595 for(pRet=pDb->pCache; pRet; pRet=pRet->pNext){
596 if( 0==strcmp(sqlite3_sql(pRet->pStmt), zSql) ){
597 return pRet;
598 }
599 }
600
601 pRet = sqlite3_malloc(sizeof(Statement));
602 rc = sqlite3_prepare_v2(pDb->db, zSql, -1, &pRet->pStmt, 0);
603 if( rc!=SQLITE_OK ){
604 sqlite_error(pErr, pDb, "prepare_v2");
605 return 0;
606 }
607 assert( 0==strcmp(sqlite3_sql(pRet->pStmt), zSql) );
608
609 pRet->pNext = pDb->pCache;
610 pDb->pCache = pRet;
611 return pRet;
612}
613
614static sqlite3_stmt *getAndBindSqlStatement(
615 Error *pErr, /* IN/OUT: Error code */
616 Sqlite *pDb, /* Database handle */
617 va_list ap /* SQL followed by parameters */
618){
619 Statement *pStatement; /* The SQLite statement wrapper */
620 sqlite3_stmt *pStmt; /* The SQLite statement to return */
621 int i; /* Used to iterate through parameters */
622
623 pStatement = getSqlStatement(pErr, pDb, va_arg(ap, const char *));
624 if( !pStatement ) return 0;
625 pStmt = pStatement->pStmt;
626 for(i=1; i<=sqlite3_bind_parameter_count(pStmt); i++){
627 const char *zName = sqlite3_bind_parameter_name(pStmt, i);
628 void * pArg = va_arg(ap, void*);
629
630 switch( zName[1] ){
631 case 'i':
632 sqlite3_bind_int64(pStmt, i, *(i64 *)pArg);
633 break;
634
635 default:
636 pErr->rc = 1;
637 pErr->zErr = sqlite3_mprintf("Cannot discern type: \"%s\"", zName);
638 pStmt = 0;
639 break;
640 }
641 }
642
643 return pStmt;
644}
645
646static i64 execsql_i64_x(
647 Error *pErr, /* IN/OUT: Error code */
648 Sqlite *pDb, /* Database handle */
649 ... /* SQL and pointers to parameter values */
650){
651 i64 iRet = 0;
652 if( pErr->rc==SQLITE_OK ){
653 sqlite3_stmt *pStmt; /* SQL statement to execute */
654 va_list ap; /* ... arguments */
dan4be02b92010-07-22 15:44:06 +0000655 va_start(ap, pDb);
656 pStmt = getAndBindSqlStatement(pErr, pDb, ap);
657 if( pStmt ){
dan4be02b92010-07-22 15:44:06 +0000658 int first = 1;
659 while( SQLITE_ROW==sqlite3_step(pStmt) ){
660 if( first && sqlite3_column_count(pStmt)>0 ){
661 iRet = sqlite3_column_int64(pStmt, 0);
662 }
663 first = 0;
664 }
665 if( SQLITE_OK!=sqlite3_reset(pStmt) ){
666 sqlite_error(pErr, pDb, "reset");
667 }
668 }
669 va_end(ap);
670 }
671 return iRet;
672}
673
674static char * execsql_text_x(
675 Error *pErr, /* IN/OUT: Error code */
676 Sqlite *pDb, /* Database handle */
677 int iSlot, /* Db handle slot to store text in */
678 ... /* SQL and pointers to parameter values */
679){
680 char *zRet = 0;
681
682 if( iSlot>=pDb->nText ){
683 int nByte = sizeof(char *)*(iSlot+1);
684 pDb->aText = (char **)sqlite3_realloc(pDb->aText, nByte);
685 memset(&pDb->aText[pDb->nText], 0, sizeof(char*)*(iSlot+1-pDb->nText));
686 pDb->nText = iSlot+1;
687 }
688
689 if( pErr->rc==SQLITE_OK ){
690 sqlite3_stmt *pStmt; /* SQL statement to execute */
691 va_list ap; /* ... arguments */
dan4be02b92010-07-22 15:44:06 +0000692 va_start(ap, iSlot);
693 pStmt = getAndBindSqlStatement(pErr, pDb, ap);
694 if( pStmt ){
dan4be02b92010-07-22 15:44:06 +0000695 int first = 1;
696 while( SQLITE_ROW==sqlite3_step(pStmt) ){
697 if( first && sqlite3_column_count(pStmt)>0 ){
698 zRet = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
699 sqlite3_free(pDb->aText[iSlot]);
700 pDb->aText[iSlot] = zRet;
701 }
702 first = 0;
703 }
704 if( SQLITE_OK!=sqlite3_reset(pStmt) ){
705 sqlite_error(pErr, pDb, "reset");
706 }
707 }
708 va_end(ap);
709 }
710
711 return zRet;
712}
713
714static void integrity_check_x(
715 Error *pErr, /* IN/OUT: Error code */
716 Sqlite *pDb /* Database handle */
717){
718 if( pErr->rc==SQLITE_OK ){
719 Statement *pStatement; /* Statement to execute */
dan4be02b92010-07-22 15:44:06 +0000720 char *zErr = 0; /* Integrity check error */
721
722 pStatement = getSqlStatement(pErr, pDb, "PRAGMA integrity_check");
723 if( pStatement ){
724 sqlite3_stmt *pStmt = pStatement->pStmt;
725 while( SQLITE_ROW==sqlite3_step(pStmt) ){
dan053542d2014-12-13 17:41:48 +0000726 const char *z = (const char*)sqlite3_column_text(pStmt, 0);
dan4be02b92010-07-22 15:44:06 +0000727 if( strcmp(z, "ok") ){
728 if( zErr==0 ){
729 zErr = sqlite3_mprintf("%s", z);
730 }else{
731 zErr = sqlite3_mprintf("%z\n%s", zErr, z);
732 }
733 }
734 }
735 sqlite3_reset(pStmt);
736
737 if( zErr ){
738 pErr->zErr = zErr;
739 pErr->rc = 1;
740 }
741 }
742 }
743}
744
745static void *launch_thread_main(void *pArg){
746 Thread *p = (Thread *)pArg;
dan053542d2014-12-13 17:41:48 +0000747 return (void *)p->xProc(p->iTid, p->pArg);
dan4be02b92010-07-22 15:44:06 +0000748}
749
750static void launch_thread_x(
751 Error *pErr, /* IN/OUT: Error code */
752 Threadset *pThreads, /* Thread set */
dan053542d2014-12-13 17:41:48 +0000753 char *(*xProc)(int, void*), /* Proc to run */
754 void *pArg /* Argument passed to thread proc */
dan4be02b92010-07-22 15:44:06 +0000755){
756 if( pErr->rc==SQLITE_OK ){
757 int iTid = ++pThreads->iMaxTid;
758 Thread *p;
759 int rc;
760
761 p = (Thread *)sqlite3_malloc(sizeof(Thread));
762 memset(p, 0, sizeof(Thread));
763 p->iTid = iTid;
dan053542d2014-12-13 17:41:48 +0000764 p->pArg = pArg;
dan4be02b92010-07-22 15:44:06 +0000765 p->xProc = xProc;
766
767 rc = pthread_create(&p->tid, NULL, launch_thread_main, (void *)p);
768 if( rc!=0 ){
769 system_error(pErr, rc);
770 sqlite3_free(p);
771 }else{
772 p->pNext = pThreads->pThread;
773 pThreads->pThread = p;
774 }
775 }
776}
777
778static void join_all_threads_x(
779 Error *pErr, /* IN/OUT: Error code */
780 Threadset *pThreads /* Thread set */
781){
782 Thread *p;
783 Thread *pNext;
784 for(p=pThreads->pThread; p; p=pNext){
785 void *ret;
786 pNext = p->pNext;
787 int rc;
788 rc = pthread_join(p->tid, &ret);
789 if( rc!=0 ){
790 if( pErr->rc==SQLITE_OK ) system_error(pErr, rc);
791 }else{
792 printf("Thread %d says: %s\n", p->iTid, (ret==0 ? "..." : (char *)ret));
drh9486c1b2014-12-30 19:26:07 +0000793 fflush(stdout);
dan4be02b92010-07-22 15:44:06 +0000794 }
795 sqlite3_free(p);
796 }
797 pThreads->pThread = 0;
798}
799
800static i64 filesize_x(
801 Error *pErr,
802 const char *zFile
803){
804 i64 iRet = 0;
805 if( pErr->rc==SQLITE_OK ){
806 struct stat sStat;
807 if( stat(zFile, &sStat) ){
808 iRet = -1;
809 }else{
810 iRet = sStat.st_size;
811 }
812 }
813 return iRet;
814}
815
816static void filecopy_x(
817 Error *pErr,
818 const char *zFrom,
819 const char *zTo
820){
821 if( pErr->rc==SQLITE_OK ){
822 i64 nByte = filesize_x(pErr, zFrom);
823 if( nByte<0 ){
824 test_error_x(pErr, sqlite3_mprintf("no such file: %s", zFrom));
825 }else{
826 i64 iOff;
827 char aBuf[1024];
828 int fd1;
829 int fd2;
830 unlink(zTo);
831
832 fd1 = open(zFrom, O_RDONLY);
833 if( fd1<0 ){
834 system_error(pErr, errno);
835 return;
836 }
837 fd2 = open(zTo, O_RDWR|O_CREAT|O_EXCL, 0644);
838 if( fd2<0 ){
839 system_error(pErr, errno);
840 close(fd1);
841 return;
842 }
843
844 iOff = 0;
845 while( iOff<nByte ){
846 int nCopy = sizeof(aBuf);
847 if( nCopy+iOff>nByte ){
848 nCopy = nByte - iOff;
849 }
850 if( nCopy!=read(fd1, aBuf, nCopy) ){
851 system_error(pErr, errno);
852 break;
853 }
854 if( nCopy!=write(fd2, aBuf, nCopy) ){
855 system_error(pErr, errno);
856 break;
857 }
858 iOff += nCopy;
859 }
860
861 close(fd1);
862 close(fd2);
863 }
864 }
865}
866
867/*
868** Used by setstoptime() and timetostop().
869*/
870static double timelimit = 0.0;
871static sqlite3_vfs *pTimelimitVfs = 0;
872
873static void setstoptime_x(
874 Error *pErr, /* IN/OUT: Error code */
875 int nMs /* Milliseconds until "stop time" */
876){
877 if( pErr->rc==SQLITE_OK ){
878 double t;
879 int rc;
880 pTimelimitVfs = sqlite3_vfs_find(0);
881 rc = pTimelimitVfs->xCurrentTime(pTimelimitVfs, &t);
882 if( rc!=SQLITE_OK ){
883 pErr->rc = rc;
884 }else{
885 timelimit = t + ((double)nMs)/(1000.0*60.0*60.0*24.0);
886 }
887 }
888}
889
890static int timetostop_x(
891 Error *pErr /* IN/OUT: Error code */
892){
893 int ret = 1;
894 if( pErr->rc==SQLITE_OK ){
895 double t;
896 int rc;
897 rc = pTimelimitVfs->xCurrentTime(pTimelimitVfs, &t);
898 if( rc!=SQLITE_OK ){
899 pErr->rc = rc;
900 }else{
901 ret = (t >= timelimit);
902 }
903 }
904 return ret;
905}
906
dan4be02b92010-07-22 15:44:06 +0000907
908/*************************************************************************
909**************************************************************************
910**************************************************************************
911** End infrastructure. Begin tests.
912*/
913
914#define WALTHREAD1_NTHREAD 10
915#define WALTHREAD3_NTHREAD 6
916
dan053542d2014-12-13 17:41:48 +0000917static char *walthread1_thread(int iTid, void *pArg){
dan4be02b92010-07-22 15:44:06 +0000918 Error err = {0}; /* Error code and message */
919 Sqlite db = {0}; /* SQLite database connection */
920 int nIter = 0; /* Iterations so far */
921
922 opendb(&err, &db, "test.db", 0);
923 while( !timetostop(&err) ){
924 const char *azSql[] = {
925 "SELECT md5sum(x) FROM t1 WHERE rowid != (SELECT max(rowid) FROM t1)",
926 "SELECT x FROM t1 WHERE rowid = (SELECT max(rowid) FROM t1)",
927 };
928 char *z1, *z2, *z3;
929
930 execsql(&err, &db, "BEGIN");
931 integrity_check(&err, &db);
932 z1 = execsql_text(&err, &db, 1, azSql[0]);
933 z2 = execsql_text(&err, &db, 2, azSql[1]);
934 z3 = execsql_text(&err, &db, 3, azSql[0]);
935 execsql(&err, &db, "COMMIT");
936
937 if( strcmp(z1, z2) || strcmp(z1, z3) ){
938 test_error(&err, "Failed read: %s %s %s", z1, z2, z3);
939 }
940
941 sql_script(&err, &db,
942 "BEGIN;"
943 "INSERT INTO t1 VALUES(randomblob(100));"
944 "INSERT INTO t1 VALUES(randomblob(100));"
945 "INSERT INTO t1 SELECT md5sum(x) FROM t1;"
946 "COMMIT;"
947 );
948 nIter++;
949 }
950 closedb(&err, &db);
951
952 print_and_free_err(&err);
953 return sqlite3_mprintf("%d iterations", nIter);
954}
955
dan053542d2014-12-13 17:41:48 +0000956static char *walthread1_ckpt_thread(int iTid, void *pArg){
dan4be02b92010-07-22 15:44:06 +0000957 Error err = {0}; /* Error code and message */
958 Sqlite db = {0}; /* SQLite database connection */
959 int nCkpt = 0; /* Checkpoints so far */
960
961 opendb(&err, &db, "test.db", 0);
962 while( !timetostop(&err) ){
963 usleep(500*1000);
964 execsql(&err, &db, "PRAGMA wal_checkpoint");
965 if( err.rc==SQLITE_OK ) nCkpt++;
966 clear_error(&err, SQLITE_BUSY);
967 }
968 closedb(&err, &db);
969
970 print_and_free_err(&err);
971 return sqlite3_mprintf("%d checkpoints", nCkpt);
972}
973
974static void walthread1(int nMs){
975 Error err = {0}; /* Error code and message */
976 Sqlite db = {0}; /* SQLite database connection */
977 Threadset threads = {0}; /* Test threads */
978 int i; /* Iterator variable */
979
980 opendb(&err, &db, "test.db", 1);
981 sql_script(&err, &db,
982 "PRAGMA journal_mode = WAL;"
983 "CREATE TABLE t1(x PRIMARY KEY);"
984 "INSERT INTO t1 VALUES(randomblob(100));"
985 "INSERT INTO t1 VALUES(randomblob(100));"
986 "INSERT INTO t1 SELECT md5sum(x) FROM t1;"
987 );
danb8a9d8d2014-12-31 18:25:21 +0000988 closedb(&err, &db);
dan4be02b92010-07-22 15:44:06 +0000989
990 setstoptime(&err, nMs);
991 for(i=0; i<WALTHREAD1_NTHREAD; i++){
992 launch_thread(&err, &threads, walthread1_thread, 0);
993 }
994 launch_thread(&err, &threads, walthread1_ckpt_thread, 0);
995 join_all_threads(&err, &threads);
996
997 print_and_free_err(&err);
998}
999
dan053542d2014-12-13 17:41:48 +00001000static char *walthread2_thread(int iTid, void *pArg){
dan4be02b92010-07-22 15:44:06 +00001001 Error err = {0}; /* Error code and message */
1002 Sqlite db = {0}; /* SQLite database connection */
1003 int anTrans[2] = {0, 0}; /* Number of WAL and Rollback transactions */
dan053542d2014-12-13 17:41:48 +00001004 int iArg = PTR2INT(pArg);
dan4be02b92010-07-22 15:44:06 +00001005
1006 const char *zJournal = "PRAGMA journal_mode = WAL";
1007 if( iArg ){ zJournal = "PRAGMA journal_mode = DELETE"; }
1008
1009 while( !timetostop(&err) ){
1010 int journal_exists = 0;
1011 int wal_exists = 0;
1012
1013 opendb(&err, &db, "test.db", 0);
1014
1015 sql_script(&err, &db, zJournal);
1016 clear_error(&err, SQLITE_BUSY);
1017 sql_script(&err, &db, "BEGIN");
1018 sql_script(&err, &db, "INSERT INTO t1 VALUES(NULL, randomblob(100))");
1019
1020 journal_exists = (filesize(&err, "test.db-journal") >= 0);
1021 wal_exists = (filesize(&err, "test.db-wal") >= 0);
1022 if( (journal_exists+wal_exists)!=1 ){
1023 test_error(&err, "File system looks incorrect (%d, %d)",
1024 journal_exists, wal_exists
1025 );
1026 }
1027 anTrans[journal_exists]++;
1028
1029 sql_script(&err, &db, "COMMIT");
1030 integrity_check(&err, &db);
1031 closedb(&err, &db);
1032 }
1033
1034 print_and_free_err(&err);
1035 return sqlite3_mprintf("W %d R %d", anTrans[0], anTrans[1]);
1036}
1037
1038static void walthread2(int nMs){
1039 Error err = {0};
1040 Sqlite db = {0};
1041 Threadset threads = {0};
1042
1043 opendb(&err, &db, "test.db", 1);
1044 sql_script(&err, &db, "CREATE TABLE t1(x INTEGER PRIMARY KEY, y UNIQUE)");
1045 closedb(&err, &db);
1046
1047 setstoptime(&err, nMs);
1048 launch_thread(&err, &threads, walthread2_thread, 0);
1049 launch_thread(&err, &threads, walthread2_thread, 0);
dan053542d2014-12-13 17:41:48 +00001050 launch_thread(&err, &threads, walthread2_thread, (void*)1);
1051 launch_thread(&err, &threads, walthread2_thread, (void*)1);
dan4be02b92010-07-22 15:44:06 +00001052 join_all_threads(&err, &threads);
1053
1054 print_and_free_err(&err);
1055}
1056
dan053542d2014-12-13 17:41:48 +00001057static char *walthread3_thread(int iTid, void *pArg){
dan4be02b92010-07-22 15:44:06 +00001058 Error err = {0}; /* Error code and message */
1059 Sqlite db = {0}; /* SQLite database connection */
1060 i64 iNextWrite; /* Next value this thread will write */
dan053542d2014-12-13 17:41:48 +00001061 int iArg = PTR2INT(pArg);
dan4be02b92010-07-22 15:44:06 +00001062
1063 opendb(&err, &db, "test.db", 0);
1064 sql_script(&err, &db, "PRAGMA wal_autocheckpoint = 10");
1065
1066 iNextWrite = iArg+1;
1067 while( 1 ){
1068 i64 sum1;
1069 i64 sum2;
1070 int stop = 0; /* True to stop executing (test timed out) */
1071
1072 while( 0==(stop = timetostop(&err)) ){
1073 i64 iMax = execsql_i64(&err, &db, "SELECT max(cnt) FROM t1");
1074 if( iMax+1==iNextWrite ) break;
1075 }
1076 if( stop ) break;
1077
1078 sum1 = execsql_i64(&err, &db, "SELECT sum(cnt) FROM t1");
1079 sum2 = execsql_i64(&err, &db, "SELECT sum(sum1) FROM t1");
1080 execsql_i64(&err, &db,
1081 "INSERT INTO t1 VALUES(:iNextWrite, :iSum1, :iSum2)",
1082 &iNextWrite, &sum1, &sum2
1083 );
1084 integrity_check(&err, &db);
1085
1086 iNextWrite += WALTHREAD3_NTHREAD;
1087 }
1088
1089 closedb(&err, &db);
1090 print_and_free_err(&err);
1091 return 0;
1092}
1093
1094static void walthread3(int nMs){
1095 Error err = {0};
1096 Sqlite db = {0};
1097 Threadset threads = {0};
1098 int i;
1099
1100 opendb(&err, &db, "test.db", 1);
1101 sql_script(&err, &db,
1102 "PRAGMA journal_mode = WAL;"
1103 "CREATE TABLE t1(cnt PRIMARY KEY, sum1, sum2);"
1104 "CREATE INDEX i1 ON t1(sum1);"
1105 "CREATE INDEX i2 ON t1(sum2);"
1106 "INSERT INTO t1 VALUES(0, 0, 0);"
1107 );
1108 closedb(&err, &db);
1109
1110 setstoptime(&err, nMs);
1111 for(i=0; i<WALTHREAD3_NTHREAD; i++){
dan053542d2014-12-13 17:41:48 +00001112 launch_thread(&err, &threads, walthread3_thread, INT2PTR(i));
dan4be02b92010-07-22 15:44:06 +00001113 }
1114 join_all_threads(&err, &threads);
1115
1116 print_and_free_err(&err);
1117}
1118
dan053542d2014-12-13 17:41:48 +00001119static char *walthread4_reader_thread(int iTid, void *pArg){
dan4be02b92010-07-22 15:44:06 +00001120 Error err = {0}; /* Error code and message */
1121 Sqlite db = {0}; /* SQLite database connection */
1122
1123 opendb(&err, &db, "test.db", 0);
1124 while( !timetostop(&err) ){
1125 integrity_check(&err, &db);
1126 }
1127 closedb(&err, &db);
1128
1129 print_and_free_err(&err);
1130 return 0;
1131}
1132
dan053542d2014-12-13 17:41:48 +00001133static char *walthread4_writer_thread(int iTid, void *pArg){
dan4be02b92010-07-22 15:44:06 +00001134 Error err = {0}; /* Error code and message */
1135 Sqlite db = {0}; /* SQLite database connection */
1136 i64 iRow = 1;
1137
1138 opendb(&err, &db, "test.db", 0);
1139 sql_script(&err, &db, "PRAGMA wal_autocheckpoint = 15;");
1140 while( !timetostop(&err) ){
1141 execsql_i64(
1142 &err, &db, "REPLACE INTO t1 VALUES(:iRow, randomblob(300))", &iRow
1143 );
1144 iRow++;
1145 if( iRow==10 ) iRow = 0;
1146 }
1147 closedb(&err, &db);
1148
1149 print_and_free_err(&err);
1150 return 0;
1151}
1152
1153static void walthread4(int nMs){
1154 Error err = {0};
1155 Sqlite db = {0};
1156 Threadset threads = {0};
1157
1158 opendb(&err, &db, "test.db", 1);
1159 sql_script(&err, &db,
1160 "PRAGMA journal_mode = WAL;"
1161 "CREATE TABLE t1(a INTEGER PRIMARY KEY, b UNIQUE);"
1162 );
1163 closedb(&err, &db);
1164
1165 setstoptime(&err, nMs);
1166 launch_thread(&err, &threads, walthread4_reader_thread, 0);
1167 launch_thread(&err, &threads, walthread4_writer_thread, 0);
1168 join_all_threads(&err, &threads);
1169
1170 print_and_free_err(&err);
1171}
1172
dan053542d2014-12-13 17:41:48 +00001173static char *walthread5_thread(int iTid, void *pArg){
dan4be02b92010-07-22 15:44:06 +00001174 Error err = {0}; /* Error code and message */
1175 Sqlite db = {0}; /* SQLite database connection */
1176 i64 nRow;
1177
1178 opendb(&err, &db, "test.db", 0);
1179 nRow = execsql_i64(&err, &db, "SELECT count(*) FROM t1");
1180 closedb(&err, &db);
1181
1182 if( nRow!=65536 ) test_error(&err, "Bad row count: %d", (int)nRow);
1183 print_and_free_err(&err);
1184 return 0;
1185}
1186static void walthread5(int nMs){
1187 Error err = {0};
1188 Sqlite db = {0};
1189 Threadset threads = {0};
1190
1191 opendb(&err, &db, "test.db", 1);
1192 sql_script(&err, &db,
1193 "PRAGMA wal_autocheckpoint = 0;"
1194 "PRAGMA page_size = 1024;"
1195 "PRAGMA journal_mode = WAL;"
1196 "CREATE TABLE t1(x);"
1197 "BEGIN;"
1198 "INSERT INTO t1 VALUES(randomblob(900));"
1199 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 2 */"
1200 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 4 */"
1201 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 8 */"
1202 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 16 */"
1203 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 32 */"
1204 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 64 */"
1205 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 128 */"
1206 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 256 */"
1207 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 512 */"
1208 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 1024 */"
1209 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 2048 */"
1210 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 4096 */"
1211 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 8192 */"
1212 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 16384 */"
1213 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 32768 */"
1214 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 65536 */"
1215 "COMMIT;"
1216 );
1217 filecopy(&err, "test.db", "test_sv.db");
1218 filecopy(&err, "test.db-wal", "test_sv.db-wal");
1219 closedb(&err, &db);
1220
1221 filecopy(&err, "test_sv.db", "test.db");
1222 filecopy(&err, "test_sv.db-wal", "test.db-wal");
1223
1224 if( err.rc==SQLITE_OK ){
1225 printf(" WAL file is %d bytes,", (int)filesize(&err,"test.db-wal"));
1226 printf(" DB file is %d.\n", (int)filesize(&err,"test.db"));
1227 }
1228
1229 setstoptime(&err, nMs);
1230 launch_thread(&err, &threads, walthread5_thread, 0);
1231 launch_thread(&err, &threads, walthread5_thread, 0);
1232 launch_thread(&err, &threads, walthread5_thread, 0);
1233 launch_thread(&err, &threads, walthread5_thread, 0);
1234 launch_thread(&err, &threads, walthread5_thread, 0);
1235 join_all_threads(&err, &threads);
1236
1237 if( err.rc==SQLITE_OK ){
1238 printf(" WAL file is %d bytes,", (int)filesize(&err,"test.db-wal"));
1239 printf(" DB file is %d.\n", (int)filesize(&err,"test.db"));
1240 }
1241
1242 print_and_free_err(&err);
1243}
1244
dan16f77202010-08-07 05:15:22 +00001245/*------------------------------------------------------------------------
1246** Test case "cgt_pager_1"
1247*/
1248#define CALLGRINDTEST1_NROW 10000
1249static void cgt_pager_1_populate(Error *pErr, Sqlite *pDb){
1250 const char *zInsert = "INSERT INTO t1 VALUES(:iRow, zeroblob(:iBlob))";
1251 i64 iRow;
1252 sql_script(pErr, pDb, "BEGIN");
1253 for(iRow=1; iRow<=CALLGRINDTEST1_NROW; iRow++){
1254 i64 iBlob = 600 + (iRow%300);
1255 execsql(pErr, pDb, zInsert, &iRow, &iBlob);
1256 }
1257 sql_script(pErr, pDb, "COMMIT");
1258}
1259static void cgt_pager_1_update(Error *pErr, Sqlite *pDb){
1260 const char *zUpdate = "UPDATE t1 SET b = zeroblob(:iBlob) WHERE a = :iRow";
1261 i64 iRow;
1262 sql_script(pErr, pDb, "BEGIN");
1263 for(iRow=1; iRow<=CALLGRINDTEST1_NROW; iRow++){
1264 i64 iBlob = 600 + ((iRow+100)%300);
1265 execsql(pErr, pDb, zUpdate, &iBlob, &iRow);
1266 }
1267 sql_script(pErr, pDb, "COMMIT");
1268}
1269static void cgt_pager_1_read(Error *pErr, Sqlite *pDb){
1270 i64 iRow;
1271 sql_script(pErr, pDb, "BEGIN");
1272 for(iRow=1; iRow<=CALLGRINDTEST1_NROW; iRow++){
1273 execsql(pErr, pDb, "SELECT * FROM t1 WHERE a = :iRow", &iRow);
1274 }
1275 sql_script(pErr, pDb, "COMMIT");
1276}
1277static void cgt_pager_1(int nMs){
1278 void (*xSub)(Error *, Sqlite *);
1279 Error err = {0};
1280 Sqlite db = {0};
1281
1282 opendb(&err, &db, "test.db", 1);
1283 sql_script(&err, &db,
1284 "PRAGMA cache_size = 2000;"
1285 "PRAGMA page_size = 1024;"
1286 "CREATE TABLE t1(a INTEGER PRIMARY KEY, b BLOB);"
1287 );
1288
1289 xSub = cgt_pager_1_populate; xSub(&err, &db);
1290 xSub = cgt_pager_1_update; xSub(&err, &db);
1291 xSub = cgt_pager_1_read; xSub(&err, &db);
1292
1293 closedb(&err, &db);
1294 print_and_free_err(&err);
1295}
1296
danec033642010-10-28 15:52:04 +00001297/*------------------------------------------------------------------------
1298** Test case "dynamic_triggers"
1299**
1300** Two threads executing statements that cause deeply nested triggers
1301** to fire. And one thread busily creating and deleting triggers. This
1302** is an attempt to find a bug reported to us.
1303*/
1304
dan053542d2014-12-13 17:41:48 +00001305static char *dynamic_triggers_1(int iTid, void *pArg){
danec033642010-10-28 15:52:04 +00001306 Error err = {0}; /* Error code and message */
1307 Sqlite db = {0}; /* SQLite database connection */
1308 int nDrop = 0;
1309 int nCreate = 0;
1310
1311 opendb(&err, &db, "test.db", 0);
1312 while( !timetostop(&err) ){
1313 int i;
1314
1315 for(i=1; i<9; i++){
1316 char *zSql = sqlite3_mprintf(
1317 "CREATE TRIGGER itr%d BEFORE INSERT ON t%d BEGIN "
1318 "INSERT INTO t%d VALUES(new.x, new.y);"
1319 "END;", i, i, i+1
1320 );
1321 execsql(&err, &db, zSql);
1322 sqlite3_free(zSql);
1323 nCreate++;
1324 }
1325
1326 for(i=1; i<9; i++){
1327 char *zSql = sqlite3_mprintf(
1328 "CREATE TRIGGER dtr%d BEFORE DELETE ON t%d BEGIN "
1329 "DELETE FROM t%d WHERE x = old.x; "
1330 "END;", i, i, i+1
1331 );
1332 execsql(&err, &db, zSql);
1333 sqlite3_free(zSql);
1334 nCreate++;
1335 }
1336
1337 for(i=1; i<9; i++){
1338 char *zSql = sqlite3_mprintf("DROP TRIGGER itr%d", i);
1339 execsql(&err, &db, zSql);
1340 sqlite3_free(zSql);
1341 nDrop++;
1342 }
1343
1344 for(i=1; i<9; i++){
1345 char *zSql = sqlite3_mprintf("DROP TRIGGER dtr%d", i);
1346 execsql(&err, &db, zSql);
1347 sqlite3_free(zSql);
1348 nDrop++;
1349 }
1350 }
dand44b7862014-12-15 08:46:17 +00001351 closedb(&err, &db);
danec033642010-10-28 15:52:04 +00001352
1353 print_and_free_err(&err);
1354 return sqlite3_mprintf("%d created, %d dropped", nCreate, nDrop);
1355}
1356
dan053542d2014-12-13 17:41:48 +00001357static char *dynamic_triggers_2(int iTid, void *pArg){
danec033642010-10-28 15:52:04 +00001358 Error err = {0}; /* Error code and message */
1359 Sqlite db = {0}; /* SQLite database connection */
1360 i64 iVal = 0;
1361 int nInsert = 0;
1362 int nDelete = 0;
1363
1364 opendb(&err, &db, "test.db", 0);
1365 while( !timetostop(&err) ){
1366 do {
1367 iVal = (iVal+1)%100;
1368 execsql(&err, &db, "INSERT INTO t1 VALUES(:iX, :iY+1)", &iVal, &iVal);
1369 nInsert++;
1370 } while( iVal );
1371
1372 do {
1373 iVal = (iVal+1)%100;
1374 execsql(&err, &db, "DELETE FROM t1 WHERE x = :iX", &iVal);
1375 nDelete++;
1376 } while( iVal );
1377 }
dand44b7862014-12-15 08:46:17 +00001378 closedb(&err, &db);
danec033642010-10-28 15:52:04 +00001379
1380 print_and_free_err(&err);
1381 return sqlite3_mprintf("%d inserts, %d deletes", nInsert, nDelete);
1382}
1383
1384static void dynamic_triggers(int nMs){
1385 Error err = {0};
1386 Sqlite db = {0};
1387 Threadset threads = {0};
1388
1389 opendb(&err, &db, "test.db", 1);
1390 sql_script(&err, &db,
1391 "PRAGMA page_size = 1024;"
1392 "PRAGMA journal_mode = WAL;"
1393 "CREATE TABLE t1(x, y);"
1394 "CREATE TABLE t2(x, y);"
1395 "CREATE TABLE t3(x, y);"
1396 "CREATE TABLE t4(x, y);"
1397 "CREATE TABLE t5(x, y);"
1398 "CREATE TABLE t6(x, y);"
1399 "CREATE TABLE t7(x, y);"
1400 "CREATE TABLE t8(x, y);"
1401 "CREATE TABLE t9(x, y);"
1402 );
dand44b7862014-12-15 08:46:17 +00001403 closedb(&err, &db);
danec033642010-10-28 15:52:04 +00001404
1405 setstoptime(&err, nMs);
1406
1407 sqlite3_enable_shared_cache(1);
1408 launch_thread(&err, &threads, dynamic_triggers_2, 0);
1409 launch_thread(&err, &threads, dynamic_triggers_2, 0);
danec033642010-10-28 15:52:04 +00001410
1411 sleep(2);
dan053542d2014-12-13 17:41:48 +00001412 sqlite3_enable_shared_cache(0);
danec033642010-10-28 15:52:04 +00001413
1414 launch_thread(&err, &threads, dynamic_triggers_2, 0);
1415 launch_thread(&err, &threads, dynamic_triggers_1, 0);
1416
1417 join_all_threads(&err, &threads);
1418
1419 print_and_free_err(&err);
1420}
1421
dan0235a032014-12-08 20:20:16 +00001422
1423
dan24cd6162010-11-19 09:58:11 +00001424#include "tt3_checkpoint.c"
dan0235a032014-12-08 20:20:16 +00001425#include "tt3_index.c"
dan85753662014-12-11 16:38:18 +00001426#include "tt3_lookaside1.c"
dan04209542014-12-12 16:39:38 +00001427#include "tt3_vacuum.c"
1428#include "tt3_stress.c"
danec033642010-10-28 15:52:04 +00001429
dan4be02b92010-07-22 15:44:06 +00001430int main(int argc, char **argv){
1431 struct ThreadTest {
drh9486c1b2014-12-30 19:26:07 +00001432 void (*xTest)(int); /* Routine for running this test */
1433 const char *zTest; /* Name of this test */
1434 int nMs; /* How long to run this test, in milliseconds */
dan4be02b92010-07-22 15:44:06 +00001435 } aTest[] = {
1436 { walthread1, "walthread1", 20000 },
1437 { walthread2, "walthread2", 20000 },
1438 { walthread3, "walthread3", 20000 },
1439 { walthread4, "walthread4", 20000 },
1440 { walthread5, "walthread5", 1000 },
dan16f77202010-08-07 05:15:22 +00001441
dan24cd6162010-11-19 09:58:11 +00001442 { cgt_pager_1, "cgt_pager_1", 0 },
danec033642010-10-28 15:52:04 +00001443 { dynamic_triggers, "dynamic_triggers", 20000 },
dan24cd6162010-11-19 09:58:11 +00001444
1445 { checkpoint_starvation_1, "checkpoint_starvation_1", 10000 },
1446 { checkpoint_starvation_2, "checkpoint_starvation_2", 10000 },
dan0235a032014-12-08 20:20:16 +00001447
1448 { create_drop_index_1, "create_drop_index_1", 10000 },
dan04209542014-12-12 16:39:38 +00001449 { lookaside1, "lookaside1", 10000 },
1450 { vacuum1, "vacuum1", 10000 },
1451 { stress1, "stress1", 10000 },
dan1ee46c02014-12-15 20:49:26 +00001452 { stress2, "stress2", 60000 },
dan4be02b92010-07-22 15:44:06 +00001453 };
drh169c4642014-12-31 18:28:59 +00001454 static char *substArgv[] = { 0, "*", 0 };
1455 int i, iArg;
drh9486c1b2014-12-30 19:26:07 +00001456 int nTestfound = 0;
dan4be02b92010-07-22 15:44:06 +00001457
1458 sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
drh169c4642014-12-31 18:28:59 +00001459 if( argc<2 ){
1460 argc = 2;
1461 argv = substArgv;
1462 }
1463 for(iArg=1; iArg<argc; iArg++){
1464 for(i=0; i<sizeof(aTest)/sizeof(aTest[0]); i++){
1465 if( sqlite3_strglob(argv[iArg],aTest[i].zTest)==0 ) break;
dan4be02b92010-07-22 15:44:06 +00001466 }
drh169c4642014-12-31 18:28:59 +00001467 if( i>=sizeof(aTest)/sizeof(aTest[0]) ) goto usage;
1468 }
1469 for(iArg=1; iArg<argc; iArg++){
1470 for(i=0; i<sizeof(aTest)/sizeof(aTest[0]); i++){
1471 char const *z = aTest[i].zTest;
1472 if( sqlite3_strglob(argv[iArg],z)==0 ){
1473 printf("Running %s for %d seconds...\n", z, aTest[i].nMs/1000);
1474 fflush(stdout);
1475 aTest[i].xTest(aTest[i].nMs);
1476 nTestfound++;
1477 }
1478 }
dan4be02b92010-07-22 15:44:06 +00001479 }
drh9486c1b2014-12-30 19:26:07 +00001480 if( nTestfound==0 ) goto usage;
dan4be02b92010-07-22 15:44:06 +00001481
drh9486c1b2014-12-30 19:26:07 +00001482 printf("%d errors out of %d tests\n", nGlobalErr, nTestfound);
dan4be02b92010-07-22 15:44:06 +00001483 return (nGlobalErr>0 ? 255 : 0);
1484
1485 usage:
dand44b7862014-12-15 08:46:17 +00001486 printf("Usage: %s [testname|testprefix*]...\n", argv[0]);
dan4be02b92010-07-22 15:44:06 +00001487 printf("Available tests are:\n");
1488 for(i=0; i<sizeof(aTest)/sizeof(aTest[0]); i++){
1489 printf(" %s\n", aTest[i].zTest);
1490 }
1491
1492 return 254;
1493}