4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
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 *************************************************************************
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
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:
26 ** -DSQLITE_THREADSAFE=0
27 ** -DSQLITE_OMIT_LOAD_EXTENSION
31 ** ./mptest $database $script
33 ** where $database is the database to use for testing and $script is a
39 # define WIN32_LEAN_AND_MEAN
49 /* The suffix to append to the child command lines, if any */
51 # define GETPID (int)GetCurrentProcessId
53 # define GETPID getpid
56 /* Mark a parameter as unused to suppress compiler warnings */
57 #define UNUSED_PARAMETER(x) (void)x
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() */
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] ){
90 for(i
=0; zMsg
[i
] && zMsg
[i
]!='\n' && zMsg
[i
]!='\r'; i
++){}
91 fprintf(pOut
, "%s%.*s\n", zPrefix
, i
, zMsg
);
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
){
102 if( a
==0 ) return -1;
108 ** Return TRUE if string z[] matches glob pattern zGlob[].
109 ** Return FALSE if the pattern does not match.
113 ** '*' Matches any sequence of zero or more characters.
115 ** '?' Matches exactly one character.
117 ** [...] Matches one character from the enclosed list of
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
){
130 while( (c
= (*(zGlob
++)))!=0 ){
132 while( (c
=(*(zGlob
++))) == '*' || c
=='?' ){
133 if( c
=='?' && (*(z
++))==0 ) return 0;
138 while( *z
&& strglob(zGlob
-1,z
) ){
143 while( (c2
= (*(z
++)))!=0 ){
146 if( c2
==0 ) return 0;
148 if( strglob(zGlob
,z
) ) return 1;
152 if( (*(z
++))==0 ) return 0;
165 if( c
==']' ) seen
= 1;
168 while( c2
&& c2
!=']' ){
169 if( c2
=='-' && zGlob
[0]!=']' && zGlob
[0]!=0 && prior_c
>0 ){
171 if( c
>=prior_c
&& c
<=c2
) seen
= 1;
181 if( c2
==0 || (seen
^ invert
)==0 ) return 0;
183 if( (z
[0]=='-' || z
[0]=='+') && isdigit(z
[1]) ) z
++;
184 if( !isdigit(z
[0]) ) return 0;
186 while( isdigit(z
[0]) ){ z
++; }
188 if( c
!=(*(z
++)) ) return 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
, ...){
208 va_start(ap
, zFormat
);
209 zMsg
= sqlite3_vmprintf(zFormat
, ap
);
211 sqlite3_snprintf(sizeof(zPrefix
), zPrefix
, "%s:ERROR: ", g
.zName
);
213 printWithPrefix(g
.pLog
, zPrefix
, zMsg
);
216 if( g
.pErrLog
&& safe_strcmp(g
.zErrLog
,g
.zLog
) ){
217 printWithPrefix(g
.pErrLog
, zPrefix
, zMsg
);
224 /* Forward declaration */
225 static int trySql(const char*, ...);
228 ** Print an error message and then quit.
230 static void fatalError(const char *zFormat
, ...){
234 va_start(ap
, zFormat
);
235 zMsg
= sqlite3_vmprintf(zFormat
, ap
);
237 sqlite3_snprintf(sizeof(zPrefix
), zPrefix
, "%s:FATAL: ", g
.zName
);
239 printWithPrefix(g
.pLog
, zPrefix
, zMsg
);
243 if( g
.pErrLog
&& safe_strcmp(g
.zErrLog
,g
.zLog
) ){
244 printWithPrefix(g
.pErrLog
, zPrefix
, zMsg
);
246 maybeClose(g
.pErrLog
);
252 while( trySql("UPDATE client SET wantHalt=1;")==SQLITE_BUSY
263 ** Print a log message
265 static void logMessage(const char *zFormat
, ...){
269 va_start(ap
, zFormat
);
270 zMsg
= sqlite3_vmprintf(zFormat
, ap
);
272 sqlite3_snprintf(sizeof(zPrefix
), zPrefix
, "%s: ", g
.zName
);
274 printWithPrefix(g
.pLog
, zPrefix
, 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
--; }
290 ** Auxiliary SQL function to return the name of the VFS
292 static void vfsNameFunc(
293 sqlite3_context
*context
,
297 sqlite3
*db
= sqlite3_context_db_handle(context
);
299 UNUSED_PARAMETER(argc
);
300 UNUSED_PARAMETER(argv
);
301 sqlite3_file_control(db
, "main", SQLITE_FCNTL_VFSNAME
, &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
);
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
);
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
, ...){
350 sqlite3_stmt
*pStmt
= 0;
351 va_start(ap
, zFormat
);
352 zSql
= sqlite3_vmprintf(zFormat
, ap
);
354 rc
= sqlite3_prepare_v2(g
.db
, zSql
, -1, &pStmt
, 0);
356 sqlite3_finalize(pStmt
);
357 fatalError("%s\n%s\n", sqlite3_errmsg(g
.db
), zSql
);
364 ** Run arbitrary SQL. Issue a fatal error on failure.
366 static void runSql(const char *zFormat
, ...){
370 va_start(ap
, zFormat
);
371 zSql
= sqlite3_vmprintf(zFormat
, ap
);
373 rc
= sqlite3_exec(g
.db
, zSql
, 0, 0, 0);
375 fatalError("%s\n%s\n", sqlite3_errmsg(g
.db
), zSql
);
381 ** Try to run arbitrary SQL. Return success code.
383 static int trySql(const char *zFormat
, ...){
387 va_start(ap
, zFormat
);
388 zSql
= sqlite3_vmprintf(zFormat
, ap
);
390 rc
= sqlite3_exec(g
.db
, zSql
, 0, 0, 0);
395 /* Structure for holding an arbitrary length string
397 typedef struct String String
;
399 char *z
; /* the string */
400 int n
; /* Slots of z[] used */
401 int nAlloc
; /* Slots of z[] allocated */
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");
420 memcpy(p
->z
+p
->n
, z
, n
);
425 /* Reset a string to an empty string */
426 static void stringReset(String
*p
){
427 if( p
->z
==0 ) stringAppend(p
, " ", 1);
432 /* Append a new token onto the end of the string */
433 static void stringAppendTerm(String
*p
, const char *z
){
435 if( p
->n
) stringAppend(p
, " ", 1);
437 stringAppend(p
, "nil", 3);
440 for(i
=0; z
[i
] && !isspace(z
[i
]); i
++){}
441 if( i
>0 && z
[i
]==0 ){
442 stringAppend(p
, z
, i
);
445 stringAppend(p
, "'", 1);
447 for(i
=0; z
[i
] && z
[i
]!='\''; i
++){}
449 stringAppend(p
, z
, i
+1);
450 stringAppend(p
, "'", 1);
453 stringAppend(p
, z
, i
);
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
;
466 UNUSED_PARAMETER(azCol
);
467 for(i
=0; i
<argc
; i
++) stringAppendTerm(p
, argv
[i
]);
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
, ...){
480 va_start(ap
, zFormat
);
481 zSql
= sqlite3_vmprintf(zFormat
, ap
);
483 assert( g
.iTimeout
>0 );
484 rc
= sqlite3_exec(g
.db
, zSql
, evalCallback
, p
, &zErrMsg
);
488 sqlite3_snprintf(sizeof(zErr
), zErr
, "error(%d)", rc
);
489 stringAppendTerm(p
, zErr
);
491 stringAppendTerm(p
, zErrMsg
);
492 sqlite3_free(zErrMsg
);
499 ** Auxiliary SQL function to recursively evaluate SQL.
501 static void evalFunc(
502 sqlite3_context
*context
,
506 sqlite3
*db
= sqlite3_context_db_handle(context
);
507 const char *zSql
= (const char*)sqlite3_value_text(argv
[0]);
511 UNUSED_PARAMETER(argc
);
512 memset(&res
, 0, sizeof(res
));
513 rc
= sqlite3_exec(db
, zSql
, evalCallback
, &res
, &zErrMsg
);
515 sqlite3_result_error(context
, zErrMsg
, -1);
516 sqlite3_free(zErrMsg
);
518 sqlite3_result_error_code(context
, rc
);
520 sqlite3_result_text(context
, res
.z
, -1, SQLITE_TRANSIENT
);
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;
544 rc
= trySql("BEGIN IMMEDIATE");
545 if( rc
==SQLITE_BUSY
){
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",
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;");
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
);
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;");
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
);
595 while( trySql("COMMIT")==SQLITE_BUSY
){
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
){
614 " SET endtime=strftime('%%Y-%%m-%%d %%H:%%M:%%f','now')"
615 " WHERE id=%d;", taskId
);
617 runSql("DELETE FROM client WHERE id=%d", iClient
);
623 ** Start up a client process for iClient, if it is not already
624 ** running. If the client is already running, then this routine
627 static void startClient(int iClient
){
628 runSql("INSERT OR IGNORE INTO client VALUES(%d,0)", iClient
);
629 if( sqlite3_changes(g
.db
) ){
632 zSys
= sqlite3_mprintf("%s \"%s\" --client %d --trace %d",
633 g
.argv0
, g
.zDbFile
, iClient
, g
.iTrace
);
635 zSys
= sqlite3_mprintf("%z --sqltrace", zSys
);
638 zSys
= sqlite3_mprintf("%z --sync", zSys
);
641 zSys
= sqlite3_mprintf("%z --vfs \"%s\"", zSys
, g
.zVfs
);
643 if( g
.iTrace
>=2 ) logMessage("system('%q')", zSys
);
645 zSys
= sqlite3_mprintf("%z &", zSys
);
647 if( rc
) errorMessage("system() fails with error code %d", rc
);
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
);
658 CloseHandle(processInfo
.hThread
);
659 CloseHandle(processInfo
.hProcess
);
661 errorMessage("CreateProcessA() fails with error code %lu",
671 ** Read the entire content of a file into memory
673 static char *readFile(const char *zFilename
){
674 FILE *in
= fopen(zFilename
, "rb");
678 fatalError("cannot open \"%s\" for reading", zFilename
);
680 fseek(in
, 0, SEEK_END
);
683 z
= sqlite3_malloc( sz
+1 );
684 sz
= (long)fread(z
, 1, sz
, in
);
691 ** Return the length of the next token.
693 static int tokenLength(const char *z
, int *pnLine
){
695 if( isspace(z
[0]) || (z
[0]=='/' && z
[1]=='*') ){
702 while( (c
= z
[n
++])!=0 ){
703 if( c
=='\n' ) (*pnLine
)++;
704 if( isspace(c
) ) continue;
705 if( inC
&& c
=='*' && z
[n
]=='/' ){
708 }else if( !inC
&& c
=='/' && z
[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]=='\'' ){
722 if( z
[n
]=='\n' ) (*pnLine
)++;
725 if( z
[n
+1]!=delim
) break;
730 for(n
=1; (c
= z
[n
])!=0 && !isspace(c
) && c
!='"' && c
!='\'' && c
!=';'; n
++){}
736 ** Copy a single token into a string buffer.
738 static int extractToken(const char *zIn
, int nIn
, char *zOut
, int nOut
){
744 for(i
=0; i
<nIn
&& i
<nOut
-1 && !isspace(zIn
[i
]); i
++){ zOut
[i
] = zIn
[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
){
754 while( z
[n
] && (strncmp(z
+n
,"--end",5) || !isspace(z
[n
+5])) ){
755 n
+= tokenLength(z
+n
, pnLine
);
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
765 static int findEndif(const char *z
, int stopAtElse
, int *pnLine
){
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]))
774 if( strncmp(z
+n
,"--if",4)==0 && isspace(z
[n
+4]) ){
775 int skip
= findEndif(z
+n
+len
, 0, pnLine
);
785 ** Wait for a client process to complete all its tasks
787 static void waitForClient(int iClient
, int iTimeout
, char *zErrPrefix
){
794 " AND client IN (SELECT id FROM client)"
795 " AND endtime IS NULL",
800 " WHERE client IN (SELECT id FROM client)"
801 " AND endtime IS NULL");
804 while( ((rc
= sqlite3_step(pStmt
))==SQLITE_BUSY
|| rc
==SQLITE_ROW
)
807 sqlite3_reset(pStmt
);
811 sqlite3_finalize(pStmt
);
812 g
.iTimeout
= DEFAULT_TIMEOUT
;
813 if( rc
!=SQLITE_DONE
){
814 if( zErrPrefix
==0 ) zErrPrefix
= "";
816 errorMessage("%stimeout waiting for client %d", zErrPrefix
, iClient
);
818 errorMessage("%stimeout waiting for all clients", zErrPrefix
);
823 /* Return a pointer to the tail of a filename
825 static char *filenameTail(char *z
){
827 for(i
=j
=0; z
[i
]; i
++) if( z
[i
]=='/' ) j
= i
+1;
832 ** Interpret zArg as a boolean value. Return either 0 or 1.
834 static int booleanValue(char *zArg
){
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 ){
842 if( sqlite3_stricmp(zArg
, "off")==0 || sqlite3_stricmp(zArg
,"no")==0 ){
845 errorMessage("unknown boolean: [%s]", zArg
);
850 /* This routine exists as a convenient place to set a debugger
853 static void test_breakpoint(void){ static volatile int cnt
= 0; cnt
++; }
855 /* Maximum number of arguments to a --command */
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. */
877 char azArg
[MX_ARG
][100];
879 memset(&sResult
, 0, sizeof(sResult
));
880 stringReset(&sResult
);
881 while( (c
= zScript
[ii
])!=0 ){
883 len
= tokenLength(zScript
+ii
, &lineno
);
884 if( isspace(c
) || (c
=='/' && zScript
[ii
+1]=='*') ){
888 if( c
!='-' || zScript
[ii
+1]!='-' || !isalpha(zScript
[ii
+2]) ){
893 /* Run any prior SQL before processing the new --command */
895 char *zSql
= sqlite3_mprintf("%.*s", ii
-iBegin
, zScript
+iBegin
);
896 evalSql(&sResult
, zSql
);
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;
915 ** Pause for N milliseconds
917 if( strcmp(zCmd
, "sleep")==0 ){
918 sqlite3_sleep(atoi(azArg
[0]));
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
);
937 ** Begin a new test case. Announce in the log that the test case
940 if( strcmp(zCmd
, "testcase")==0 ){
941 if( g
.iTrace
==1 ) logMessage("%.*s", len
- 1, zScript
+ii
);
942 stringReset(&sResult
);
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);
958 ** Reset accumulated results back to an empty string
960 if( strcmp(zCmd
, "reset")==0 ){
961 stringReset(&sResult
);
967 ** Check to see if output matches ANSWER. Report an error if not.
969 if( strcmp(zCmd
, "match")==0 ){
971 char *zAns
= zScript
+ii
;
972 for(jj
=7; jj
<len
-1 && isspace(zAns
[jj
]); 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
);
979 stringReset(&sResult
);
984 ** --notglob ANSWER....
986 ** Check to see if output does or does not match the glob pattern
989 if( strcmp(zCmd
, "glob")==0 || strcmp(zCmd
, "notglob")==0 ){
991 char *zAns
= zScript
+ii
;
993 int isGlob
= (zCmd
[0]=='g');
994 for(jj
=9-3*isGlob
; jj
<len
-1 && isspace(zAns
[jj
]); 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
);
1003 stringReset(&sResult
);
1009 ** Output the result of the previous SQL.
1011 if( strcmp(zCmd
, "output")==0 ){
1012 logMessage("%s", sResult
.z
);
1016 ** --source FILENAME
1018 ** Run a subscript from a separate file.
1020 if( strcmp(zCmd
, "source")==0 ){
1021 char *zNewFile
, *zNewScript
;
1023 zNewFile
= azArg
[0];
1024 if( zNewFile
[0]!='/' ){
1026 for(k
=(int)strlen(zFilename
)-1; k
>=0 && zFilename
[k
]!='/'; k
--){}
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
);
1040 ** --print MESSAGE....
1042 ** Output the remainder of the line to the log file
1044 if( strcmp(zCmd
, "print")==0 ){
1046 for(jj
=7; jj
<len
&& isspace(zScript
[ii
+jj
]); jj
++){}
1047 logMessage("%.*s", len
-jj
, zScript
+ii
+jj
);
1053 ** Skip forward to the next matching --endif or --else if EXPR is false.
1055 if( strcmp(zCmd
, "if")==0 ){
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
);
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
);
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 ){
1090 ** Start up the given client.
1092 if( strcmp(zCmd
, "start")==0 && iClient
==0 ){
1093 int iNewClient
= atoi(azArg
[0]);
1095 startClient(iNewClient
);
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
);
1115 ** <task-content-here>
1118 ** Assign work to a client. Start the client if it is not running
1121 if( strcmp(zCmd
, "task")==0 && iClient
==0 ){
1122 int iTarget
= atoi(azArg
[0]);
1126 iEnd
= findEnd(zScript
+ii
+len
, &lineno
);
1128 errorMessage("line %d of %s: bad client number: %d",
1129 prevLine
, zFilename
, iTarget
);
1131 zTask
= sqlite3_mprintf("%.*s", iEnd
, zScript
+ii
+len
);
1133 zTName
= sqlite3_mprintf("%s", azArg
[1]);
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
);
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 ){
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;
1169 errorMessage("line %d of %s: unknown command --%s",
1170 prevLine
, zFilename
, zCmd
);
1175 char *zSql
= sqlite3_mprintf("%.*s", ii
-iBegin
, zScript
+iBegin
);
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
1190 static char *findOption(
1193 const char *zOption
,
1200 assert( hasArg
==0 || hasArg
==1 );
1201 for(i
=0; i
<nArg
; i
++){
1203 if( i
+hasArg
>= nArg
) break;
1205 if( z
[0]!='-' ) continue;
1208 if( z
[1]==0 ) break;
1211 if( strcmp(z
,zOption
)==0 ){
1212 if( hasArg
&& i
==nArg
-1 ){
1213 fatalError("command-line option \"--%s\" requires an argument", z
);
1216 zReturn
= azArg
[i
+1];
1220 j
= i
+1+(hasArg
!=0);
1221 while( j
<nArg
) azArg
[i
++] = azArg
[j
++];
1229 /* Print a usage message for the program and exit */
1230 static void usage(const char *argv0
){
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
);
1240 /* Report on unrecognized arguments */
1241 static void unrecognizedArguments(
1247 fprintf(stderr
,"%s: unrecognized arguments:", argv0
);
1248 for(i
=0; i
<nArg
; i
++){
1249 fprintf(stderr
," %s", azArg
[i
]);
1251 fprintf(stderr
,"\n");
1255 int main(int argc
, char **argv
){
1256 const char *zClient
;
1259 int openFlags
= SQLITE_OPEN_READWRITE
;
1264 const char *zCOption
;
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"
1275 sqlite3_sourceid(), SQLITE_SOURCE_ID
);
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;
1290 g
.pErrLog
= fopen(g
.zErrLog
, "a");
1295 g
.pLog
= fopen(g
.zLog
, "a");
1300 sqlite3_config(SQLITE_CONFIG_LOG
, sqlErrorCallback
, 0);
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",
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
);
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,
1324 sqlite3_create_function(g
.db
, "eval", 1, SQLITE_UTF8
, 0,
1326 g
.iTimeout
= DEFAULT_TIMEOUT
;
1327 if( g
.bSqlTrace
) sqlite3_trace(g
.db
, sqlTraceCallback
, 0);
1328 if( !g
.bSync
) trySql("PRAGMA synchronous=OFF");
1330 if( n
>0 ) unrecognizedArguments(argv
[0], n
, argv
+2);
1331 if( g
.iTrace
) logMessage("start-client");
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
);
1343 if( g
.iTrace
) logMessage("end-client");
1345 sqlite3_stmt
*pStmt
;
1348 fatalError("missing script filename");
1350 if( n
>1 ) unrecognizedArguments(argv
[0], n
, argv
+2);
1352 "CREATE TABLE task(\n"
1353 " id INTEGER PRIMARY KEY,\n"
1355 " client INTEGER,\n"
1356 " starttime DATE,\n"
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");
1376 while( ((rc
= trySql("SELECT 1 FROM client"))==SQLITE_BUSY
1377 || rc
==SQLITE_ROW
) && iTimeout
>0 ){
1382 pStmt
= prepareSql("SELECT nError, nTest FROM counters");
1384 while( (rc
= sqlite3_step(pStmt
))==SQLITE_BUSY
&& iTimeout
>0 ){
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
);
1396 maybeClose(g
.pErrLog
);
1398 printf("Summary: %d errors in %d tests\n", g
.nError
, g
.nTest
);