blob: 5c1bbb154ef8c6f2097234f85de45b1ab92c8e92 [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
44extern char *sqlite_mprintf(const char *zFormat, ...);
45extern char *sqlite_vmprintf(const char *zFormat, va_list);
46
47/*
drh7bc09d32002-11-01 01:55:36 +000048** When a lock occurs, yield.
49*/
50static int db_is_locked(void *NotUsed, const char *zNotUsed, int iNotUsed){
51 /* sched_yield(); */
52 if( verbose ) printf("BUSY %s\n", (char*)NotUsed);
53 usleep(100);
54 return 1;
55}
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++] =
93 sqlite_mprintf("%s",azArg[i] ? azArg[i] : "");
94 }
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);
109 zSql = sqlite_vmprintf(zFormat, ap);
110 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);
drhb1feb842002-02-18 22:50:26 +0000114 rc = sqlite_exec(db, zSql, db_query_callback, &sResult, &zErrMsg);
drh7bc09d32002-11-01 01:55:36 +0000115 if( rc==SQLITE_SCHEMA ){
116 if( zErrMsg ) free(zErrMsg);
117 rc = sqlite_exec(db, zSql, db_query_callback, &sResult, &zErrMsg);
118 }
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 }
drhfa173a72002-07-10 21:26:00 +0000126 sqlite_freemem(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);
143 zSql = sqlite_vmprintf(zFormat, ap);
144 va_end(ap);
drh7bc09d32002-11-01 01:55:36 +0000145 if( verbose ) printf("EXEC %s: %s\n", zFile, zSql);
drhb1feb842002-02-18 22:50:26 +0000146 rc = sqlite_exec(db, zSql, 0, 0, &zErrMsg);
drh7bc09d32002-11-01 01:55:36 +0000147 if( rc==SQLITE_SCHEMA ){
148 if( zErrMsg ) free(zErrMsg);
149 rc = sqlite_exec(db, zSql, 0, 0, &zErrMsg);
150 }
151 if( verbose ) printf("DONE %s: %s\n", zFile, zSql);
drhb1feb842002-02-18 22:50:26 +0000152 if( zErrMsg ){
drh7bc09d32002-11-01 01:55:36 +0000153 fprintf(stdout,"%s: command failed: %s - %s\n", zFile, zSql, zErrMsg);
drhb1feb842002-02-18 22:50:26 +0000154 free(zErrMsg);
drhfa173a72002-07-10 21:26:00 +0000155 sqlite_freemem(zSql);
drhb1feb842002-02-18 22:50:26 +0000156 Exit(1);
157 }
drhfa173a72002-07-10 21:26:00 +0000158 sqlite_freemem(zSql);
drhb1feb842002-02-18 22:50:26 +0000159}
160
161/*
162** Free the results of a db_query() call.
163*/
164void db_query_free(char **az){
165 int i;
166 for(i=0; az[i]; i++){
drhfa173a72002-07-10 21:26:00 +0000167 sqlite_freemem(az[i]);
drhb1feb842002-02-18 22:50:26 +0000168 }
169 free(az);
170}
171
172/*
173** Check results
174*/
175void db_check(const char *zFile, const char *zMsg, char **az, ...){
176 va_list ap;
177 int i;
178 char *z;
179 va_start(ap, az);
180 for(i=0; (z = va_arg(ap, char*))!=0; i++){
181 if( az[i]==0 || strcmp(az[i],z)!=0 ){
drh7bc09d32002-11-01 01:55:36 +0000182 fprintf(stdout,"%s: %s: bad result in column %d: %s\n",
drhb1feb842002-02-18 22:50:26 +0000183 zFile, zMsg, i+1, az[i]);
184 db_query_free(az);
185 Exit(1);
186 }
187 }
188 va_end(ap);
189 db_query_free(az);
190}
191
192pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
193pthread_cond_t sig = PTHREAD_COND_INITIALIZER;
194int thread_cnt = 0;
195
196static void *worker_bee(void *pArg){
197 const char *zFilename = (char*)pArg;
198 char *azErr;
199 int i, cnt;
drh7bc09d32002-11-01 01:55:36 +0000200 int t = atoi(zFilename);
drhb1feb842002-02-18 22:50:26 +0000201 char **az;
202 sqlite *db;
203
204 pthread_mutex_lock(&lock);
205 thread_cnt++;
206 pthread_mutex_unlock(&lock);
207 printf("%s: START\n", zFilename);
208 fflush(stdout);
209 for(cnt=0; cnt<10; cnt++){
drh7bc09d32002-11-01 01:55:36 +0000210 db = sqlite_open(&zFilename[2], 0, &azErr);
drhb1feb842002-02-18 22:50:26 +0000211 if( db==0 ){
drh7bc09d32002-11-01 01:55:36 +0000212 fprintf(stdout,"%s: can't open\n", zFilename);
drhb1feb842002-02-18 22:50:26 +0000213 Exit(1);
214 }
drh7bc09d32002-11-01 01:55:36 +0000215 sqlite_busy_handler(db, db_is_locked, zFilename);
216 db_execute(db, zFilename, "CREATE TABLE t%d(a,b,c);", t);
drhb1feb842002-02-18 22:50:26 +0000217 for(i=1; i<=100; i++){
drh7bc09d32002-11-01 01:55:36 +0000218 db_execute(db, zFilename, "INSERT INTO t%d VALUES(%d,%d,%d);",
219 t, i, i*2, i*i);
drhb1feb842002-02-18 22:50:26 +0000220 }
drh7bc09d32002-11-01 01:55:36 +0000221 az = db_query(db, zFilename, "SELECT count(*) FROM t%d", t);
222 db_check(zFilename, "tX size", az, "100", 0);
223 az = db_query(db, zFilename, "SELECT avg(b) FROM t%d", t);
224 db_check(zFilename, "tX avg", az, "101", 0);
225 db_execute(db, zFilename, "DELETE FROM t%d WHERE a>50", t);
226 az = db_query(db, zFilename, "SELECT avg(b) FROM t%d", t);
227 db_check(zFilename, "tX avg2", az, "51", 0);
drhb1feb842002-02-18 22:50:26 +0000228 for(i=1; i<=50; i++){
229 char z1[30], z2[30];
drh7bc09d32002-11-01 01:55:36 +0000230 az = db_query(db, zFilename, "SELECT b, c FROM t%d WHERE a=%d", t, i);
drhb1feb842002-02-18 22:50:26 +0000231 sprintf(z1, "%d", i*2);
232 sprintf(z2, "%d", i*i);
233 db_check(zFilename, "readback", az, z1, z2, 0);
234 }
drh7bc09d32002-11-01 01:55:36 +0000235 db_execute(db, zFilename, "DROP TABLE t%d;", t);
drhb1feb842002-02-18 22:50:26 +0000236 sqlite_close(db);
237 }
238 printf("%s: END\n", zFilename);
drh7bc09d32002-11-01 01:55:36 +0000239 /* unlink(zFilename); */
drhb1feb842002-02-18 22:50:26 +0000240 fflush(stdout);
241 pthread_mutex_lock(&lock);
242 thread_cnt--;
243 if( thread_cnt<=0 ){
244 pthread_cond_signal(&sig);
245 }
246 pthread_mutex_unlock(&lock);
247 return 0;
248}
249
250int main(int argc, char **argv){
251 char *zFile;
252 int i, n;
253 pthread_t id;
drh7bc09d32002-11-01 01:55:36 +0000254 if( argc>2 && strcmp(argv[1], "-v")==0 ){
255 verbose = 1;
256 argc--;
257 argv++;
258 }
drhb1feb842002-02-18 22:50:26 +0000259 if( argc<2 || (n=atoi(argv[1]))<1 ) n = 10;
260 for(i=0; i<n; i++){
drh7bc09d32002-11-01 01:55:36 +0000261 char zBuf[200];
262 sprintf(zBuf, "testdb-%d", (i+1)/2);
263 unlink(zBuf);
264 }
265 for(i=0; i<n; i++){
266 zFile = sqlite_mprintf("%d.testdb-%d", i%2+1, (i+2)/2);
drhb1feb842002-02-18 22:50:26 +0000267 unlink(zFile);
268 pthread_create(&id, 0, worker_bee, (void*)zFile);
269 pthread_detach(id);
270 }
271 pthread_mutex_lock(&lock);
272 while( thread_cnt>0 ){
273 pthread_cond_wait(&sig, &lock);
274 }
275 pthread_mutex_unlock(&lock);
drh7bc09d32002-11-01 01:55:36 +0000276 for(i=0; i<n; i++){
277 char zBuf[200];
278 sprintf(zBuf, "testdb-%d", (i+1)/2);
279 unlink(zBuf);
280 }
drhb1feb842002-02-18 22:50:26 +0000281 return 0;
282}