blob: f5ff112e60dbe2c40603ff4f5d5b1c62fea4cb47 [file] [log] [blame]
drhb1feb842002-02-18 22:50:26 +00001/*
2** 2002 January 15
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** This file implements a simple standalone program used to test whether
13** or not the SQLite library is threadsafe.
14**
15** Testing the thread safety of SQLite is difficult because there are very
16** few places in the code that are even potentially unsafe, and those
17** places execute for very short periods of time. So even if the library
18** is compiled with its mutexes disabled, it is likely to work correctly
19** in a multi-threaded program most of the time.
drhc713bdc2004-01-14 03:32:37 +000020**
21** This file is NOT part of the standard SQLite library. It is used for
22** testing only.
drhb1feb842002-02-18 22:50:26 +000023*/
24#include "sqlite.h"
25#include <pthread.h>
drh7bc09d32002-11-01 01:55:36 +000026#include <sched.h>
drhb1feb842002-02-18 22:50:26 +000027#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <unistd.h>
31
drhb1feb842002-02-18 22:50:26 +000032/*
drh7bc09d32002-11-01 01:55:36 +000033** Enable for tracing
34*/
35static int verbose = 0;
36
37/*
drhb1feb842002-02-18 22:50:26 +000038** Come here to die.
39*/
40static void Exit(int rc){
41 exit(rc);
42}
43
drh5fdae772004-06-29 03:29:00 +000044extern char *sqlite3_mprintf(const char *zFormat, ...);
45extern char *sqlite3_vmprintf(const char *zFormat, va_list);
drhb1feb842002-02-18 22:50:26 +000046
47/*
drh7bc09d32002-11-01 01:55:36 +000048** When a lock occurs, yield.
49*/
drh5fdae772004-06-29 03:29:00 +000050static int db_is_locked(void *NotUsed, int iCount){
drh7bc09d32002-11-01 01:55:36 +000051 /* sched_yield(); */
drh5fdae772004-06-29 03:29:00 +000052 if( verbose ) printf("BUSY %s #%d\n", (char*)NotUsed, iCount);
drh7bc09d32002-11-01 01:55:36 +000053 usleep(100);
drh5fdae772004-06-29 03:29:00 +000054 return iCount<25;
drh7bc09d32002-11-01 01:55:36 +000055}
56
57/*
drhb1feb842002-02-18 22:50:26 +000058** Used to accumulate query results by db_query()
59*/
60struct QueryResult {
61 const char *zFile; /* Filename - used for error reporting */
62 int nElem; /* Number of used entries in azElem[] */
63 int nAlloc; /* Number of slots allocated for azElem[] */
64 char **azElem; /* The result of the query */
65};
66
67/*
68** The callback function for db_query
69*/
70static int db_query_callback(
71 void *pUser, /* Pointer to the QueryResult structure */
72 int nArg, /* Number of columns in this result row */
73 char **azArg, /* Text of data in all columns */
74 char **NotUsed /* Names of the columns */
75){
76 struct QueryResult *pResult = (struct QueryResult*)pUser;
77 int i;
78 if( pResult->nElem + nArg >= pResult->nAlloc ){
79 if( pResult->nAlloc==0 ){
80 pResult->nAlloc = nArg+1;
81 }else{
82 pResult->nAlloc = pResult->nAlloc*2 + nArg + 1;
83 }
84 pResult->azElem = realloc( pResult->azElem, pResult->nAlloc*sizeof(char*));
85 if( pResult->azElem==0 ){
drh7bc09d32002-11-01 01:55:36 +000086 fprintf(stdout,"%s: malloc failed\n", pResult->zFile);
drhb1feb842002-02-18 22:50:26 +000087 return 1;
88 }
89 }
90 if( azArg==0 ) return 0;
91 for(i=0; i<nArg; i++){
92 pResult->azElem[pResult->nElem++] =
drh5fdae772004-06-29 03:29:00 +000093 sqlite3_mprintf("%s",azArg[i] ? azArg[i] : "");
drhb1feb842002-02-18 22:50:26 +000094 }
95 return 0;
96}
97
98/*
99** Execute a query against the database. NULL values are returned
100** as an empty string. The list is terminated by a single NULL pointer.
101*/
102char **db_query(sqlite *db, const char *zFile, const char *zFormat, ...){
103 char *zSql;
104 int rc;
105 char *zErrMsg = 0;
106 va_list ap;
107 struct QueryResult sResult;
108 va_start(ap, zFormat);
drh5fdae772004-06-29 03:29:00 +0000109 zSql = sqlite3_vmprintf(zFormat, ap);
drhb1feb842002-02-18 22:50:26 +0000110 va_end(ap);
111 memset(&sResult, 0, sizeof(sResult));
112 sResult.zFile = zFile;
drh7bc09d32002-11-01 01:55:36 +0000113 if( verbose ) printf("QUERY %s: %s\n", zFile, zSql);
drh5fdae772004-06-29 03:29:00 +0000114 rc = sqlite3_exec(db, zSql, db_query_callback, &sResult, &zErrMsg);
drh7bc09d32002-11-01 01:55:36 +0000115 if( rc==SQLITE_SCHEMA ){
116 if( zErrMsg ) free(zErrMsg);
drh5fdae772004-06-29 03:29:00 +0000117 rc = sqlite3_exec(db, zSql, db_query_callback, &sResult, &zErrMsg);
drh7bc09d32002-11-01 01:55:36 +0000118 }
119 if( verbose ) printf("DONE %s %s\n", zFile, zSql);
drhb1feb842002-02-18 22:50:26 +0000120 if( zErrMsg ){
drh7bc09d32002-11-01 01:55:36 +0000121 fprintf(stdout,"%s: query failed: %s - %s\n", zFile, zSql, zErrMsg);
drhb1feb842002-02-18 22:50:26 +0000122 free(zErrMsg);
123 free(zSql);
124 Exit(1);
125 }
drh5fdae772004-06-29 03:29:00 +0000126 sqlite3_free(zSql);
drhb1feb842002-02-18 22:50:26 +0000127 if( sResult.azElem==0 ){
128 db_query_callback(&sResult, 0, 0, 0);
129 }
130 sResult.azElem[sResult.nElem] = 0;
131 return sResult.azElem;
132}
133
134/*
135** Execute an SQL statement.
136*/
137void db_execute(sqlite *db, const char *zFile, const char *zFormat, ...){
138 char *zSql;
139 int rc;
140 char *zErrMsg = 0;
141 va_list ap;
142 va_start(ap, zFormat);
drh5fdae772004-06-29 03:29:00 +0000143 zSql = sqlite3_vmprintf(zFormat, ap);
drhb1feb842002-02-18 22:50:26 +0000144 va_end(ap);
drh7bc09d32002-11-01 01:55:36 +0000145 if( verbose ) printf("EXEC %s: %s\n", zFile, zSql);
drh5fdae772004-06-29 03:29:00 +0000146 do{
147 rc = sqlite3_exec(db, zSql, 0, 0, &zErrMsg);
148 }while( rc==SQLITE_BUSY );
drh7bc09d32002-11-01 01:55:36 +0000149 if( verbose ) printf("DONE %s: %s\n", zFile, zSql);
drhb1feb842002-02-18 22:50:26 +0000150 if( zErrMsg ){
drh7bc09d32002-11-01 01:55:36 +0000151 fprintf(stdout,"%s: command failed: %s - %s\n", zFile, zSql, zErrMsg);
drhb1feb842002-02-18 22:50:26 +0000152 free(zErrMsg);
drh5fdae772004-06-29 03:29:00 +0000153 sqlite3_free(zSql);
drhb1feb842002-02-18 22:50:26 +0000154 Exit(1);
155 }
drh5fdae772004-06-29 03:29:00 +0000156 sqlite3_free(zSql);
drhb1feb842002-02-18 22:50:26 +0000157}
158
159/*
160** Free the results of a db_query() call.
161*/
162void db_query_free(char **az){
163 int i;
164 for(i=0; az[i]; i++){
drh5fdae772004-06-29 03:29:00 +0000165 sqlite3_free(az[i]);
drhb1feb842002-02-18 22:50:26 +0000166 }
167 free(az);
168}
169
170/*
171** Check results
172*/
173void db_check(const char *zFile, const char *zMsg, char **az, ...){
174 va_list ap;
175 int i;
176 char *z;
177 va_start(ap, az);
178 for(i=0; (z = va_arg(ap, char*))!=0; i++){
179 if( az[i]==0 || strcmp(az[i],z)!=0 ){
drh7bc09d32002-11-01 01:55:36 +0000180 fprintf(stdout,"%s: %s: bad result in column %d: %s\n",
drhb1feb842002-02-18 22:50:26 +0000181 zFile, zMsg, i+1, az[i]);
182 db_query_free(az);
183 Exit(1);
184 }
185 }
186 va_end(ap);
187 db_query_free(az);
188}
189
190pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
191pthread_cond_t sig = PTHREAD_COND_INITIALIZER;
192int thread_cnt = 0;
193
194static void *worker_bee(void *pArg){
195 const char *zFilename = (char*)pArg;
196 char *azErr;
197 int i, cnt;
drh7bc09d32002-11-01 01:55:36 +0000198 int t = atoi(zFilename);
drhb1feb842002-02-18 22:50:26 +0000199 char **az;
200 sqlite *db;
201
202 pthread_mutex_lock(&lock);
203 thread_cnt++;
204 pthread_mutex_unlock(&lock);
205 printf("%s: START\n", zFilename);
206 fflush(stdout);
207 for(cnt=0; cnt<10; cnt++){
drh5fdae772004-06-29 03:29:00 +0000208 sqlite3_open(&zFilename[2], &db);
drhb1feb842002-02-18 22:50:26 +0000209 if( db==0 ){
drh7bc09d32002-11-01 01:55:36 +0000210 fprintf(stdout,"%s: can't open\n", zFilename);
drhb1feb842002-02-18 22:50:26 +0000211 Exit(1);
212 }
drh5fdae772004-06-29 03:29:00 +0000213 sqlite3_busy_handler(db, db_is_locked, zFilename);
drh7bc09d32002-11-01 01:55:36 +0000214 db_execute(db, zFilename, "CREATE TABLE t%d(a,b,c);", t);
drhb1feb842002-02-18 22:50:26 +0000215 for(i=1; i<=100; i++){
drh7bc09d32002-11-01 01:55:36 +0000216 db_execute(db, zFilename, "INSERT INTO t%d VALUES(%d,%d,%d);",
217 t, i, i*2, i*i);
drhb1feb842002-02-18 22:50:26 +0000218 }
drh7bc09d32002-11-01 01:55:36 +0000219 az = db_query(db, zFilename, "SELECT count(*) FROM t%d", t);
220 db_check(zFilename, "tX size", az, "100", 0);
221 az = db_query(db, zFilename, "SELECT avg(b) FROM t%d", t);
222 db_check(zFilename, "tX avg", az, "101", 0);
223 db_execute(db, zFilename, "DELETE FROM t%d WHERE a>50", t);
224 az = db_query(db, zFilename, "SELECT avg(b) FROM t%d", t);
225 db_check(zFilename, "tX avg2", az, "51", 0);
drhb1feb842002-02-18 22:50:26 +0000226 for(i=1; i<=50; i++){
227 char z1[30], z2[30];
drh7bc09d32002-11-01 01:55:36 +0000228 az = db_query(db, zFilename, "SELECT b, c FROM t%d WHERE a=%d", t, i);
drhb1feb842002-02-18 22:50:26 +0000229 sprintf(z1, "%d", i*2);
230 sprintf(z2, "%d", i*i);
231 db_check(zFilename, "readback", az, z1, z2, 0);
232 }
drh7bc09d32002-11-01 01:55:36 +0000233 db_execute(db, zFilename, "DROP TABLE t%d;", t);
drh5fdae772004-06-29 03:29:00 +0000234 sqlite3_close(db);
drhb1feb842002-02-18 22:50:26 +0000235 }
236 printf("%s: END\n", zFilename);
drh7bc09d32002-11-01 01:55:36 +0000237 /* unlink(zFilename); */
drhb1feb842002-02-18 22:50:26 +0000238 fflush(stdout);
239 pthread_mutex_lock(&lock);
240 thread_cnt--;
241 if( thread_cnt<=0 ){
242 pthread_cond_signal(&sig);
243 }
244 pthread_mutex_unlock(&lock);
245 return 0;
246}
247
248int main(int argc, char **argv){
249 char *zFile;
250 int i, n;
251 pthread_t id;
drh7bc09d32002-11-01 01:55:36 +0000252 if( argc>2 && strcmp(argv[1], "-v")==0 ){
253 verbose = 1;
254 argc--;
255 argv++;
256 }
drhb1feb842002-02-18 22:50:26 +0000257 if( argc<2 || (n=atoi(argv[1]))<1 ) n = 10;
258 for(i=0; i<n; i++){
drh7bc09d32002-11-01 01:55:36 +0000259 char zBuf[200];
260 sprintf(zBuf, "testdb-%d", (i+1)/2);
261 unlink(zBuf);
262 }
263 for(i=0; i<n; i++){
drh5fdae772004-06-29 03:29:00 +0000264 zFile = sqlite3_mprintf("%d.testdb-%d", i%2+1, (i+2)/2);
drhb1feb842002-02-18 22:50:26 +0000265 unlink(zFile);
266 pthread_create(&id, 0, worker_bee, (void*)zFile);
267 pthread_detach(id);
268 }
269 pthread_mutex_lock(&lock);
270 while( thread_cnt>0 ){
271 pthread_cond_wait(&sig, &lock);
272 }
273 pthread_mutex_unlock(&lock);
drh7bc09d32002-11-01 01:55:36 +0000274 for(i=0; i<n; i++){
275 char zBuf[200];
276 sprintf(zBuf, "testdb-%d", (i+1)/2);
277 unlink(zBuf);
278 }
drhb1feb842002-02-18 22:50:26 +0000279 return 0;
280}