Skip to content

Commit da8491f

Browse files
authored
Merge pull request #65 from go-kivik/race
Add locking around db name changes to prevent race
2 parents 5f9cf60 + 0867ab6 commit da8491f

File tree

4 files changed

+11
-0
lines changed

4 files changed

+11
-0
lines changed

client_gen.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,9 @@ func (c *driverClient) DB(ctx context.Context, arg0 string, options map[string]i
190190
if err := c.nextExpectation(expected); err != nil {
191191
return nil, err
192192
}
193+
expected.ret0.mu.Lock()
193194
expected.ret0.name = arg0
195+
expected.ret0.mu.Unlock()
194196
if expected.callback != nil {
195197
return expected.callback(ctx, arg0, options)
196198
}

dbmock.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package kivikmock
22

3+
import "sync"
4+
35
// MockDB is deprecated
46
type MockDB = DB
57

@@ -10,6 +12,7 @@ type DB struct {
1012
id int
1113
client *Client
1214
count int
15+
mu sync.RWMutex
1316
}
1417

1518
func (db *DB) expectations() int {

gen/templates/drivermethod.tmpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ func ({{if .DBMethod}}db *driverDB{{else}}c *driverClient{{end}}) {{ .Name }}({{
66
return {{ .ZeroReturns }}
77
}
88
{{- if eq .Name "DB" }}
9+
expected.ret0.mu.Lock()
910
expected.ret0.name = arg0
11+
expected.ret0.mu.Unlock()
1012
{{- end }}
1113
if expected.callback != nil {
1214
return expected.callback({{.CallbackArgs}})

meets.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ func dbMeetsExpectation(a, e *DB) bool {
2727
if e == nil {
2828
return true
2929
}
30+
e.mu.RLock()
31+
defer e.mu.RUnlock()
32+
a.mu.RLock()
33+
defer a.mu.RUnlock()
3034
return e.name == a.name && e.id == a.id
3135
}
3236

0 commit comments

Comments
 (0)