sqlite3_opt_unlock_notify_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // Copyright (C) 2018 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
  2. //
  3. // Use of this source code is governed by an MIT-style
  4. // license that can be found in the LICENSE file.
  5. // +build sqlite_unlock_notify
  6. package sqlite3
  7. import (
  8. "database/sql"
  9. "fmt"
  10. "os"
  11. "sync"
  12. "testing"
  13. "time"
  14. )
  15. func TestUnlockNotify(t *testing.T) {
  16. tempFilename := TempFilename(t)
  17. defer os.Remove(tempFilename)
  18. dsn := fmt.Sprintf("file:%s?cache=shared&mode=rwc&_busy_timeout=%d", tempFilename, 500)
  19. db, err := sql.Open("sqlite3", dsn)
  20. if err != nil {
  21. t.Fatal("Failed to open database:", err)
  22. }
  23. defer db.Close()
  24. _, err = db.Exec("CREATE TABLE foo(id INTEGER, status INTEGER)")
  25. if err != nil {
  26. t.Fatal("Failed to create table:", err)
  27. }
  28. tx, err := db.Begin()
  29. if err != nil {
  30. t.Fatal("Failed to begin transaction:", err)
  31. }
  32. _, err = tx.Exec("INSERT INTO foo(id, status) VALUES(1, 100)")
  33. if err != nil {
  34. t.Fatal("Failed to insert null:", err)
  35. }
  36. _, err = tx.Exec("UPDATE foo SET status = 200 WHERE id = 1")
  37. if err != nil {
  38. t.Fatal("Failed to update table:", err)
  39. }
  40. wg := sync.WaitGroup{}
  41. wg.Add(1)
  42. timer := time.NewTimer(500 * time.Millisecond)
  43. go func() {
  44. <-timer.C
  45. err := tx.Commit()
  46. if err != nil {
  47. t.Fatal("Failed to commit transaction:", err)
  48. }
  49. wg.Done()
  50. }()
  51. rows, err := db.Query("SELECT count(*) from foo")
  52. if err != nil {
  53. t.Fatal("Unable to query foo table:", err)
  54. }
  55. if rows.Next() {
  56. var count int
  57. if err := rows.Scan(&count); err != nil {
  58. t.Fatal("Failed to Scan rows", err)
  59. }
  60. }
  61. if err := rows.Err(); err != nil {
  62. t.Fatal("Failed at the call to Next:", err)
  63. }
  64. wg.Wait()
  65. }
  66. func TestUnlockNotifyMany(t *testing.T) {
  67. tempFilename := TempFilename(t)
  68. defer os.Remove(tempFilename)
  69. dsn := fmt.Sprintf("file:%s?cache=shared&mode=rwc&_busy_timeout=%d", tempFilename, 500)
  70. db, err := sql.Open("sqlite3", dsn)
  71. if err != nil {
  72. t.Fatal("Failed to open database:", err)
  73. }
  74. defer db.Close()
  75. _, err = db.Exec("CREATE TABLE foo(id INTEGER, status INTEGER)")
  76. if err != nil {
  77. t.Fatal("Failed to create table:", err)
  78. }
  79. tx, err := db.Begin()
  80. if err != nil {
  81. t.Fatal("Failed to begin transaction:", err)
  82. }
  83. _, err = tx.Exec("INSERT INTO foo(id, status) VALUES(1, 100)")
  84. if err != nil {
  85. t.Fatal("Failed to insert null:", err)
  86. }
  87. _, err = tx.Exec("UPDATE foo SET status = 200 WHERE id = 1")
  88. if err != nil {
  89. t.Fatal("Failed to update table:", err)
  90. }
  91. wg := sync.WaitGroup{}
  92. wg.Add(1)
  93. timer := time.NewTimer(500 * time.Millisecond)
  94. go func() {
  95. <-timer.C
  96. err := tx.Commit()
  97. if err != nil {
  98. t.Fatal("Failed to commit transaction:", err)
  99. }
  100. wg.Done()
  101. }()
  102. const concurrentQueries = 1000
  103. wg.Add(concurrentQueries)
  104. for i := 0; i < concurrentQueries; i++ {
  105. go func() {
  106. rows, err := db.Query("SELECT count(*) from foo")
  107. if err != nil {
  108. t.Fatal("Unable to query foo table:", err)
  109. }
  110. if rows.Next() {
  111. var count int
  112. if err := rows.Scan(&count); err != nil {
  113. t.Fatal("Failed to Scan rows", err)
  114. }
  115. }
  116. if err := rows.Err(); err != nil {
  117. t.Fatal("Failed at the call to Next:", err)
  118. }
  119. wg.Done()
  120. }()
  121. }
  122. wg.Wait()
  123. }
  124. func TestUnlockNotifyDeadlock(t *testing.T) {
  125. tempFilename := TempFilename(t)
  126. defer os.Remove(tempFilename)
  127. dsn := fmt.Sprintf("file:%s?cache=shared&mode=rwc&_busy_timeout=%d", tempFilename, 500)
  128. db, err := sql.Open("sqlite3", dsn)
  129. if err != nil {
  130. t.Fatal("Failed to open database:", err)
  131. }
  132. defer db.Close()
  133. _, err = db.Exec("CREATE TABLE foo(id INTEGER, status INTEGER)")
  134. if err != nil {
  135. t.Fatal("Failed to create table:", err)
  136. }
  137. tx, err := db.Begin()
  138. if err != nil {
  139. t.Fatal("Failed to begin transaction:", err)
  140. }
  141. _, err = tx.Exec("INSERT INTO foo(id, status) VALUES(1, 100)")
  142. if err != nil {
  143. t.Fatal("Failed to insert null:", err)
  144. }
  145. _, err = tx.Exec("UPDATE foo SET status = 200 WHERE id = 1")
  146. if err != nil {
  147. t.Fatal("Failed to update table:", err)
  148. }
  149. wg := sync.WaitGroup{}
  150. wg.Add(1)
  151. timer := time.NewTimer(500 * time.Millisecond)
  152. go func() {
  153. <-timer.C
  154. err := tx.Commit()
  155. if err != nil {
  156. t.Fatal("Failed to commit transaction:", err)
  157. }
  158. wg.Done()
  159. }()
  160. wg.Add(1)
  161. go func() {
  162. tx2, err := db.Begin()
  163. if err != nil {
  164. t.Fatal("Failed to begin transaction:", err)
  165. }
  166. defer tx2.Rollback()
  167. _, err = tx2.Exec("DELETE FROM foo")
  168. if err != nil {
  169. t.Fatal("Failed to delete table:", err)
  170. }
  171. err = tx2.Commit()
  172. if err != nil {
  173. t.Fatal("Failed to commit transaction:", err)
  174. }
  175. wg.Done()
  176. }()
  177. rows, err := tx.Query("SELECT count(*) from foo")
  178. if err != nil {
  179. t.Fatal("Unable to query foo table:", err)
  180. }
  181. if rows.Next() {
  182. var count int
  183. if err := rows.Scan(&count); err != nil {
  184. t.Fatal("Failed to Scan rows", err)
  185. }
  186. }
  187. if err := rows.Err(); err != nil {
  188. t.Fatal("Failed at the call to Next:", err)
  189. }
  190. wg.Wait()
  191. }