blob: 89c79a4c7cf74c205621263b272a458ffced77ec [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.
20*/
21#include "sqlite.h"
22#include <pthread.h>
drh7bc09d32002-11-01 01:55:36 +000023#include <sched.h>
drhb1feb842002-02-18 22:50:26 +000024#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <unistd.h>
28
drhb1feb842002-02-18 22:50:26 +000029/*
drh7bc09d32002-11-01 01:55:36 +000030** Enable for tracing
31*/
32static int verbose = 0;
33
34/*
drhb1feb842002-02-18 22:50:26 +000035** Come here to die.
36*/
37static void Exit(int rc){
38 exit(rc);
39}
40
41extern char *sqlite_mprintf(const char *zFormat, ...);
42extern char *sqlite_vmprintf(const char *zFormat, va_list);
43
44/*
drh7bc09d32002-11-01 01:55:36 +000045** When a lock occurs, yield.
46*/
47static int db_is_locked(void *NotUsed, const char *zNotUsed, int iNotUsed){
48 /* sched_yield(); */
49 if( verbose ) printf("BUSY %s\n", (char*)NotUsed);
50 usleep(100);
51 return 1;
52}
53
54/*
drhb1feb842002-02-18 22:50:26 +000055** Used to accumulate query results by db_query()
56*/
57struct QueryResult {
58 const char *zFile; /* Filename - used for error reporting */
59 int nElem; /* Number of used entries in azElem[] */
60 int nAlloc; /* Number of slots allocated for azElem[] */
61 char **azElem; /* The result of the query */
62};
63
64/*
65** The callback function for db_query
66*/
67static int db_query_callback(
68 void *pUser, /* Pointer to the QueryResult structure */
69 int nArg, /* Number of columns in this result row */
70 char **azArg, /* Text of data in all columns */
71 char **NotUsed /* Names of the columns */
72){
73 struct QueryResult *pResult = (struct QueryResult*)pUser;
74 int i;
75 if( pResult->nElem + nArg >= pResult->nAlloc ){
76 if( pResult->nAlloc==0 ){
77 pResult->nAlloc = nArg+1;
78 }else{
79 pResult->nAlloc = pResult->nAlloc*2 + nArg + 1;
80 }
81 pResult->azElem = realloc( pResult->azElem, pResult->nAlloc*sizeof(char*));
82 if( pResult->azElem==0 ){
drh7bc09d32002-11-01 01:55:36 +000083 fprintf(stdout,"%s: malloc failed\n", pResult->zFile);
drhb1feb842002-02-18 22:50:26 +000084 return 1;
85 }
86 }
87 if( azArg==0 ) return 0;
88 for(i=0; i<nArg; i++){
89 pResult->azElem[pResult->nElem++] =
90 sqlite_mprintf("%s",azArg[i] ? azArg[i] : "");
91 }
92 return 0;
93}
94
95/*
96** Execute a query against the database. NULL values are returned
97** as an empty string. The list is terminated by a single NULL pointer.
98*/
99char **db_query(sqlite *db, const char *zFile, const char *zFormat, ...){
100 char *zSql;
101 int rc;
102 char *zErrMsg = 0;
103 va_list ap;
104 struct QueryResult sResult;
105 va_start(ap, zFormat);
106 zSql = sqlite_vmprintf(zFormat, ap);
107 va_end(ap);
108 memset(&sResult, 0, sizeof(sResult));
109 sResult.zFile = zFile;
drh7bc09d32002-11-01 01:55:36 +0000110 if( verbose ) printf("QUERY %s: %s\n", zFile, zSql);
drhb1feb842002-02-18 22:50:26 +0000111 rc = sqlite_exec(db, zSql, db_query_callback, &sResult, &zErrMsg);
drh7bc09d32002-11-01 01:55:36 +0000112 if( rc==SQLITE_SCHEMA ){
113 if( zErrMsg ) free(zErrMsg);
114 rc = sqlite_exec(db, zSql, db_query_callback, &sResult, &zErrMsg);
115 }
116 if( verbose ) printf("DONE %s %s\n", zFile, zSql);
drhb1feb842002-02-18 22:50:26 +0000117 if( zErrMsg ){
drh7bc09d32002-11-01 01:55:36 +0000118 fprintf(stdout,"%s: query failed: %s - %s\n", zFile, zSql, zErrMsg);
drhb1feb842002-02-18 22:50:26 +0000119 free(zErrMsg);
120 free(zSql);
121 Exit(1);
122 }
drhfa173a72002-07-10 21:26:00 +0000123 sqlite_freemem(zSql);
drhb1feb842002-02-18 22:50:26 +0000124 if( sResult.azElem==0 ){
125 db_query_callback(&sResult, 0, 0, 0);
126 }
127 sResult.azElem[sResult.nElem] = 0;
128 return sResult.azElem;
129}
130
131/*
132** Execute an SQL statement.
133*/
134void db_execute(sqlite *db, const char *zFile, const char *zFormat, ...){
135 char *zSql;
136 int rc;
137 char *zErrMsg = 0;
138 va_list ap;
139 va_start(ap, zFormat);
140 zSql = sqlite_vmprintf(zFormat, ap);
141 va_end(ap);
drh7bc09d32002-11-01 01:55:36 +0000142 if( verbose ) printf("EXEC %s: %s\n", zFile, zSql);
drhb1feb842002-02-18 22:50:26 +0000143 rc = sqlite_exec(db, zSql, 0, 0, &zErrMsg);
drh7bc09d32002-11-01 01:55:36 +0000144 if( rc==SQLITE_SCHEMA ){
145 if( zErrMsg ) free(zErrMsg);
146 rc = sqlite_exec(db, zSql, 0, 0, &zErrMsg);
147 }
148 if( verbose ) printf("DONE %s: %s\n", zFile, zSql);
drhb1feb842002-02-18 22:50:26 +0000149 if( zErrMsg ){
drh7bc09d32002-11-01 01:55:36 +0000150 fprintf(stdout,"%s: command failed: %s - %s\n", zFile, zSql, zErrMsg);
drhb1feb842002-02-18 22:50:26 +0000151 free(zErrMsg);
drhfa173a72002-07-10 21:26:00 +0000152 sqlite_freemem(zSql);
drhb1feb842002-02-18 22:50:26 +0000153 Exit(1);
154 }
drhfa173a72002-07-10 21:26:00 +0000155 sqlite_freemem(zSql);
drhb1feb842002-02-18 22:50:26 +0000156}
157
158/*
159** Free the results of a db_query() call.
160*/
161void db_query_free(char **az){
162 int i;
163 for(i=0; az[i]; i++){
drhfa173a72002-07-10 21:26:00 +0000164 sqlite_freemem(az[i]);
drhb1feb842002-02-18 22:50:26 +0000165 }
166 free(az);
167}
168
169/*
170** Check results
171*/
172void db_check(const char *zFile, const char *zMsg, char **az, ...){
173 va_list ap;
174 int i;
175 char *z;
176 va_start(ap, az);
177 for(i=0; (z = va_arg(ap, char*))!=0; i++){
178 if( az[i]==0 || strcmp(az[i],z)!=0 ){
drh7bc09d32002-11-01 01:55:36 +0000179 fprintf(stdout,"%s: %s: bad result in column %d: %s\n",
drhb1feb842002-02-18 22:50:26 +0000180 zFile, zMsg, i+1, az[i]);
181 db_query_free(az);
182 Exit(1);
183 }
184 }
185 va_end(ap);
186 db_query_free(az);
187}
188
189pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
190pthread_cond_t sig = PTHREAD_COND_INITIALIZER;
191int thread_cnt = 0;
192
193static void *worker_bee(void *pArg){
194 const char *zFilename = (char*)pArg;
195 char *azErr;
196 int i, cnt;
drh7bc09d32002-11-01 01:55:36 +0000197 int t = atoi(zFilename);
drhb1feb842002-02-18 22:50:26 +0000198 char **az;
199 sqlite *db;
200
201 pthread_mutex_lock(&lock);
202 thread_cnt++;
203 pthread_mutex_unlock(&lock);
204 printf("%s: START\n", zFilename);
205 fflush(stdout);
206 for(cnt=0; cnt<10; cnt++){
drh7bc09d32002-11-01 01:55:36 +0000207 db = sqlite_open(&zFilename[2], 0, &azErr);
drhb1feb842002-02-18 22:50:26 +0000208 if( db==0 ){
drh7bc09d32002-11-01 01:55:36 +0000209 fprintf(stdout,"%s: can't open\n", zFilename);
drhb1feb842002-02-18 22:50:26 +0000210 Exit(1);
211 }
drh7bc09d32002-11-01 01:55:36 +0000212 sqlite_busy_handler(db, db_is_locked, zFilename);
213 db_execute(db, zFilename, "CREATE TABLE t%d(a,b,c);", t);
drhb1feb842002-02-18 22:50:26 +0000214 for(i=1; i<=100; i++){
drh7bc09d32002-11-01 01:55:36 +0000215 db_execute(db, zFilename, "INSERT INTO t%d VALUES(%d,%d,%d);",
216 t, i, i*2, i*i);
drhb1feb842002-02-18 22:50:26 +0000217 }
drh7bc09d32002-11-01 01:55:36 +0000218 az = db_query(db, zFilename, "SELECT count(*) FROM t%d", t);
219 db_check(zFilename, "tX size", az, "100", 0);
220 az = db_query(db, zFilename, "SELECT avg(b) FROM t%d", t);
221 db_check(zFilename, "tX avg", az, "101", 0);
222 db_execute(db, zFilename, "DELETE FROM t%d WHERE a>50", t);
223 az = db_query(db, zFilename, "SELECT avg(b) FROM t%d", t);
224 db_check(zFilename, "tX avg2", az, "51", 0);
drhb1feb842002-02-18 22:50:26 +0000225 for(i=1; i<=50; i++){
226 char z1[30], z2[30];
drh7bc09d32002-11-01 01:55:36 +0000227 az = db_query(db, zFilename, "SELECT b, c FROM t%d WHERE a=%d", t, i);
drhb1feb842002-02-18 22:50:26 +0000228 sprintf(z1, "%d", i*2);
229 sprintf(z2, "%d", i*i);
230 db_check(zFilename, "readback", az, z1, z2, 0);
231 }
drh7bc09d32002-11-01 01:55:36 +0000232 db_execute(db, zFilename, "DROP TABLE t%d;", t);
drhb1feb842002-02-18 22:50:26 +0000233 sqlite_close(db);
234 }
235 printf("%s: END\n", zFilename);
drh7bc09d32002-11-01 01:55:36 +0000236 /* unlink(zFilename); */
drhb1feb842002-02-18 22:50:26 +0000237 fflush(stdout);
238 pthread_mutex_lock(&lock);
239 thread_cnt--;
240 if( thread_cnt<=0 ){
241 pthread_cond_signal(&sig);
242 }
243 pthread_mutex_unlock(&lock);
244 return 0;
245}
246
247int main(int argc, char **argv){
248 char *zFile;
249 int i, n;
250 pthread_t id;
drh7bc09d32002-11-01 01:55:36 +0000251 if( argc>2 && strcmp(argv[1], "-v")==0 ){
252 verbose = 1;
253 argc--;
254 argv++;
255 }
drhb1feb842002-02-18 22:50:26 +0000256 if( argc<2 || (n=atoi(argv[1]))<1 ) n = 10;
257 for(i=0; i<n; i++){
drh7bc09d32002-11-01 01:55:36 +0000258 char zBuf[200];
259 sprintf(zBuf, "testdb-%d", (i+1)/2);
260 unlink(zBuf);
261 }
262 for(i=0; i<n; i++){
263 zFile = sqlite_mprintf("%d.testdb-%d", i%2+1, (i+2)/2);
drhb1feb842002-02-18 22:50:26 +0000264 unlink(zFile);
265 pthread_create(&id, 0, worker_bee, (void*)zFile);
266 pthread_detach(id);
267 }
268 pthread_mutex_lock(&lock);
269 while( thread_cnt>0 ){
270 pthread_cond_wait(&sig, &lock);
271 }
272 pthread_mutex_unlock(&lock);
drh7bc09d32002-11-01 01:55:36 +0000273 for(i=0; i<n; i++){
274 char zBuf[200];
275 sprintf(zBuf, "testdb-%d", (i+1)/2);
276 unlink(zBuf);
277 }
drhb1feb842002-02-18 22:50:26 +0000278 return 0;
279}