forked from patrickxb/fgosqlite
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathbusy_test.go
119 lines (102 loc) · 3.11 KB
/
busy_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package sqlite_test
import (
"io/ioutil"
"os"
"testing"
"time"
"github.com/bmizerany/assert"
. "github.com/gwenn/gosqlite"
)
func TestInterrupt(t *testing.T) {
skipIfCgoCheckActive(t)
db := open(t)
defer checkClose(db, t)
db.CreateScalarFunction("interrupt", 0, false, nil, func(ctx *ScalarContext, nArg int) {
db.Interrupt()
ctx.ResultText("ok")
}, nil)
s, err := db.Prepare("SELECT interrupt() FROM (SELECT 1 UNION SELECT 2 UNION SELECT 3)")
checkNoError(t, err, "couldn't prepare stmt: %#v")
defer checkFinalize(s, t)
err = s.Select(func(s *Stmt) (err error) {
return
})
if err == nil {
t.Fatalf("got %v; want interrupt", err)
}
//println(err.Error())
if se, ok := err.(StmtError); !ok || se.Code() != ErrInterrupt {
t.Errorf("got %#v; want interrupt", err)
}
}
func openTwoConnSameDb(t *testing.T) (*os.File, *Conn, *Conn) {
f, err := ioutil.TempFile("", "gosqlite-test")
checkNoError(t, err, "couldn't create temp file: %s")
checkNoError(t, f.Close(), "couldn't close temp file: %s")
db1, err := Open(f.Name(), OpenReadWrite, OpenCreate, OpenFullMutex)
checkNoError(t, err, "couldn't open database file: %s")
db2, err := Open(f.Name(), OpenReadWrite, OpenCreate, OpenFullMutex)
checkNoError(t, err, "couldn't open database file: %s")
return f, db1, db2
}
func TestDefaultBusy(t *testing.T) {
f, db1, db2 := openTwoConnSameDb(t)
defer os.Remove(f.Name())
defer checkClose(db1, t)
defer checkClose(db2, t)
checkNoError(t, db1.BeginTransaction(Exclusive), "couldn't begin transaction: %s")
defer db1.Rollback()
_, err := db2.SchemaVersion("")
if err == nil {
t.Fatalf("got %v; want lock", err)
}
if se, ok := err.(StmtError); !ok || se.Code() != ErrBusy {
t.Fatalf("got %#v; want lock", err)
}
}
func TestBusyTimeout(t *testing.T) {
f, db1, db2 := openTwoConnSameDb(t)
defer os.Remove(f.Name())
defer checkClose(db1, t)
defer checkClose(db2, t)
checkNoError(t, db1.BeginTransaction(Exclusive), "couldn't begin transaction: %s")
//join := make(chan bool)
checkNoError(t, db2.BusyTimeout(500*time.Millisecond), "couldn't set busy timeout: %s")
go func() {
time.Sleep(time.Millisecond)
db1.Rollback()
//join <- true
}()
_, err := db2.SchemaVersion("")
checkNoError(t, err, "couldn't query schema version: %#v")
//<- join
}
func TestBusyHandler(t *testing.T) {
skipIfCgoCheckActive(t)
f, db1, db2 := openTwoConnSameDb(t)
defer os.Remove(f.Name())
defer checkClose(db1, t)
defer checkClose(db2, t)
//c := make(chan bool)
var called bool
_ = db2.BusyHandler(func(udp interface{}, count int) bool {
if b, ok := udp.(*bool); ok {
*b = true
}
//c <- true
time.Sleep(time.Millisecond)
return true
}, &called)
checkNoError(t, db1.BeginTransaction(Exclusive), "couldn't begin transaction: %s")
go func() {
time.Sleep(time.Millisecond)
//_ = <- c
db1.Rollback()
}()
_, err := db2.SchemaVersion("")
checkNoError(t, err, "couldn't query schema version: %#v")
assert.T(t, called, "expected busy handler to be called")
}