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
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 */
55 # define GETPID (int)GetCurrentProcessId
57 # define GETPID getpid
60 /* The directory separator character(s) */
62 # define isDirSep(c) (((c) == '/') || ((c) == '\\'))
64 # define isDirSep(c) ((c) == '/')
67 /* Mark a parameter as unused to suppress compiler warnings */
68 #define UNUSED_PARAMETER(x) (void)x
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() */
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] ){
101 for(i
=0; zMsg
[i
] && zMsg
[i
]!='\n' && zMsg
[i
]!='\r'; i
++){}
102 fprintf(pOut
, "%s%.*s\n", zPrefix
, i
, zMsg
);
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
){
113 if( a
==0 ) return -1;
119 ** Return TRUE if string z[] matches glob pattern zGlob[].
120 ** Return FALSE if the pattern does not match.
124 ** '*' Matches any sequence of zero or more characters.
126 ** '?' Matches exactly one character.
128 ** [...] Matches one character from the enclosed list of
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
){
141 while( (c
= (*(zGlob
++)))!=0 ){
143 while( (c
=(*(zGlob
++))) == '*' || c
=='?' ){
144 if( c
=='?' && (*(z
++))==0 ) return 0;
149 while( *z
&& strglob(zGlob
-1,z
) ){
154 while( (c2
= (*(z
++)))!=0 ){
157 if( c2
==0 ) return 0;
159 if( strglob(zGlob
,z
) ) return 1;
163 if( (*(z
++))==0 ) return 0;
176 if( c
==']' ) seen
= 1;
179 while( c2
&& c2
!=']' ){
180 if( c2
=='-' && zGlob
[0]!=']' && zGlob
[0]!=0 && prior_c
>0 ){
182 if( c
>=prior_c
&& c
<=c2
) seen
= 1;
192 if( c2
==0 || (seen
^ invert
)==0 ) return 0;
194 if( (z
[0]=='-' || z
[0]=='+') && ISDIGIT(z
[1]) ) z
++;
195 if( !ISDIGIT(z
[0]) ) return 0;
197 while( ISDIGIT(z
[0]) ){ z
++; }
199 if( c
!=(*(z
++)) ) return 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
, ...){
219 va_start(ap
, zFormat
);
220 zMsg
= sqlite3_vmprintf(zFormat
, ap
);
222 sqlite3_snprintf(sizeof(zPrefix
), zPrefix
, "%s:ERROR: ", g
.zName
);
224 printWithPrefix(g
.pLog
, zPrefix
, zMsg
);
227 if( g
.pErrLog
&& safe_strcmp(g
.zErrLog
,g
.zLog
) ){
228 printWithPrefix(g
.pErrLog
, zPrefix
, zMsg
);
235 /* Forward declaration */
236 static int trySql(const char*, ...);
239 ** Print an error message and then quit.
241 static void fatalError(const char *zFormat
, ...){
245 va_start(ap
, zFormat
);
246 zMsg
= sqlite3_vmprintf(zFormat
, ap
);
248 sqlite3_snprintf(sizeof(zPrefix
), zPrefix
, "%s:FATAL: ", g
.zName
);
250 printWithPrefix(g
.pLog
, zPrefix
, zMsg
);
254 if( g
.pErrLog
&& safe_strcmp(g
.zErrLog
,g
.zLog
) ){
255 printWithPrefix(g
.pErrLog
, zPrefix
, zMsg
);
257 maybeClose(g
.pErrLog
);
263 while( trySql("UPDATE client SET wantHalt=1;")==SQLITE_BUSY
274 ** Print a log message
276 static void logMessage(const char *zFormat
, ...){
280 va_start(ap
, zFormat
);
281 zMsg
= sqlite3_vmprintf(zFormat
, ap
);
283 sqlite3_snprintf(sizeof(zPrefix
), zPrefix
, "%s: ", g
.zName
);
285 printWithPrefix(g
.pLog
, zPrefix
, 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
--; }
301 ** Auxiliary SQL function to return the name of the VFS
303 static void vfsNameFunc(
304 sqlite3_context
*context
,
308 sqlite3
*db
= sqlite3_context_db_handle(context
);
310 UNUSED_PARAMETER(argc
);
311 UNUSED_PARAMETER(argv
);
312 sqlite3_file_control(db
, "main", SQLITE_FCNTL_VFSNAME
, &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
);
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
);
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
, ...){
361 sqlite3_stmt
*pStmt
= 0;
362 va_start(ap
, zFormat
);
363 zSql
= sqlite3_vmprintf(zFormat
, ap
);
365 rc
= sqlite3_prepare_v2(g
.db
, zSql
, -1, &pStmt
, 0);
367 sqlite3_finalize(pStmt
);
368 fatalError("%s\n%s\n", sqlite3_errmsg(g
.db
), zSql
);
375 ** Run arbitrary SQL. Issue a fatal error on failure.
377 static void runSql(const char *zFormat
, ...){
381 va_start(ap
, zFormat
);
382 zSql
= sqlite3_vmprintf(zFormat
, ap
);
384 rc
= sqlite3_exec(g
.db
, zSql
, 0, 0, 0);
386 fatalError("%s\n%s\n", sqlite3_errmsg(g
.db
), zSql
);
392 ** Try to run arbitrary SQL. Return success code.
394 static int trySql(const char *zFormat
, ...){
398 va_start(ap
, zFormat
);
399 zSql
= sqlite3_vmprintf(zFormat
, ap
);
401 rc
= sqlite3_exec(g
.db
, zSql
, 0, 0, 0);
406 /* Structure for holding an arbitrary length string
408 typedef struct String String
;
410 char *z
; /* the string */
411 int n
; /* Slots of z[] used */
412 int nAlloc
; /* Slots of z[] allocated */
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");
431 memcpy(p
->z
+p
->n
, z
, n
);
436 /* Reset a string to an empty string */
437 static void stringReset(String
*p
){
438 if( p
->z
==0 ) stringAppend(p
, " ", 1);
443 /* Append a new token onto the end of the string */
444 static void stringAppendTerm(String
*p
, const char *z
){
446 if( p
->n
) stringAppend(p
, " ", 1);
448 stringAppend(p
, "nil", 3);
451 for(i
=0; z
[i
] && !ISSPACE(z
[i
]); i
++){}
452 if( i
>0 && z
[i
]==0 ){
453 stringAppend(p
, z
, i
);
456 stringAppend(p
, "'", 1);
458 for(i
=0; z
[i
] && z
[i
]!='\''; i
++){}
460 stringAppend(p
, z
, i
+1);
461 stringAppend(p
, "'", 1);
464 stringAppend(p
, z
, i
);
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
;
477 UNUSED_PARAMETER(azCol
);
478 for(i
=0; i
<argc
; i
++) stringAppendTerm(p
, argv
[i
]);
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
, ...){
491 va_start(ap
, zFormat
);
492 zSql
= sqlite3_vmprintf(zFormat
, ap
);
494 assert( g
.iTimeout
>0 );
495 rc
= sqlite3_exec(g
.db
, zSql
, evalCallback
, p
, &zErrMsg
);
499 sqlite3_snprintf(sizeof(zErr
), zErr
, "error(%d)", rc
);
500 stringAppendTerm(p
, zErr
);
502 stringAppendTerm(p
, zErrMsg
);
503 sqlite3_free(zErrMsg
);
510 ** Auxiliary SQL function to recursively evaluate SQL.
512 static void evalFunc(
513 sqlite3_context
*context
,
517 sqlite3
*db
= sqlite3_context_db_handle(context
);
518 const char *zSql
= (const char*)sqlite3_value_text(argv
[0]);
522 UNUSED_PARAMETER(argc
);
523 memset(&res
, 0, sizeof(res
));
524 rc
= sqlite3_exec(db
, zSql
, evalCallback
, &res
, &zErrMsg
);
526 sqlite3_result_error(context
, zErrMsg
, -1);
527 sqlite3_free(zErrMsg
);
529 sqlite3_result_error_code(context
, rc
);
531 sqlite3_result_text(context
, res
.z
, -1, SQLITE_TRANSIENT
);
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;
555 rc
= trySql("BEGIN IMMEDIATE");
556 if( rc
==SQLITE_BUSY
){
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",
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;");
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
);
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;");
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
);
606 while( trySql("COMMIT")==SQLITE_BUSY
){
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
){
625 " SET endtime=strftime('%%Y-%%m-%%d %%H:%%M:%%f','now')"
626 " WHERE id=%d;", taskId
);
628 runSql("DELETE FROM client WHERE id=%d", iClient
);
634 ** Start up a client process for iClient, if it is not already
635 ** running. If the client is already running, then this routine
638 static void startClient(int iClient
){
639 runSql("INSERT OR IGNORE INTO client VALUES(%d,0)", iClient
);
640 if( sqlite3_changes(g
.db
) ){
643 zSys
= sqlite3_mprintf("%s \"%s\" --client %d --trace %d",
644 g
.argv0
, g
.zDbFile
, iClient
, g
.iTrace
);
646 zSys
= sqlite3_mprintf("%z --sqltrace", zSys
);
649 zSys
= sqlite3_mprintf("%z --sync", zSys
);
652 zSys
= sqlite3_mprintf("%z --vfs \"%s\"", zSys
, g
.zVfs
);
654 if( g
.iTrace
>=2 ) logMessage("system('%q')", zSys
);
656 zSys
= sqlite3_mprintf("%z &", zSys
);
658 if( rc
) errorMessage("system() fails with error code %d", rc
);
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
);
669 CloseHandle(processInfo
.hThread
);
670 CloseHandle(processInfo
.hProcess
);
672 errorMessage("CreateProcessA() fails with error code %lu",
682 ** Read the entire content of a file into memory
684 static char *readFile(const char *zFilename
){
685 FILE *in
= fopen(zFilename
, "rb");
689 fatalError("cannot open \"%s\" for reading", zFilename
);
691 fseek(in
, 0, SEEK_END
);
694 z
= sqlite3_malloc( sz
+1 );
695 sz
= (long)fread(z
, 1, sz
, in
);
702 ** Return the length of the next token.
704 static int tokenLength(const char *z
, int *pnLine
){
706 if( ISSPACE(z
[0]) || (z
[0]=='/' && z
[1]=='*') ){
713 while( (c
= z
[n
++])!=0 ){
714 if( c
=='\n' ) (*pnLine
)++;
715 if( ISSPACE(c
) ) continue;
716 if( inC
&& c
=='*' && z
[n
]=='/' ){
719 }else if( !inC
&& c
=='/' && z
[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]=='\'' ){
733 if( z
[n
]=='\n' ) (*pnLine
)++;
736 if( z
[n
+1]!=delim
) break;
741 for(n
=1; (c
= z
[n
])!=0 && !ISSPACE(c
) && c
!='"' && c
!='\'' && c
!=';'; n
++){}
747 ** Copy a single token into a string buffer.
749 static int extractToken(const char *zIn
, int nIn
, char *zOut
, int nOut
){
755 for(i
=0; i
<nIn
&& i
<nOut
-1 && !ISSPACE(zIn
[i
]); i
++){ zOut
[i
] = zIn
[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
){
765 while( z
[n
] && (strncmp(z
+n
,"--end",5) || !ISSPACE(z
[n
+5])) ){
766 n
+= tokenLength(z
+n
, pnLine
);
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
776 static int findEndif(const char *z
, int stopAtElse
, int *pnLine
){
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]))
785 if( strncmp(z
+n
,"--if",4)==0 && ISSPACE(z
[n
+4]) ){
786 int skip
= findEndif(z
+n
+len
, 0, pnLine
);
796 ** Wait for a client process to complete all its tasks
798 static void waitForClient(int iClient
, int iTimeout
, char *zErrPrefix
){
805 " AND client IN (SELECT id FROM client)"
806 " AND endtime IS NULL",
811 " WHERE client IN (SELECT id FROM client)"
812 " AND endtime IS NULL");
815 while( ((rc
= sqlite3_step(pStmt
))==SQLITE_BUSY
|| rc
==SQLITE_ROW
)
818 sqlite3_reset(pStmt
);
822 sqlite3_finalize(pStmt
);
823 g
.iTimeout
= DEFAULT_TIMEOUT
;
824 if( rc
!=SQLITE_DONE
){
825 if( zErrPrefix
==0 ) zErrPrefix
= "";
827 errorMessage("%stimeout waiting for client %d", zErrPrefix
, iClient
);
829 errorMessage("%stimeout waiting for all clients", zErrPrefix
);
834 /* Return a pointer to the tail of a filename
836 static char *filenameTail(char *z
){
838 for(i
=j
=0; z
[i
]; i
++) if( isDirSep(z
[i
]) ) j
= i
+1;
843 ** Interpret zArg as a boolean value. Return either 0 or 1.
845 static int booleanValue(char *zArg
){
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 ){
853 if( sqlite3_stricmp(zArg
, "off")==0 || sqlite3_stricmp(zArg
,"no")==0 ){
856 errorMessage("unknown boolean: [%s]", zArg
);
861 /* This routine exists as a convenient place to set a debugger
864 static void test_breakpoint(void){ static volatile int cnt
= 0; cnt
++; }
866 /* Maximum number of arguments to a --command */
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. */
888 char azArg
[MX_ARG
][100];
890 memset(&sResult
, 0, sizeof(sResult
));
891 stringReset(&sResult
);
892 while( (c
= zScript
[ii
])!=0 ){
894 len
= tokenLength(zScript
+ii
, &lineno
);
895 if( ISSPACE(c
) || (c
=='/' && zScript
[ii
+1]=='*') ){
899 if( c
!='-' || zScript
[ii
+1]!='-' || !isalpha(zScript
[ii
+2]) ){
904 /* Run any prior SQL before processing the new --command */
906 char *zSql
= sqlite3_mprintf("%.*s", ii
-iBegin
, zScript
+iBegin
);
907 evalSql(&sResult
, zSql
);
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;
926 ** Pause for N milliseconds
928 if( strcmp(zCmd
, "sleep")==0 ){
929 sqlite3_sleep(atoi(azArg
[0]));
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
);
948 ** Begin a new test case. Announce in the log that the test case
951 if( strcmp(zCmd
, "testcase")==0 ){
952 if( g
.iTrace
==1 ) logMessage("%.*s", len
- 1, zScript
+ii
);
953 stringReset(&sResult
);
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);
969 ** Reset accumulated results back to an empty string
971 if( strcmp(zCmd
, "reset")==0 ){
972 stringReset(&sResult
);
978 ** Check to see if output matches ANSWER. Report an error if not.
980 if( strcmp(zCmd
, "match")==0 ){
982 char *zAns
= zScript
+ii
;
983 for(jj
=7; jj
<len
-1 && ISSPACE(zAns
[jj
]); 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
);
990 stringReset(&sResult
);
995 ** --notglob ANSWER....
997 ** Check to see if output does or does not match the glob pattern
1000 if( strcmp(zCmd
, "glob")==0 || strcmp(zCmd
, "notglob")==0 ){
1002 char *zAns
= zScript
+ii
;
1004 int isGlob
= (zCmd
[0]=='g');
1005 for(jj
=9-3*isGlob
; jj
<len
-1 && ISSPACE(zAns
[jj
]); 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
);
1014 stringReset(&sResult
);
1020 ** Output the result of the previous SQL.
1022 if( strcmp(zCmd
, "output")==0 ){
1023 logMessage("%s", sResult
.z
);
1027 ** --source FILENAME
1029 ** Run a subscript from a separate file.
1031 if( strcmp(zCmd
, "source")==0 ){
1032 char *zNewFile
, *zNewScript
;
1034 zNewFile
= azArg
[0];
1035 if( !isDirSep(zNewFile
[0]) ){
1037 for(k
=(int)strlen(zFilename
)-1; k
>=0 && !isDirSep(zFilename
[k
]); k
--){}
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
);
1051 ** --print MESSAGE....
1053 ** Output the remainder of the line to the log file
1055 if( strcmp(zCmd
, "print")==0 ){
1057 for(jj
=7; jj
<len
&& ISSPACE(zScript
[ii
+jj
]); jj
++){}
1058 logMessage("%.*s", len
-jj
, zScript
+ii
+jj
);
1064 ** Skip forward to the next matching --endif or --else if EXPR is false.
1066 if( strcmp(zCmd
, "if")==0 ){
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
);
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
);
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 ){
1101 ** Start up the given client.
1103 if( strcmp(zCmd
, "start")==0 && iClient
==0 ){
1104 int iNewClient
= atoi(azArg
[0]);
1106 startClient(iNewClient
);
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
);
1126 ** <task-content-here>
1129 ** Assign work to a client. Start the client if it is not running
1132 if( strcmp(zCmd
, "task")==0 && iClient
==0 ){
1133 int iTarget
= atoi(azArg
[0]);
1137 iEnd
= findEnd(zScript
+ii
+len
, &lineno
);
1139 errorMessage("line %d of %s: bad client number: %d",
1140 prevLine
, zFilename
, iTarget
);
1142 zTask
= sqlite3_mprintf("%.*s", iEnd
, zScript
+ii
+len
);
1144 zTName
= sqlite3_mprintf("%s", azArg
[1]);
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
);
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 ){
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;
1180 errorMessage("line %d of %s: unknown command --%s",
1181 prevLine
, zFilename
, zCmd
);
1186 char *zSql
= sqlite3_mprintf("%.*s", ii
-iBegin
, zScript
+iBegin
);
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
1201 static char *findOption(
1204 const char *zOption
,
1211 assert( hasArg
==0 || hasArg
==1 );
1212 for(i
=0; i
<nArg
; i
++){
1214 if( i
+hasArg
>= nArg
) break;
1216 if( z
[0]!='-' ) continue;
1219 if( z
[1]==0 ) break;
1222 if( strcmp(z
,zOption
)==0 ){
1223 if( hasArg
&& i
==nArg
-1 ){
1224 fatalError("command-line option \"--%s\" requires an argument", z
);
1227 zReturn
= azArg
[i
+1];
1231 j
= i
+1+(hasArg
!=0);
1232 while( j
<nArg
) azArg
[i
++] = azArg
[j
++];
1240 /* Print a usage message for the program and exit */
1241 static void usage(const char *argv0
){
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
);
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"
1264 /* Report on unrecognized arguments */
1265 static void unrecognizedArguments(
1271 fprintf(stderr
,"%s: unrecognized arguments:", argv0
);
1272 for(i
=0; i
<nArg
; i
++){
1273 fprintf(stderr
," %s", azArg
[i
]);
1275 fprintf(stderr
,"\n");
1279 int SQLITE_CDECL
main(int argc
, char **argv
){
1280 const char *zClient
;
1283 int openFlags
= SQLITE_OPEN_READWRITE
;
1288 const char *zCOption
;
1292 int iTmout
= 0; /* Default: no timeout */
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"
1304 sqlite3_sourceid(), SQLITE_SOURCE_ID
);
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;
1325 g
.pErrLog
= fopen(g
.zErrLog
, "a");
1330 g
.pLog
= fopen(g
.zLog
, "a");
1335 sqlite3_config(SQLITE_CONFIG_LOG
, sqlErrorCallback
, 0);
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",
1344 printf("BEGIN: %s", argv
[0]);
1345 for(i
=1; i
<argc
; i
++) printf(" %s", argv
[i
]);
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
);
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 );
1361 fatalError("unable to unlink '%s' after %d attempts\n",
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
);
1372 if( sqlite3_stricmp(zJMode
,"persist")==0
1373 || sqlite3_stricmp(zJMode
,"truncate")==0
1375 printf("Changing journal mode to DELETE from %s", zJMode
);
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,
1386 sqlite3_create_function(g
.db
, "eval", 1, SQLITE_UTF8
, 0,
1388 g
.iTimeout
= DEFAULT_TIMEOUT
;
1389 if( g
.bSqlTrace
) sqlite3_trace(g
.db
, sqlTraceCallback
, 0);
1391 if( n
>0 ) unrecognizedArguments(argv
[0], n
, argv
+2);
1392 if( g
.iTrace
) logMessage("start-client");
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
);
1404 if( g
.iTrace
) logMessage("end-client");
1406 sqlite3_stmt
*pStmt
;
1409 fatalError("missing script filename");
1411 if( n
>1 ) unrecognizedArguments(argv
[0], n
, argv
+2);
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"
1419 " client INTEGER,\n"
1420 " starttime DATE,\n"
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");
1442 while( ((rc
= trySql("SELECT 1 FROM client"))==SQLITE_BUSY
1443 || rc
==SQLITE_ROW
) && iTimeout
>0 ){
1448 pStmt
= prepareSql("SELECT nError, nTest FROM counters");
1450 while( (rc
= sqlite3_step(pStmt
))==SQLITE_BUSY
&& iTimeout
>0 ){
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
);
1462 maybeClose(g
.pErrLog
);
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
]);