Add the crashtest1.c program used to test the ability of the database to

survive a program crash or power failure.  Ticket #599. (CVS 1210)

FossilOrigin-Name: 597a59a72d662b291fb09a069547efd08aa5adb1
This commit is contained in:
drh 2004-02-08 06:06:37 +00:00
parent 34e79ceeb6
commit 585a3d7c22
3 changed files with 103 additions and 6 deletions

View File

@ -1,5 +1,5 @@
C Fix\sinaccuracies\sand\sadd\sdetails\sto\scomments\sin\sthe\spager.\s\sChange\sthe\sname\nof\sone\sfunction\sto\smake\sits\spurpose\sclearer.\s\sTicket\s#599.\s(CVS\s1209)
D 2004-02-08T06:05:46
C Add\sthe\scrashtest1.c\sprogram\sused\sto\stest\sthe\sability\sof\sthe\sdatabase\sto\nsurvive\sa\sprogram\scrash\sor\spower\sfailure.\s\sTicket\s#599.\s(CVS\s1210)
D 2004-02-08T06:06:37
F Makefile.in 0515ff9218ad8d5a8f6220f0494b8ef94c67013b
F Makefile.linux-gcc b86a99c493a5bfb402d1d9178dcdc4bd4b32f906
F README f1de682fbbd94899d50aca13d387d1b3fd3be2dd
@ -82,6 +82,7 @@ F test/btree4rb.test ae6f0438512edcb45cf483471cd6070a765963a9
F test/capi2.test ec96e0e235d87b53cbaef3d8e3e0f8ccf32c71ca
F test/conflict.test 0911bb2f079046914a6e9c3341b36658c4e2103e
F test/copy.test 88dabd4e811b17644b726aa81d404e73b7635c84
F test/crashtest1.c 09c1c7d728ccf4feb9e481671e29dda5669bbcc2
F test/date.test bb3ce39211cc6687a187133efeb2066b48993643
F test/delete.test 92256384f1801760180ded129f7427884cf28886
F test/expr.test c4cc292d601019c2f2ce95093caaa5d10284b105
@ -182,7 +183,7 @@ F www/sqlite.tcl 3c83b08cf9f18aa2d69453ff441a36c40e431604
F www/tclsqlite.tcl b9271d44dcf147a93c98f8ecf28c927307abd6da
F www/vdbe.tcl 9b9095d4495f37697fd1935d10e14c6015e80aa1
F www/whentouse.tcl a8335bce47cc2fddb07f19052cb0cb4d9129a8e4
P dc5be2c82b591a385adf02863d89e113272e2ebd
R 99f43def4bb0b74954a57e70fb60a6e4
P 48832d35ed0d5ba02908822c749591e76b790c48
R 40064791331193ea6f04232cf94213af
U drh
Z d377e040c4a9be8631352dbc02c26b62
Z 622fa1ce91c5b90bffb9ff584cd80de3

View File

@ -1 +1 @@
48832d35ed0d5ba02908822c749591e76b790c48
597a59a72d662b291fb09a069547efd08aa5adb1

96
test/crashtest1.c Normal file
View File

@ -0,0 +1,96 @@
/*
** This program tests the ability of SQLite database to recover from a crash.
** This program runs under Unix only, but the results are applicable to all
** systems.
**
** The main process first constructs a test database, then starts creating
** subprocesses that write to that database. Each subprocess is killed off,
** without a chance to clean up its database connection, after a random
** delay. This killing of the subprocesses simulates a crash or power
** failure. The next subprocess to open the database should rollback
** whatever operation was in process at the time of the simulated crash.
**
** If any problems are encountered, an error is reported and the test stops.
** If no problems are seen after a large number of tests, we assume that
** the rollback mechanism is working.
*/
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <sched.h>
#include "sqlite.h"
static void do_some_sql(int parent){
char *zErr;
int rc = SQLITE_OK;
sqlite *db;
int cnt = 0;
static char zBig[] =
"-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
"-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
if( access("./test.db-journal",0)==0 ){
/*printf("pid %d: journal exists. rollback will be required\n",getpid());*/ unlink("test.db-saved");
system("cp test.db test.db-saved");
unlink("test.db-journal-saved");
system("cp test.db-journal test.db-journal-saved");
}
db = sqlite_open("./test.db", 0, &zErr);
if( db==0 ){
printf("ERROR: %s\n", zErr);
if( strcmp(zErr,"database disk image is malformed")==0 ){
kill(parent, SIGKILL);
}
exit(1);
}
srand(getpid());
while( rc==SQLITE_OK ){
cnt++;
rc = sqlite_exec_printf(db,
"INSERT INTO t1 VALUES(%d,'%d%s')", 0, 0, &zErr,
rand(), rand(), zBig);
}
if( rc!=SQLITE_OK ){
printf("ERROR #%d: %s\n", rc, zErr);
if( rc==SQLITE_CORRUPT ){
kill(parent, SIGKILL);
}
}
printf("pid %d: cnt=%d\n", getpid(), cnt);
}
int main(int argc, char **argv){
int i;
sqlite *db;
char *zErr;
int status;
int parent = getpid();
unlink("test.db");
unlink("test.db-journal");
db = sqlite_open("test.db", 0, &zErr);
if( db==0 ){
printf("Cannot initialize: %s\n", zErr);
return 1;
}
sqlite_exec(db, "CREATE TABLE t1(a,b)", 0, 0, 0);
sqlite_close(db);
for(i=0; i<10000; i++){
int pid = fork();
if( pid==0 ){
sched_yield();
do_some_sql(parent);
return 0;
}
printf("test %d, pid=%d\n", i, pid);
usleep(rand()%10000 + 1000);
kill(pid, SIGKILL);
waitpid(pid, &status, 0);
}
return 0;
}