Add ICU message format support
[chromium-blink-merge.git] / third_party / sqlite / src / mptest / mptest.c
blob059ae102fafb8bc398606d4265788821b5489da9
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 <stdlib.h>
45 #include <string.h>
46 #include <assert.h>
47 #include <ctype.h>
49 /* The suffix to append to the child command lines, if any */
50 #if defined(_WIN32)
51 # define GETPID (int)GetCurrentProcessId
52 #else
53 # define GETPID getpid
54 #endif
56 /* Mark a parameter as unused to suppress compiler warnings */
57 #define UNUSED_PARAMETER(x) (void)x
59 /* Global data
61 static struct Global {
62 char *argv0; /* Name of the executable */
63 const char *zVfs; /* Name of VFS to use. Often NULL meaning "default" */
64 char *zDbFile; /* Name of the database */
65 sqlite3 *db; /* Open connection to database */
66 char *zErrLog; /* Filename for error log */
67 FILE *pErrLog; /* Where to write errors */
68 char *zLog; /* Name of output log file */
69 FILE *pLog; /* Where to write log messages */
70 char zName[32]; /* Symbolic name of this process */
71 int taskId; /* Task ID. 0 means supervisor. */
72 int iTrace; /* Tracing level */
73 int bSqlTrace; /* True to trace SQL commands */
74 int bIgnoreSqlErrors; /* Ignore errors in SQL statements */
75 int nError; /* Number of errors */
76 int nTest; /* Number of --match operators */
77 int iTimeout; /* Milliseconds until a busy timeout */
78 int bSync; /* Call fsync() */
79 } g;
81 /* Default timeout */
82 #define DEFAULT_TIMEOUT 10000
85 ** Print a message adding zPrefix[] to the beginning of every line.
87 static void printWithPrefix(FILE *pOut, const char *zPrefix, const char *zMsg){
88 while( zMsg && zMsg[0] ){
89 int i;
90 for(i=0; zMsg[i] && zMsg[i]!='\n' && zMsg[i]!='\r'; i++){}
91 fprintf(pOut, "%s%.*s\n", zPrefix, i, zMsg);
92 zMsg += i;
93 while( zMsg[0]=='\n' || zMsg[0]=='\r' ) zMsg++;
98 ** Compare two pointers to strings, where the pointers might be NULL.
100 static int safe_strcmp(const char *a, const char *b){
101 if( a==b ) return 0;
102 if( a==0 ) return -1;
103 if( b==0 ) return 1;
104 return strcmp(a,b);
108 ** Return TRUE if string z[] matches glob pattern zGlob[].
109 ** Return FALSE if the pattern does not match.
111 ** Globbing rules:
113 ** '*' Matches any sequence of zero or more characters.
115 ** '?' Matches exactly one character.
117 ** [...] Matches one character from the enclosed list of
118 ** characters.
120 ** [^...] Matches one character not in the enclosed list.
122 ** '#' Matches any sequence of one or more digits with an
123 ** optional + or - sign in front
125 int strglob(const char *zGlob, const char *z){
126 int c, c2;
127 int invert;
128 int seen;
130 while( (c = (*(zGlob++)))!=0 ){
131 if( c=='*' ){
132 while( (c=(*(zGlob++))) == '*' || c=='?' ){
133 if( c=='?' && (*(z++))==0 ) return 0;
135 if( c==0 ){
136 return 1;
137 }else if( c=='[' ){
138 while( *z && strglob(zGlob-1,z) ){
139 z++;
141 return (*z)!=0;
143 while( (c2 = (*(z++)))!=0 ){
144 while( c2!=c ){
145 c2 = *(z++);
146 if( c2==0 ) return 0;
148 if( strglob(zGlob,z) ) return 1;
150 return 0;
151 }else if( c=='?' ){
152 if( (*(z++))==0 ) return 0;
153 }else if( c=='[' ){
154 int prior_c = 0;
155 seen = 0;
156 invert = 0;
157 c = *(z++);
158 if( c==0 ) return 0;
159 c2 = *(zGlob++);
160 if( c2=='^' ){
161 invert = 1;
162 c2 = *(zGlob++);
164 if( c2==']' ){
165 if( c==']' ) seen = 1;
166 c2 = *(zGlob++);
168 while( c2 && c2!=']' ){
169 if( c2=='-' && zGlob[0]!=']' && zGlob[0]!=0 && prior_c>0 ){
170 c2 = *(zGlob++);
171 if( c>=prior_c && c<=c2 ) seen = 1;
172 prior_c = 0;
173 }else{
174 if( c==c2 ){
175 seen = 1;
177 prior_c = c2;
179 c2 = *(zGlob++);
181 if( c2==0 || (seen ^ invert)==0 ) return 0;
182 }else if( c=='#' ){
183 if( (z[0]=='-' || z[0]=='+') && isdigit(z[1]) ) z++;
184 if( !isdigit(z[0]) ) return 0;
185 z++;
186 while( isdigit(z[0]) ){ z++; }
187 }else{
188 if( c!=(*(z++)) ) return 0;
191 return *z==0;
195 ** Close output stream pOut if it is not stdout or stderr
197 static void maybeClose(FILE *pOut){
198 if( pOut!=stdout && pOut!=stderr ) fclose(pOut);
202 ** Print an error message
204 static void errorMessage(const char *zFormat, ...){
205 va_list ap;
206 char *zMsg;
207 char zPrefix[30];
208 va_start(ap, zFormat);
209 zMsg = sqlite3_vmprintf(zFormat, ap);
210 va_end(ap);
211 sqlite3_snprintf(sizeof(zPrefix), zPrefix, "%s:ERROR: ", g.zName);
212 if( g.pLog ){
213 printWithPrefix(g.pLog, zPrefix, zMsg);
214 fflush(g.pLog);
216 if( g.pErrLog && safe_strcmp(g.zErrLog,g.zLog) ){
217 printWithPrefix(g.pErrLog, zPrefix, zMsg);
218 fflush(g.pErrLog);
220 sqlite3_free(zMsg);
221 g.nError++;
224 /* Forward declaration */
225 static int trySql(const char*, ...);
228 ** Print an error message and then quit.
230 static void fatalError(const char *zFormat, ...){
231 va_list ap;
232 char *zMsg;
233 char zPrefix[30];
234 va_start(ap, zFormat);
235 zMsg = sqlite3_vmprintf(zFormat, ap);
236 va_end(ap);
237 sqlite3_snprintf(sizeof(zPrefix), zPrefix, "%s:FATAL: ", g.zName);
238 if( g.pLog ){
239 printWithPrefix(g.pLog, zPrefix, zMsg);
240 fflush(g.pLog);
241 maybeClose(g.pLog);
243 if( g.pErrLog && safe_strcmp(g.zErrLog,g.zLog) ){
244 printWithPrefix(g.pErrLog, zPrefix, zMsg);
245 fflush(g.pErrLog);
246 maybeClose(g.pErrLog);
248 sqlite3_free(zMsg);
249 if( g.db ){
250 int nTry = 0;
251 g.iTimeout = 0;
252 while( trySql("UPDATE client SET wantHalt=1;")==SQLITE_BUSY
253 && (nTry++)<100 ){
254 sqlite3_sleep(10);
257 sqlite3_close(g.db);
258 exit(1);
263 ** Print a log message
265 static void logMessage(const char *zFormat, ...){
266 va_list ap;
267 char *zMsg;
268 char zPrefix[30];
269 va_start(ap, zFormat);
270 zMsg = sqlite3_vmprintf(zFormat, ap);
271 va_end(ap);
272 sqlite3_snprintf(sizeof(zPrefix), zPrefix, "%s: ", g.zName);
273 if( g.pLog ){
274 printWithPrefix(g.pLog, zPrefix, zMsg);
275 fflush(g.pLog);
277 sqlite3_free(zMsg);
281 ** Return the length of a string omitting trailing whitespace
283 static int clipLength(const char *z){
284 int n = (int)strlen(z);
285 while( n>0 && isspace(z[n-1]) ){ n--; }
286 return n;
290 ** Auxiliary SQL function to return the name of the VFS
292 static void vfsNameFunc(
293 sqlite3_context *context,
294 int argc,
295 sqlite3_value **argv
297 sqlite3 *db = sqlite3_context_db_handle(context);
298 char *zVfs = 0;
299 UNUSED_PARAMETER(argc);
300 UNUSED_PARAMETER(argv);
301 sqlite3_file_control(db, "main", SQLITE_FCNTL_VFSNAME, &zVfs);
302 if( zVfs ){
303 sqlite3_result_text(context, zVfs, -1, sqlite3_free);
308 ** Busy handler with a g.iTimeout-millisecond timeout
310 static int busyHandler(void *pCD, int count){
311 UNUSED_PARAMETER(pCD);
312 if( count*10>g.iTimeout ){
313 if( g.iTimeout>0 ) errorMessage("timeout after %dms", g.iTimeout);
314 return 0;
316 sqlite3_sleep(10);
317 return 1;
321 ** SQL Trace callback
323 static void sqlTraceCallback(void *NotUsed1, const char *zSql){
324 UNUSED_PARAMETER(NotUsed1);
325 logMessage("[%.*s]", clipLength(zSql), zSql);
329 ** SQL error log callback
331 static void sqlErrorCallback(void *pArg, int iErrCode, const char *zMsg){
332 UNUSED_PARAMETER(pArg);
333 if( iErrCode==SQLITE_ERROR && g.bIgnoreSqlErrors ) return;
334 if( (iErrCode&0xff)==SQLITE_SCHEMA && g.iTrace<3 ) return;
335 if( g.iTimeout==0 && (iErrCode&0xff)==SQLITE_BUSY && g.iTrace<3 ) return;
336 if( (iErrCode&0xff)==SQLITE_NOTICE ){
337 logMessage("(info) %s", zMsg);
338 }else{
339 errorMessage("(errcode=%d) %s", iErrCode, zMsg);
344 ** Prepare an SQL statement. Issue a fatal error if unable.
346 static sqlite3_stmt *prepareSql(const char *zFormat, ...){
347 va_list ap;
348 char *zSql;
349 int rc;
350 sqlite3_stmt *pStmt = 0;
351 va_start(ap, zFormat);
352 zSql = sqlite3_vmprintf(zFormat, ap);
353 va_end(ap);
354 rc = sqlite3_prepare_v2(g.db, zSql, -1, &pStmt, 0);
355 if( rc!=SQLITE_OK ){
356 sqlite3_finalize(pStmt);
357 fatalError("%s\n%s\n", sqlite3_errmsg(g.db), zSql);
359 sqlite3_free(zSql);
360 return pStmt;
364 ** Run arbitrary SQL. Issue a fatal error on failure.
366 static void runSql(const char *zFormat, ...){
367 va_list ap;
368 char *zSql;
369 int rc;
370 va_start(ap, zFormat);
371 zSql = sqlite3_vmprintf(zFormat, ap);
372 va_end(ap);
373 rc = sqlite3_exec(g.db, zSql, 0, 0, 0);
374 if( rc!=SQLITE_OK ){
375 fatalError("%s\n%s\n", sqlite3_errmsg(g.db), zSql);
377 sqlite3_free(zSql);
381 ** Try to run arbitrary SQL. Return success code.
383 static int trySql(const char *zFormat, ...){
384 va_list ap;
385 char *zSql;
386 int rc;
387 va_start(ap, zFormat);
388 zSql = sqlite3_vmprintf(zFormat, ap);
389 va_end(ap);
390 rc = sqlite3_exec(g.db, zSql, 0, 0, 0);
391 sqlite3_free(zSql);
392 return rc;
395 /* Structure for holding an arbitrary length string
397 typedef struct String String;
398 struct String {
399 char *z; /* the string */
400 int n; /* Slots of z[] used */
401 int nAlloc; /* Slots of z[] allocated */
404 /* Free a string */
405 static void stringFree(String *p){
406 if( p->z ) sqlite3_free(p->z);
407 memset(p, 0, sizeof(*p));
410 /* Append n bytes of text to a string. If n<0 append the entire string. */
411 static void stringAppend(String *p, const char *z, int n){
412 if( n<0 ) n = (int)strlen(z);
413 if( p->n+n>=p->nAlloc ){
414 int nAlloc = p->nAlloc*2 + n + 100;
415 char *z = sqlite3_realloc(p->z, nAlloc);
416 if( z==0 ) fatalError("out of memory");
417 p->z = z;
418 p->nAlloc = nAlloc;
420 memcpy(p->z+p->n, z, n);
421 p->n += n;
422 p->z[p->n] = 0;
425 /* Reset a string to an empty string */
426 static void stringReset(String *p){
427 if( p->z==0 ) stringAppend(p, " ", 1);
428 p->n = 0;
429 p->z[0] = 0;
432 /* Append a new token onto the end of the string */
433 static void stringAppendTerm(String *p, const char *z){
434 int i;
435 if( p->n ) stringAppend(p, " ", 1);
436 if( z==0 ){
437 stringAppend(p, "nil", 3);
438 return;
440 for(i=0; z[i] && !isspace(z[i]); i++){}
441 if( i>0 && z[i]==0 ){
442 stringAppend(p, z, i);
443 return;
445 stringAppend(p, "'", 1);
446 while( z[0] ){
447 for(i=0; z[i] && z[i]!='\''; i++){}
448 if( z[i] ){
449 stringAppend(p, z, i+1);
450 stringAppend(p, "'", 1);
451 z += i+1;
452 }else{
453 stringAppend(p, z, i);
454 break;
457 stringAppend(p, "'", 1);
461 ** Callback function for evalSql()
463 static int evalCallback(void *pCData, int argc, char **argv, char **azCol){
464 String *p = (String*)pCData;
465 int i;
466 UNUSED_PARAMETER(azCol);
467 for(i=0; i<argc; i++) stringAppendTerm(p, argv[i]);
468 return 0;
472 ** Run arbitrary SQL and record the results in an output string
473 ** given by the first parameter.
475 static int evalSql(String *p, const char *zFormat, ...){
476 va_list ap;
477 char *zSql;
478 int rc;
479 char *zErrMsg = 0;
480 va_start(ap, zFormat);
481 zSql = sqlite3_vmprintf(zFormat, ap);
482 va_end(ap);
483 assert( g.iTimeout>0 );
484 rc = sqlite3_exec(g.db, zSql, evalCallback, p, &zErrMsg);
485 sqlite3_free(zSql);
486 if( rc ){
487 char zErr[30];
488 sqlite3_snprintf(sizeof(zErr), zErr, "error(%d)", rc);
489 stringAppendTerm(p, zErr);
490 if( zErrMsg ){
491 stringAppendTerm(p, zErrMsg);
492 sqlite3_free(zErrMsg);
495 return rc;
499 ** Auxiliary SQL function to recursively evaluate SQL.
501 static void evalFunc(
502 sqlite3_context *context,
503 int argc,
504 sqlite3_value **argv
506 sqlite3 *db = sqlite3_context_db_handle(context);
507 const char *zSql = (const char*)sqlite3_value_text(argv[0]);
508 String res;
509 char *zErrMsg = 0;
510 int rc;
511 UNUSED_PARAMETER(argc);
512 memset(&res, 0, sizeof(res));
513 rc = sqlite3_exec(db, zSql, evalCallback, &res, &zErrMsg);
514 if( zErrMsg ){
515 sqlite3_result_error(context, zErrMsg, -1);
516 sqlite3_free(zErrMsg);
517 }else if( rc ){
518 sqlite3_result_error_code(context, rc);
519 }else{
520 sqlite3_result_text(context, res.z, -1, SQLITE_TRANSIENT);
522 stringFree(&res);
526 ** Look up the next task for client iClient in the database.
527 ** Return the task script and the task number and mark that
528 ** task as being under way.
530 static int startScript(
531 int iClient, /* The client number */
532 char **pzScript, /* Write task script here */
533 int *pTaskId, /* Write task number here */
534 char **pzTaskName /* Name of the task */
536 sqlite3_stmt *pStmt = 0;
537 int taskId;
538 int rc;
539 int totalTime = 0;
541 *pzScript = 0;
542 g.iTimeout = 0;
543 while(1){
544 rc = trySql("BEGIN IMMEDIATE");
545 if( rc==SQLITE_BUSY ){
546 sqlite3_sleep(10);
547 totalTime += 10;
548 continue;
550 if( rc!=SQLITE_OK ){
551 fatalError("in startScript: %s", sqlite3_errmsg(g.db));
553 if( g.nError || g.nTest ){
554 runSql("UPDATE counters SET nError=nError+%d, nTest=nTest+%d",
555 g.nError, g.nTest);
556 g.nError = 0;
557 g.nTest = 0;
559 pStmt = prepareSql("SELECT 1 FROM client WHERE id=%d AND wantHalt",iClient);
560 rc = sqlite3_step(pStmt);
561 sqlite3_finalize(pStmt);
562 if( rc==SQLITE_ROW ){
563 runSql("DELETE FROM client WHERE id=%d", iClient);
564 g.iTimeout = DEFAULT_TIMEOUT;
565 runSql("COMMIT TRANSACTION;");
566 return SQLITE_DONE;
568 pStmt = prepareSql(
569 "SELECT script, id, name FROM task"
570 " WHERE client=%d AND starttime IS NULL"
571 " ORDER BY id LIMIT 1", iClient);
572 rc = sqlite3_step(pStmt);
573 if( rc==SQLITE_ROW ){
574 int n = sqlite3_column_bytes(pStmt, 0);
575 *pzScript = sqlite3_malloc(n+1);
576 strcpy(*pzScript, (const char*)sqlite3_column_text(pStmt, 0));
577 *pTaskId = taskId = sqlite3_column_int(pStmt, 1);
578 *pzTaskName = sqlite3_mprintf("%s", sqlite3_column_text(pStmt, 2));
579 sqlite3_finalize(pStmt);
580 runSql("UPDATE task"
581 " SET starttime=strftime('%%Y-%%m-%%d %%H:%%M:%%f','now')"
582 " WHERE id=%d;", taskId);
583 g.iTimeout = DEFAULT_TIMEOUT;
584 runSql("COMMIT TRANSACTION;");
585 return SQLITE_OK;
587 sqlite3_finalize(pStmt);
588 if( rc==SQLITE_DONE ){
589 if( totalTime>30000 ){
590 errorMessage("Waited over 30 seconds with no work. Giving up.");
591 runSql("DELETE FROM client WHERE id=%d; COMMIT;", iClient);
592 sqlite3_close(g.db);
593 exit(1);
595 while( trySql("COMMIT")==SQLITE_BUSY ){
596 sqlite3_sleep(10);
597 totalTime += 10;
599 sqlite3_sleep(100);
600 totalTime += 100;
601 continue;
603 fatalError("%s", sqlite3_errmsg(g.db));
605 g.iTimeout = DEFAULT_TIMEOUT;
609 ** Mark a script as having finished. Remove the CLIENT table entry
610 ** if bShutdown is true.
612 static int finishScript(int iClient, int taskId, int bShutdown){
613 runSql("UPDATE task"
614 " SET endtime=strftime('%%Y-%%m-%%d %%H:%%M:%%f','now')"
615 " WHERE id=%d;", taskId);
616 if( bShutdown ){
617 runSql("DELETE FROM client WHERE id=%d", iClient);
619 return SQLITE_OK;
623 ** Start up a client process for iClient, if it is not already
624 ** running. If the client is already running, then this routine
625 ** is a no-op.
627 static void startClient(int iClient){
628 runSql("INSERT OR IGNORE INTO client VALUES(%d,0)", iClient);
629 if( sqlite3_changes(g.db) ){
630 char *zSys;
631 int rc;
632 zSys = sqlite3_mprintf("%s \"%s\" --client %d --trace %d",
633 g.argv0, g.zDbFile, iClient, g.iTrace);
634 if( g.bSqlTrace ){
635 zSys = sqlite3_mprintf("%z --sqltrace", zSys);
637 if( g.bSync ){
638 zSys = sqlite3_mprintf("%z --sync", zSys);
640 if( g.zVfs ){
641 zSys = sqlite3_mprintf("%z --vfs \"%s\"", zSys, g.zVfs);
643 if( g.iTrace>=2 ) logMessage("system('%q')", zSys);
644 #if !defined(_WIN32)
645 zSys = sqlite3_mprintf("%z &", zSys);
646 rc = system(zSys);
647 if( rc ) errorMessage("system() fails with error code %d", rc);
648 #else
650 STARTUPINFOA startupInfo;
651 PROCESS_INFORMATION processInfo;
652 memset(&startupInfo, 0, sizeof(startupInfo));
653 startupInfo.cb = sizeof(startupInfo);
654 memset(&processInfo, 0, sizeof(processInfo));
655 rc = CreateProcessA(NULL, zSys, NULL, NULL, FALSE, 0, NULL, NULL,
656 &startupInfo, &processInfo);
657 if( rc ){
658 CloseHandle(processInfo.hThread);
659 CloseHandle(processInfo.hProcess);
660 }else{
661 errorMessage("CreateProcessA() fails with error code %lu",
662 GetLastError());
665 #endif
666 sqlite3_free(zSys);
671 ** Read the entire content of a file into memory
673 static char *readFile(const char *zFilename){
674 FILE *in = fopen(zFilename, "rb");
675 long sz;
676 char *z;
677 if( in==0 ){
678 fatalError("cannot open \"%s\" for reading", zFilename);
680 fseek(in, 0, SEEK_END);
681 sz = ftell(in);
682 rewind(in);
683 z = sqlite3_malloc( sz+1 );
684 sz = (long)fread(z, 1, sz, in);
685 z[sz] = 0;
686 fclose(in);
687 return z;
691 ** Return the length of the next token.
693 static int tokenLength(const char *z, int *pnLine){
694 int n = 0;
695 if( isspace(z[0]) || (z[0]=='/' && z[1]=='*') ){
696 int inC = 0;
697 int c;
698 if( z[0]=='/' ){
699 inC = 1;
700 n = 2;
702 while( (c = z[n++])!=0 ){
703 if( c=='\n' ) (*pnLine)++;
704 if( isspace(c) ) continue;
705 if( inC && c=='*' && z[n]=='/' ){
706 n++;
707 inC = 0;
708 }else if( !inC && c=='/' && z[n]=='*' ){
709 n++;
710 inC = 1;
711 }else if( !inC ){
712 break;
715 n--;
716 }else if( z[0]=='-' && z[1]=='-' ){
717 for(n=2; z[n] && z[n]!='\n'; n++){}
718 if( z[n] ){ (*pnLine)++; n++; }
719 }else if( z[0]=='"' || z[0]=='\'' ){
720 int delim = z[0];
721 for(n=1; z[n]; n++){
722 if( z[n]=='\n' ) (*pnLine)++;
723 if( z[n]==delim ){
724 n++;
725 if( z[n+1]!=delim ) break;
728 }else{
729 int c;
730 for(n=1; (c = z[n])!=0 && !isspace(c) && c!='"' && c!='\'' && c!=';'; n++){}
732 return n;
736 ** Copy a single token into a string buffer.
738 static int extractToken(const char *zIn, int nIn, char *zOut, int nOut){
739 int i;
740 if( nIn<=0 ){
741 zOut[0] = 0;
742 return 0;
744 for(i=0; i<nIn && i<nOut-1 && !isspace(zIn[i]); i++){ zOut[i] = zIn[i]; }
745 zOut[i] = 0;
746 return i;
750 ** Find the number of characters up to the start of the next "--end" token.
752 static int findEnd(const char *z, int *pnLine){
753 int n = 0;
754 while( z[n] && (strncmp(z+n,"--end",5) || !isspace(z[n+5])) ){
755 n += tokenLength(z+n, pnLine);
757 return n;
761 ** Find the number of characters up to the first character past the
762 ** of the next "--endif" or "--else" token. Nested --if commands are
763 ** also skipped.
765 static int findEndif(const char *z, int stopAtElse, int *pnLine){
766 int n = 0;
767 while( z[n] ){
768 int len = tokenLength(z+n, pnLine);
769 if( (strncmp(z+n,"--endif",7)==0 && isspace(z[n+7]))
770 || (stopAtElse && strncmp(z+n,"--else",6)==0 && isspace(z[n+6]))
772 return n+len;
774 if( strncmp(z+n,"--if",4)==0 && isspace(z[n+4]) ){
775 int skip = findEndif(z+n+len, 0, pnLine);
776 n += skip + len;
777 }else{
778 n += len;
781 return n;
785 ** Wait for a client process to complete all its tasks
787 static void waitForClient(int iClient, int iTimeout, char *zErrPrefix){
788 sqlite3_stmt *pStmt;
789 int rc;
790 if( iClient>0 ){
791 pStmt = prepareSql(
792 "SELECT 1 FROM task"
793 " WHERE client=%d"
794 " AND client IN (SELECT id FROM client)"
795 " AND endtime IS NULL",
796 iClient);
797 }else{
798 pStmt = prepareSql(
799 "SELECT 1 FROM task"
800 " WHERE client IN (SELECT id FROM client)"
801 " AND endtime IS NULL");
803 g.iTimeout = 0;
804 while( ((rc = sqlite3_step(pStmt))==SQLITE_BUSY || rc==SQLITE_ROW)
805 && iTimeout>0
807 sqlite3_reset(pStmt);
808 sqlite3_sleep(50);
809 iTimeout -= 50;
811 sqlite3_finalize(pStmt);
812 g.iTimeout = DEFAULT_TIMEOUT;
813 if( rc!=SQLITE_DONE ){
814 if( zErrPrefix==0 ) zErrPrefix = "";
815 if( iClient>0 ){
816 errorMessage("%stimeout waiting for client %d", zErrPrefix, iClient);
817 }else{
818 errorMessage("%stimeout waiting for all clients", zErrPrefix);
823 /* Return a pointer to the tail of a filename
825 static char *filenameTail(char *z){
826 int i, j;
827 for(i=j=0; z[i]; i++) if( z[i]=='/' ) j = i+1;
828 return z+j;
832 ** Interpret zArg as a boolean value. Return either 0 or 1.
834 static int booleanValue(char *zArg){
835 int i;
836 if( zArg==0 ) return 0;
837 for(i=0; zArg[i]>='0' && zArg[i]<='9'; i++){}
838 if( i>0 && zArg[i]==0 ) return atoi(zArg);
839 if( sqlite3_stricmp(zArg, "on")==0 || sqlite3_stricmp(zArg,"yes")==0 ){
840 return 1;
842 if( sqlite3_stricmp(zArg, "off")==0 || sqlite3_stricmp(zArg,"no")==0 ){
843 return 0;
845 errorMessage("unknown boolean: [%s]", zArg);
846 return 0;
850 /* This routine exists as a convenient place to set a debugger
851 ** breakpoint.
853 static void test_breakpoint(void){ static volatile int cnt = 0; cnt++; }
855 /* Maximum number of arguments to a --command */
856 #define MX_ARG 2
859 ** Run a script.
861 static void runScript(
862 int iClient, /* The client number, or 0 for the master */
863 int taskId, /* The task ID for clients. 0 for master */
864 char *zScript, /* Text of the script */
865 char *zFilename /* File from which script was read. */
867 int lineno = 1;
868 int prevLine = 1;
869 int ii = 0;
870 int iBegin = 0;
871 int n, c, j;
872 int len;
873 int nArg;
874 String sResult;
875 char zCmd[30];
876 char zError[1000];
877 char azArg[MX_ARG][100];
879 memset(&sResult, 0, sizeof(sResult));
880 stringReset(&sResult);
881 while( (c = zScript[ii])!=0 ){
882 prevLine = lineno;
883 len = tokenLength(zScript+ii, &lineno);
884 if( isspace(c) || (c=='/' && zScript[ii+1]=='*') ){
885 ii += len;
886 continue;
888 if( c!='-' || zScript[ii+1]!='-' || !isalpha(zScript[ii+2]) ){
889 ii += len;
890 continue;
893 /* Run any prior SQL before processing the new --command */
894 if( ii>iBegin ){
895 char *zSql = sqlite3_mprintf("%.*s", ii-iBegin, zScript+iBegin);
896 evalSql(&sResult, zSql);
897 sqlite3_free(zSql);
898 iBegin = ii + len;
901 /* Parse the --command */
902 if( g.iTrace>=2 ) logMessage("%.*s", len, zScript+ii);
903 n = extractToken(zScript+ii+2, len-2, zCmd, sizeof(zCmd));
904 for(nArg=0; n<len-2 && nArg<MX_ARG; nArg++){
905 while( n<len-2 && isspace(zScript[ii+2+n]) ){ n++; }
906 if( n>=len-2 ) break;
907 n += extractToken(zScript+ii+2+n, len-2-n,
908 azArg[nArg], sizeof(azArg[nArg]));
910 for(j=nArg; j<MX_ARG; j++) azArg[j++][0] = 0;
913 ** --sleep N
915 ** Pause for N milliseconds
917 if( strcmp(zCmd, "sleep")==0 ){
918 sqlite3_sleep(atoi(azArg[0]));
919 }else
922 ** --exit N
924 ** Exit this process. If N>0 then exit without shutting down
925 ** SQLite. (In other words, simulate a crash.)
927 if( strcmp(zCmd, "exit")==0 ){
928 int rc = atoi(azArg[0]);
929 finishScript(iClient, taskId, 1);
930 if( rc==0 ) sqlite3_close(g.db);
931 exit(rc);
932 }else
935 ** --testcase NAME
937 ** Begin a new test case. Announce in the log that the test case
938 ** has begun.
940 if( strcmp(zCmd, "testcase")==0 ){
941 if( g.iTrace==1 ) logMessage("%.*s", len - 1, zScript+ii);
942 stringReset(&sResult);
943 }else
946 ** --finish
948 ** Mark the current task as having finished, even if it is not.
949 ** This can be used in conjunction with --exit to simulate a crash.
951 if( strcmp(zCmd, "finish")==0 && iClient>0 ){
952 finishScript(iClient, taskId, 1);
953 }else
956 ** --reset
958 ** Reset accumulated results back to an empty string
960 if( strcmp(zCmd, "reset")==0 ){
961 stringReset(&sResult);
962 }else
965 ** --match ANSWER...
967 ** Check to see if output matches ANSWER. Report an error if not.
969 if( strcmp(zCmd, "match")==0 ){
970 int jj;
971 char *zAns = zScript+ii;
972 for(jj=7; jj<len-1 && isspace(zAns[jj]); jj++){}
973 zAns += jj;
974 if( len-jj-1!=sResult.n || strncmp(sResult.z, zAns, len-jj-1) ){
975 errorMessage("line %d of %s:\nExpected [%.*s]\n Got [%s]",
976 prevLine, zFilename, len-jj-1, zAns, sResult.z);
978 g.nTest++;
979 stringReset(&sResult);
980 }else
983 ** --glob ANSWER...
984 ** --notglob ANSWER....
986 ** Check to see if output does or does not match the glob pattern
987 ** ANSWER.
989 if( strcmp(zCmd, "glob")==0 || strcmp(zCmd, "notglob")==0 ){
990 int jj;
991 char *zAns = zScript+ii;
992 char *zCopy;
993 int isGlob = (zCmd[0]=='g');
994 for(jj=9-3*isGlob; jj<len-1 && isspace(zAns[jj]); jj++){}
995 zAns += jj;
996 zCopy = sqlite3_mprintf("%.*s", len-jj-1, zAns);
997 if( (sqlite3_strglob(zCopy, sResult.z)==0)^isGlob ){
998 errorMessage("line %d of %s:\nExpected [%s]\n Got [%s]",
999 prevLine, zFilename, zCopy, sResult.z);
1001 sqlite3_free(zCopy);
1002 g.nTest++;
1003 stringReset(&sResult);
1004 }else
1007 ** --output
1009 ** Output the result of the previous SQL.
1011 if( strcmp(zCmd, "output")==0 ){
1012 logMessage("%s", sResult.z);
1013 }else
1016 ** --source FILENAME
1018 ** Run a subscript from a separate file.
1020 if( strcmp(zCmd, "source")==0 ){
1021 char *zNewFile, *zNewScript;
1022 char *zToDel = 0;
1023 zNewFile = azArg[0];
1024 if( zNewFile[0]!='/' ){
1025 int k;
1026 for(k=(int)strlen(zFilename)-1; k>=0 && zFilename[k]!='/'; k--){}
1027 if( k>0 ){
1028 zNewFile = zToDel = sqlite3_mprintf("%.*s/%s", k,zFilename,zNewFile);
1031 zNewScript = readFile(zNewFile);
1032 if( g.iTrace ) logMessage("begin script [%s]\n", zNewFile);
1033 runScript(0, 0, zNewScript, zNewFile);
1034 sqlite3_free(zNewScript);
1035 if( g.iTrace ) logMessage("end script [%s]\n", zNewFile);
1036 sqlite3_free(zToDel);
1037 }else
1040 ** --print MESSAGE....
1042 ** Output the remainder of the line to the log file
1044 if( strcmp(zCmd, "print")==0 ){
1045 int jj;
1046 for(jj=7; jj<len && isspace(zScript[ii+jj]); jj++){}
1047 logMessage("%.*s", len-jj, zScript+ii+jj);
1048 }else
1051 ** --if EXPR
1053 ** Skip forward to the next matching --endif or --else if EXPR is false.
1055 if( strcmp(zCmd, "if")==0 ){
1056 int jj, rc;
1057 sqlite3_stmt *pStmt;
1058 for(jj=4; jj<len && isspace(zScript[ii+jj]); jj++){}
1059 pStmt = prepareSql("SELECT %.*s", len-jj, zScript+ii+jj);
1060 rc = sqlite3_step(pStmt);
1061 if( rc!=SQLITE_ROW || sqlite3_column_int(pStmt, 0)==0 ){
1062 ii += findEndif(zScript+ii+len, 1, &lineno);
1064 sqlite3_finalize(pStmt);
1065 }else
1068 ** --else
1070 ** This command can only be encountered if currently inside an --if that
1071 ** is true. Skip forward to the next matching --endif.
1073 if( strcmp(zCmd, "else")==0 ){
1074 ii += findEndif(zScript+ii+len, 0, &lineno);
1075 }else
1078 ** --endif
1080 ** This command can only be encountered if currently inside an --if that
1081 ** is true or an --else of a false if. This is a no-op.
1083 if( strcmp(zCmd, "endif")==0 ){
1084 /* no-op */
1085 }else
1088 ** --start CLIENT
1090 ** Start up the given client.
1092 if( strcmp(zCmd, "start")==0 && iClient==0 ){
1093 int iNewClient = atoi(azArg[0]);
1094 if( iNewClient>0 ){
1095 startClient(iNewClient);
1097 }else
1100 ** --wait CLIENT TIMEOUT
1102 ** Wait until all tasks complete for the given client. If CLIENT is
1103 ** "all" then wait for all clients to complete. Wait no longer than
1104 ** TIMEOUT milliseconds (default 10,000)
1106 if( strcmp(zCmd, "wait")==0 && iClient==0 ){
1107 int iTimeout = nArg>=2 ? atoi(azArg[1]) : 10000;
1108 sqlite3_snprintf(sizeof(zError),zError,"line %d of %s\n",
1109 prevLine, zFilename);
1110 waitForClient(atoi(azArg[0]), iTimeout, zError);
1111 }else
1114 ** --task CLIENT
1115 ** <task-content-here>
1116 ** --end
1118 ** Assign work to a client. Start the client if it is not running
1119 ** already.
1121 if( strcmp(zCmd, "task")==0 && iClient==0 ){
1122 int iTarget = atoi(azArg[0]);
1123 int iEnd;
1124 char *zTask;
1125 char *zTName;
1126 iEnd = findEnd(zScript+ii+len, &lineno);
1127 if( iTarget<0 ){
1128 errorMessage("line %d of %s: bad client number: %d",
1129 prevLine, zFilename, iTarget);
1130 }else{
1131 zTask = sqlite3_mprintf("%.*s", iEnd, zScript+ii+len);
1132 if( nArg>1 ){
1133 zTName = sqlite3_mprintf("%s", azArg[1]);
1134 }else{
1135 zTName = sqlite3_mprintf("%s:%d", filenameTail(zFilename), prevLine);
1137 startClient(iTarget);
1138 runSql("INSERT INTO task(client,script,name)"
1139 " VALUES(%d,'%q',%Q)", iTarget, zTask, zTName);
1140 sqlite3_free(zTask);
1141 sqlite3_free(zTName);
1143 iEnd += tokenLength(zScript+ii+len+iEnd, &lineno);
1144 len += iEnd;
1145 iBegin = ii+len;
1146 }else
1149 ** --breakpoint
1151 ** This command calls "test_breakpoint()" which is a routine provided
1152 ** as a convenient place to set a debugger breakpoint.
1154 if( strcmp(zCmd, "breakpoint")==0 ){
1155 test_breakpoint();
1156 }else
1159 ** --show-sql-errors BOOLEAN
1161 ** Turn display of SQL errors on and off.
1163 if( strcmp(zCmd, "show-sql-errors")==0 ){
1164 g.bIgnoreSqlErrors = nArg>=1 ? !booleanValue(azArg[0]) : 1;
1165 }else
1168 /* error */{
1169 errorMessage("line %d of %s: unknown command --%s",
1170 prevLine, zFilename, zCmd);
1172 ii += len;
1174 if( iBegin<ii ){
1175 char *zSql = sqlite3_mprintf("%.*s", ii-iBegin, zScript+iBegin);
1176 runSql(zSql);
1177 sqlite3_free(zSql);
1179 stringFree(&sResult);
1183 ** Look for a command-line option. If present, return a pointer.
1184 ** Return NULL if missing.
1186 ** hasArg==0 means the option is a flag. It is either present or not.
1187 ** hasArg==1 means the option has an argument. Return a pointer to the
1188 ** argument.
1190 static char *findOption(
1191 char **azArg,
1192 int *pnArg,
1193 const char *zOption,
1194 int hasArg
1196 int i, j;
1197 char *zReturn = 0;
1198 int nArg = *pnArg;
1200 assert( hasArg==0 || hasArg==1 );
1201 for(i=0; i<nArg; i++){
1202 const char *z;
1203 if( i+hasArg >= nArg ) break;
1204 z = azArg[i];
1205 if( z[0]!='-' ) continue;
1206 z++;
1207 if( z[0]=='-' ){
1208 if( z[1]==0 ) break;
1209 z++;
1211 if( strcmp(z,zOption)==0 ){
1212 if( hasArg && i==nArg-1 ){
1213 fatalError("command-line option \"--%s\" requires an argument", z);
1215 if( hasArg ){
1216 zReturn = azArg[i+1];
1217 }else{
1218 zReturn = azArg[i];
1220 j = i+1+(hasArg!=0);
1221 while( j<nArg ) azArg[i++] = azArg[j++];
1222 *pnArg = i;
1223 return zReturn;
1226 return zReturn;
1229 /* Print a usage message for the program and exit */
1230 static void usage(const char *argv0){
1231 int i;
1232 const char *zTail = argv0;
1233 for(i=0; argv0[i]; i++){
1234 if( argv0[i]=='/' ) zTail = argv0+i+1;
1236 fprintf(stderr,"Usage: %s DATABASE ?OPTIONS? ?SCRIPT?\n", zTail);
1237 exit(1);
1240 /* Report on unrecognized arguments */
1241 static void unrecognizedArguments(
1242 const char *argv0,
1243 int nArg,
1244 char **azArg
1246 int i;
1247 fprintf(stderr,"%s: unrecognized arguments:", argv0);
1248 for(i=0; i<nArg; i++){
1249 fprintf(stderr," %s", azArg[i]);
1251 fprintf(stderr,"\n");
1252 exit(1);
1255 int main(int argc, char **argv){
1256 const char *zClient;
1257 int iClient;
1258 int n, i;
1259 int openFlags = SQLITE_OPEN_READWRITE;
1260 int rc;
1261 char *zScript;
1262 int taskId;
1263 const char *zTrace;
1264 const char *zCOption;
1266 g.argv0 = argv[0];
1267 g.iTrace = 1;
1268 if( argc<2 ) usage(argv[0]);
1269 g.zDbFile = argv[1];
1270 if( strglob("*.test", g.zDbFile) ) usage(argv[0]);
1271 if( strcmp(sqlite3_sourceid(), SQLITE_SOURCE_ID)!=0 ){
1272 fprintf(stderr, "SQLite library and header mismatch\n"
1273 "Library: %s\n"
1274 "Header: %s\n",
1275 sqlite3_sourceid(), SQLITE_SOURCE_ID);
1276 exit(1);
1278 n = argc-2;
1279 sqlite3_snprintf(sizeof(g.zName), g.zName, "%05d.mptest", GETPID());
1280 g.zVfs = findOption(argv+2, &n, "vfs", 1);
1281 zClient = findOption(argv+2, &n, "client", 1);
1282 g.zErrLog = findOption(argv+2, &n, "errlog", 1);
1283 g.zLog = findOption(argv+2, &n, "log", 1);
1284 zTrace = findOption(argv+2, &n, "trace", 1);
1285 if( zTrace ) g.iTrace = atoi(zTrace);
1286 if( findOption(argv+2, &n, "quiet", 0)!=0 ) g.iTrace = 0;
1287 g.bSqlTrace = findOption(argv+2, &n, "sqltrace", 0)!=0;
1288 g.bSync = findOption(argv+2, &n, "sync", 0)!=0;
1289 if( g.zErrLog ){
1290 g.pErrLog = fopen(g.zErrLog, "a");
1291 }else{
1292 g.pErrLog = stderr;
1294 if( g.zLog ){
1295 g.pLog = fopen(g.zLog, "a");
1296 }else{
1297 g.pLog = stdout;
1300 sqlite3_config(SQLITE_CONFIG_LOG, sqlErrorCallback, 0);
1301 if( zClient ){
1302 iClient = atoi(zClient);
1303 if( iClient<1 ) fatalError("illegal client number: %d\n", iClient);
1304 sqlite3_snprintf(sizeof(g.zName), g.zName, "%05d.client%02d",
1305 GETPID(), iClient);
1306 }else{
1307 if( g.iTrace>0 ){
1308 printf("With SQLite " SQLITE_VERSION " " SQLITE_SOURCE_ID "\n" );
1309 for(i=0; (zCOption = sqlite3_compileoption_get(i))!=0; i++){
1310 printf("-DSQLITE_%s\n", zCOption);
1312 fflush(stdout);
1314 iClient = 0;
1315 unlink(g.zDbFile);
1316 openFlags |= SQLITE_OPEN_CREATE;
1318 rc = sqlite3_open_v2(g.zDbFile, &g.db, openFlags, g.zVfs);
1319 if( rc ) fatalError("cannot open [%s]", g.zDbFile);
1320 sqlite3_enable_load_extension(g.db, 1);
1321 sqlite3_busy_handler(g.db, busyHandler, 0);
1322 sqlite3_create_function(g.db, "vfsname", 0, SQLITE_UTF8, 0,
1323 vfsNameFunc, 0, 0);
1324 sqlite3_create_function(g.db, "eval", 1, SQLITE_UTF8, 0,
1325 evalFunc, 0, 0);
1326 g.iTimeout = DEFAULT_TIMEOUT;
1327 if( g.bSqlTrace ) sqlite3_trace(g.db, sqlTraceCallback, 0);
1328 if( !g.bSync ) trySql("PRAGMA synchronous=OFF");
1329 if( iClient>0 ){
1330 if( n>0 ) unrecognizedArguments(argv[0], n, argv+2);
1331 if( g.iTrace ) logMessage("start-client");
1332 while(1){
1333 char *zTaskName = 0;
1334 rc = startScript(iClient, &zScript, &taskId, &zTaskName);
1335 if( rc==SQLITE_DONE ) break;
1336 if( g.iTrace ) logMessage("begin %s (%d)", zTaskName, taskId);
1337 runScript(iClient, taskId, zScript, zTaskName);
1338 if( g.iTrace ) logMessage("end %s (%d)", zTaskName, taskId);
1339 finishScript(iClient, taskId, 0);
1340 sqlite3_free(zTaskName);
1341 sqlite3_sleep(10);
1343 if( g.iTrace ) logMessage("end-client");
1344 }else{
1345 sqlite3_stmt *pStmt;
1346 int iTimeout;
1347 if( n==0 ){
1348 fatalError("missing script filename");
1350 if( n>1 ) unrecognizedArguments(argv[0], n, argv+2);
1351 runSql(
1352 "CREATE TABLE task(\n"
1353 " id INTEGER PRIMARY KEY,\n"
1354 " name TEXT,\n"
1355 " client INTEGER,\n"
1356 " starttime DATE,\n"
1357 " endtime DATE,\n"
1358 " script TEXT\n"
1359 ");"
1360 "CREATE INDEX task_i1 ON task(client, starttime);\n"
1361 "CREATE INDEX task_i2 ON task(client, endtime);\n"
1362 "CREATE TABLE counters(nError,nTest);\n"
1363 "INSERT INTO counters VALUES(0,0);\n"
1364 "CREATE TABLE client(id INTEGER PRIMARY KEY, wantHalt);\n"
1366 zScript = readFile(argv[2]);
1367 if( g.iTrace ) logMessage("begin script [%s]\n", argv[2]);
1368 runScript(0, 0, zScript, argv[2]);
1369 sqlite3_free(zScript);
1370 if( g.iTrace ) logMessage("end script [%s]\n", argv[2]);
1371 waitForClient(0, 2000, "during shutdown...\n");
1372 trySql("UPDATE client SET wantHalt=1");
1373 sqlite3_sleep(10);
1374 g.iTimeout = 0;
1375 iTimeout = 1000;
1376 while( ((rc = trySql("SELECT 1 FROM client"))==SQLITE_BUSY
1377 || rc==SQLITE_ROW) && iTimeout>0 ){
1378 sqlite3_sleep(10);
1379 iTimeout -= 10;
1381 sqlite3_sleep(100);
1382 pStmt = prepareSql("SELECT nError, nTest FROM counters");
1383 iTimeout = 1000;
1384 while( (rc = sqlite3_step(pStmt))==SQLITE_BUSY && iTimeout>0 ){
1385 sqlite3_sleep(10);
1386 iTimeout -= 10;
1388 if( rc==SQLITE_ROW ){
1389 g.nError += sqlite3_column_int(pStmt, 0);
1390 g.nTest += sqlite3_column_int(pStmt, 1);
1392 sqlite3_finalize(pStmt);
1394 sqlite3_close(g.db);
1395 maybeClose(g.pLog);
1396 maybeClose(g.pErrLog);
1397 if( iClient==0 ){
1398 printf("Summary: %d errors in %d tests\n", g.nError, g.nTest);
1400 return g.nError>0;