Snapshot of upstream SQLite 3.45.3
[sqlcipher.git] / mptest / mptest.c
blob5022b009e6bf7772a1d6eba7189f2fde67e988e3
1 /*
2 ** 2013-04-05
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.
11 *************************************************************************
12 **
13 ** This is a program used for testing SQLite, and specifically for testing
14 ** the ability of independent processes to access the same SQLite database
15 ** concurrently.
17 ** Compile this program as follows:
19 ** gcc -g -c -Wall sqlite3.c $(OPTS)
20 ** gcc -g -o mptest mptest.c sqlite3.o $(LIBS)
22 ** Recommended options:
24 ** -DHAVE_USLEEP
25 ** -DSQLITE_NO_SYNC
26 ** -DSQLITE_THREADSAFE=0
27 ** -DSQLITE_OMIT_LOAD_EXTENSION
29 ** Run like this:
31 ** ./mptest $database $script
33 ** where $database is the database to use for testing and $script is a
34 ** test script.
36 #include "sqlite3.h"
37 #include <stdio.h>
38 #if defined(_WIN32)
39 # define WIN32_LEAN_AND_MEAN
40 # include <windows.h>
41 #else
42 # include <unistd.h>
43 #endif
44 #include <errno.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <assert.h>
48 #include <ctype.h>
50 #define ISSPACE(X) isspace((unsigned char)(X))
51 #define ISDIGIT(X) isdigit((unsigned char)(X))
53 /* The suffix to append to the child command lines, if any */
54 #if defined(_WIN32)
55 # define GETPID (int)GetCurrentProcessId
56 #else
57 # define GETPID getpid
58 #endif
60 /* The directory separator character(s) */
61 #if defined(_WIN32)
62 # define isDirSep(c) (((c) == '/') || ((c) == '\\'))
63 #else
64 # define isDirSep(c) ((c) == '/')
65 #endif
67 /* Mark a parameter as unused to suppress compiler warnings */
68 #define UNUSED_PARAMETER(x) (void)x
70 /* Global data
72 static struct Global {
73 char *argv0; /* Name of the executable */
74 const char *zVfs; /* Name of VFS to use. Often NULL meaning "default" */
75 char *zDbFile; /* Name of the database */
76 sqlite3 *db; /* Open connection to database */
77 char *zErrLog; /* Filename for error log */
78 FILE *pErrLog; /* Where to write errors */
79 char *zLog; /* Name of output log file */
80 FILE *pLog; /* Where to write log messages */
81 char zName[32]; /* Symbolic name of this process */
82 int taskId; /* Task ID. 0 means supervisor. */
83 int iTrace; /* Tracing level */
84 int bSqlTrace; /* True to trace SQL commands */
85 int bIgnoreSqlErrors; /* Ignore errors in SQL statements */
86 int nError; /* Number of errors */
87 int nTest; /* Number of --match operators */
88 int iTimeout; /* Milliseconds until a busy timeout */
89 int bSync; /* Call fsync() */
90 } g;
92 /* Default timeout */
93 #define DEFAULT_TIMEOUT 10000
96 ** Print a message adding zPrefix[] to the beginning of every line.
98 static void printWithPrefix(FILE *pOut, const char *zPrefix, const char *zMsg){
99 while( zMsg && zMsg[0] ){
100 int i;
101 for(i=0; zMsg[i] && zMsg[i]!='\n' && zMsg[i]!='\r'; i++){}
102 fprintf(pOut, "%s%.*s\n", zPrefix, i, zMsg);
103 zMsg += i;
104 while( zMsg[0]=='\n' || zMsg[0]=='\r' ) zMsg++;
109 ** Compare two pointers to strings, where the pointers might be NULL.
111 static int safe_strcmp(const char *a, const char *b){
112 if( a==b ) return 0;
113 if( a==0 ) return -1;
114 if( b==0 ) return 1;
115 return strcmp(a,b);
119 ** Return TRUE if string z[] matches glob pattern zGlob[].
120 ** Return FALSE if the pattern does not match.
122 ** Globbing rules:
124 ** '*' Matches any sequence of zero or more characters.
126 ** '?' Matches exactly one character.
128 ** [...] Matches one character from the enclosed list of
129 ** characters.
131 ** [^...] Matches one character not in the enclosed list.
133 ** '#' Matches any sequence of one or more digits with an
134 ** optional + or - sign in front
136 int strglob(const char *zGlob, const char *z){
137 int c, c2;
138 int invert;
139 int seen;
141 while( (c = (*(zGlob++)))!=0 ){
142 if( c=='*' ){
143 while( (c=(*(zGlob++))) == '*' || c=='?' ){
144 if( c=='?' && (*(z++))==0 ) return 0;
146 if( c==0 ){
147 return 1;
148 }else if( c=='[' ){
149 while( *z && strglob(zGlob-1,z) ){
150 z++;
152 return (*z)!=0;
154 while( (c2 = (*(z++)))!=0 ){
155 while( c2!=c ){
156 c2 = *(z++);
157 if( c2==0 ) return 0;
159 if( strglob(zGlob,z) ) return 1;
161 return 0;
162 }else if( c=='?' ){
163 if( (*(z++))==0 ) return 0;
164 }else if( c=='[' ){
165 int prior_c = 0;
166 seen = 0;
167 invert = 0;
168 c = *(z++);
169 if( c==0 ) return 0;
170 c2 = *(zGlob++);
171 if( c2=='^' ){
172 invert = 1;
173 c2 = *(zGlob++);
175 if( c2==']' ){
176 if( c==']' ) seen = 1;
177 c2 = *(zGlob++);
179 while( c2 && c2!=']' ){
180 if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
181 c2 = *(zGlob++);
182 if( c>=prior_c && c<=c2 ) seen = 1;
183 prior_c = 0;
184 }else{
185 if( c==c2 ){
186 seen = 1;
188 prior_c = c2;
190 c2 = *(zGlob++);
192 if( c2==0 || (seen ^ invert)==0 ) return 0;
193 }else if( c=='#' ){
194 if( (z[0]=='-' || z[0]=='+') && ISDIGIT(z[1]) ) z++;
195 if( !ISDIGIT(z[0]) ) return 0;
196 z++;
197 while( ISDIGIT(z[0]) ){ z++; }
198 }else{
199 if( c!=(*(z++)) ) return 0;
202 return *z==0;
206 ** Close output stream pOut if it is not stdout or stderr
208 static void maybeClose(FILE *pOut){
209 if( pOut!=stdout && pOut!=stderr ) fclose(pOut);
213 ** Print an error message
215 static void errorMessage(const char *zFormat, ...){
216 va_list ap;
217 char *zMsg;
218 char zPrefix[30];
219 va_start(ap, zFormat);
220 zMsg = sqlite3_vmprintf(zFormat, ap);
221 va_end(ap);
222 sqlite3_snprintf(sizeof(zPrefix), zPrefix, "%s:ERROR: ", g.zName);
223 if( g.pLog ){
224 printWithPrefix(g.pLog, zPrefix, zMsg);
225 fflush(g.pLog);
227 if( g.pErrLog && safe_strcmp(g.zErrLog,g.zLog) ){
228 printWithPrefix(g.pErrLog, zPrefix, zMsg);
229 fflush(g.pErrLog);
231 sqlite3_free(zMsg);
232 g.nError++;
235 /* Forward declaration */
236 static int trySql(const char*, ...);
239 ** Print an error message and then quit.
241 static void fatalError(const char *zFormat, ...){
242 va_list ap;
243 char *zMsg;
244 char zPrefix[30];
245 va_start(ap, zFormat);
246 zMsg = sqlite3_vmprintf(zFormat, ap);
247 va_end(ap);
248 sqlite3_snprintf(sizeof(zPrefix), zPrefix, "%s:FATAL: ", g.zName);
249 if( g.pLog ){
250 printWithPrefix(g.pLog, zPrefix, zMsg);
251 fflush(g.pLog);
252 maybeClose(g.pLog);
254 if( g.pErrLog && safe_strcmp(g.zErrLog,g.zLog) ){
255 printWithPrefix(g.pErrLog, zPrefix, zMsg);
256 fflush(g.pErrLog);
257 maybeClose(g.pErrLog);
259 sqlite3_free(zMsg);
260 if( g.db ){
261 int nTry = 0;
262 g.iTimeout = 0;
263 while( trySql("UPDATE client SET wantHalt=1;")==SQLITE_BUSY
264 && (nTry++)<100 ){
265 sqlite3_sleep(10);
268 sqlite3_close(g.db);
269 exit(1);
274 ** Print a log message
276 static void logMessage(const char *zFormat, ...){
277 va_list ap;
278 char *zMsg;
279 char zPrefix[30];
280 va_start(ap, zFormat);
281 zMsg = sqlite3_vmprintf(zFormat, ap);
282 va_end(ap);
283 sqlite3_snprintf(sizeof(zPrefix), zPrefix, "%s: ", g.zName);
284 if( g.pLog ){
285 printWithPrefix(g.pLog, zPrefix, zMsg);
286 fflush(g.pLog);
288 sqlite3_free(zMsg);
292 ** Return the length of a string omitting trailing whitespace
294 static int clipLength(const char *z){
295 int n = (int)strlen(z);
296 while( n>0 && ISSPACE(z[n-1]) ){ n--; }
297 return n;
301 ** Auxiliary SQL function to return the name of the VFS
303 static void vfsNameFunc(
304 sqlite3_context *context,
305 int argc,
306 sqlite3_value **argv
308 sqlite3 *db = sqlite3_context_db_handle(context);
309 char *zVfs = 0;
310 UNUSED_PARAMETER(argc);
311 UNUSED_PARAMETER(argv);
312 sqlite3_file_control(db, "main", SQLITE_FCNTL_VFSNAME, &zVfs);
313 if( zVfs ){
314 sqlite3_result_text(context, zVfs, -1, sqlite3_free);
319 ** Busy handler with a g.iTimeout-millisecond timeout
321 static int busyHandler(void *pCD, int count){
322 UNUSED_PARAMETER(pCD);
323 if( count*10>g.iTimeout ){
324 if( g.iTimeout>0 ) errorMessage("timeout after %dms", g.iTimeout);
325 return 0;
327 sqlite3_sleep(10);
328 return 1;
332 ** SQL Trace callback
334 static void sqlTraceCallback(void *NotUsed1, const char *zSql){
335 UNUSED_PARAMETER(NotUsed1);
336 logMessage("[%.*s]", clipLength(zSql), zSql);
340 ** SQL error log callback
342 static void sqlErrorCallback(void *pArg, int iErrCode, const char *zMsg){
343 UNUSED_PARAMETER(pArg);
344 if( iErrCode==SQLITE_ERROR && g.bIgnoreSqlErrors ) return;
345 if( (iErrCode&0xff)==SQLITE_SCHEMA && g.iTrace<3 ) return;
346 if( g.iTimeout==0 && (iErrCode&0xff)==SQLITE_BUSY && g.iTrace<3 ) return;
347 if( (iErrCode&0xff)==SQLITE_NOTICE ){
348 logMessage("(info) %s", zMsg);
349 }else{
350 errorMessage("(errcode=%d) %s", iErrCode, zMsg);
355 ** Prepare an SQL statement. Issue a fatal error if unable.
357 static sqlite3_stmt *prepareSql(const char *zFormat, ...){
358 va_list ap;
359 char *zSql;
360 int rc;
361 sqlite3_stmt *pStmt = 0;
362 va_start(ap, zFormat);
363 zSql = sqlite3_vmprintf(zFormat, ap);
364 va_end(ap);
365 rc = sqlite3_prepare_v2(g.db, zSql, -1, &pStmt, 0);
366 if( rc!=SQLITE_OK ){
367 sqlite3_finalize(pStmt);
368 fatalError("%s\n%s\n", sqlite3_errmsg(g.db), zSql);
370 sqlite3_free(zSql);
371 return pStmt;
375 ** Run arbitrary SQL. Issue a fatal error on failure.
377 static void runSql(const char *zFormat, ...){
378 va_list ap;
379 char *zSql;
380 int rc;
381 va_start(ap, zFormat);
382 zSql = sqlite3_vmprintf(zFormat, ap);
383 va_end(ap);
384 rc = sqlite3_exec(g.db, zSql, 0, 0, 0);
385 if( rc!=SQLITE_OK ){
386 fatalError("%s\n%s\n", sqlite3_errmsg(g.db), zSql);
388 sqlite3_free(zSql);
392 ** Try to run arbitrary SQL. Return success code.
394 static int trySql(const char *zFormat, ...){
395 va_list ap;
396 char *zSql;
397 int rc;
398 va_start(ap, zFormat);
399 zSql = sqlite3_vmprintf(zFormat, ap);
400 va_end(ap);
401 rc = sqlite3_exec(g.db, zSql, 0, 0, 0);
402 sqlite3_free(zSql);
403 return rc;
406 /* Structure for holding an arbitrary length string
408 typedef struct String String;
409 struct String {
410 char *z; /* the string */
411 int n; /* Slots of z[] used */
412 int nAlloc; /* Slots of z[] allocated */
415 /* Free a string */
416 static void stringFree(String *p){
417 if( p->z ) sqlite3_free(p->z);
418 memset(p, 0, sizeof(*p));
421 /* Append n bytes of text to a string. If n<0 append the entire string. */
422 static void stringAppend(String *p, const char *z, int n){
423 if( n<0 ) n = (int)strlen(z);
424 if( p->n+n>=p->nAlloc ){
425 int nAlloc = p->nAlloc*2 + n + 100;
426 char *zNew = sqlite3_realloc(p->z, nAlloc);
427 if( zNew==0 ) fatalError("out of memory");
428 p->z = zNew;
429 p->nAlloc = nAlloc;
431 memcpy(p->z+p->n, z, n);
432 p->n += n;
433 p->z[p->n] = 0;
436 /* Reset a string to an empty string */
437 static void stringReset(String *p){
438 if( p->z==0 ) stringAppend(p, " ", 1);
439 p->n = 0;
440 p->z[0] = 0;
443 /* Append a new token onto the end of the string */
444 static void stringAppendTerm(String *p, const char *z){
445 int i;
446 if( p->n ) stringAppend(p, " ", 1);
447 if( z==0 ){
448 stringAppend(p, "nil", 3);
449 return;
451 for(i=0; z[i] && !ISSPACE(z[i]); i++){}
452 if( i>0 && z[i]==0 ){
453 stringAppend(p, z, i);
454 return;
456 stringAppend(p, "'", 1);
457 while( z[0] ){
458 for(i=0; z[i] && z[i]!='\''; i++){}
459 if( z[i] ){
460 stringAppend(p, z, i+1);
461 stringAppend(p, "'", 1);
462 z += i+1;
463 }else{
464 stringAppend(p, z, i);
465 break;
468 stringAppend(p, "'", 1);
472 ** Callback function for evalSql()
474 static int evalCallback(void *pCData, int argc, char **argv, char **azCol){
475 String *p = (String*)pCData;
476 int i;
477 UNUSED_PARAMETER(azCol);
478 for(i=0; i<argc; i++) stringAppendTerm(p, argv[i]);
479 return 0;
483 ** Run arbitrary SQL and record the results in an output string
484 ** given by the first parameter.
486 static int evalSql(String *p, const char *zFormat, ...){
487 va_list ap;
488 char *zSql;
489 int rc;
490 char *zErrMsg = 0;
491 va_start(ap, zFormat);
492 zSql = sqlite3_vmprintf(zFormat, ap);
493 va_end(ap);
494 assert( g.iTimeout>0 );
495 rc = sqlite3_exec(g.db, zSql, evalCallback, p, &zErrMsg);
496 sqlite3_free(zSql);
497 if( rc ){
498 char zErr[30];
499 sqlite3_snprintf(sizeof(zErr), zErr, "error(%d)", rc);
500 stringAppendTerm(p, zErr);
501 if( zErrMsg ){
502 stringAppendTerm(p, zErrMsg);
503 sqlite3_free(zErrMsg);
506 return rc;
510 ** Auxiliary SQL function to recursively evaluate SQL.
512 static void evalFunc(
513 sqlite3_context *context,
514 int argc,
515 sqlite3_value **argv
517 sqlite3 *db = sqlite3_context_db_handle(context);
518 const char *zSql = (const char*)sqlite3_value_text(argv[0]);
519 String res;
520 char *zErrMsg = 0;
521 int rc;
522 UNUSED_PARAMETER(argc);
523 memset(&res, 0, sizeof(res));
524 rc = sqlite3_exec(db, zSql, evalCallback, &res, &zErrMsg);
525 if( zErrMsg ){
526 sqlite3_result_error(context, zErrMsg, -1);
527 sqlite3_free(zErrMsg);
528 }else if( rc ){
529 sqlite3_result_error_code(context, rc);
530 }else{
531 sqlite3_result_text(context, res.z, -1, SQLITE_TRANSIENT);
533 stringFree(&res);
537 ** Look up the next task for client iClient in the database.
538 ** Return the task script and the task number and mark that
539 ** task as being under way.
541 static int startScript(
542 int iClient, /* The client number */
543 char **pzScript, /* Write task script here */
544 int *pTaskId, /* Write task number here */
545 char **pzTaskName /* Name of the task */
547 sqlite3_stmt *pStmt = 0;
548 int taskId;
549 int rc;
550 int totalTime = 0;
552 *pzScript = 0;
553 g.iTimeout = 0;
554 while(1){
555 rc = trySql("BEGIN IMMEDIATE");
556 if( rc==SQLITE_BUSY ){
557 sqlite3_sleep(10);
558 totalTime += 10;
559 continue;
561 if( rc!=SQLITE_OK ){
562 fatalError("in startScript: %s", sqlite3_errmsg(g.db));
564 if( g.nError || g.nTest ){
565 runSql("UPDATE counters SET nError=nError+%d, nTest=nTest+%d",
566 g.nError, g.nTest);
567 g.nError = 0;
568 g.nTest = 0;
570 pStmt = prepareSql("SELECT 1 FROM client WHERE id=%d AND wantHalt",iClient);
571 rc = sqlite3_step(pStmt);
572 sqlite3_finalize(pStmt);
573 if( rc==SQLITE_ROW ){
574 runSql("DELETE FROM client WHERE id=%d", iClient);
575 g.iTimeout = DEFAULT_TIMEOUT;
576 runSql("COMMIT TRANSACTION;");
577 return SQLITE_DONE;
579 pStmt = prepareSql(
580 "SELECT script, id, name FROM task"
581 " WHERE client=%d AND starttime IS NULL"
582 " ORDER BY id LIMIT 1", iClient);
583 rc = sqlite3_step(pStmt);
584 if( rc==SQLITE_ROW ){
585 int n = sqlite3_column_bytes(pStmt, 0);
586 *pzScript = sqlite3_malloc(n+1);
587 strcpy(*pzScript, (const char*)sqlite3_column_text(pStmt, 0));
588 *pTaskId = taskId = sqlite3_column_int(pStmt, 1);
589 *pzTaskName = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 2));
590 sqlite3_finalize(pStmt);
591 runSql("UPDATE task"
592 " SET starttime=strftime('%%Y-%%m-%%d %%H:%%M:%%f','now')"
593 " WHERE id=%d;", taskId);
594 g.iTimeout = DEFAULT_TIMEOUT;
595 runSql("COMMIT TRANSACTION;");
596 return SQLITE_OK;
598 sqlite3_finalize(pStmt);
599 if( rc==SQLITE_DONE ){
600 if( totalTime>30000 ){
601 errorMessage("Waited over 30 seconds with no work. Giving up.");
602 runSql("DELETE FROM client WHERE id=%d; COMMIT;", iClient);
603 sqlite3_close(g.db);
604 exit(1);
606 while( trySql("COMMIT")==SQLITE_BUSY ){
607 sqlite3_sleep(10);
608 totalTime += 10;
610 sqlite3_sleep(100);
611 totalTime += 100;
612 continue;
614 fatalError("%s", sqlite3_errmsg(g.db));
616 g.iTimeout = DEFAULT_TIMEOUT;
620 ** Mark a script as having finished. Remove the CLIENT table entry
621 ** if bShutdown is true.
623 static int finishScript(int iClient, int taskId, int bShutdown){
624 runSql("UPDATE task"
625 " SET endtime=strftime('%%Y-%%m-%%d %%H:%%M:%%f','now')"
626 " WHERE id=%d;", taskId);
627 if( bShutdown ){
628 runSql("DELETE FROM client WHERE id=%d", iClient);
630 return SQLITE_OK;
634 ** Start up a client process for iClient, if it is not already
635 ** running. If the client is already running, then this routine
636 ** is a no-op.
638 static void startClient(int iClient){
639 runSql("INSERT OR IGNORE INTO client VALUES(%d,0)", iClient);
640 if( sqlite3_changes(g.db) ){
641 char *zSys;
642 int rc;
643 zSys = sqlite3_mprintf("%s \"%s\" --client %d --trace %d",
644 g.argv0, g.zDbFile, iClient, g.iTrace);
645 if( g.bSqlTrace ){
646 zSys = sqlite3_mprintf("%z --sqltrace", zSys);
648 if( g.bSync ){
649 zSys = sqlite3_mprintf("%z --sync", zSys);
651 if( g.zVfs ){
652 zSys = sqlite3_mprintf("%z --vfs \"%s\"", zSys, g.zVfs);
654 if( g.iTrace>=2 ) logMessage("system('%q')", zSys);
655 #if !defined(_WIN32)
656 zSys = sqlite3_mprintf("%z &", zSys);
657 rc = system(zSys);
658 if( rc ) errorMessage("system() fails with error code %d", rc);
659 #else
661 STARTUPINFOA startupInfo;
662 PROCESS_INFORMATION processInfo;
663 memset(&startupInfo, 0, sizeof(startupInfo));
664 startupInfo.cb = sizeof(startupInfo);
665 memset(&processInfo, 0, sizeof(processInfo));
666 rc = CreateProcessA(NULL, zSys, NULL, NULL, FALSE, 0, NULL, NULL,
667 &startupInfo, &processInfo);
668 if( rc ){
669 CloseHandle(processInfo.hThread);
670 CloseHandle(processInfo.hProcess);
671 }else{
672 errorMessage("CreateProcessA() fails with error code %lu",
673 GetLastError());
676 #endif
677 sqlite3_free(zSys);
682 ** Read the entire content of a file into memory
684 static char *readFile(const char *zFilename){
685 FILE *in = fopen(zFilename, "rb");
686 long sz;
687 char *z;
688 if( in==0 ){
689 fatalError("cannot open \"%s\" for reading", zFilename);
691 fseek(in, 0, SEEK_END);
692 sz = ftell(in);
693 rewind(in);
694 z = sqlite3_malloc( sz+1 );
695 sz = (long)fread(z, 1, sz, in);
696 z[sz] = 0;
697 fclose(in);
698 return z;
702 ** Return the length of the next token.
704 static int tokenLength(const char *z, int *pnLine){
705 int n = 0;
706 if( ISSPACE(z[0]) || (z[0]=='/' && z[1]=='*') ){
707 int inC = 0;
708 int c;
709 if( z[0]=='/' ){
710 inC = 1;
711 n = 2;
713 while( (c = z[n++])!=0 ){
714 if( c=='\n' ) (*pnLine)++;
715 if( ISSPACE(c) ) continue;
716 if( inC && c=='*' && z[n]=='/' ){
717 n++;
718 inC = 0;
719 }else if( !inC && c=='/' && z[n]=='*' ){
720 n++;
721 inC = 1;
722 }else if( !inC ){
723 break;
726 n--;
727 }else if( z[0]=='-' && z[1]=='-' ){
728 for(n=2; z[n] && z[n]!='\n'; n++){}
729 if( z[n] ){ (*pnLine)++; n++; }
730 }else if( z[0]=='"' || z[0]=='\'' ){
731 int delim = z[0];
732 for(n=1; z[n]; n++){
733 if( z[n]=='\n' ) (*pnLine)++;
734 if( z[n]==delim ){
735 n++;
736 if( z[n+1]!=delim ) break;
739 }else{
740 int c;
741 for(n=1; (c = z[n])!=0 && !ISSPACE(c) && c!='"' && c!='\'' && c!=';'; n++){}
743 return n;
747 ** Copy a single token into a string buffer.
749 static int extractToken(const char *zIn, int nIn, char *zOut, int nOut){
750 int i;
751 if( nIn<=0 ){
752 zOut[0] = 0;
753 return 0;
755 for(i=0; i<nIn && i<nOut-1 && !ISSPACE(zIn[i]); i++){ zOut[i] = zIn[i]; }
756 zOut[i] = 0;
757 return i;
761 ** Find the number of characters up to the start of the next "--end" token.
763 static int findEnd(const char *z, int *pnLine){
764 int n = 0;
765 while( z[n] && (strncmp(z+n,"--end",5) || !ISSPACE(z[n+5])) ){
766 n += tokenLength(z+n, pnLine);
768 return n;
772 ** Find the number of characters up to the first character past the
773 ** of the next "--endif" or "--else" token. Nested --if commands are
774 ** also skipped.
776 static int findEndif(const char *z, int stopAtElse, int *pnLine){
777 int n = 0;
778 while( z[n] ){
779 int len = tokenLength(z+n, pnLine);
780 if( (strncmp(z+n,"--endif",7)==0 && ISSPACE(z[n+7]))
781 || (stopAtElse && strncmp(z+n,"--else",6)==0 && ISSPACE(z[n+6]))
783 return n+len;
785 if( strncmp(z+n,"--if",4)==0 && ISSPACE(z[n+4]) ){
786 int skip = findEndif(z+n+len, 0, pnLine);
787 n += skip + len;
788 }else{
789 n += len;
792 return n;
796 ** Wait for a client process to complete all its tasks
798 static void waitForClient(int iClient, int iTimeout, char *zErrPrefix){
799 sqlite3_stmt *pStmt;
800 int rc;
801 if( iClient>0 ){
802 pStmt = prepareSql(
803 "SELECT 1 FROM task"
804 " WHERE client=%d"
805 " AND client IN (SELECT id FROM client)"
806 " AND endtime IS NULL",
807 iClient);
808 }else{
809 pStmt = prepareSql(
810 "SELECT 1 FROM task"
811 " WHERE client IN (SELECT id FROM client)"
812 " AND endtime IS NULL");
814 g.iTimeout = 0;
815 while( ((rc = sqlite3_step(pStmt))==SQLITE_BUSY || rc==SQLITE_ROW)
816 && iTimeout>0
818 sqlite3_reset(pStmt);
819 sqlite3_sleep(50);
820 iTimeout -= 50;
822 sqlite3_finalize(pStmt);
823 g.iTimeout = DEFAULT_TIMEOUT;
824 if( rc!=SQLITE_DONE ){
825 if( zErrPrefix==0 ) zErrPrefix = "";
826 if( iClient>0 ){
827 errorMessage("%stimeout waiting for client %d", zErrPrefix, iClient);
828 }else{
829 errorMessage("%stimeout waiting for all clients", zErrPrefix);
834 /* Return a pointer to the tail of a filename
836 static char *filenameTail(char *z){
837 int i, j;
838 for(i=j=0; z[i]; i++) if( isDirSep(z[i]) ) j = i+1;
839 return z+j;
843 ** Interpret zArg as a boolean value. Return either 0 or 1.
845 static int booleanValue(char *zArg){
846 int i;
847 if( zArg==0 ) return 0;
848 for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
849 if( i>0 && zArg[i]==0 ) return atoi(zArg);
850 if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
851 return 1;
853 if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
854 return 0;
856 errorMessage("unknown boolean: [%s]", zArg);
857 return 0;
861 /* This routine exists as a convenient place to set a debugger
862 ** breakpoint.
864 static void test_breakpoint(void){ static volatile int cnt = 0; cnt++; }
866 /* Maximum number of arguments to a --command */
867 #define MX_ARG 2
870 ** Run a script.
872 static void runScript(
873 int iClient, /* The client number, or 0 for the master */
874 int taskId, /* The task ID for clients. 0 for master */
875 char *zScript, /* Text of the script */
876 char *zFilename /* File from which script was read. */
878 int lineno = 1;
879 int prevLine = 1;
880 int ii = 0;
881 int iBegin = 0;
882 int n, c, j;
883 int len;
884 int nArg;
885 String sResult;
886 char zCmd[30];
887 char zError[1000];
888 char azArg[MX_ARG][100];
890 memset(&sResult, 0, sizeof(sResult));
891 stringReset(&sResult);
892 while( (c = zScript[ii])!=0 ){
893 prevLine = lineno;
894 len = tokenLength(zScript+ii, &lineno);
895 if( ISSPACE(c) || (c=='/' && zScript[ii+1]=='*') ){
896 ii += len;
897 continue;
899 if( c!='-' || zScript[ii+1]!='-' || !isalpha(zScript[ii+2]) ){
900 ii += len;
901 continue;
904 /* Run any prior SQL before processing the new --command */
905 if( ii>iBegin ){
906 char *zSql = sqlite3_mprintf("%.*s", ii-iBegin, zScript+iBegin);
907 evalSql(&sResult, zSql);
908 sqlite3_free(zSql);
909 iBegin = ii + len;
912 /* Parse the --command */
913 if( g.iTrace>=2 ) logMessage("%.*s", len, zScript+ii);
914 n = extractToken(zScript+ii+2, len-2, zCmd, sizeof(zCmd));
915 for(nArg=0; n<len-2 && nArg<MX_ARG; nArg++){
916 while( n<len-2 && ISSPACE(zScript[ii+2+n]) ){ n++; }
917 if( n>=len-2 ) break;
918 n += extractToken(zScript+ii+2+n, len-2-n,
919 azArg[nArg], sizeof(azArg[nArg]));
921 for(j=nArg; j<MX_ARG; j++) azArg[j++][0] = 0;
924 ** --sleep N
926 ** Pause for N milliseconds
928 if( strcmp(zCmd, "sleep")==0 ){
929 sqlite3_sleep(atoi(azArg[0]));
930 }else
933 ** --exit N
935 ** Exit this process. If N>0 then exit without shutting down
936 ** SQLite. (In other words, simulate a crash.)
938 if( strcmp(zCmd, "exit")==0 ){
939 int rc = atoi(azArg[0]);
940 finishScript(iClient, taskId, 1);
941 if( rc==0 ) sqlite3_close(g.db);
942 exit(rc);
943 }else
946 ** --testcase NAME
948 ** Begin a new test case. Announce in the log that the test case
949 ** has begun.
951 if( strcmp(zCmd, "testcase")==0 ){
952 if( g.iTrace==1 ) logMessage("%.*s", len - 1, zScript+ii);
953 stringReset(&sResult);
954 }else
957 ** --finish
959 ** Mark the current task as having finished, even if it is not.
960 ** This can be used in conjunction with --exit to simulate a crash.
962 if( strcmp(zCmd, "finish")==0 && iClient>0 ){
963 finishScript(iClient, taskId, 1);
964 }else
967 ** --reset
969 ** Reset accumulated results back to an empty string
971 if( strcmp(zCmd, "reset")==0 ){
972 stringReset(&sResult);
973 }else
976 ** --match ANSWER...
978 ** Check to see if output matches ANSWER. Report an error if not.
980 if( strcmp(zCmd, "match")==0 ){
981 int jj;
982 char *zAns = zScript+ii;
983 for(jj=7; jj<len-1 && ISSPACE(zAns[jj]); jj++){}
984 zAns += jj;
985 if( len-jj-1!=sResult.n || strncmp(sResult.z, zAns, len-jj-1) ){
986 errorMessage("line %d of %s:\nExpected [%.*s]\n Got [%s]",
987 prevLine, zFilename, len-jj-1, zAns, sResult.z);
989 g.nTest++;
990 stringReset(&sResult);
991 }else
994 ** --glob ANSWER...
995 ** --notglob ANSWER....
997 ** Check to see if output does or does not match the glob pattern
998 ** ANSWER.
1000 if( strcmp(zCmd, "glob")==0 || strcmp(zCmd, "notglob")==0 ){
1001 int jj;
1002 char *zAns = zScript+ii;
1003 char *zCopy;
1004 int isGlob = (zCmd[0]=='g');
1005 for(jj=9-3*isGlob; jj<len-1 && ISSPACE(zAns[jj]); jj++){}
1006 zAns += jj;
1007 zCopy = sqlite3_mprintf("%.*s", len-jj-1, zAns);
1008 if( (sqlite3_strglob(zCopy, sResult.z)==0)^isGlob ){
1009 errorMessage("line %d of %s:\nExpected [%s]\n Got [%s]",
1010 prevLine, zFilename, zCopy, sResult.z);
1012 sqlite3_free(zCopy);
1013 g.nTest++;
1014 stringReset(&sResult);
1015 }else
1018 ** --output
1020 ** Output the result of the previous SQL.
1022 if( strcmp(zCmd, "output")==0 ){
1023 logMessage("%s", sResult.z);
1024 }else
1027 ** --source FILENAME
1029 ** Run a subscript from a separate file.
1031 if( strcmp(zCmd, "source")==0 ){
1032 char *zNewFile, *zNewScript;
1033 char *zToDel = 0;
1034 zNewFile = azArg[0];
1035 if( !isDirSep(zNewFile[0]) ){
1036 int k;
1037 for(k=(int)strlen(zFilename)-1; k>=0 && !isDirSep(zFilename[k]); k--){}
1038 if( k>0 ){
1039 zNewFile = zToDel = sqlite3_mprintf("%.*s/%s", k,zFilename,zNewFile);
1042 zNewScript = readFile(zNewFile);
1043 if( g.iTrace ) logMessage("begin script [%s]\n", zNewFile);
1044 runScript(0, 0, zNewScript, zNewFile);
1045 sqlite3_free(zNewScript);
1046 if( g.iTrace ) logMessage("end script [%s]\n", zNewFile);
1047 sqlite3_free(zToDel);
1048 }else
1051 ** --print MESSAGE....
1053 ** Output the remainder of the line to the log file
1055 if( strcmp(zCmd, "print")==0 ){
1056 int jj;
1057 for(jj=7; jj<len && ISSPACE(zScript[ii+jj]); jj++){}
1058 logMessage("%.*s", len-jj, zScript+ii+jj);
1059 }else
1062 ** --if EXPR
1064 ** Skip forward to the next matching --endif or --else if EXPR is false.
1066 if( strcmp(zCmd, "if")==0 ){
1067 int jj, rc;
1068 sqlite3_stmt *pStmt;
1069 for(jj=4; jj<len && ISSPACE(zScript[ii+jj]); jj++){}
1070 pStmt = prepareSql("SELECT %.*s", len-jj, zScript+ii+jj);
1071 rc = sqlite3_step(pStmt);
1072 if( rc!=SQLITE_ROW || sqlite3_column_int(pStmt, 0)==0 ){
1073 ii += findEndif(zScript+ii+len, 1, &lineno);
1075 sqlite3_finalize(pStmt);
1076 }else
1079 ** --else
1081 ** This command can only be encountered if currently inside an --if that
1082 ** is true. Skip forward to the next matching --endif.
1084 if( strcmp(zCmd, "else")==0 ){
1085 ii += findEndif(zScript+ii+len, 0, &lineno);
1086 }else
1089 ** --endif
1091 ** This command can only be encountered if currently inside an --if that
1092 ** is true or an --else of a false if. This is a no-op.
1094 if( strcmp(zCmd, "endif")==0 ){
1095 /* no-op */
1096 }else
1099 ** --start CLIENT
1101 ** Start up the given client.
1103 if( strcmp(zCmd, "start")==0 && iClient==0 ){
1104 int iNewClient = atoi(azArg[0]);
1105 if( iNewClient>0 ){
1106 startClient(iNewClient);
1108 }else
1111 ** --wait CLIENT TIMEOUT
1113 ** Wait until all tasks complete for the given client. If CLIENT is
1114 ** "all" then wait for all clients to complete. Wait no longer than
1115 ** TIMEOUT milliseconds (default 10,000)
1117 if( strcmp(zCmd, "wait")==0 && iClient==0 ){
1118 int iTimeout = nArg>=2 ? atoi(azArg[1]) : 10000;
1119 sqlite3_snprintf(sizeof(zError),zError,"line %d of %s\n",
1120 prevLine, zFilename);
1121 waitForClient(atoi(azArg[0]), iTimeout, zError);
1122 }else
1125 ** --task CLIENT
1126 ** <task-content-here>
1127 ** --end
1129 ** Assign work to a client. Start the client if it is not running
1130 ** already.
1132 if( strcmp(zCmd, "task")==0 && iClient==0 ){
1133 int iTarget = atoi(azArg[0]);
1134 int iEnd;
1135 char *zTask;
1136 char *zTName;
1137 iEnd = findEnd(zScript+ii+len, &lineno);
1138 if( iTarget<0 ){
1139 errorMessage("line %d of %s: bad client number: %d",
1140 prevLine, zFilename, iTarget);
1141 }else{
1142 zTask = sqlite3_mprintf("%.*s", iEnd, zScript+ii+len);
1143 if( nArg>1 ){
1144 zTName = sqlite3_mprintf("%s", azArg[1]);
1145 }else{
1146 zTName = sqlite3_mprintf("%s:%d", filenameTail(zFilename), prevLine);
1148 startClient(iTarget);
1149 runSql("INSERT INTO task(client,script,name)"
1150 " VALUES(%d,'%q',%Q)", iTarget, zTask, zTName);
1151 sqlite3_free(zTask);
1152 sqlite3_free(zTName);
1154 iEnd += tokenLength(zScript+ii+len+iEnd, &lineno);
1155 len += iEnd;
1156 iBegin = ii+len;
1157 }else
1160 ** --breakpoint
1162 ** This command calls "test_breakpoint()" which is a routine provided
1163 ** as a convenient place to set a debugger breakpoint.
1165 if( strcmp(zCmd, "breakpoint")==0 ){
1166 test_breakpoint();
1167 }else
1170 ** --show-sql-errors BOOLEAN
1172 ** Turn display of SQL errors on and off.
1174 if( strcmp(zCmd, "show-sql-errors")==0 ){
1175 g.bIgnoreSqlErrors = nArg>=1 ? !booleanValue(azArg[0]) : 1;
1176 }else
1179 /* error */{
1180 errorMessage("line %d of %s: unknown command --%s",
1181 prevLine, zFilename, zCmd);
1183 ii += len;
1185 if( iBegin<ii ){
1186 char *zSql = sqlite3_mprintf("%.*s", ii-iBegin, zScript+iBegin);
1187 runSql(zSql);
1188 sqlite3_free(zSql);
1190 stringFree(&sResult);
1194 ** Look for a command-line option. If present, return a pointer.
1195 ** Return NULL if missing.
1197 ** hasArg==0 means the option is a flag. It is either present or not.
1198 ** hasArg==1 means the option has an argument. Return a pointer to the
1199 ** argument.
1201 static char *findOption(
1202 char **azArg,
1203 int *pnArg,
1204 const char *zOption,
1205 int hasArg
1207 int i, j;
1208 char *zReturn = 0;
1209 int nArg = *pnArg;
1211 assert( hasArg==0 || hasArg==1 );
1212 for(i=0; i<nArg; i++){
1213 const char *z;
1214 if( i+hasArg >= nArg ) break;
1215 z = azArg[i];
1216 if( z[0]!='-' ) continue;
1217 z++;
1218 if( z[0]=='-' ){
1219 if( z[1]==0 ) break;
1220 z++;
1222 if( strcmp(z,zOption)==0 ){
1223 if( hasArg && i==nArg-1 ){
1224 fatalError("command-line option \"--%s\" requires an argument", z);
1226 if( hasArg ){
1227 zReturn = azArg[i+1];
1228 }else{
1229 zReturn = azArg[i];
1231 j = i+1+(hasArg!=0);
1232 while( j<nArg ) azArg[i++] = azArg[j++];
1233 *pnArg = i;
1234 return zReturn;
1237 return zReturn;
1240 /* Print a usage message for the program and exit */
1241 static void usage(const char *argv0){
1242 int i;
1243 const char *zTail = argv0;
1244 for(i=0; argv0[i]; i++){
1245 if( isDirSep(argv0[i]) ) zTail = argv0+i+1;
1247 fprintf(stderr,"Usage: %s DATABASE ?OPTIONS? ?SCRIPT?\n", zTail);
1248 fprintf(stderr,
1249 "Options:\n"
1250 " --errlog FILENAME Write errors to FILENAME\n"
1251 " --journalmode MODE Use MODE as the journal_mode\n"
1252 " --log FILENAME Log messages to FILENAME\n"
1253 " --quiet Suppress unnecessary output\n"
1254 " --vfs NAME Use NAME as the VFS\n"
1255 " --repeat N Repeat the test N times\n"
1256 " --sqltrace Enable SQL tracing\n"
1257 " --sync Enable synchronous disk writes\n"
1258 " --timeout MILLISEC Busy timeout is MILLISEC\n"
1259 " --trace BOOLEAN Enable or disable tracing\n"
1261 exit(1);
1264 /* Report on unrecognized arguments */
1265 static void unrecognizedArguments(
1266 const char *argv0,
1267 int nArg,
1268 char **azArg
1270 int i;
1271 fprintf(stderr,"%s: unrecognized arguments:", argv0);
1272 for(i=0; i<nArg; i++){
1273 fprintf(stderr," %s", azArg[i]);
1275 fprintf(stderr,"\n");
1276 exit(1);
1279 int SQLITE_CDECL main(int argc, char **argv){
1280 const char *zClient;
1281 int iClient;
1282 int n, i;
1283 int openFlags = SQLITE_OPEN_READWRITE;
1284 int rc;
1285 char *zScript;
1286 int taskId;
1287 const char *zTrace;
1288 const char *zCOption;
1289 const char *zJMode;
1290 const char *zNRep;
1291 int nRep = 1, iRep;
1292 int iTmout = 0; /* Default: no timeout */
1293 const char *zTmout;
1295 g.argv0 = argv[0];
1296 g.iTrace = 1;
1297 if( argc<2 ) usage(argv[0]);
1298 g.zDbFile = argv[1];
1299 if( strglob("*.test", g.zDbFile) ) usage(argv[0]);
1300 if( strcmp(sqlite3_sourceid(), SQLITE_SOURCE_ID)!=0 ){
1301 fprintf(stderr, "SQLite library and header mismatch\n"
1302 "Library: %s\n"
1303 "Header: %s\n",
1304 sqlite3_sourceid(), SQLITE_SOURCE_ID);
1305 exit(1);
1307 n = argc-2;
1308 sqlite3_snprintf(sizeof(g.zName), g.zName, "%05d.mptest", GETPID());
1309 zJMode = findOption(argv+2, &n, "journalmode", 1);
1310 zNRep = findOption(argv+2, &n, "repeat", 1);
1311 if( zNRep ) nRep = atoi(zNRep);
1312 if( nRep<1 ) nRep = 1;
1313 g.zVfs = findOption(argv+2, &n, "vfs", 1);
1314 zClient = findOption(argv+2, &n, "client", 1);
1315 g.zErrLog = findOption(argv+2, &n, "errlog", 1);
1316 g.zLog = findOption(argv+2, &n, "log", 1);
1317 zTrace = findOption(argv+2, &n, "trace", 1);
1318 if( zTrace ) g.iTrace = atoi(zTrace);
1319 if( findOption(argv+2, &n, "quiet", 0)!=0 ) g.iTrace = 0;
1320 zTmout = findOption(argv+2, &n, "timeout", 1);
1321 if( zTmout ) iTmout = atoi(zTmout);
1322 g.bSqlTrace = findOption(argv+2, &n, "sqltrace", 0)!=0;
1323 g.bSync = findOption(argv+2, &n, "sync", 0)!=0;
1324 if( g.zErrLog ){
1325 g.pErrLog = fopen(g.zErrLog, "a");
1326 }else{
1327 g.pErrLog = stderr;
1329 if( g.zLog ){
1330 g.pLog = fopen(g.zLog, "a");
1331 }else{
1332 g.pLog = stdout;
1335 sqlite3_config(SQLITE_CONFIG_LOG, sqlErrorCallback, 0);
1336 if( zClient ){
1337 iClient = atoi(zClient);
1338 if( iClient<1 ) fatalError("illegal client number: %d\n", iClient);
1339 sqlite3_snprintf(sizeof(g.zName), g.zName, "%05d.client%02d",
1340 GETPID(), iClient);
1341 }else{
1342 int nTry = 0;
1343 if( g.iTrace>0 ){
1344 printf("BEGIN: %s", argv[0]);
1345 for(i=1; i<argc; i++) printf(" %s", argv[i]);
1346 printf("\n");
1347 printf("With SQLite " SQLITE_VERSION " " SQLITE_SOURCE_ID "\n" );
1348 for(i=0; (zCOption = sqlite3_compileoption_get(i))!=0; i++){
1349 printf("-DSQLITE_%s\n", zCOption);
1351 fflush(stdout);
1353 iClient = 0;
1355 if( (nTry%5)==4 ) printf("... %strying to unlink '%s'\n",
1356 nTry>5 ? "still " : "", g.zDbFile);
1357 rc = unlink(g.zDbFile);
1358 if( rc && errno==ENOENT ) rc = 0;
1359 }while( rc!=0 && (++nTry)<60 && sqlite3_sleep(1000)>0 );
1360 if( rc!=0 ){
1361 fatalError("unable to unlink '%s' after %d attempts\n",
1362 g.zDbFile, nTry);
1364 openFlags |= SQLITE_OPEN_CREATE;
1366 rc = sqlite3_open_v2(g.zDbFile, &g.db, openFlags, g.zVfs);
1367 if( rc ) fatalError("cannot open [%s]", g.zDbFile);
1368 if( iTmout>0 ) sqlite3_busy_timeout(g.db, iTmout);
1370 if( zJMode ){
1371 #if defined(_WIN32)
1372 if( sqlite3_stricmp(zJMode,"persist")==0
1373 || sqlite3_stricmp(zJMode,"truncate")==0
1375 printf("Changing journal mode to DELETE from %s", zJMode);
1376 zJMode = "DELETE";
1378 #endif
1379 runSql("PRAGMA journal_mode=%Q;", zJMode);
1381 if( !g.bSync ) trySql("PRAGMA synchronous=OFF");
1382 sqlite3_enable_load_extension(g.db, 1);
1383 sqlite3_busy_handler(g.db, busyHandler, 0);
1384 sqlite3_create_function(g.db, "vfsname", 0, SQLITE_UTF8, 0,
1385 vfsNameFunc, 0, 0);
1386 sqlite3_create_function(g.db, "eval", 1, SQLITE_UTF8, 0,
1387 evalFunc, 0, 0);
1388 g.iTimeout = DEFAULT_TIMEOUT;
1389 if( g.bSqlTrace ) sqlite3_trace(g.db, sqlTraceCallback, 0);
1390 if( iClient>0 ){
1391 if( n>0 ) unrecognizedArguments(argv[0], n, argv+2);
1392 if( g.iTrace ) logMessage("start-client");
1393 while(1){
1394 char *zTaskName = 0;
1395 rc = startScript(iClient, &zScript, &taskId, &zTaskName);
1396 if( rc==SQLITE_DONE ) break;
1397 if( g.iTrace ) logMessage("begin %s (%d)", zTaskName, taskId);
1398 runScript(iClient, taskId, zScript, zTaskName);
1399 if( g.iTrace ) logMessage("end %s (%d)", zTaskName, taskId);
1400 finishScript(iClient, taskId, 0);
1401 sqlite3_free(zTaskName);
1402 sqlite3_sleep(10);
1404 if( g.iTrace ) logMessage("end-client");
1405 }else{
1406 sqlite3_stmt *pStmt;
1407 int iTimeout;
1408 if( n==0 ){
1409 fatalError("missing script filename");
1411 if( n>1 ) unrecognizedArguments(argv[0], n, argv+2);
1412 runSql(
1413 "DROP TABLE IF EXISTS task;\n"
1414 "DROP TABLE IF EXISTS counters;\n"
1415 "DROP TABLE IF EXISTS client;\n"
1416 "CREATE TABLE task(\n"
1417 " id INTEGER PRIMARY KEY,\n"
1418 " name TEXT,\n"
1419 " client INTEGER,\n"
1420 " starttime DATE,\n"
1421 " endtime DATE,\n"
1422 " script TEXT\n"
1423 ");"
1424 "CREATE INDEX task_i1 ON task(client, starttime);\n"
1425 "CREATE INDEX task_i2 ON task(client, endtime);\n"
1426 "CREATE TABLE counters(nError,nTest);\n"
1427 "INSERT INTO counters VALUES(0,0);\n"
1428 "CREATE TABLE client(id INTEGER PRIMARY KEY, wantHalt);\n"
1430 zScript = readFile(argv[2]);
1431 for(iRep=1; iRep<=nRep; iRep++){
1432 if( g.iTrace ) logMessage("begin script [%s] cycle %d\n", argv[2], iRep);
1433 runScript(0, 0, zScript, argv[2]);
1434 if( g.iTrace ) logMessage("end script [%s] cycle %d\n", argv[2], iRep);
1436 sqlite3_free(zScript);
1437 waitForClient(0, 2000, "during shutdown...\n");
1438 trySql("UPDATE client SET wantHalt=1");
1439 sqlite3_sleep(10);
1440 g.iTimeout = 0;
1441 iTimeout = 1000;
1442 while( ((rc = trySql("SELECT 1 FROM client"))==SQLITE_BUSY
1443 || rc==SQLITE_ROW) && iTimeout>0 ){
1444 sqlite3_sleep(10);
1445 iTimeout -= 10;
1447 sqlite3_sleep(100);
1448 pStmt = prepareSql("SELECT nError, nTest FROM counters");
1449 iTimeout = 1000;
1450 while( (rc = sqlite3_step(pStmt))==SQLITE_BUSY && iTimeout>0 ){
1451 sqlite3_sleep(10);
1452 iTimeout -= 10;
1454 if( rc==SQLITE_ROW ){
1455 g.nError += sqlite3_column_int(pStmt, 0);
1456 g.nTest += sqlite3_column_int(pStmt, 1);
1458 sqlite3_finalize(pStmt);
1460 sqlite3_close(g.db);
1461 maybeClose(g.pLog);
1462 maybeClose(g.pErrLog);
1463 if( iClient==0 ){
1464 printf("Summary: %d errors out of %d tests\n", g.nError, g.nTest);
1465 printf("END: %s", argv[0]);
1466 for(i=1; i<argc; i++) printf(" %s", argv[i]);
1467 printf("\n");
1469 return g.nError>0;