-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmysql_mutex_test.go
More file actions
74 lines (66 loc) · 1.27 KB
/
mysql_mutex_test.go
File metadata and controls
74 lines (66 loc) · 1.27 KB
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
package main
import (
"errors"
"testing"
)
type dbMock struct {
mockError bool
mockValue int
}
func (d *dbMock) Query(query string, args ...interface{}) (QueryableResponse, error) {
if d.mockError {
return nil, errors.New("Terrible crash")
}
return &respMock{
mockValue: d.mockValue,
rowsAvailable: 1,
}, nil
}
type respMock struct {
mockValue int
rowsAvailable int
}
func (r *respMock) Close() error {
return nil
}
func (r *respMock) Next() bool {
haveSome := r.rowsAvailable > 0
r.rowsAvailable--
return haveSome
}
func (r *respMock) Scan(dest ...interface{}) error {
*(dest[0].(*int)) = r.mockValue
return nil
}
func TestLockDBError(t *testing.T) {
db := &dbMock{
mockError: true,
}
mutex := NewMysqlMutex(db, "foo", 0)
err := mutex.Lock()
if err == nil {
t.Errorf("Expected error on query error.\n")
}
}
func TestLockAcquired(t *testing.T) {
db := &dbMock{
mockError: false,
mockValue: 1,
}
mutex := NewMysqlMutex(db, "foo", 0)
err := mutex.Lock()
if err != nil {
t.Errorf("Error '%s' was unexpected", err)
}
}
func TestLockNotAcquired(t *testing.T) {
db := &dbMock{
mockError: false,
mockValue: 0,
}
mutex := NewMysqlMutex(db, "foo", 0)
err := mutex.Lock()
if err == nil {
t.Errorf("Expected error on query error.\n")
}
}