aboutsummaryrefslogtreecommitdiff
path: root/all_test.go
diff options
context:
space:
mode:
authorJan Mercl <0xjnml@gmail.com>2019-12-31 16:46:08 +0100
committerJan Mercl <0xjnml@gmail.com>2019-12-31 16:46:08 +0100
commitef38ac9c3be91d0b0c15bdcfa89e0469f7150ba0 (patch)
tree6917153245d6dceb223350fa0e37d73484dbaacc /all_test.go
parentea2990f2041f1564cf05b1e6510bdae19f2f25ff (diff)
use sqlite3_unlock_notify, fixes #20.
Diffstat (limited to 'all_test.go')
-rw-r--r--all_test.go72
1 files changed, 72 insertions, 0 deletions
diff --git a/all_test.go b/all_test.go
index 1c830c8..09e150d 100644
--- a/all_test.go
+++ b/all_test.go
@@ -11,11 +11,13 @@ import (
"flag"
"fmt"
"io/ioutil"
+ "math/rand"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
+ "runtime/debug"
"strconv"
"strings"
"sync"
@@ -55,6 +57,8 @@ func TODO(...interface{}) string { //TODOOK
return fmt.Sprintf("# TODO: %s:%d:\n", path.Base(fn), fl) //TODOOK
}
+func stack() string { return string(debug.Stack()) }
+
func use(...interface{}) {}
func init() {
@@ -651,3 +655,71 @@ INSERT INTO "products" ("id", "user_id", "name", "description", "created_at", "c
t.Fatalf("expected result for the enabled select query %d, we received %d\n", 3, count)
}
}
+
+func mustExec(t *testing.T, db *sql.DB, sql string, args ...interface{}) sql.Result {
+ res, err := db.Exec(sql, args...)
+ if err != nil {
+ t.Fatalf("Error running %q: %v", sql, err)
+ }
+
+ return res
+}
+
+// https://gitlab.com/cznic/sqlite/issues/20
+func TestIssue20(t *testing.T) {
+ const TablePrefix = "gosqltest_"
+
+ tempDir, err := ioutil.TempDir("", "")
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ defer os.RemoveAll(tempDir)
+
+ db, err := sql.Open("sqlite", filepath.Join(tempDir, "foo.db"))
+ if err != nil {
+ t.Fatalf("foo.db open fail: %v", err)
+ }
+
+ mustExec(t, db, "CREATE TABLE "+TablePrefix+"t (count INT)")
+ sel, err := db.PrepareContext(context.Background(), "SELECT count FROM "+TablePrefix+"t ORDER BY count DESC")
+ if err != nil {
+ t.Fatalf("prepare 1: %v", err)
+ }
+
+ ins, err := db.PrepareContext(context.Background(), "INSERT INTO "+TablePrefix+"t (count) VALUES (?)")
+ if err != nil {
+ t.Fatalf("prepare 2: %v", err)
+ }
+
+ for n := 1; n <= 3; n++ {
+ if _, err := ins.Exec(n); err != nil {
+ t.Fatalf("insert(%d) = %v", n, err)
+ }
+ }
+
+ const nRuns = 10
+ ch := make(chan bool)
+ for i := 0; i < nRuns; i++ {
+ go func() {
+ defer func() {
+ ch <- true
+ }()
+ for j := 0; j < 10; j++ {
+ count := 0
+ if err := sel.QueryRow().Scan(&count); err != nil && err != sql.ErrNoRows {
+ t.Errorf("Query: %v", err)
+ return
+ }
+
+ if _, err := ins.Exec(rand.Intn(100)); err != nil {
+ t.Errorf("Insert: %v", err)
+ return
+ }
+ }
+ }()
+ }
+ for i := 0; i < nRuns; i++ {
+ <-ch
+ }
+}