exclusive.test 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. # 2007 March 24
  2. #
  3. # The author disclaims copyright to this source code. In place of
  4. # a legal notice, here is a blessing:
  5. #
  6. # May you do good and not evil.
  7. # May you find forgiveness for yourself and forgive others.
  8. # May you share freely, never taking more than you give.
  9. #
  10. #***********************************************************************
  11. # This file implements regression tests for SQLite library. The focus
  12. # of these tests is exclusive access mode (i.e. the thing activated by
  13. # "PRAGMA locking_mode = EXCLUSIVE").
  14. #
  15. # $Id: exclusive.test,v 1.15 2009/06/26 12:30:40 danielk1977 Exp $
  16. set testdir [file dirname $argv0]
  17. source $testdir/tester.tcl
  18. ifcapable {!pager_pragmas} {
  19. finish_test
  20. return
  21. }
  22. forcedelete test2.db-journal
  23. forcedelete test2.db
  24. forcedelete test3.db-journal
  25. forcedelete test3.db
  26. forcedelete test4.db-journal
  27. forcedelete test4.db
  28. #----------------------------------------------------------------------
  29. # Test cases exclusive-1.X test the PRAGMA logic.
  30. #
  31. do_test exclusive-1.0 {
  32. execsql {
  33. pragma locking_mode;
  34. pragma main.locking_mode;
  35. pragma temp.locking_mode;
  36. }
  37. } [list normal normal exclusive]
  38. do_test exclusive-1.1 {
  39. execsql {
  40. pragma locking_mode = exclusive;
  41. }
  42. } {exclusive}
  43. do_test exclusive-1.2 {
  44. execsql {
  45. pragma locking_mode;
  46. pragma main.locking_mode;
  47. pragma temp.locking_mode;
  48. }
  49. } [list exclusive exclusive exclusive]
  50. do_test exclusive-1.3 {
  51. execsql {
  52. pragma locking_mode = normal;
  53. }
  54. } {normal}
  55. do_test exclusive-1.4 {
  56. execsql {
  57. pragma locking_mode;
  58. pragma main.locking_mode;
  59. pragma temp.locking_mode;
  60. }
  61. } [list normal normal exclusive]
  62. do_test exclusive-1.5 {
  63. execsql {
  64. pragma locking_mode = invalid;
  65. }
  66. } {normal}
  67. do_test exclusive-1.6 {
  68. execsql {
  69. pragma locking_mode;
  70. pragma main.locking_mode;
  71. pragma temp.locking_mode;
  72. }
  73. } [list normal normal exclusive]
  74. ifcapable attach {
  75. do_test exclusive-1.7 {
  76. execsql {
  77. pragma locking_mode = exclusive;
  78. ATTACH 'test2.db' as aux;
  79. }
  80. execsql {
  81. pragma main.locking_mode;
  82. pragma aux.locking_mode;
  83. }
  84. } {exclusive exclusive}
  85. do_test exclusive-1.8 {
  86. execsql {
  87. pragma main.locking_mode = normal;
  88. }
  89. execsql {
  90. pragma main.locking_mode;
  91. pragma temp.locking_mode;
  92. pragma aux.locking_mode;
  93. }
  94. } [list normal exclusive exclusive]
  95. do_test exclusive-1.9 {
  96. execsql {
  97. pragma locking_mode;
  98. }
  99. } {exclusive}
  100. do_test exclusive-1.10 {
  101. execsql {
  102. ATTACH 'test3.db' as aux2;
  103. }
  104. execsql {
  105. pragma main.locking_mode;
  106. pragma aux.locking_mode;
  107. pragma aux2.locking_mode;
  108. }
  109. } {normal exclusive exclusive}
  110. do_test exclusive-1.11 {
  111. execsql {
  112. pragma aux.locking_mode = normal;
  113. }
  114. execsql {
  115. pragma main.locking_mode;
  116. pragma aux.locking_mode;
  117. pragma aux2.locking_mode;
  118. }
  119. } {normal normal exclusive}
  120. do_test exclusive-1.12 {
  121. execsql {
  122. pragma locking_mode = normal;
  123. }
  124. execsql {
  125. pragma main.locking_mode;
  126. pragma temp.locking_mode;
  127. pragma aux.locking_mode;
  128. pragma aux2.locking_mode;
  129. }
  130. } [list normal exclusive normal normal]
  131. do_test exclusive-1.13 {
  132. execsql {
  133. ATTACH 'test4.db' as aux3;
  134. }
  135. execsql {
  136. pragma main.locking_mode;
  137. pragma temp.locking_mode;
  138. pragma aux.locking_mode;
  139. pragma aux2.locking_mode;
  140. pragma aux3.locking_mode;
  141. }
  142. } [list normal exclusive normal normal normal]
  143. do_test exclusive-1.99 {
  144. execsql {
  145. DETACH aux;
  146. DETACH aux2;
  147. DETACH aux3;
  148. }
  149. } {}
  150. }
  151. #----------------------------------------------------------------------
  152. # Test cases exclusive-2.X verify that connections in exclusive
  153. # locking_mode do not relinquish locks.
  154. #
  155. do_test exclusive-2.0 {
  156. execsql {
  157. CREATE TABLE abc(a, b, c);
  158. INSERT INTO abc VALUES(1, 2, 3);
  159. PRAGMA locking_mode = exclusive;
  160. }
  161. } {exclusive}
  162. do_test exclusive-2.1 {
  163. sqlite3 db2 test.db
  164. execsql {
  165. INSERT INTO abc VALUES(4, 5, 6);
  166. SELECT * FROM abc;
  167. } db2
  168. } {1 2 3 4 5 6}
  169. do_test exclusive-2.2 {
  170. # This causes connection 'db' (in exclusive mode) to establish
  171. # a shared-lock on the db. The other connection should now be
  172. # locked out as a writer.
  173. execsql {
  174. SELECT * FROM abc;
  175. } db
  176. } {1 2 3 4 5 6}
  177. do_test exclusive-2.4 {
  178. execsql {
  179. SELECT * FROM abc;
  180. } db2
  181. } {1 2 3 4 5 6}
  182. do_test exclusive-2.5 {
  183. catchsql {
  184. INSERT INTO abc VALUES(7, 8, 9);
  185. } db2
  186. } {1 {database is locked}}
  187. sqlite3_soft_heap_limit 0
  188. do_test exclusive-2.6 {
  189. # Because connection 'db' only has a shared-lock, the other connection
  190. # will be able to get a RESERVED, but will fail to upgrade to EXCLUSIVE.
  191. execsql {
  192. BEGIN;
  193. INSERT INTO abc VALUES(7, 8, 9);
  194. } db2
  195. catchsql {
  196. COMMIT
  197. } db2
  198. } {1 {database is locked}}
  199. do_test exclusive-2.7 {
  200. catchsql {
  201. COMMIT
  202. } db2
  203. } {1 {database is locked}}
  204. do_test exclusive-2.8 {
  205. execsql {
  206. ROLLBACK;
  207. } db2
  208. } {}
  209. sqlite3_soft_heap_limit $cmdlinearg(soft-heap-limit)
  210. do_test exclusive-2.9 {
  211. # Write the database to establish the exclusive lock with connection 'db.
  212. execsql {
  213. INSERT INTO abc VALUES(7, 8, 9);
  214. } db
  215. catchsql {
  216. SELECT * FROM abc;
  217. } db2
  218. } {1 {database is locked}}
  219. do_test exclusive-2.10 {
  220. # Changing the locking-mode does not release any locks.
  221. execsql {
  222. PRAGMA locking_mode = normal;
  223. } db
  224. catchsql {
  225. SELECT * FROM abc;
  226. } db2
  227. } {1 {database is locked}}
  228. do_test exclusive-2.11 {
  229. # After changing the locking mode, accessing the db releases locks.
  230. execsql {
  231. SELECT * FROM abc;
  232. } db
  233. execsql {
  234. SELECT * FROM abc;
  235. } db2
  236. } {1 2 3 4 5 6 7 8 9}
  237. db2 close
  238. #----------------------------------------------------------------------
  239. # Tests exclusive-3.X - test that a connection in exclusive mode
  240. # truncates instead of deletes the journal file when committing
  241. # a transaction.
  242. #
  243. # These tests are not run on windows because the windows backend
  244. # opens the journal file for exclusive access, preventing its contents
  245. # from being inspected externally.
  246. #
  247. if {$tcl_platform(platform) != "windows"} {
  248. # Return a list of two booleans (either 0 or 1). The first is true
  249. # if the named file exists. The second is true only if the file
  250. # exists and the first 28 bytes contain at least one non-zero byte.
  251. #
  252. proc filestate {fname} {
  253. set exists 0
  254. set content 0
  255. if {[file exists $fname]} {
  256. set exists 1
  257. set hdr [hexio_read $fname 0 28]
  258. set content [expr {0==[string match $hdr [string repeat 0 56]]}]
  259. }
  260. list $exists $content
  261. }
  262. do_test exclusive-3.0 {
  263. filestate test.db-journal
  264. } {0 0}
  265. do_test exclusive-3.1 {
  266. execsql {
  267. PRAGMA locking_mode = exclusive;
  268. BEGIN;
  269. DELETE FROM abc;
  270. }
  271. filestate test.db-journal
  272. } {1 1}
  273. do_test exclusive-3.2 {
  274. execsql {
  275. COMMIT;
  276. }
  277. filestate test.db-journal
  278. } {1 0}
  279. do_test exclusive-3.3 {
  280. execsql {
  281. INSERT INTO abc VALUES('A', 'B', 'C');
  282. SELECT * FROM abc;
  283. }
  284. } {A B C}
  285. do_test exclusive-3.4 {
  286. execsql {
  287. BEGIN;
  288. UPDATE abc SET a = 1, b = 2, c = 3;
  289. ROLLBACK;
  290. SELECT * FROM abc;
  291. }
  292. } {A B C}
  293. do_test exclusive-3.5 {
  294. filestate test.db-journal
  295. } {1 0}
  296. do_test exclusive-3.6 {
  297. execsql {
  298. PRAGMA locking_mode = normal;
  299. SELECT * FROM abc;
  300. }
  301. filestate test.db-journal
  302. } {0 0}
  303. }
  304. #----------------------------------------------------------------------
  305. # Tests exclusive-4.X - test that rollback works correctly when
  306. # in exclusive-access mode.
  307. #
  308. # The following procedure computes a "signature" for table "t3". If
  309. # T3 changes in any way, the signature should change.
  310. #
  311. # This is used to test ROLLBACK. We gather a signature for t3, then
  312. # make lots of changes to t3, then rollback and take another signature.
  313. # The two signatures should be the same.
  314. #
  315. proc signature {} {
  316. return [db eval {SELECT count(*), md5sum(x) FROM t3}]
  317. }
  318. do_test exclusive-4.0 {
  319. execsql { PRAGMA locking_mode = exclusive; }
  320. execsql { PRAGMA default_cache_size = 10; }
  321. execsql {
  322. BEGIN;
  323. CREATE TABLE t3(x TEXT);
  324. INSERT INTO t3 VALUES(randstr(10,400));
  325. INSERT INTO t3 VALUES(randstr(10,400));
  326. INSERT INTO t3 SELECT randstr(10,400) FROM t3;
  327. INSERT INTO t3 SELECT randstr(10,400) FROM t3;
  328. INSERT INTO t3 SELECT randstr(10,400) FROM t3;
  329. INSERT INTO t3 SELECT randstr(10,400) FROM t3;
  330. COMMIT;
  331. }
  332. execsql {SELECT count(*) FROM t3;}
  333. } {32}
  334. set ::X [signature]
  335. do_test exclusive-4.1 {
  336. execsql {
  337. BEGIN;
  338. DELETE FROM t3 WHERE random()%10!=0;
  339. INSERT INTO t3 SELECT randstr(10,10)||x FROM t3;
  340. INSERT INTO t3 SELECT randstr(10,10)||x FROM t3;
  341. SELECT count(*) FROM t3;
  342. ROLLBACK;
  343. }
  344. signature
  345. } $::X
  346. do_test exclusive-4.2 {
  347. execsql {
  348. BEGIN;
  349. DELETE FROM t3 WHERE random()%10!=0;
  350. INSERT INTO t3 SELECT randstr(10,10)||x FROM t3;
  351. DELETE FROM t3 WHERE random()%10!=0;
  352. INSERT INTO t3 SELECT randstr(10,10)||x FROM t3;
  353. ROLLBACK;
  354. }
  355. signature
  356. } $::X
  357. do_test exclusive-4.3 {
  358. execsql {
  359. INSERT INTO t3 SELECT randstr(10,400) FROM t3 WHERE random()%10==0;
  360. }
  361. } {}
  362. do_test exclusive-4.4 {
  363. catch {set ::X [signature]}
  364. } {0}
  365. do_test exclusive-4.5 {
  366. execsql {
  367. PRAGMA locking_mode = NORMAL;
  368. DROP TABLE t3;
  369. DROP TABLE abc;
  370. }
  371. } {normal}
  372. #----------------------------------------------------------------------
  373. # Tests exclusive-5.X - test that statement journals are truncated
  374. # instead of deleted when in exclusive access mode.
  375. #
  376. # Close and reopen the database so that the temp database is no
  377. # longer active.
  378. #
  379. db close
  380. sqlite3 db test.db
  381. # if we're using proxy locks, we use 3 filedescriptors for a db
  382. # that is open but NOT writing changes, normally
  383. # sqlite uses 1 (proxy locking adds the conch and the local lock)
  384. set using_proxy 0
  385. foreach {name value} [array get env SQLITE_FORCE_PROXY_LOCKING] {
  386. set using_proxy $value
  387. }
  388. set extrafds 0
  389. if {$using_proxy!=0} {
  390. set extrafds 2
  391. }
  392. do_test exclusive-5.0 {
  393. execsql {
  394. CREATE TABLE abc(a UNIQUE, b UNIQUE, c UNIQUE);
  395. BEGIN;
  396. INSERT INTO abc VALUES(1, 2, 3);
  397. INSERT INTO abc SELECT a+1, b+1, c+1 FROM abc;
  398. }
  399. } {}
  400. do_test exclusive-5.1 {
  401. # Three files are open: The db, journal and statement-journal.
  402. set sqlite_open_file_count
  403. expr $sqlite_open_file_count-$extrafds
  404. } [expr 3 - ($TEMP_STORE>=2)]
  405. do_test exclusive-5.2 {
  406. execsql {
  407. COMMIT;
  408. }
  409. # One file open: the db.
  410. set sqlite_open_file_count
  411. expr $sqlite_open_file_count-$extrafds
  412. } {1}
  413. do_test exclusive-5.3 {
  414. execsql {
  415. PRAGMA locking_mode = exclusive;
  416. BEGIN;
  417. INSERT INTO abc VALUES(5, 6, 7);
  418. }
  419. # Two files open: the db and journal.
  420. set sqlite_open_file_count
  421. expr $sqlite_open_file_count-$extrafds
  422. } {2}
  423. do_test exclusive-5.4 {
  424. execsql {
  425. INSERT INTO abc SELECT a+10, b+10, c+10 FROM abc;
  426. }
  427. # Three files are open: The db, journal and statement-journal.
  428. set sqlite_open_file_count
  429. expr $sqlite_open_file_count-$extrafds
  430. } [expr 3 - ($TEMP_STORE>=2)]
  431. do_test exclusive-5.5 {
  432. execsql {
  433. COMMIT;
  434. }
  435. # Three files are still open: The db, journal and statement-journal.
  436. set sqlite_open_file_count
  437. expr $sqlite_open_file_count-$extrafds
  438. } [expr 3 - ($TEMP_STORE>=2)]
  439. do_test exclusive-5.6 {
  440. execsql {
  441. PRAGMA locking_mode = normal;
  442. SELECT * FROM abc;
  443. }
  444. } {normal 1 2 3 2 3 4 5 6 7 11 12 13 12 13 14 15 16 17}
  445. do_test exclusive-5.7 {
  446. # Just the db open.
  447. set sqlite_open_file_count
  448. expr $sqlite_open_file_count-$extrafds
  449. } {1}
  450. #-------------------------------------------------------------------------
  451. do_execsql_test exclusive-6.1 {
  452. CREATE TABLE t4(a, b);
  453. INSERT INTO t4 VALUES('Eden', 1955);
  454. BEGIN;
  455. INSERT INTO t4 VALUES('Macmillan', 1957);
  456. INSERT INTO t4 VALUES('Douglas-Home', 1963);
  457. INSERT INTO t4 VALUES('Wilson', 1964);
  458. }
  459. do_test exclusive-6.2 {
  460. forcedelete test2.db test2.db-journal
  461. copy_file test.db test2.db
  462. copy_file test.db-journal test2.db-journal
  463. sqlite3 db test2.db
  464. } {}
  465. do_execsql_test exclusive-6.3 {
  466. PRAGMA locking_mode = EXCLUSIVE;
  467. SELECT * FROM t4;
  468. } {exclusive Eden 1955}
  469. do_test exclusive-6.4 {
  470. db close
  471. forcedelete test.db test.db-journal
  472. set fd [open test.db-journal w]
  473. puts $fd x
  474. close $fd
  475. sqlite3 db test.db
  476. } {}
  477. do_execsql_test exclusive-6.5 {
  478. PRAGMA locking_mode = EXCLUSIVE;
  479. SELECT * FROM sqlite_master;
  480. } {exclusive}
  481. finish_test