crashtest1.c 2.7 KB

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