blob: 4ca791c4f05ead3c8c408db2e7446020e1be7e95 [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>
danbb2d2a52021-07-19 16:49:13 +000081
82#ifdef _WIN32
83# include <stdio.h>
84# include <string.h>
85# include <assert.h>
86# include <process.h>
87# include <windows.h>
88# include <sys/types.h>
89# include <sys/stat.h>
90# include <errno.h>
91# include <fcntl.h>
92# include <io.h>
93#else
94# include <unistd.h>
95# include <stdio.h>
96# include <pthread.h>
97# include <assert.h>
98# include <sys/types.h>
99# include <sys/stat.h>
100# include <string.h>
101# include <fcntl.h>
102# include <errno.h>
103
104# define O_BINARY 0
105#endif
dan4be02b92010-07-22 15:44:06 +0000106
dan48c06f32015-12-03 11:51:18 +0000107#include "test_multiplex.h"
108
dan5fcc1ba2015-12-03 12:01:54 +0000109/* Required to link test_multiplex.c */
110#ifndef SQLITE_OMIT_WSD
111int sqlite3PendingByte = 0x40000000;
112#endif
113
dan4be02b92010-07-22 15:44:06 +0000114/*
115 * This code implements the MD5 message-digest algorithm.
116 * The algorithm is due to Ron Rivest. This code was
117 * written by Colin Plumb in 1993, no copyright is claimed.
118 * This code is in the public domain; do with it what you wish.
119 *
120 * Equivalent code is available from RSA Data Security, Inc.
121 * This code has been tested against that, and is equivalent,
122 * except that you don't need to include two pages of legalese
123 * with every copy.
124 *
125 * To compute the message digest of a chunk of bytes, declare an
126 * MD5Context structure, pass it to MD5Init, call MD5Update as
127 * needed on buffers full of bytes, and then call MD5Final, which
128 * will fill a supplied 16-byte array with the digest.
129 */
130
131/*
132 * If compiled on a machine that doesn't have a 32-bit integer,
133 * you just set "uint32" to the appropriate datatype for an
134 * unsigned 32-bit integer. For example:
135 *
136 * cc -Duint32='unsigned long' md5.c
137 *
138 */
139#ifndef uint32
140# define uint32 unsigned int
141#endif
142
143struct MD5Context {
144 int isInit;
145 uint32 buf[4];
146 uint32 bits[2];
dan1ee46c02014-12-15 20:49:26 +0000147 union {
148 unsigned char in[64];
149 uint32 in32[16];
150 } u;
dan4be02b92010-07-22 15:44:06 +0000151};
152typedef struct MD5Context MD5Context;
153
154/*
155 * Note: this code is harmless on little-endian machines.
156 */
157static void byteReverse (unsigned char *buf, unsigned longs){
158 uint32 t;
159 do {
160 t = (uint32)((unsigned)buf[3]<<8 | buf[2]) << 16 |
161 ((unsigned)buf[1]<<8 | buf[0]);
162 *(uint32 *)buf = t;
163 buf += 4;
164 } while (--longs);
165}
166/* The four core functions - F1 is optimized somewhat */
167
168/* #define F1(x, y, z) (x & y | ~x & z) */
169#define F1(x, y, z) (z ^ (x & (y ^ z)))
170#define F2(x, y, z) F1(z, x, y)
171#define F3(x, y, z) (x ^ y ^ z)
172#define F4(x, y, z) (y ^ (x | ~z))
173
174/* This is the central step in the MD5 algorithm. */
175#define MD5STEP(f, w, x, y, z, data, s) \
176 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
177
178/*
179 * The core of the MD5 algorithm, this alters an existing MD5 hash to
180 * reflect the addition of 16 longwords of new data. MD5Update blocks
181 * the data and converts bytes into longwords for this routine.
182 */
183static void MD5Transform(uint32 buf[4], const uint32 in[16]){
184 register uint32 a, b, c, d;
185
186 a = buf[0];
187 b = buf[1];
188 c = buf[2];
189 d = buf[3];
190
191 MD5STEP(F1, a, b, c, d, in[ 0]+0xd76aa478, 7);
192 MD5STEP(F1, d, a, b, c, in[ 1]+0xe8c7b756, 12);
193 MD5STEP(F1, c, d, a, b, in[ 2]+0x242070db, 17);
194 MD5STEP(F1, b, c, d, a, in[ 3]+0xc1bdceee, 22);
195 MD5STEP(F1, a, b, c, d, in[ 4]+0xf57c0faf, 7);
196 MD5STEP(F1, d, a, b, c, in[ 5]+0x4787c62a, 12);
197 MD5STEP(F1, c, d, a, b, in[ 6]+0xa8304613, 17);
198 MD5STEP(F1, b, c, d, a, in[ 7]+0xfd469501, 22);
199 MD5STEP(F1, a, b, c, d, in[ 8]+0x698098d8, 7);
200 MD5STEP(F1, d, a, b, c, in[ 9]+0x8b44f7af, 12);
201 MD5STEP(F1, c, d, a, b, in[10]+0xffff5bb1, 17);
202 MD5STEP(F1, b, c, d, a, in[11]+0x895cd7be, 22);
203 MD5STEP(F1, a, b, c, d, in[12]+0x6b901122, 7);
204 MD5STEP(F1, d, a, b, c, in[13]+0xfd987193, 12);
205 MD5STEP(F1, c, d, a, b, in[14]+0xa679438e, 17);
206 MD5STEP(F1, b, c, d, a, in[15]+0x49b40821, 22);
207
208 MD5STEP(F2, a, b, c, d, in[ 1]+0xf61e2562, 5);
209 MD5STEP(F2, d, a, b, c, in[ 6]+0xc040b340, 9);
210 MD5STEP(F2, c, d, a, b, in[11]+0x265e5a51, 14);
211 MD5STEP(F2, b, c, d, a, in[ 0]+0xe9b6c7aa, 20);
212 MD5STEP(F2, a, b, c, d, in[ 5]+0xd62f105d, 5);
213 MD5STEP(F2, d, a, b, c, in[10]+0x02441453, 9);
214 MD5STEP(F2, c, d, a, b, in[15]+0xd8a1e681, 14);
215 MD5STEP(F2, b, c, d, a, in[ 4]+0xe7d3fbc8, 20);
216 MD5STEP(F2, a, b, c, d, in[ 9]+0x21e1cde6, 5);
217 MD5STEP(F2, d, a, b, c, in[14]+0xc33707d6, 9);
218 MD5STEP(F2, c, d, a, b, in[ 3]+0xf4d50d87, 14);
219 MD5STEP(F2, b, c, d, a, in[ 8]+0x455a14ed, 20);
220 MD5STEP(F2, a, b, c, d, in[13]+0xa9e3e905, 5);
221 MD5STEP(F2, d, a, b, c, in[ 2]+0xfcefa3f8, 9);
222 MD5STEP(F2, c, d, a, b, in[ 7]+0x676f02d9, 14);
223 MD5STEP(F2, b, c, d, a, in[12]+0x8d2a4c8a, 20);
224
225 MD5STEP(F3, a, b, c, d, in[ 5]+0xfffa3942, 4);
226 MD5STEP(F3, d, a, b, c, in[ 8]+0x8771f681, 11);
227 MD5STEP(F3, c, d, a, b, in[11]+0x6d9d6122, 16);
228 MD5STEP(F3, b, c, d, a, in[14]+0xfde5380c, 23);
229 MD5STEP(F3, a, b, c, d, in[ 1]+0xa4beea44, 4);
230 MD5STEP(F3, d, a, b, c, in[ 4]+0x4bdecfa9, 11);
231 MD5STEP(F3, c, d, a, b, in[ 7]+0xf6bb4b60, 16);
232 MD5STEP(F3, b, c, d, a, in[10]+0xbebfbc70, 23);
233 MD5STEP(F3, a, b, c, d, in[13]+0x289b7ec6, 4);
234 MD5STEP(F3, d, a, b, c, in[ 0]+0xeaa127fa, 11);
235 MD5STEP(F3, c, d, a, b, in[ 3]+0xd4ef3085, 16);
236 MD5STEP(F3, b, c, d, a, in[ 6]+0x04881d05, 23);
237 MD5STEP(F3, a, b, c, d, in[ 9]+0xd9d4d039, 4);
238 MD5STEP(F3, d, a, b, c, in[12]+0xe6db99e5, 11);
239 MD5STEP(F3, c, d, a, b, in[15]+0x1fa27cf8, 16);
240 MD5STEP(F3, b, c, d, a, in[ 2]+0xc4ac5665, 23);
241
242 MD5STEP(F4, a, b, c, d, in[ 0]+0xf4292244, 6);
243 MD5STEP(F4, d, a, b, c, in[ 7]+0x432aff97, 10);
244 MD5STEP(F4, c, d, a, b, in[14]+0xab9423a7, 15);
245 MD5STEP(F4, b, c, d, a, in[ 5]+0xfc93a039, 21);
246 MD5STEP(F4, a, b, c, d, in[12]+0x655b59c3, 6);
247 MD5STEP(F4, d, a, b, c, in[ 3]+0x8f0ccc92, 10);
248 MD5STEP(F4, c, d, a, b, in[10]+0xffeff47d, 15);
249 MD5STEP(F4, b, c, d, a, in[ 1]+0x85845dd1, 21);
250 MD5STEP(F4, a, b, c, d, in[ 8]+0x6fa87e4f, 6);
251 MD5STEP(F4, d, a, b, c, in[15]+0xfe2ce6e0, 10);
252 MD5STEP(F4, c, d, a, b, in[ 6]+0xa3014314, 15);
253 MD5STEP(F4, b, c, d, a, in[13]+0x4e0811a1, 21);
254 MD5STEP(F4, a, b, c, d, in[ 4]+0xf7537e82, 6);
255 MD5STEP(F4, d, a, b, c, in[11]+0xbd3af235, 10);
256 MD5STEP(F4, c, d, a, b, in[ 2]+0x2ad7d2bb, 15);
257 MD5STEP(F4, b, c, d, a, in[ 9]+0xeb86d391, 21);
258
259 buf[0] += a;
260 buf[1] += b;
261 buf[2] += c;
262 buf[3] += d;
263}
264
265/*
266 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
267 * initialization constants.
268 */
269static void MD5Init(MD5Context *ctx){
270 ctx->isInit = 1;
271 ctx->buf[0] = 0x67452301;
272 ctx->buf[1] = 0xefcdab89;
273 ctx->buf[2] = 0x98badcfe;
274 ctx->buf[3] = 0x10325476;
275 ctx->bits[0] = 0;
276 ctx->bits[1] = 0;
277}
278
279/*
280 * Update context to reflect the concatenation of another buffer full
281 * of bytes.
282 */
283static
284void MD5Update(MD5Context *ctx, const unsigned char *buf, unsigned int len){
285 uint32 t;
286
287 /* Update bitcount */
288
289 t = ctx->bits[0];
290 if ((ctx->bits[0] = t + ((uint32)len << 3)) < t)
291 ctx->bits[1]++; /* Carry from low to high */
292 ctx->bits[1] += len >> 29;
293
294 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
295
296 /* Handle any leading odd-sized chunks */
297
298 if ( t ) {
dan1ee46c02014-12-15 20:49:26 +0000299 unsigned char *p = (unsigned char *)ctx->u.in + t;
dan4be02b92010-07-22 15:44:06 +0000300
301 t = 64-t;
302 if (len < t) {
303 memcpy(p, buf, len);
304 return;
305 }
306 memcpy(p, buf, t);
dan1ee46c02014-12-15 20:49:26 +0000307 byteReverse(ctx->u.in, 16);
308 MD5Transform(ctx->buf, (uint32 *)ctx->u.in);
dan4be02b92010-07-22 15:44:06 +0000309 buf += t;
310 len -= t;
311 }
312
313 /* Process data in 64-byte chunks */
314
315 while (len >= 64) {
dan1ee46c02014-12-15 20:49:26 +0000316 memcpy(ctx->u.in, buf, 64);
317 byteReverse(ctx->u.in, 16);
318 MD5Transform(ctx->buf, (uint32 *)ctx->u.in);
dan4be02b92010-07-22 15:44:06 +0000319 buf += 64;
320 len -= 64;
321 }
322
323 /* Handle any remaining bytes of data. */
324
dan1ee46c02014-12-15 20:49:26 +0000325 memcpy(ctx->u.in, buf, len);
dan4be02b92010-07-22 15:44:06 +0000326}
327
328/*
329 * Final wrapup - pad to 64-byte boundary with the bit pattern
330 * 1 0* (64-bit count of bits processed, MSB-first)
331 */
332static void MD5Final(unsigned char digest[16], MD5Context *ctx){
333 unsigned count;
334 unsigned char *p;
335
336 /* Compute number of bytes mod 64 */
337 count = (ctx->bits[0] >> 3) & 0x3F;
338
339 /* Set the first char of padding to 0x80. This is safe since there is
340 always at least one byte free */
dan1ee46c02014-12-15 20:49:26 +0000341 p = ctx->u.in + count;
dan4be02b92010-07-22 15:44:06 +0000342 *p++ = 0x80;
343
344 /* Bytes of padding needed to make 64 bytes */
345 count = 64 - 1 - count;
346
347 /* Pad out to 56 mod 64 */
348 if (count < 8) {
349 /* Two lots of padding: Pad the first block to 64 bytes */
350 memset(p, 0, count);
dan1ee46c02014-12-15 20:49:26 +0000351 byteReverse(ctx->u.in, 16);
352 MD5Transform(ctx->buf, (uint32 *)ctx->u.in);
dan4be02b92010-07-22 15:44:06 +0000353
354 /* Now fill the next block with 56 bytes */
dan1ee46c02014-12-15 20:49:26 +0000355 memset(ctx->u.in, 0, 56);
dan4be02b92010-07-22 15:44:06 +0000356 } else {
357 /* Pad block to 56 bytes */
358 memset(p, 0, count-8);
359 }
dan1ee46c02014-12-15 20:49:26 +0000360 byteReverse(ctx->u.in, 14);
dan4be02b92010-07-22 15:44:06 +0000361
362 /* Append length in bits and transform */
dan1ee46c02014-12-15 20:49:26 +0000363 ctx->u.in32[14] = ctx->bits[0];
364 ctx->u.in32[15] = ctx->bits[1];
dan4be02b92010-07-22 15:44:06 +0000365
dan1ee46c02014-12-15 20:49:26 +0000366 MD5Transform(ctx->buf, (uint32 *)ctx->u.in);
dan4be02b92010-07-22 15:44:06 +0000367 byteReverse((unsigned char *)ctx->buf, 4);
368 memcpy(digest, ctx->buf, 16);
dan053542d2014-12-13 17:41:48 +0000369 memset(ctx, 0, sizeof(*ctx)); /* In case it is sensitive */
dan4be02b92010-07-22 15:44:06 +0000370}
371
372/*
373** Convert a 128-bit MD5 digest into a 32-digit base-16 number.
374*/
375static void MD5DigestToBase16(unsigned char *digest, char *zBuf){
376 static char const zEncode[] = "0123456789abcdef";
377 int i, j;
378
379 for(j=i=0; i<16; i++){
380 int a = digest[i];
381 zBuf[j++] = zEncode[(a>>4)&0xf];
382 zBuf[j++] = zEncode[a & 0xf];
383 }
384 zBuf[j] = 0;
385}
386
387/*
388** During testing, the special md5sum() aggregate function is available.
389** inside SQLite. The following routines implement that function.
390*/
391static void md5step(sqlite3_context *context, int argc, sqlite3_value **argv){
392 MD5Context *p;
393 int i;
394 if( argc<1 ) return;
395 p = sqlite3_aggregate_context(context, sizeof(*p));
396 if( p==0 ) return;
397 if( !p->isInit ){
398 MD5Init(p);
399 }
400 for(i=0; i<argc; i++){
401 const char *zData = (char*)sqlite3_value_text(argv[i]);
402 if( zData ){
403 MD5Update(p, (unsigned char*)zData, strlen(zData));
404 }
405 }
406}
407static void md5finalize(sqlite3_context *context){
408 MD5Context *p;
409 unsigned char digest[16];
410 char zBuf[33];
411 p = sqlite3_aggregate_context(context, sizeof(*p));
412 MD5Final(digest,p);
413 MD5DigestToBase16(digest, zBuf);
414 sqlite3_result_text(context, zBuf, -1, SQLITE_TRANSIENT);
415}
416
drh9486c1b2014-12-30 19:26:07 +0000417/*
dan4be02b92010-07-22 15:44:06 +0000418** End of copied md5sum() code.
drh9486c1b2014-12-30 19:26:07 +0000419**************************************************************************/
dan4be02b92010-07-22 15:44:06 +0000420
421typedef sqlite3_int64 i64;
422
423typedef struct Error Error;
424typedef struct Sqlite Sqlite;
425typedef struct Statement Statement;
426
427typedef struct Threadset Threadset;
428typedef struct Thread Thread;
429
430/* Total number of errors in this process so far. */
431static int nGlobalErr = 0;
432
dan4be02b92010-07-22 15:44:06 +0000433struct Error {
434 int rc;
435 int iLine;
436 char *zErr;
437};
438
439struct Sqlite {
440 sqlite3 *db; /* Database handle */
441 Statement *pCache; /* Linked list of cached statements */
442 int nText; /* Size of array at aText[] */
443 char **aText; /* Stored text results */
444};
445
446struct Statement {
447 sqlite3_stmt *pStmt; /* Pre-compiled statement handle */
448 Statement *pNext; /* Next statement in linked-list */
449};
450
451struct Thread {
452 int iTid; /* Thread number within test */
dan053542d2014-12-13 17:41:48 +0000453 void* pArg; /* Pointer argument passed by caller */
dan4be02b92010-07-22 15:44:06 +0000454
danbb2d2a52021-07-19 16:49:13 +0000455#ifdef _WIN32
456 uintptr_t winTid; /* Thread handle */
457#else
dan4be02b92010-07-22 15:44:06 +0000458 pthread_t tid; /* Thread id */
danbb2d2a52021-07-19 16:49:13 +0000459#endif
dan053542d2014-12-13 17:41:48 +0000460 char *(*xProc)(int, void*); /* Thread main proc */
danbb2d2a52021-07-19 16:49:13 +0000461 char *zRes; /* Value returned by xProc */
dan4be02b92010-07-22 15:44:06 +0000462 Thread *pNext; /* Next in this list of threads */
463};
464
465struct Threadset {
466 int iMaxTid; /* Largest iTid value allocated so far */
467 Thread *pThread; /* Linked list of threads */
468};
469
470static void free_err(Error *p){
471 sqlite3_free(p->zErr);
472 p->zErr = 0;
473 p->rc = 0;
474}
475
476static void print_err(Error *p){
477 if( p->rc!=SQLITE_OK ){
drhbcbac682014-12-31 18:55:09 +0000478 int isWarn = 0;
479 if( p->rc==SQLITE_SCHEMA ) isWarn = 1;
480 if( sqlite3_strglob("* - no such table: *",p->zErr)==0 ) isWarn = 1;
481 printf("%s: (%d) \"%s\" at line %d\n", isWarn ? "Warning" : "Error",
482 p->rc, p->zErr, p->iLine);
483 if( !isWarn ) nGlobalErr++;
drh9486c1b2014-12-30 19:26:07 +0000484 fflush(stdout);
dan4be02b92010-07-22 15:44:06 +0000485 }
486}
487
488static void print_and_free_err(Error *p){
489 print_err(p);
490 free_err(p);
491}
492
493static void system_error(Error *pErr, int iSys){
494 pErr->rc = iSys;
danbb2d2a52021-07-19 16:49:13 +0000495#if _WIN32
496 pErr->zErr = sqlite3_mprintf("%s", strerror(iSys));
497#else
dan4be02b92010-07-22 15:44:06 +0000498 pErr->zErr = (char *)sqlite3_malloc(512);
499 strerror_r(iSys, pErr->zErr, 512);
500 pErr->zErr[511] = '\0';
danbb2d2a52021-07-19 16:49:13 +0000501#endif
dan4be02b92010-07-22 15:44:06 +0000502}
503
504static void sqlite_error(
505 Error *pErr,
506 Sqlite *pDb,
507 const char *zFunc
508){
509 pErr->rc = sqlite3_errcode(pDb->db);
510 pErr->zErr = sqlite3_mprintf(
511 "sqlite3_%s() - %s (%d)", zFunc, sqlite3_errmsg(pDb->db),
512 sqlite3_extended_errcode(pDb->db)
513 );
514}
515
516static void test_error_x(
517 Error *pErr,
518 char *zErr
519){
520 if( pErr->rc==SQLITE_OK ){
521 pErr->rc = 1;
522 pErr->zErr = zErr;
523 }else{
524 sqlite3_free(zErr);
525 }
526}
527
528static void clear_error_x(
529 Error *pErr,
530 int rc
531){
532 if( pErr->rc==rc ){
533 pErr->rc = SQLITE_OK;
534 sqlite3_free(pErr->zErr);
535 pErr->zErr = 0;
536 }
537}
538
539static int busyhandler(void *pArg, int n){
danbb2d2a52021-07-19 16:49:13 +0000540 sqlite3_sleep(10);
dan4be02b92010-07-22 15:44:06 +0000541 return 1;
542}
543
544static void opendb_x(
545 Error *pErr, /* IN/OUT: Error code */
546 Sqlite *pDb, /* OUT: Database handle */
547 const char *zFile, /* Database file name */
548 int bDelete /* True to delete db file before opening */
549){
550 if( pErr->rc==SQLITE_OK ){
551 int rc;
dan053542d2014-12-13 17:41:48 +0000552 int flags = SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE | SQLITE_OPEN_URI;
dan4be02b92010-07-22 15:44:06 +0000553 if( bDelete ) unlink(zFile);
dan053542d2014-12-13 17:41:48 +0000554 rc = sqlite3_open_v2(zFile, &pDb->db, flags, 0);
dan4be02b92010-07-22 15:44:06 +0000555 if( rc ){
556 sqlite_error(pErr, pDb, "open");
557 sqlite3_close(pDb->db);
558 pDb->db = 0;
559 }else{
560 sqlite3_create_function(
561 pDb->db, "md5sum", -1, SQLITE_UTF8, 0, 0, md5step, md5finalize
562 );
563 sqlite3_busy_handler(pDb->db, busyhandler, 0);
dan16f77202010-08-07 05:15:22 +0000564 sqlite3_exec(pDb->db, "PRAGMA synchronous=OFF", 0, 0, 0);
dan4be02b92010-07-22 15:44:06 +0000565 }
566 }
567}
568
569static void closedb_x(
570 Error *pErr, /* IN/OUT: Error code */
571 Sqlite *pDb /* OUT: Database handle */
572){
573 int rc;
574 int i;
575 Statement *pIter;
576 Statement *pNext;
577 for(pIter=pDb->pCache; pIter; pIter=pNext){
578 pNext = pIter->pNext;
579 sqlite3_finalize(pIter->pStmt);
580 sqlite3_free(pIter);
581 }
582 for(i=0; i<pDb->nText; i++){
583 sqlite3_free(pDb->aText[i]);
584 }
585 sqlite3_free(pDb->aText);
586 rc = sqlite3_close(pDb->db);
587 if( rc && pErr->rc==SQLITE_OK ){
588 pErr->zErr = sqlite3_mprintf("%s", sqlite3_errmsg(pDb->db));
589 }
590 memset(pDb, 0, sizeof(Sqlite));
591}
592
593static void sql_script_x(
594 Error *pErr, /* IN/OUT: Error code */
595 Sqlite *pDb, /* Database handle */
596 const char *zSql /* SQL script to execute */
597){
598 if( pErr->rc==SQLITE_OK ){
599 pErr->rc = sqlite3_exec(pDb->db, zSql, 0, 0, &pErr->zErr);
600 }
601}
602
dan053542d2014-12-13 17:41:48 +0000603static void sql_script_printf_x(
604 Error *pErr, /* IN/OUT: Error code */
605 Sqlite *pDb, /* Database handle */
606 const char *zFormat, /* SQL printf format string */
607 ... /* Printf args */
608){
609 va_list ap; /* ... printf arguments */
610 va_start(ap, zFormat);
611 if( pErr->rc==SQLITE_OK ){
612 char *zSql = sqlite3_vmprintf(zFormat, ap);
613 pErr->rc = sqlite3_exec(pDb->db, zSql, 0, 0, &pErr->zErr);
614 sqlite3_free(zSql);
615 }
616 va_end(ap);
617}
618
dan4be02b92010-07-22 15:44:06 +0000619static Statement *getSqlStatement(
620 Error *pErr, /* IN/OUT: Error code */
621 Sqlite *pDb, /* Database handle */
622 const char *zSql /* SQL statement */
623){
624 Statement *pRet;
625 int rc;
626
627 for(pRet=pDb->pCache; pRet; pRet=pRet->pNext){
628 if( 0==strcmp(sqlite3_sql(pRet->pStmt), zSql) ){
629 return pRet;
630 }
631 }
632
633 pRet = sqlite3_malloc(sizeof(Statement));
634 rc = sqlite3_prepare_v2(pDb->db, zSql, -1, &pRet->pStmt, 0);
635 if( rc!=SQLITE_OK ){
636 sqlite_error(pErr, pDb, "prepare_v2");
637 return 0;
638 }
639 assert( 0==strcmp(sqlite3_sql(pRet->pStmt), zSql) );
640
641 pRet->pNext = pDb->pCache;
642 pDb->pCache = pRet;
643 return pRet;
644}
645
646static sqlite3_stmt *getAndBindSqlStatement(
647 Error *pErr, /* IN/OUT: Error code */
648 Sqlite *pDb, /* Database handle */
649 va_list ap /* SQL followed by parameters */
650){
651 Statement *pStatement; /* The SQLite statement wrapper */
652 sqlite3_stmt *pStmt; /* The SQLite statement to return */
653 int i; /* Used to iterate through parameters */
654
655 pStatement = getSqlStatement(pErr, pDb, va_arg(ap, const char *));
656 if( !pStatement ) return 0;
657 pStmt = pStatement->pStmt;
658 for(i=1; i<=sqlite3_bind_parameter_count(pStmt); i++){
659 const char *zName = sqlite3_bind_parameter_name(pStmt, i);
660 void * pArg = va_arg(ap, void*);
661
662 switch( zName[1] ){
663 case 'i':
664 sqlite3_bind_int64(pStmt, i, *(i64 *)pArg);
665 break;
666
667 default:
668 pErr->rc = 1;
669 pErr->zErr = sqlite3_mprintf("Cannot discern type: \"%s\"", zName);
670 pStmt = 0;
671 break;
672 }
673 }
674
675 return pStmt;
676}
677
678static i64 execsql_i64_x(
679 Error *pErr, /* IN/OUT: Error code */
680 Sqlite *pDb, /* Database handle */
681 ... /* SQL and pointers to parameter values */
682){
683 i64 iRet = 0;
684 if( pErr->rc==SQLITE_OK ){
685 sqlite3_stmt *pStmt; /* SQL statement to execute */
686 va_list ap; /* ... arguments */
dan4be02b92010-07-22 15:44:06 +0000687 va_start(ap, pDb);
688 pStmt = getAndBindSqlStatement(pErr, pDb, ap);
689 if( pStmt ){
dan4be02b92010-07-22 15:44:06 +0000690 int first = 1;
691 while( SQLITE_ROW==sqlite3_step(pStmt) ){
692 if( first && sqlite3_column_count(pStmt)>0 ){
693 iRet = sqlite3_column_int64(pStmt, 0);
694 }
695 first = 0;
696 }
697 if( SQLITE_OK!=sqlite3_reset(pStmt) ){
698 sqlite_error(pErr, pDb, "reset");
699 }
700 }
701 va_end(ap);
702 }
703 return iRet;
704}
705
706static char * execsql_text_x(
707 Error *pErr, /* IN/OUT: Error code */
708 Sqlite *pDb, /* Database handle */
709 int iSlot, /* Db handle slot to store text in */
710 ... /* SQL and pointers to parameter values */
711){
712 char *zRet = 0;
713
714 if( iSlot>=pDb->nText ){
715 int nByte = sizeof(char *)*(iSlot+1);
716 pDb->aText = (char **)sqlite3_realloc(pDb->aText, nByte);
717 memset(&pDb->aText[pDb->nText], 0, sizeof(char*)*(iSlot+1-pDb->nText));
718 pDb->nText = iSlot+1;
719 }
720
721 if( pErr->rc==SQLITE_OK ){
722 sqlite3_stmt *pStmt; /* SQL statement to execute */
723 va_list ap; /* ... arguments */
dan4be02b92010-07-22 15:44:06 +0000724 va_start(ap, iSlot);
725 pStmt = getAndBindSqlStatement(pErr, pDb, ap);
726 if( pStmt ){
dan4be02b92010-07-22 15:44:06 +0000727 int first = 1;
728 while( SQLITE_ROW==sqlite3_step(pStmt) ){
729 if( first && sqlite3_column_count(pStmt)>0 ){
730 zRet = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 0));
731 sqlite3_free(pDb->aText[iSlot]);
732 pDb->aText[iSlot] = zRet;
733 }
734 first = 0;
735 }
736 if( SQLITE_OK!=sqlite3_reset(pStmt) ){
737 sqlite_error(pErr, pDb, "reset");
738 }
739 }
740 va_end(ap);
741 }
742
743 return zRet;
744}
745
746static void integrity_check_x(
747 Error *pErr, /* IN/OUT: Error code */
748 Sqlite *pDb /* Database handle */
749){
750 if( pErr->rc==SQLITE_OK ){
751 Statement *pStatement; /* Statement to execute */
dan4be02b92010-07-22 15:44:06 +0000752 char *zErr = 0; /* Integrity check error */
753
754 pStatement = getSqlStatement(pErr, pDb, "PRAGMA integrity_check");
755 if( pStatement ){
756 sqlite3_stmt *pStmt = pStatement->pStmt;
757 while( SQLITE_ROW==sqlite3_step(pStmt) ){
dan053542d2014-12-13 17:41:48 +0000758 const char *z = (const char*)sqlite3_column_text(pStmt, 0);
dan4be02b92010-07-22 15:44:06 +0000759 if( strcmp(z, "ok") ){
760 if( zErr==0 ){
761 zErr = sqlite3_mprintf("%s", z);
762 }else{
763 zErr = sqlite3_mprintf("%z\n%s", zErr, z);
764 }
765 }
766 }
767 sqlite3_reset(pStmt);
768
769 if( zErr ){
770 pErr->zErr = zErr;
771 pErr->rc = 1;
772 }
773 }
774 }
775}
776
danbb2d2a52021-07-19 16:49:13 +0000777#ifdef _WIN32
778static unsigned __stdcall launch_thread_main(void *pArg){
779 Thread *p = (Thread *)pArg;
780 p->zRes = p->xProc(p->iTid, p->pArg);
781 _endthreadex(0);
782 return 0; /* NOT REACHED */
783}
784#else
dan4be02b92010-07-22 15:44:06 +0000785static void *launch_thread_main(void *pArg){
786 Thread *p = (Thread *)pArg;
danbb2d2a52021-07-19 16:49:13 +0000787 p->zRes = p->xProc(p->iTid, p->pArg);
788 return 0;
dan4be02b92010-07-22 15:44:06 +0000789}
danbb2d2a52021-07-19 16:49:13 +0000790#endif
dan4be02b92010-07-22 15:44:06 +0000791
792static void launch_thread_x(
793 Error *pErr, /* IN/OUT: Error code */
794 Threadset *pThreads, /* Thread set */
dan053542d2014-12-13 17:41:48 +0000795 char *(*xProc)(int, void*), /* Proc to run */
796 void *pArg /* Argument passed to thread proc */
dan4be02b92010-07-22 15:44:06 +0000797){
798 if( pErr->rc==SQLITE_OK ){
799 int iTid = ++pThreads->iMaxTid;
800 Thread *p;
801 int rc;
802
803 p = (Thread *)sqlite3_malloc(sizeof(Thread));
804 memset(p, 0, sizeof(Thread));
805 p->iTid = iTid;
dan053542d2014-12-13 17:41:48 +0000806 p->pArg = pArg;
dan4be02b92010-07-22 15:44:06 +0000807 p->xProc = xProc;
808
danbb2d2a52021-07-19 16:49:13 +0000809#ifdef _WIN32
810 rc = SQLITE_OK;
811 p->winTid = _beginthreadex(0, 0, launch_thread_main, (void*)p, 0, 0);
812 if( p->winTid==0 ) rc = errno ? errno : rc;
813#else
dan4be02b92010-07-22 15:44:06 +0000814 rc = pthread_create(&p->tid, NULL, launch_thread_main, (void *)p);
danbb2d2a52021-07-19 16:49:13 +0000815#endif
dan4be02b92010-07-22 15:44:06 +0000816 if( rc!=0 ){
817 system_error(pErr, rc);
818 sqlite3_free(p);
819 }else{
820 p->pNext = pThreads->pThread;
821 pThreads->pThread = p;
822 }
823 }
824}
825
826static void join_all_threads_x(
827 Error *pErr, /* IN/OUT: Error code */
828 Threadset *pThreads /* Thread set */
829){
830 Thread *p;
831 Thread *pNext;
832 for(p=pThreads->pThread; p; p=pNext){
833 void *ret;
dan4be02b92010-07-22 15:44:06 +0000834 int rc;
danbb2d2a52021-07-19 16:49:13 +0000835 pNext = p->pNext;
836
837#ifdef _WIN32
838 do {
839 rc = WaitForSingleObjectEx((HANDLE)p->winTid, INFINITE, TRUE);
840 }while( rc==WAIT_IO_COMPLETION );
841 CloseHandle((HANDLE)p->winTid);
842#else
dan4be02b92010-07-22 15:44:06 +0000843 rc = pthread_join(p->tid, &ret);
danbb2d2a52021-07-19 16:49:13 +0000844#endif
845
dan4be02b92010-07-22 15:44:06 +0000846 if( rc!=0 ){
847 if( pErr->rc==SQLITE_OK ) system_error(pErr, rc);
848 }else{
danbb2d2a52021-07-19 16:49:13 +0000849 printf("Thread %d says: %s\n", p->iTid, (p->zRes==0 ? "..." : p->zRes));
drh9486c1b2014-12-30 19:26:07 +0000850 fflush(stdout);
dan4be02b92010-07-22 15:44:06 +0000851 }
danbb2d2a52021-07-19 16:49:13 +0000852 sqlite3_free(p->zRes);
dan4be02b92010-07-22 15:44:06 +0000853 sqlite3_free(p);
854 }
855 pThreads->pThread = 0;
856}
857
danbb2d2a52021-07-19 16:49:13 +0000858#ifdef _WIN32
859# define THREADTEST3_STAT _stat
860#else
861# define THREADTEST3_STAT stat
862#endif
863
dan4be02b92010-07-22 15:44:06 +0000864static i64 filesize_x(
865 Error *pErr,
866 const char *zFile
867){
868 i64 iRet = 0;
869 if( pErr->rc==SQLITE_OK ){
danbb2d2a52021-07-19 16:49:13 +0000870 struct THREADTEST3_STAT sStat;
871 if( THREADTEST3_STAT(zFile, &sStat) ){
dan4be02b92010-07-22 15:44:06 +0000872 iRet = -1;
873 }else{
874 iRet = sStat.st_size;
875 }
876 }
877 return iRet;
878}
879
880static void filecopy_x(
881 Error *pErr,
882 const char *zFrom,
883 const char *zTo
884){
885 if( pErr->rc==SQLITE_OK ){
886 i64 nByte = filesize_x(pErr, zFrom);
887 if( nByte<0 ){
888 test_error_x(pErr, sqlite3_mprintf("no such file: %s", zFrom));
889 }else{
890 i64 iOff;
891 char aBuf[1024];
892 int fd1;
893 int fd2;
894 unlink(zTo);
895
danbb2d2a52021-07-19 16:49:13 +0000896 fd1 = open(zFrom, O_RDONLY|O_BINARY);
dan4be02b92010-07-22 15:44:06 +0000897 if( fd1<0 ){
898 system_error(pErr, errno);
899 return;
900 }
danbb2d2a52021-07-19 16:49:13 +0000901 fd2 = open(zTo, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0644);
dan4be02b92010-07-22 15:44:06 +0000902 if( fd2<0 ){
903 system_error(pErr, errno);
904 close(fd1);
905 return;
906 }
907
908 iOff = 0;
909 while( iOff<nByte ){
910 int nCopy = sizeof(aBuf);
911 if( nCopy+iOff>nByte ){
912 nCopy = nByte - iOff;
913 }
914 if( nCopy!=read(fd1, aBuf, nCopy) ){
915 system_error(pErr, errno);
916 break;
917 }
918 if( nCopy!=write(fd2, aBuf, nCopy) ){
919 system_error(pErr, errno);
920 break;
921 }
922 iOff += nCopy;
923 }
924
925 close(fd1);
926 close(fd2);
927 }
928 }
929}
930
931/*
932** Used by setstoptime() and timetostop().
933*/
934static double timelimit = 0.0;
drhf8b0be42015-11-30 19:15:25 +0000935
936static double currentTime(void){
937 double t;
938 static sqlite3_vfs *pTimelimitVfs = 0;
939 if( pTimelimitVfs==0 ) pTimelimitVfs = sqlite3_vfs_find(0);
drhe40cf402016-01-19 21:36:26 +0000940 if( pTimelimitVfs->iVersion>=2 && pTimelimitVfs->xCurrentTimeInt64!=0 ){
drhf8b0be42015-11-30 19:15:25 +0000941 sqlite3_int64 tm;
942 pTimelimitVfs->xCurrentTimeInt64(pTimelimitVfs, &tm);
943 t = tm/86400000.0;
944 }else{
945 pTimelimitVfs->xCurrentTime(pTimelimitVfs, &t);
946 }
947 return t;
948}
dan4be02b92010-07-22 15:44:06 +0000949
950static void setstoptime_x(
951 Error *pErr, /* IN/OUT: Error code */
952 int nMs /* Milliseconds until "stop time" */
953){
954 if( pErr->rc==SQLITE_OK ){
drhf8b0be42015-11-30 19:15:25 +0000955 double t = currentTime();
956 timelimit = t + ((double)nMs)/(1000.0*60.0*60.0*24.0);
dan4be02b92010-07-22 15:44:06 +0000957 }
958}
959
960static int timetostop_x(
961 Error *pErr /* IN/OUT: Error code */
962){
963 int ret = 1;
964 if( pErr->rc==SQLITE_OK ){
drhf8b0be42015-11-30 19:15:25 +0000965 double t = currentTime();
966 ret = (t >= timelimit);
dan4be02b92010-07-22 15:44:06 +0000967 }
968 return ret;
969}
970
dan4be02b92010-07-22 15:44:06 +0000971
972/*************************************************************************
973**************************************************************************
974**************************************************************************
975** End infrastructure. Begin tests.
976*/
977
978#define WALTHREAD1_NTHREAD 10
979#define WALTHREAD3_NTHREAD 6
980
dan053542d2014-12-13 17:41:48 +0000981static char *walthread1_thread(int iTid, void *pArg){
dan4be02b92010-07-22 15:44:06 +0000982 Error err = {0}; /* Error code and message */
983 Sqlite db = {0}; /* SQLite database connection */
984 int nIter = 0; /* Iterations so far */
985
986 opendb(&err, &db, "test.db", 0);
987 while( !timetostop(&err) ){
988 const char *azSql[] = {
989 "SELECT md5sum(x) FROM t1 WHERE rowid != (SELECT max(rowid) FROM t1)",
990 "SELECT x FROM t1 WHERE rowid = (SELECT max(rowid) FROM t1)",
991 };
992 char *z1, *z2, *z3;
993
994 execsql(&err, &db, "BEGIN");
995 integrity_check(&err, &db);
996 z1 = execsql_text(&err, &db, 1, azSql[0]);
997 z2 = execsql_text(&err, &db, 2, azSql[1]);
998 z3 = execsql_text(&err, &db, 3, azSql[0]);
999 execsql(&err, &db, "COMMIT");
1000
1001 if( strcmp(z1, z2) || strcmp(z1, z3) ){
1002 test_error(&err, "Failed read: %s %s %s", z1, z2, z3);
1003 }
1004
1005 sql_script(&err, &db,
1006 "BEGIN;"
1007 "INSERT INTO t1 VALUES(randomblob(100));"
1008 "INSERT INTO t1 VALUES(randomblob(100));"
1009 "INSERT INTO t1 SELECT md5sum(x) FROM t1;"
1010 "COMMIT;"
1011 );
1012 nIter++;
1013 }
1014 closedb(&err, &db);
1015
1016 print_and_free_err(&err);
1017 return sqlite3_mprintf("%d iterations", nIter);
1018}
1019
dan053542d2014-12-13 17:41:48 +00001020static char *walthread1_ckpt_thread(int iTid, void *pArg){
dan4be02b92010-07-22 15:44:06 +00001021 Error err = {0}; /* Error code and message */
1022 Sqlite db = {0}; /* SQLite database connection */
1023 int nCkpt = 0; /* Checkpoints so far */
1024
1025 opendb(&err, &db, "test.db", 0);
1026 while( !timetostop(&err) ){
danbb2d2a52021-07-19 16:49:13 +00001027 sqlite3_sleep(500);
dan4be02b92010-07-22 15:44:06 +00001028 execsql(&err, &db, "PRAGMA wal_checkpoint");
1029 if( err.rc==SQLITE_OK ) nCkpt++;
1030 clear_error(&err, SQLITE_BUSY);
1031 }
1032 closedb(&err, &db);
1033
1034 print_and_free_err(&err);
1035 return sqlite3_mprintf("%d checkpoints", nCkpt);
1036}
1037
1038static void walthread1(int nMs){
1039 Error err = {0}; /* Error code and message */
1040 Sqlite db = {0}; /* SQLite database connection */
1041 Threadset threads = {0}; /* Test threads */
1042 int i; /* Iterator variable */
1043
1044 opendb(&err, &db, "test.db", 1);
1045 sql_script(&err, &db,
1046 "PRAGMA journal_mode = WAL;"
1047 "CREATE TABLE t1(x PRIMARY KEY);"
1048 "INSERT INTO t1 VALUES(randomblob(100));"
1049 "INSERT INTO t1 VALUES(randomblob(100));"
1050 "INSERT INTO t1 SELECT md5sum(x) FROM t1;"
1051 );
danb8a9d8d2014-12-31 18:25:21 +00001052 closedb(&err, &db);
dan4be02b92010-07-22 15:44:06 +00001053
1054 setstoptime(&err, nMs);
1055 for(i=0; i<WALTHREAD1_NTHREAD; i++){
1056 launch_thread(&err, &threads, walthread1_thread, 0);
1057 }
1058 launch_thread(&err, &threads, walthread1_ckpt_thread, 0);
1059 join_all_threads(&err, &threads);
1060
1061 print_and_free_err(&err);
1062}
1063
dan053542d2014-12-13 17:41:48 +00001064static char *walthread2_thread(int iTid, void *pArg){
dan4be02b92010-07-22 15:44:06 +00001065 Error err = {0}; /* Error code and message */
1066 Sqlite db = {0}; /* SQLite database connection */
1067 int anTrans[2] = {0, 0}; /* Number of WAL and Rollback transactions */
dan053542d2014-12-13 17:41:48 +00001068 int iArg = PTR2INT(pArg);
dan4be02b92010-07-22 15:44:06 +00001069
1070 const char *zJournal = "PRAGMA journal_mode = WAL";
1071 if( iArg ){ zJournal = "PRAGMA journal_mode = DELETE"; }
1072
1073 while( !timetostop(&err) ){
1074 int journal_exists = 0;
1075 int wal_exists = 0;
1076
1077 opendb(&err, &db, "test.db", 0);
1078
1079 sql_script(&err, &db, zJournal);
1080 clear_error(&err, SQLITE_BUSY);
1081 sql_script(&err, &db, "BEGIN");
1082 sql_script(&err, &db, "INSERT INTO t1 VALUES(NULL, randomblob(100))");
1083
1084 journal_exists = (filesize(&err, "test.db-journal") >= 0);
1085 wal_exists = (filesize(&err, "test.db-wal") >= 0);
1086 if( (journal_exists+wal_exists)!=1 ){
1087 test_error(&err, "File system looks incorrect (%d, %d)",
1088 journal_exists, wal_exists
1089 );
1090 }
1091 anTrans[journal_exists]++;
1092
1093 sql_script(&err, &db, "COMMIT");
1094 integrity_check(&err, &db);
1095 closedb(&err, &db);
1096 }
1097
1098 print_and_free_err(&err);
1099 return sqlite3_mprintf("W %d R %d", anTrans[0], anTrans[1]);
1100}
1101
1102static void walthread2(int nMs){
1103 Error err = {0};
1104 Sqlite db = {0};
1105 Threadset threads = {0};
1106
1107 opendb(&err, &db, "test.db", 1);
1108 sql_script(&err, &db, "CREATE TABLE t1(x INTEGER PRIMARY KEY, y UNIQUE)");
1109 closedb(&err, &db);
1110
1111 setstoptime(&err, nMs);
1112 launch_thread(&err, &threads, walthread2_thread, 0);
1113 launch_thread(&err, &threads, walthread2_thread, 0);
dan053542d2014-12-13 17:41:48 +00001114 launch_thread(&err, &threads, walthread2_thread, (void*)1);
1115 launch_thread(&err, &threads, walthread2_thread, (void*)1);
dan4be02b92010-07-22 15:44:06 +00001116 join_all_threads(&err, &threads);
1117
1118 print_and_free_err(&err);
1119}
1120
dan053542d2014-12-13 17:41:48 +00001121static char *walthread3_thread(int iTid, void *pArg){
dan4be02b92010-07-22 15:44:06 +00001122 Error err = {0}; /* Error code and message */
1123 Sqlite db = {0}; /* SQLite database connection */
1124 i64 iNextWrite; /* Next value this thread will write */
dan053542d2014-12-13 17:41:48 +00001125 int iArg = PTR2INT(pArg);
dan4be02b92010-07-22 15:44:06 +00001126
1127 opendb(&err, &db, "test.db", 0);
1128 sql_script(&err, &db, "PRAGMA wal_autocheckpoint = 10");
1129
1130 iNextWrite = iArg+1;
1131 while( 1 ){
1132 i64 sum1;
1133 i64 sum2;
1134 int stop = 0; /* True to stop executing (test timed out) */
1135
1136 while( 0==(stop = timetostop(&err)) ){
1137 i64 iMax = execsql_i64(&err, &db, "SELECT max(cnt) FROM t1");
1138 if( iMax+1==iNextWrite ) break;
1139 }
1140 if( stop ) break;
1141
1142 sum1 = execsql_i64(&err, &db, "SELECT sum(cnt) FROM t1");
1143 sum2 = execsql_i64(&err, &db, "SELECT sum(sum1) FROM t1");
1144 execsql_i64(&err, &db,
1145 "INSERT INTO t1 VALUES(:iNextWrite, :iSum1, :iSum2)",
1146 &iNextWrite, &sum1, &sum2
1147 );
1148 integrity_check(&err, &db);
1149
1150 iNextWrite += WALTHREAD3_NTHREAD;
1151 }
1152
1153 closedb(&err, &db);
1154 print_and_free_err(&err);
1155 return 0;
1156}
1157
1158static void walthread3(int nMs){
1159 Error err = {0};
1160 Sqlite db = {0};
1161 Threadset threads = {0};
1162 int i;
1163
1164 opendb(&err, &db, "test.db", 1);
1165 sql_script(&err, &db,
1166 "PRAGMA journal_mode = WAL;"
1167 "CREATE TABLE t1(cnt PRIMARY KEY, sum1, sum2);"
1168 "CREATE INDEX i1 ON t1(sum1);"
1169 "CREATE INDEX i2 ON t1(sum2);"
1170 "INSERT INTO t1 VALUES(0, 0, 0);"
1171 );
1172 closedb(&err, &db);
1173
1174 setstoptime(&err, nMs);
1175 for(i=0; i<WALTHREAD3_NTHREAD; i++){
dan053542d2014-12-13 17:41:48 +00001176 launch_thread(&err, &threads, walthread3_thread, INT2PTR(i));
dan4be02b92010-07-22 15:44:06 +00001177 }
1178 join_all_threads(&err, &threads);
1179
1180 print_and_free_err(&err);
1181}
1182
dan053542d2014-12-13 17:41:48 +00001183static char *walthread4_reader_thread(int iTid, void *pArg){
dan4be02b92010-07-22 15:44:06 +00001184 Error err = {0}; /* Error code and message */
1185 Sqlite db = {0}; /* SQLite database connection */
1186
1187 opendb(&err, &db, "test.db", 0);
1188 while( !timetostop(&err) ){
1189 integrity_check(&err, &db);
1190 }
1191 closedb(&err, &db);
1192
1193 print_and_free_err(&err);
1194 return 0;
1195}
1196
dan053542d2014-12-13 17:41:48 +00001197static char *walthread4_writer_thread(int iTid, void *pArg){
dan4be02b92010-07-22 15:44:06 +00001198 Error err = {0}; /* Error code and message */
1199 Sqlite db = {0}; /* SQLite database connection */
1200 i64 iRow = 1;
1201
1202 opendb(&err, &db, "test.db", 0);
1203 sql_script(&err, &db, "PRAGMA wal_autocheckpoint = 15;");
1204 while( !timetostop(&err) ){
1205 execsql_i64(
1206 &err, &db, "REPLACE INTO t1 VALUES(:iRow, randomblob(300))", &iRow
1207 );
1208 iRow++;
1209 if( iRow==10 ) iRow = 0;
1210 }
1211 closedb(&err, &db);
1212
1213 print_and_free_err(&err);
1214 return 0;
1215}
1216
1217static void walthread4(int nMs){
1218 Error err = {0};
1219 Sqlite db = {0};
1220 Threadset threads = {0};
1221
1222 opendb(&err, &db, "test.db", 1);
1223 sql_script(&err, &db,
1224 "PRAGMA journal_mode = WAL;"
1225 "CREATE TABLE t1(a INTEGER PRIMARY KEY, b UNIQUE);"
1226 );
1227 closedb(&err, &db);
1228
1229 setstoptime(&err, nMs);
1230 launch_thread(&err, &threads, walthread4_reader_thread, 0);
1231 launch_thread(&err, &threads, walthread4_writer_thread, 0);
1232 join_all_threads(&err, &threads);
1233
1234 print_and_free_err(&err);
1235}
1236
dan053542d2014-12-13 17:41:48 +00001237static char *walthread5_thread(int iTid, void *pArg){
dan4be02b92010-07-22 15:44:06 +00001238 Error err = {0}; /* Error code and message */
1239 Sqlite db = {0}; /* SQLite database connection */
1240 i64 nRow;
1241
1242 opendb(&err, &db, "test.db", 0);
1243 nRow = execsql_i64(&err, &db, "SELECT count(*) FROM t1");
1244 closedb(&err, &db);
1245
1246 if( nRow!=65536 ) test_error(&err, "Bad row count: %d", (int)nRow);
1247 print_and_free_err(&err);
1248 return 0;
1249}
1250static void walthread5(int nMs){
1251 Error err = {0};
1252 Sqlite db = {0};
1253 Threadset threads = {0};
1254
1255 opendb(&err, &db, "test.db", 1);
1256 sql_script(&err, &db,
1257 "PRAGMA wal_autocheckpoint = 0;"
1258 "PRAGMA page_size = 1024;"
1259 "PRAGMA journal_mode = WAL;"
1260 "CREATE TABLE t1(x);"
1261 "BEGIN;"
1262 "INSERT INTO t1 VALUES(randomblob(900));"
1263 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 2 */"
1264 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 4 */"
1265 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 8 */"
1266 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 16 */"
1267 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 32 */"
1268 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 64 */"
1269 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 128 */"
1270 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 256 */"
1271 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 512 */"
1272 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 1024 */"
1273 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 2048 */"
1274 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 4096 */"
1275 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 8192 */"
1276 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 16384 */"
1277 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 32768 */"
1278 "INSERT INTO t1 SELECT randomblob(900) FROM t1; /* 65536 */"
1279 "COMMIT;"
1280 );
1281 filecopy(&err, "test.db", "test_sv.db");
1282 filecopy(&err, "test.db-wal", "test_sv.db-wal");
1283 closedb(&err, &db);
1284
1285 filecopy(&err, "test_sv.db", "test.db");
1286 filecopy(&err, "test_sv.db-wal", "test.db-wal");
1287
1288 if( err.rc==SQLITE_OK ){
1289 printf(" WAL file is %d bytes,", (int)filesize(&err,"test.db-wal"));
1290 printf(" DB file is %d.\n", (int)filesize(&err,"test.db"));
1291 }
1292
1293 setstoptime(&err, nMs);
1294 launch_thread(&err, &threads, walthread5_thread, 0);
1295 launch_thread(&err, &threads, walthread5_thread, 0);
1296 launch_thread(&err, &threads, walthread5_thread, 0);
1297 launch_thread(&err, &threads, walthread5_thread, 0);
1298 launch_thread(&err, &threads, walthread5_thread, 0);
1299 join_all_threads(&err, &threads);
1300
1301 if( err.rc==SQLITE_OK ){
1302 printf(" WAL file is %d bytes,", (int)filesize(&err,"test.db-wal"));
1303 printf(" DB file is %d.\n", (int)filesize(&err,"test.db"));
1304 }
1305
1306 print_and_free_err(&err);
1307}
1308
dan16f77202010-08-07 05:15:22 +00001309/*------------------------------------------------------------------------
1310** Test case "cgt_pager_1"
1311*/
1312#define CALLGRINDTEST1_NROW 10000
1313static void cgt_pager_1_populate(Error *pErr, Sqlite *pDb){
1314 const char *zInsert = "INSERT INTO t1 VALUES(:iRow, zeroblob(:iBlob))";
1315 i64 iRow;
1316 sql_script(pErr, pDb, "BEGIN");
1317 for(iRow=1; iRow<=CALLGRINDTEST1_NROW; iRow++){
1318 i64 iBlob = 600 + (iRow%300);
1319 execsql(pErr, pDb, zInsert, &iRow, &iBlob);
1320 }
1321 sql_script(pErr, pDb, "COMMIT");
1322}
1323static void cgt_pager_1_update(Error *pErr, Sqlite *pDb){
1324 const char *zUpdate = "UPDATE t1 SET b = zeroblob(:iBlob) WHERE a = :iRow";
1325 i64 iRow;
1326 sql_script(pErr, pDb, "BEGIN");
1327 for(iRow=1; iRow<=CALLGRINDTEST1_NROW; iRow++){
1328 i64 iBlob = 600 + ((iRow+100)%300);
1329 execsql(pErr, pDb, zUpdate, &iBlob, &iRow);
1330 }
1331 sql_script(pErr, pDb, "COMMIT");
1332}
1333static void cgt_pager_1_read(Error *pErr, Sqlite *pDb){
1334 i64 iRow;
1335 sql_script(pErr, pDb, "BEGIN");
1336 for(iRow=1; iRow<=CALLGRINDTEST1_NROW; iRow++){
1337 execsql(pErr, pDb, "SELECT * FROM t1 WHERE a = :iRow", &iRow);
1338 }
1339 sql_script(pErr, pDb, "COMMIT");
1340}
1341static void cgt_pager_1(int nMs){
1342 void (*xSub)(Error *, Sqlite *);
1343 Error err = {0};
1344 Sqlite db = {0};
1345
1346 opendb(&err, &db, "test.db", 1);
1347 sql_script(&err, &db,
1348 "PRAGMA cache_size = 2000;"
1349 "PRAGMA page_size = 1024;"
1350 "CREATE TABLE t1(a INTEGER PRIMARY KEY, b BLOB);"
1351 );
1352
1353 xSub = cgt_pager_1_populate; xSub(&err, &db);
1354 xSub = cgt_pager_1_update; xSub(&err, &db);
1355 xSub = cgt_pager_1_read; xSub(&err, &db);
1356
1357 closedb(&err, &db);
1358 print_and_free_err(&err);
1359}
1360
danec033642010-10-28 15:52:04 +00001361/*------------------------------------------------------------------------
1362** Test case "dynamic_triggers"
1363**
1364** Two threads executing statements that cause deeply nested triggers
1365** to fire. And one thread busily creating and deleting triggers. This
1366** is an attempt to find a bug reported to us.
1367*/
1368
dan053542d2014-12-13 17:41:48 +00001369static char *dynamic_triggers_1(int iTid, void *pArg){
danec033642010-10-28 15:52:04 +00001370 Error err = {0}; /* Error code and message */
1371 Sqlite db = {0}; /* SQLite database connection */
1372 int nDrop = 0;
1373 int nCreate = 0;
1374
1375 opendb(&err, &db, "test.db", 0);
1376 while( !timetostop(&err) ){
1377 int i;
1378
1379 for(i=1; i<9; i++){
1380 char *zSql = sqlite3_mprintf(
1381 "CREATE TRIGGER itr%d BEFORE INSERT ON t%d BEGIN "
1382 "INSERT INTO t%d VALUES(new.x, new.y);"
1383 "END;", i, i, i+1
1384 );
1385 execsql(&err, &db, zSql);
1386 sqlite3_free(zSql);
1387 nCreate++;
1388 }
1389
1390 for(i=1; i<9; i++){
1391 char *zSql = sqlite3_mprintf(
1392 "CREATE TRIGGER dtr%d BEFORE DELETE ON t%d BEGIN "
1393 "DELETE FROM t%d WHERE x = old.x; "
1394 "END;", i, i, i+1
1395 );
1396 execsql(&err, &db, zSql);
1397 sqlite3_free(zSql);
1398 nCreate++;
1399 }
1400
1401 for(i=1; i<9; i++){
1402 char *zSql = sqlite3_mprintf("DROP TRIGGER itr%d", i);
1403 execsql(&err, &db, zSql);
1404 sqlite3_free(zSql);
1405 nDrop++;
1406 }
1407
1408 for(i=1; i<9; i++){
1409 char *zSql = sqlite3_mprintf("DROP TRIGGER dtr%d", i);
1410 execsql(&err, &db, zSql);
1411 sqlite3_free(zSql);
1412 nDrop++;
1413 }
1414 }
dand44b7862014-12-15 08:46:17 +00001415 closedb(&err, &db);
danec033642010-10-28 15:52:04 +00001416
1417 print_and_free_err(&err);
1418 return sqlite3_mprintf("%d created, %d dropped", nCreate, nDrop);
1419}
1420
dan053542d2014-12-13 17:41:48 +00001421static char *dynamic_triggers_2(int iTid, void *pArg){
danec033642010-10-28 15:52:04 +00001422 Error err = {0}; /* Error code and message */
1423 Sqlite db = {0}; /* SQLite database connection */
1424 i64 iVal = 0;
1425 int nInsert = 0;
1426 int nDelete = 0;
1427
1428 opendb(&err, &db, "test.db", 0);
1429 while( !timetostop(&err) ){
1430 do {
1431 iVal = (iVal+1)%100;
1432 execsql(&err, &db, "INSERT INTO t1 VALUES(:iX, :iY+1)", &iVal, &iVal);
1433 nInsert++;
1434 } while( iVal );
1435
1436 do {
1437 iVal = (iVal+1)%100;
1438 execsql(&err, &db, "DELETE FROM t1 WHERE x = :iX", &iVal);
1439 nDelete++;
1440 } while( iVal );
1441 }
dand44b7862014-12-15 08:46:17 +00001442 closedb(&err, &db);
danec033642010-10-28 15:52:04 +00001443
1444 print_and_free_err(&err);
1445 return sqlite3_mprintf("%d inserts, %d deletes", nInsert, nDelete);
1446}
1447
1448static void dynamic_triggers(int nMs){
1449 Error err = {0};
1450 Sqlite db = {0};
1451 Threadset threads = {0};
1452
1453 opendb(&err, &db, "test.db", 1);
1454 sql_script(&err, &db,
1455 "PRAGMA page_size = 1024;"
1456 "PRAGMA journal_mode = WAL;"
1457 "CREATE TABLE t1(x, y);"
1458 "CREATE TABLE t2(x, y);"
1459 "CREATE TABLE t3(x, y);"
1460 "CREATE TABLE t4(x, y);"
1461 "CREATE TABLE t5(x, y);"
1462 "CREATE TABLE t6(x, y);"
1463 "CREATE TABLE t7(x, y);"
1464 "CREATE TABLE t8(x, y);"
1465 "CREATE TABLE t9(x, y);"
1466 );
dand44b7862014-12-15 08:46:17 +00001467 closedb(&err, &db);
danec033642010-10-28 15:52:04 +00001468
1469 setstoptime(&err, nMs);
1470
1471 sqlite3_enable_shared_cache(1);
1472 launch_thread(&err, &threads, dynamic_triggers_2, 0);
1473 launch_thread(&err, &threads, dynamic_triggers_2, 0);
danec033642010-10-28 15:52:04 +00001474
danbb2d2a52021-07-19 16:49:13 +00001475 sqlite3_sleep(2*1000);
dan053542d2014-12-13 17:41:48 +00001476 sqlite3_enable_shared_cache(0);
danec033642010-10-28 15:52:04 +00001477
1478 launch_thread(&err, &threads, dynamic_triggers_2, 0);
1479 launch_thread(&err, &threads, dynamic_triggers_1, 0);
1480
1481 join_all_threads(&err, &threads);
1482
1483 print_and_free_err(&err);
1484}
1485
dan740fe882021-07-16 18:30:27 +00001486typedef struct Walthread6 Walthread6;
1487struct Walthread6 {
1488 int nInsert;
1489 int nBusy;
1490 int nMaxFrame;
1491};
1492
1493static int walthread6_walhook(
1494 void *pArg, /* Pointer to Walthread6 structure */
1495 sqlite3 *db, /* Database handle */
1496 const char *zDb, /* "main" */
1497 int nFrame /* Frames current in wal file */
1498){
1499 int rc = SQLITE_OK;
1500 Walthread6 *p = (Walthread6*)pArg;
1501
1502 if( nFrame>p->nMaxFrame ) p->nMaxFrame = nFrame;
1503 if( nFrame>1000 ){
1504 sqlite3_wal_checkpoint_v2(db, zDb, SQLITE_CHECKPOINT_RESTART, 0, 0);
1505 }
1506
1507 return rc;
1508}
1509
dan9a9876e2021-07-22 10:44:31 +00001510static char *contention1_thread(int iTid, void *pArg){
dan740fe882021-07-16 18:30:27 +00001511 Error err = {0};
1512 Sqlite db = {0};
dan8ecc6072021-07-19 20:34:08 +00001513 int nBlk = 0;
1514 int nInsertPerTransaction = 1;
1515 i64 iPrev = -1000000;
dan740fe882021-07-16 18:30:27 +00001516
1517 Walthread6 res;
1518 memset(&res, 0, sizeof(res));
1519
dan8ecc6072021-07-19 20:34:08 +00001520
dan740fe882021-07-16 18:30:27 +00001521 opendb(&err, &db, "test.db", 0);
1522 sqlite3_busy_timeout(db.db, 1000);
1523 sqlite3_wal_hook(db.db, walthread6_walhook, (void*)&res);
1524
1525 while( !timetostop(&err) ){
1526 int i;
1527 execsql(&err, &db, "BEGIN IMMEDIATE");
1528 if( err.rc==SQLITE_BUSY ){
1529 res.nBusy++;
1530 clear_error(&err, SQLITE_BUSY);
1531 }else{
dan8ecc6072021-07-19 20:34:08 +00001532 i64 iRowid;
dan740fe882021-07-16 18:30:27 +00001533 res.nInsert++;
dan8ecc6072021-07-19 20:34:08 +00001534 for(i=0; i<nInsertPerTransaction; i++){
dan740fe882021-07-16 18:30:27 +00001535 execsql(&err, &db, "INSERT INTO t1(b) VALUES(random())");
1536 }
dan8ecc6072021-07-19 20:34:08 +00001537
1538 iRowid = sqlite3_last_insert_rowid(db.db);
1539 if( iRowid!=(iPrev+nInsertPerTransaction) ) nBlk++;
1540 iPrev = iRowid;
1541
1542 sqlite3_sleep(10);
dan740fe882021-07-16 18:30:27 +00001543 execsql(&err, &db, "COMMIT");
dan8ecc6072021-07-19 20:34:08 +00001544 sqlite3_sleep(1);
dan740fe882021-07-16 18:30:27 +00001545 }
1546 }
1547
1548 closedb(&err, &db);
1549 print_and_free_err(&err);
1550 return sqlite3_mprintf(
dan9a9876e2021-07-22 10:44:31 +00001551 "%d transactions in %d runs (%d busy), max-wal-size=%d frames",
1552 res.nInsert, nBlk, res.nBusy, res.nMaxFrame
dan740fe882021-07-16 18:30:27 +00001553 );
1554}
1555
1556/*
dan9a9876e2021-07-22 10:44:31 +00001557** Test case "contention1".
1558**
1559** Start with a database in wal mode containing a single table. Then run
1560** two threads in a loop as follows:
1561**
1562** while( not-finished ){
1563** BEGIN IMMEDIATE;
1564** INSERT INTO tbl ...
1565** sleep( 10ms );
1566** COMMIT;
1567** sleep( 1ms );
1568** }
dan740fe882021-07-16 18:30:27 +00001569**
dan9a9876e2021-07-22 10:44:31 +00001570** Each thread is configured with sqlite3_busy_timeout(1000ms). Each
1571** thread reports:
1572**
1573** * the number of successful inserts,
1574** * the number of runs the inserts are separated into (a run is a
1575** series of contiguous inserts not interrupted by an insert from
1576** the other thread), and
1577** * the number of SQLITE_BUSY errors.
dan740fe882021-07-16 18:30:27 +00001578*/
dan9a9876e2021-07-22 10:44:31 +00001579static void contention1(int nMs){
dan740fe882021-07-16 18:30:27 +00001580 Error err = {0};
1581 Sqlite db = {0};
1582 Threadset threads = {0};
1583
1584 int i;
1585 int nThread = 2;
1586
1587 opendb(&err, &db, "test.db", 1);
1588 sql_script(&err, &db,
1589 "PRAGMA page_size = 1024;"
1590 "PRAGMA journal_mode = WAL;"
1591 "CREATE TABLE t1(a INTEGER PRIMARY KEY, b TEXT);"
1592 );
1593 closedb(&err, &db);
1594
1595 setstoptime(&err, nMs);
1596 for(i=0; i<nThread; i++){
dan9a9876e2021-07-22 10:44:31 +00001597 launch_thread(&err, &threads, contention1_thread, 0);
dan740fe882021-07-16 18:30:27 +00001598 }
1599 join_all_threads(&err, &threads);
1600
1601 print_and_free_err(&err);
1602}
1603
dan0235a032014-12-08 20:20:16 +00001604
1605
dan24cd6162010-11-19 09:58:11 +00001606#include "tt3_checkpoint.c"
dan0235a032014-12-08 20:20:16 +00001607#include "tt3_index.c"
dan85753662014-12-11 16:38:18 +00001608#include "tt3_lookaside1.c"
dan04209542014-12-12 16:39:38 +00001609#include "tt3_vacuum.c"
1610#include "tt3_stress.c"
dan78f04752020-09-04 19:10:43 +00001611#include "tt3_shared.c"
danec033642010-10-28 15:52:04 +00001612
dan4be02b92010-07-22 15:44:06 +00001613int main(int argc, char **argv){
1614 struct ThreadTest {
drh9486c1b2014-12-30 19:26:07 +00001615 void (*xTest)(int); /* Routine for running this test */
1616 const char *zTest; /* Name of this test */
1617 int nMs; /* How long to run this test, in milliseconds */
dan4be02b92010-07-22 15:44:06 +00001618 } aTest[] = {
1619 { walthread1, "walthread1", 20000 },
1620 { walthread2, "walthread2", 20000 },
1621 { walthread3, "walthread3", 20000 },
1622 { walthread4, "walthread4", 20000 },
1623 { walthread5, "walthread5", 1000 },
dan740fe882021-07-16 18:30:27 +00001624
dan9a9876e2021-07-22 10:44:31 +00001625 { contention1, "contention1", 10000 },
dan16f77202010-08-07 05:15:22 +00001626
dan24cd6162010-11-19 09:58:11 +00001627 { cgt_pager_1, "cgt_pager_1", 0 },
danec033642010-10-28 15:52:04 +00001628 { dynamic_triggers, "dynamic_triggers", 20000 },
dan24cd6162010-11-19 09:58:11 +00001629
1630 { checkpoint_starvation_1, "checkpoint_starvation_1", 10000 },
1631 { checkpoint_starvation_2, "checkpoint_starvation_2", 10000 },
dan0235a032014-12-08 20:20:16 +00001632
1633 { create_drop_index_1, "create_drop_index_1", 10000 },
dan04209542014-12-12 16:39:38 +00001634 { lookaside1, "lookaside1", 10000 },
1635 { vacuum1, "vacuum1", 10000 },
1636 { stress1, "stress1", 10000 },
dan1ee46c02014-12-15 20:49:26 +00001637 { stress2, "stress2", 60000 },
dan78f04752020-09-04 19:10:43 +00001638 { shared1, "shared1", 10000 },
dan4be02b92010-07-22 15:44:06 +00001639 };
drh169c4642014-12-31 18:28:59 +00001640 static char *substArgv[] = { 0, "*", 0 };
1641 int i, iArg;
drh9486c1b2014-12-30 19:26:07 +00001642 int nTestfound = 0;
dan4be02b92010-07-22 15:44:06 +00001643
1644 sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
drh169c4642014-12-31 18:28:59 +00001645 if( argc<2 ){
1646 argc = 2;
1647 argv = substArgv;
1648 }
dan48c06f32015-12-03 11:51:18 +00001649
1650 /* Loop through the command-line arguments to ensure that each argument
1651 ** selects at least one test. If not, assume there is a typo on the
1652 ** command-line and bail out with the usage message. */
drh169c4642014-12-31 18:28:59 +00001653 for(iArg=1; iArg<argc; iArg++){
dan48c06f32015-12-03 11:51:18 +00001654 const char *zArg = argv[iArg];
1655 if( zArg[0]=='-' ){
1656 if( sqlite3_stricmp(zArg, "-multiplexor")==0 ){
1657 /* Install the multiplexor VFS as the default */
1658 int rc = sqlite3_multiplex_initialize(0, 1);
1659 if( rc!=SQLITE_OK ){
1660 fprintf(stderr, "Failed to install multiplexor VFS (%d)\n", rc);
1661 return 253;
1662 }
1663 }
1664 else {
1665 goto usage;
1666 }
1667
1668 continue;
1669 }
1670
drh169c4642014-12-31 18:28:59 +00001671 for(i=0; i<sizeof(aTest)/sizeof(aTest[0]); i++){
dan48c06f32015-12-03 11:51:18 +00001672 if( sqlite3_strglob(zArg, aTest[i].zTest)==0 ) break;
dan4be02b92010-07-22 15:44:06 +00001673 }
drh169c4642014-12-31 18:28:59 +00001674 if( i>=sizeof(aTest)/sizeof(aTest[0]) ) goto usage;
1675 }
dan48c06f32015-12-03 11:51:18 +00001676
drh169c4642014-12-31 18:28:59 +00001677 for(iArg=1; iArg<argc; iArg++){
dan48c06f32015-12-03 11:51:18 +00001678 if( argv[iArg][0]=='-' ) continue;
drh169c4642014-12-31 18:28:59 +00001679 for(i=0; i<sizeof(aTest)/sizeof(aTest[0]); i++){
1680 char const *z = aTest[i].zTest;
1681 if( sqlite3_strglob(argv[iArg],z)==0 ){
1682 printf("Running %s for %d seconds...\n", z, aTest[i].nMs/1000);
1683 fflush(stdout);
1684 aTest[i].xTest(aTest[i].nMs);
1685 nTestfound++;
1686 }
1687 }
dan4be02b92010-07-22 15:44:06 +00001688 }
drh9486c1b2014-12-30 19:26:07 +00001689 if( nTestfound==0 ) goto usage;
dan4be02b92010-07-22 15:44:06 +00001690
drh9486c1b2014-12-30 19:26:07 +00001691 printf("%d errors out of %d tests\n", nGlobalErr, nTestfound);
dan4be02b92010-07-22 15:44:06 +00001692 return (nGlobalErr>0 ? 255 : 0);
1693
1694 usage:
dan48c06f32015-12-03 11:51:18 +00001695 printf("Usage: %s [-multiplexor] [testname|testprefix*]...\n", argv[0]);
dan4be02b92010-07-22 15:44:06 +00001696 printf("Available tests are:\n");
1697 for(i=0; i<sizeof(aTest)/sizeof(aTest[0]); i++){
1698 printf(" %s\n", aTest[i].zTest);
1699 }
1700
1701 return 254;
1702}