-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_mutex_unix_test.go
118 lines (98 loc) · 2.9 KB
/
file_mutex_unix_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
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
package locker
import (
"os"
"testing"
. "github.com/smartystreets/goconvey/convey" //nolint:golint,stylecheck
)
func TestFileMutexUnix(t *testing.T) {
Convey("Checking locker.NewFileMutex (unix)", t, func() {
fileLock := "./testdata/file.lock"
os.Remove(fileLock)
Convey("Checking the creation of the .lock file", func() {
stat, err := os.Stat(fileLock)
So(os.IsNotExist(err), ShouldBeTrue)
So(stat, ShouldBeNil)
mutex, err := NewFileMutex(fileLock)
So(err, ShouldBeNil)
So(mutex, ShouldNotBeNil)
stat, err = os.Stat(fileLock)
So(err, ShouldBeNil)
So(stat, ShouldNotBeNil)
Convey("Checking for mutex re-creation", func() {
mutex2, err := NewFileMutex(fileLock)
So(err, ShouldBeNil)
So(mutex2, ShouldNotBeNil)
})
Convey("Checking TryLock", func() {
So(mutex.Lock(), ShouldBeNil)
lock, err := mutex.TryLock()
So(err, ShouldBeNil)
So(lock, ShouldBeFalse)
So(mutex.Unlock(), ShouldBeNil)
lock, err = mutex.TryLock()
So(err, ShouldBeNil)
So(lock, ShouldBeTrue)
mutex2, err := NewFileMutex(fileLock)
So(err, ShouldBeNil)
So(mutex2, ShouldNotBeNil)
lock2, err := mutex2.TryLock()
So(err, ShouldBeNil)
So(lock2, ShouldBeFalse)
})
Convey("Checking Unlock - After Lock", func() {
So(mutex.Lock(), ShouldBeNil)
So(mutex.Unlock(), ShouldBeNil)
lock, err := mutex.TryLock()
So(err, ShouldBeNil)
So(lock, ShouldBeTrue)
mutex2, err := NewFileMutex(fileLock)
So(err, ShouldBeNil)
So(mutex2, ShouldNotBeNil)
lock2, err := mutex2.TryLock()
So(err, ShouldBeNil)
So(lock2, ShouldBeFalse)
})
Convey("Checking Unlock - After TryLock", func() {
So(mutex.Lock(), ShouldBeNil)
So(mutex.Unlock(), ShouldBeNil)
lock, err := mutex.TryLock()
So(err, ShouldBeNil)
So(lock, ShouldBeTrue)
So(mutex.Unlock(), ShouldBeNil)
mutex2, err := NewFileMutex(fileLock)
So(err, ShouldBeNil)
So(mutex2, ShouldNotBeNil)
lock2, err := mutex2.TryLock()
So(err, ShouldBeNil)
So(lock2, ShouldBeTrue)
})
Convey("Checking Lock and Unlock", func() {
So(mutex.Lock(), ShouldBeNil)
So(mutex.Unlock(), ShouldBeNil)
})
Convey("Checking Unlock", func() {
So(mutex.Lock(), ShouldBeNil)
So(mutex.Unlock(), ShouldBeNil)
lock, err := mutex.TryLock()
So(err, ShouldBeNil)
So(lock, ShouldBeTrue)
So(mutex.Unlock(), ShouldBeNil)
})
Convey("Checking Unlock - We check that it is impossible to unlock someone else's lock", func() {
lock, err := mutex.TryLock()
So(err, ShouldBeNil)
So(lock, ShouldBeTrue)
mutex2, err := NewFileMutex(fileLock)
So(err, ShouldBeNil)
So(mutex2, ShouldNotBeNil)
lock2, err := mutex2.TryLock()
So(err, ShouldBeNil)
So(lock2, ShouldBeFalse)
So(mutex.Unlock(), ShouldBeNil)
lock2, err = mutex2.TryLock()
So(err, ShouldBeNil)
So(lock2, ShouldBeTrue)
})
})
})
}