interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / mptest / mptest.c

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