-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathdbcleaner.go
179 lines (146 loc) · 4.27 KB
/
dbcleaner.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
// Package dbcleaner helps cleaning up database's tables upon unit test.
// With the help of https://github.com/stretchr/testify/tree/master/suite, we can easily
// acquire the tables using in the test in SetupTest or SetupSuite, and cleanup all data
// using TearDownTest or TearDownSuite
package dbcleaner
import (
"context"
"errors"
"fmt"
"sync"
"time"
filemutex "github.com/alexflint/go-filemutex"
"github.com/khaiql/dbcleaner/engine"
"github.com/khaiql/dbcleaner/logging"
)
// DbCleaner interface
type DbCleaner interface {
// SetEngine sets dbEngine, can be mysql, postgres...
SetEngine(dbEngine engine.Engine)
// Acquire will lock tables passed in params so data in the table would not be deleted by other test cases
Acquire(tables ...string)
// Clean calls Truncate the tables
Clean(tables ...string)
// Close calls corresponding method on dbEngine to release connection to db
Close() error
}
var (
// ErrTableNeverLockBefore is paniced if calling Release on table that havent' been acquired before
ErrTableNeverLockBefore = errors.New("table has never been locked before")
)
// New returns a default Cleaner with Noop Engine. Call SetEngine to set an actual working engine
func New(opts ...Option) DbCleaner {
options := &Options{
Logger: &logging.Noop{},
LockTimeout: 10 * time.Second,
NumberOfRetry: 5,
RetryInterval: 10 * time.Second,
LockFileDir: "/tmp/",
}
for _, opt := range opts {
opt(options)
}
return &cleanerImpl{
locks: sync.Map{},
dbEngine: &engine.NoOp{},
options: options,
}
}
type cleanerImpl struct {
locks sync.Map
dbEngine engine.Engine
options *Options
}
func (c *cleanerImpl) loadFileMutexForTable(table string) (*filemutex.FileMutex, error) {
fmutex, err := filemutex.New(c.options.LockFileDir + table + ".lock")
if err != nil {
return nil, err
}
value, _ := c.locks.LoadOrStore(table, fmutex)
return value.(*filemutex.FileMutex), nil
}
func (c *cleanerImpl) SetEngine(dbEngine engine.Engine) {
c.dbEngine = dbEngine
}
func (c *cleanerImpl) acquireTable(ctx context.Context, table string) error {
c.options.Logger.Println("Acquiring table %s", table)
f := func(locker *filemutex.FileMutex, d chan struct{}) {
locker.Lock()
d <- struct{}{}
}
if err := c.actOnTable(table, f); err != nil {
return fmt.Errorf("error %s on acquire lock for table %s", err.Error(), table)
}
c.options.Logger.Println("Acquired lock on table %s", table)
return nil
}
func (c *cleanerImpl) actOnTable(table string, f func(locker *filemutex.FileMutex, doneChan chan struct{})) error {
locker, err := c.loadFileMutexForTable(table)
if err != nil {
panic(err)
}
done := make(chan struct{})
ctx, cancel := context.WithTimeout(context.Background(), c.options.LockTimeout)
defer cancel()
go f(locker, done)
for {
select {
case <-done:
return nil
case <-ctx.Done():
return ctx.Err()
}
}
}
func (c *cleanerImpl) releaseTable(table string) error {
f := func(locker *filemutex.FileMutex, done chan struct{}) {
locker.Unlock()
locker.Close()
c.locks.Delete(table)
done <- struct{}{}
}
if err := c.actOnTable(table, f); err != nil {
return fmt.Errorf("releaseTable %s error=%s", table, err.Error())
}
return nil
}
func (c *cleanerImpl) Acquire(tables ...string) {
tried := 0
for tried < c.options.NumberOfRetry {
tried++
c.options.Logger.Println("Trying to acquire %d times\n", tried)
var err error
acquiredTables := []string{}
for _, table := range tables {
err = c.acquireTable(context.Background(), table)
if err != nil {
break
}
acquiredTables = append(acquiredTables, table)
}
if err == nil {
return
}
c.options.Logger.Println("Failed to acquired with error=%s", err.Error())
for _, table := range acquiredTables {
c.releaseTable(table)
}
time.Sleep(c.options.RetryInterval)
}
panic(fmt.Errorf("failed to ACQUIRE tables %v after %d times", tables, tried))
}
func (c *cleanerImpl) Clean(tables ...string) {
for _, table := range tables {
c.options.Logger.Println("Truncate table %s", table)
if err := c.dbEngine.Truncate(table); err != nil {
panic(err)
}
if err := c.releaseTable(table); err != nil {
panic(err)
}
c.options.Logger.Println("Released lock for table %s", table)
}
}
func (c *cleanerImpl) Close() error {
return c.dbEngine.Close()
}