This repository has been archived by the owner on Jun 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
testForUpdate.go
141 lines (123 loc) · 2.47 KB
/
testForUpdate.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package tests
import (
"sync"
"testing"
"time"
"github.com/go-xorm/core"
"github.com/go-xorm/xorm"
)
type ForUpdate struct {
Id int64 `xorm:"pk"`
Name string
}
func setupForUpdate(engine *xorm.Engine) error {
v := new(ForUpdate)
err := engine.DropTables(v)
if err != nil {
return err
}
err = engine.CreateTables(v)
if err != nil {
return err
}
list := []ForUpdate{
{1, "data1"},
{2, "data2"},
{3, "data3"},
}
for _, f := range list {
_, err = engine.Insert(f)
if err != nil {
return err
}
}
return nil
}
func TestForUpdate(engine *xorm.Engine, t *testing.T) {
testForUpdate(engine, t)
}
func testForUpdate(engine *xorm.Engine, t *testing.T) {
if engine.DriverName() == "tidb" || engine.DriverName() == "sqlite3" || engine.Dialect().DBType() == core.MSSQL {
return
}
err := setupForUpdate(engine)
if err != nil {
t.Error(err)
return
}
session1 := engine.NewSession()
session2 := engine.NewSession()
session3 := engine.NewSession()
defer session1.Close()
defer session2.Close()
defer session3.Close()
// start transaction
err = session1.Begin()
if err != nil {
t.Error(err)
return
}
// use lock
fList := make([]ForUpdate, 0)
session1.ForUpdate()
session1.Where("(id) = ?", 1)
err = session1.Find(&fList)
switch {
case err != nil:
t.Error(err)
return
case len(fList) != 1:
t.Errorf("find not returned single row")
return
case fList[0].Name != "data1":
t.Errorf("for_update.name must be `data1`")
return
}
// wait for lock
wg := &sync.WaitGroup{}
// lock is used
wg.Add(1)
go func() {
f2 := new(ForUpdate)
session2.Where("(id) = ?", 1).ForUpdate()
has, err := session2.Get(f2) // wait release lock
switch {
case err != nil:
t.Error(err)
case !has:
t.Errorf("cannot find target row. for_update.id = 1")
case f2.Name != "updated by session1":
t.Errorf("read lock failed")
}
wg.Done()
}()
// lock is NOT used
wg.Add(1)
go func() {
f3 := new(ForUpdate)
session3.Where("(id) = ?", 1)
has, err := session3.Get(f3) // wait release lock
switch {
case err != nil:
t.Error(err)
case !has:
t.Errorf("cannot find target row. for_update.id = 1")
case f3.Name != "data1":
t.Errorf("read lock failed")
}
wg.Done()
}()
// wait for go rountines
time.Sleep(50 * time.Millisecond)
f := new(ForUpdate)
f.Name = "updated by session1"
session1.Where("(id) = ?", 1)
session1.Update(f)
// release lock
err = session1.Commit()
if err != nil {
t.Error(err)
return
}
wg.Wait()
}