xref: /onnv-gate/usr/src/lib/libsqlite/test/crashtest1.c (revision 4520:7dbeadedd7fe)
1*4520Snw141292 
2*4520Snw141292 #pragma ident	"%Z%%M%	%I%	%E% SMI"
3*4520Snw141292 
4*4520Snw141292 /*
5*4520Snw141292 ** This program tests the ability of SQLite database to recover from a crash.
6*4520Snw141292 ** This program runs under Unix only, but the results are applicable to all
7*4520Snw141292 ** systems.
8*4520Snw141292 **
9*4520Snw141292 ** The main process first constructs a test database, then starts creating
10*4520Snw141292 ** subprocesses that write to that database.  Each subprocess is killed off,
11*4520Snw141292 ** without a chance to clean up its database connection, after a random
12*4520Snw141292 ** delay.  This killing of the subprocesses simulates a crash or power
13*4520Snw141292 ** failure.  The next subprocess to open the database should rollback
14*4520Snw141292 ** whatever operation was in process at the time of the simulated crash.
15*4520Snw141292 **
16*4520Snw141292 ** If any problems are encountered, an error is reported and the test stops.
17*4520Snw141292 ** If no problems are seen after a large number of tests, we assume that
18*4520Snw141292 ** the rollback mechanism is working.
19*4520Snw141292 */
20*4520Snw141292 #include <stdio.h>
21*4520Snw141292 #include <unistd.h>
22*4520Snw141292 #include <sys/types.h>
23*4520Snw141292 #include <sys/wait.h>
24*4520Snw141292 #include <signal.h>
25*4520Snw141292 #include <stdlib.h>
26*4520Snw141292 #include <string.h>
27*4520Snw141292 #include <sched.h>
28*4520Snw141292 #include "sqlite.h"
29*4520Snw141292 
do_some_sql(int parent)30*4520Snw141292 static void do_some_sql(int parent){
31*4520Snw141292   char *zErr;
32*4520Snw141292   int rc = SQLITE_OK;
33*4520Snw141292   sqlite *db;
34*4520Snw141292   int cnt = 0;
35*4520Snw141292   static char zBig[] =
36*4520Snw141292     "-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
37*4520Snw141292     "-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
38*4520Snw141292 
39*4520Snw141292   if( access("./test.db-journal",0)==0 ){
40*4520Snw141292     /*printf("pid %d: journal exists.  rollback will be required\n",getpid());*/    unlink("test.db-saved");
41*4520Snw141292     system("cp test.db test.db-saved");
42*4520Snw141292     unlink("test.db-journal-saved");
43*4520Snw141292     system("cp test.db-journal test.db-journal-saved");
44*4520Snw141292   }
45*4520Snw141292   db = sqlite_open("./test.db", 0, &zErr);
46*4520Snw141292   if( db==0 ){
47*4520Snw141292     printf("ERROR: %s\n", zErr);
48*4520Snw141292     if( strcmp(zErr,"database disk image is malformed")==0 ){
49*4520Snw141292       kill(parent, SIGKILL);
50*4520Snw141292     }
51*4520Snw141292     exit(1);
52*4520Snw141292   }
53*4520Snw141292   srand(getpid());
54*4520Snw141292   while( rc==SQLITE_OK ){
55*4520Snw141292     cnt++;
56*4520Snw141292     rc = sqlite_exec_printf(db,
57*4520Snw141292        "INSERT INTO t1 VALUES(%d,'%d%s')", 0, 0, &zErr,
58*4520Snw141292        rand(), rand(), zBig);
59*4520Snw141292   }
60*4520Snw141292   if( rc!=SQLITE_OK ){
61*4520Snw141292     printf("ERROR #%d: %s\n", rc, zErr);
62*4520Snw141292     if( rc==SQLITE_CORRUPT ){
63*4520Snw141292       kill(parent, SIGKILL);
64*4520Snw141292     }
65*4520Snw141292   }
66*4520Snw141292   printf("pid %d: cnt=%d\n", getpid(), cnt);
67*4520Snw141292 }
68*4520Snw141292 
69*4520Snw141292 
main(int argc,char ** argv)70*4520Snw141292 int main(int argc, char **argv){
71*4520Snw141292   int i;
72*4520Snw141292   sqlite *db;
73*4520Snw141292   char *zErr;
74*4520Snw141292   int status;
75*4520Snw141292   int parent = getpid();
76*4520Snw141292 
77*4520Snw141292   unlink("test.db");
78*4520Snw141292   unlink("test.db-journal");
79*4520Snw141292   db = sqlite_open("test.db", 0, &zErr);
80*4520Snw141292   if( db==0 ){
81*4520Snw141292     printf("Cannot initialize: %s\n", zErr);
82*4520Snw141292     return 1;
83*4520Snw141292   }
84*4520Snw141292   sqlite_exec(db, "CREATE TABLE t1(a,b)", 0, 0, 0);
85*4520Snw141292   sqlite_close(db);
86*4520Snw141292   for(i=0; i<10000; i++){
87*4520Snw141292     int pid = fork();
88*4520Snw141292     if( pid==0 ){
89*4520Snw141292       sched_yield();
90*4520Snw141292       do_some_sql(parent);
91*4520Snw141292       return 0;
92*4520Snw141292     }
93*4520Snw141292     printf("test %d, pid=%d\n", i, pid);
94*4520Snw141292     usleep(rand()%10000 + 1000);
95*4520Snw141292     kill(pid, SIGKILL);
96*4520Snw141292     waitpid(pid, &status, 0);
97*4520Snw141292   }
98*4520Snw141292   return 0;
99*4520Snw141292 }
100