Improvements in threadtest.c (for Unix) and some minor bug fixes that result

from the better testing. (CVS 776)

FossilOrigin-Name: 326e0983c34b584a3c4a2300399bff0a8281b9f8
This commit is contained in:
drh
2002-11-01 01:55:36 +00:00
parent 194a7b6469
commit 7bc09d3111
5 changed files with 82 additions and 32 deletions
+65 -20
View File
@@ -20,11 +20,17 @@
*/
#include "sqlite.h"
#include <pthread.h>
#include <sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/*
** Enable for tracing
*/
static int verbose = 0;
/*
** Come here to die.
*/
@@ -35,6 +41,16 @@ static void Exit(int rc){
extern char *sqlite_mprintf(const char *zFormat, ...);
extern char *sqlite_vmprintf(const char *zFormat, va_list);
/*
** When a lock occurs, yield.
*/
static int db_is_locked(void *NotUsed, const char *zNotUsed, int iNotUsed){
/* sched_yield(); */
if( verbose ) printf("BUSY %s\n", (char*)NotUsed);
usleep(100);
return 1;
}
/*
** Used to accumulate query results by db_query()
*/
@@ -64,7 +80,7 @@ static int db_query_callback(
}
pResult->azElem = realloc( pResult->azElem, pResult->nAlloc*sizeof(char*));
if( pResult->azElem==0 ){
fprintf(stderr,"%s: malloc failed\n", pResult->zFile);
fprintf(stdout,"%s: malloc failed\n", pResult->zFile);
return 1;
}
}
@@ -91,9 +107,15 @@ char **db_query(sqlite *db, const char *zFile, const char *zFormat, ...){
va_end(ap);
memset(&sResult, 0, sizeof(sResult));
sResult.zFile = zFile;
if( verbose ) printf("QUERY %s: %s\n", zFile, zSql);
rc = sqlite_exec(db, zSql, db_query_callback, &sResult, &zErrMsg);
if( rc==SQLITE_SCHEMA ){
if( zErrMsg ) free(zErrMsg);
rc = sqlite_exec(db, zSql, db_query_callback, &sResult, &zErrMsg);
}
if( verbose ) printf("DONE %s %s\n", zFile, zSql);
if( zErrMsg ){
fprintf(stderr,"%s: query failed: %s - %s\n", zFile, zSql, zErrMsg);
fprintf(stdout,"%s: query failed: %s - %s\n", zFile, zSql, zErrMsg);
free(zErrMsg);
free(zSql);
Exit(1);
@@ -117,9 +139,15 @@ void db_execute(sqlite *db, const char *zFile, const char *zFormat, ...){
va_start(ap, zFormat);
zSql = sqlite_vmprintf(zFormat, ap);
va_end(ap);
if( verbose ) printf("EXEC %s: %s\n", zFile, zSql);
rc = sqlite_exec(db, zSql, 0, 0, &zErrMsg);
if( rc==SQLITE_SCHEMA ){
if( zErrMsg ) free(zErrMsg);
rc = sqlite_exec(db, zSql, 0, 0, &zErrMsg);
}
if( verbose ) printf("DONE %s: %s\n", zFile, zSql);
if( zErrMsg ){
fprintf(stderr,"%s: command failed: %s - %s\n", zFile, zSql, zErrMsg);
fprintf(stdout,"%s: command failed: %s - %s\n", zFile, zSql, zErrMsg);
free(zErrMsg);
sqlite_freemem(zSql);
Exit(1);
@@ -148,7 +176,7 @@ void db_check(const char *zFile, const char *zMsg, char **az, ...){
va_start(ap, az);
for(i=0; (z = va_arg(ap, char*))!=0; i++){
if( az[i]==0 || strcmp(az[i],z)!=0 ){
fprintf(stderr,"%s: %s: bad result in column %d: %s\n",
fprintf(stdout,"%s: %s: bad result in column %d: %s\n",
zFile, zMsg, i+1, az[i]);
db_query_free(az);
Exit(1);
@@ -166,6 +194,7 @@ static void *worker_bee(void *pArg){
const char *zFilename = (char*)pArg;
char *azErr;
int i, cnt;
int t = atoi(zFilename);
char **az;
sqlite *db;
@@ -175,35 +204,36 @@ static void *worker_bee(void *pArg){
printf("%s: START\n", zFilename);
fflush(stdout);
for(cnt=0; cnt<10; cnt++){
db = sqlite_open(zFilename, 0, &azErr);
db = sqlite_open(&zFilename[2], 0, &azErr);
if( db==0 ){
fprintf(stderr,"%s: can't open\n", zFilename);
fprintf(stdout,"%s: can't open\n", zFilename);
Exit(1);
}
db_execute(db, zFilename, "BEGIN; CREATE TABLE t1(a,b,c);");
sqlite_busy_handler(db, db_is_locked, zFilename);
db_execute(db, zFilename, "CREATE TABLE t%d(a,b,c);", t);
for(i=1; i<=100; i++){
db_execute(db, zFilename, "INSERT INTO t1 VALUES(%d,%d,%d);",
i, i*2, i*i);
db_execute(db, zFilename, "INSERT INTO t%d VALUES(%d,%d,%d);",
t, i, i*2, i*i);
}
az = db_query(db, zFilename, "SELECT count(*) FROM t1");
db_check(zFilename, "t1 size", az, "100", 0);
az = db_query(db, zFilename, "SELECT avg(b) FROM t1");
db_check(zFilename, "t1 avg", az, "101", 0);
db_execute(db, zFilename, "DELETE FROM t1 WHERE a>50");
az = db_query(db, zFilename, "SELECT avg(b) FROM t1");
db_check(zFilename, "t1 avg2", az, "51", 0);
az = db_query(db, zFilename, "SELECT count(*) FROM t%d", t);
db_check(zFilename, "tX size", az, "100", 0);
az = db_query(db, zFilename, "SELECT avg(b) FROM t%d", t);
db_check(zFilename, "tX avg", az, "101", 0);
db_execute(db, zFilename, "DELETE FROM t%d WHERE a>50", t);
az = db_query(db, zFilename, "SELECT avg(b) FROM t%d", t);
db_check(zFilename, "tX avg2", az, "51", 0);
for(i=1; i<=50; i++){
char z1[30], z2[30];
az = db_query(db, zFilename, "SELECT b, c FROM t1 WHERE a=%d", i);
az = db_query(db, zFilename, "SELECT b, c FROM t%d WHERE a=%d", t, i);
sprintf(z1, "%d", i*2);
sprintf(z2, "%d", i*i);
db_check(zFilename, "readback", az, z1, z2, 0);
}
db_execute(db, zFilename, "COMMIT; DROP TABLE t1;");
db_execute(db, zFilename, "DROP TABLE t%d;", t);
sqlite_close(db);
}
printf("%s: END\n", zFilename);
unlink(zFilename);
/* unlink(zFilename); */
fflush(stdout);
pthread_mutex_lock(&lock);
thread_cnt--;
@@ -218,9 +248,19 @@ int main(int argc, char **argv){
char *zFile;
int i, n;
pthread_t id;
if( argc>2 && strcmp(argv[1], "-v")==0 ){
verbose = 1;
argc--;
argv++;
}
if( argc<2 || (n=atoi(argv[1]))<1 ) n = 10;
for(i=0; i<n; i++){
zFile = sqlite_mprintf("testdb-%d", i+1);
char zBuf[200];
sprintf(zBuf, "testdb-%d", (i+1)/2);
unlink(zBuf);
}
for(i=0; i<n; i++){
zFile = sqlite_mprintf("%d.testdb-%d", i%2+1, (i+2)/2);
unlink(zFile);
pthread_create(&id, 0, worker_bee, (void*)zFile);
pthread_detach(id);
@@ -230,5 +270,10 @@ int main(int argc, char **argv){
pthread_cond_wait(&sig, &lock);
}
pthread_mutex_unlock(&lock);
for(i=0; i<n; i++){
char zBuf[200];
sprintf(zBuf, "testdb-%d", (i+1)/2);
unlink(zBuf);
}
return 0;
}