Merge updates from trunk.
[sqlite.git] / test / tt3_lookaside1.c
blobe785e7998baa958c70d36ba1c144009681f9fee2
1 /*
2 ** 2014 December 9
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.
11 *************************************************************************
13 ** lookaside1
17 ** The test in this file attempts to expose a specific race condition
18 ** that is suspected to exist at time of writing.
21 static char *lookaside1_thread_reader(int iTid, void *pArg){
22 Error err = {0}; /* Error code and message */
23 Sqlite db = {0}; /* SQLite database connection */
25 opendb(&err, &db, "test.db", 0);
27 while( !timetostop(&err) ){
28 sqlite3_stmt *pStmt = 0;
29 int rc;
31 sqlite3_prepare_v2(db.db, "SELECT 1 FROM t1", -1, &pStmt, 0);
32 while( sqlite3_step(pStmt)==SQLITE_ROW ){
33 execsql(&err, &db, "SELECT length(x||y||z) FROM t2");
35 rc = sqlite3_finalize(pStmt);
36 if( err.rc==SQLITE_OK && rc!=SQLITE_OK ){
37 sqlite_error(&err, &db, "finalize");
41 closedb(&err, &db);
42 print_and_free_err(&err);
43 return sqlite3_mprintf("ok");
46 static char *lookaside1_thread_writer(int iTid, void *pArg){
47 Error err = {0}; /* Error code and message */
48 Sqlite db = {0}; /* SQLite database connection */
50 opendb(&err, &db, "test.db", 0);
52 do{
53 sql_script(&err, &db,
54 "BEGIN;"
55 "UPDATE t3 SET i=i+1 WHERE x=1;"
56 "ROLLBACK;"
58 }while( !timetostop(&err) );
60 closedb(&err, &db);
61 print_and_free_err(&err);
62 return sqlite3_mprintf("ok");
66 static void lookaside1(int nMs){
67 Error err = {0};
68 Sqlite db = {0};
69 Threadset threads = {0};
71 opendb(&err, &db, "test.db", 1);
72 sql_script(&err, &db,
73 "CREATE TABLE t1(x PRIMARY KEY) WITHOUT ROWID;"
74 "WITH data(x,y) AS ("
75 " SELECT 1, quote(randomblob(750)) UNION ALL "
76 " SELECT x*2, y||y FROM data WHERE x<5) "
77 "INSERT INTO t1 SELECT y FROM data;"
79 "CREATE TABLE t3(x PRIMARY KEY,i) WITHOUT ROWID;"
80 "INSERT INTO t3 VALUES(1, 1);"
82 "CREATE TABLE t2(x,y,z);"
83 "INSERT INTO t2 VALUES(randomblob(50), randomblob(50), randomblob(50));"
85 closedb(&err, &db);
87 setstoptime(&err, nMs);
89 sqlite3_enable_shared_cache(1);
90 launch_thread(&err, &threads, lookaside1_thread_reader, 0);
91 launch_thread(&err, &threads, lookaside1_thread_reader, 0);
92 launch_thread(&err, &threads, lookaside1_thread_reader, 0);
93 launch_thread(&err, &threads, lookaside1_thread_reader, 0);
94 launch_thread(&err, &threads, lookaside1_thread_reader, 0);
95 launch_thread(&err, &threads, lookaside1_thread_writer, 0);
96 join_all_threads(&err, &threads);
97 sqlite3_enable_shared_cache(0);
98 print_and_free_err(&err);