-
Notifications
You must be signed in to change notification settings - Fork 1
/
flock_test.go
67 lines (56 loc) · 1.68 KB
/
flock_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
package flock
import (
"encoding/json"
"errors"
"github.com/golang/mock/gomock"
"github.com/opensvc/locker"
mockLocker "github.com/opensvc/locker/mock_locker"
"github.com/stretchr/testify/assert"
"testing"
"time"
)
func setup(t *testing.T) (prov func(string) locker.Locker, lck *mockLocker.MockLocker) {
ctrl := gomock.NewController(t)
lck = mockLocker.NewMockLocker(ctrl)
prov = func(string) locker.Locker {
return lck
}
return
}
func TestLock(t *testing.T) {
t.Run("Ensure write data to lock file when lock succeed", func(t *testing.T) {
prov, mockLck := setup(t)
lck := New("foo.lck", "session1", prov)
var b []byte
mockLck.EXPECT().LockContext(gomock.Any(), 500*time.Millisecond).Return(nil)
mockLck.EXPECT().
Write(gomock.AssignableToTypeOf(b)).
Do(func(arg []byte) {
b = arg
}).
Return(0, nil)
err := lck.Lock(100*time.Millisecond, "intent1")
assert.Equal(t, nil, err)
found := meta{}
if err := json.Unmarshal(b, &found); err != nil {
t.Fatalf("expected written data : %+v\n", b)
}
assert.Equal(t, "intent1", found.Intent)
assert.Equal(t, "session1", found.SessionID)
})
t.Run("Ensure return error if lock fail", func(t *testing.T) {
prov, mockLck := setup(t)
mockLck.EXPECT().LockContext(gomock.Any(), gomock.Any()).Return(errors.New("can't lock"))
err := New("foo.lck", "sessionId2", prov).Lock(100*time.Millisecond, "intent1")
assert.Equal(t, errors.New("can't lock"), err)
})
}
func TestUnLock(t *testing.T) {
t.Run("Ensure unlock succeed", func(t *testing.T) {
prov, mockLck := setup(t)
mockLck.EXPECT().UnLock().Return(nil)
l := New("foo.lck", "sessionX", prov)
err := l.UnLock()
assert.Equal(t, nil, err)
})
}