-
Notifications
You must be signed in to change notification settings - Fork 3
/
filelock_test.go
188 lines (140 loc) · 5.1 KB
/
filelock_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
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
180
181
182
183
184
185
186
187
188
package filelock_test
import (
"io"
"os/exec"
"time"
"code.cloudfoundry.org/filelock"
"os"
"path/filepath"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
"github.com/onsi/gomega/gexec"
)
var _ = Describe("Locking using a file", func() {
var path string
var tempDir string
BeforeEach(func() {
var err error
tempDir, err = os.MkdirTemp("", "")
Expect(err).NotTo(HaveOccurred())
path = filepath.Join(tempDir, "dir1", "dir2", "some-file.json")
})
AfterEach(func() {
Expect(os.RemoveAll(tempDir)).To(Succeed())
})
AssertBasicThingsWork := func(expectedInitialContents []byte) {
It("returns a file usable for read and write", func() {
lock := filelock.NewLocker(path)
file, err := lock.Open()
Expect(err).NotTo(HaveOccurred())
initialContents, err := io.ReadAll(file)
Expect(err).NotTo(HaveOccurred())
Expect(initialContents).To(Equal(expectedInitialContents))
Expect(file.Truncate(0)).To(Succeed())
_, err = file.Seek(0, 0)
Expect(err).NotTo(HaveOccurred())
_, err = file.Write([]byte("hello"))
Expect(err).NotTo(HaveOccurred())
_, err = file.Seek(0, 0)
Expect(err).NotTo(HaveOccurred())
allBytes, err := io.ReadAll(file)
Expect(err).NotTo(HaveOccurred())
Expect(allBytes).To(Equal([]byte("hello")))
Expect(file.Close()).To(Succeed())
allBytes, err = os.ReadFile(path)
Expect(err).NotTo(HaveOccurred())
Expect(allBytes).To(Equal([]byte("hello")))
})
}
Context("when the file does not already exist", func() {
BeforeEach(func() {
Expect(os.RemoveAll(tempDir)).To(Succeed())
})
AssertBasicThingsWork([]byte{})
})
Context("when the file already exists", func() {
var preExistingContents = []byte("foo bar baz this is some prior data")
BeforeEach(func() {
Expect(os.MkdirAll(filepath.Dir(path), 0700)).To(Succeed())
Expect(os.WriteFile(path, preExistingContents, 0600)).To(Succeed())
})
AssertBasicThingsWork(preExistingContents)
})
Context("when the path has already been opened by a locker in the same OS process", func() {
var theFirstFileHandle filelock.LockedFile
BeforeEach(func() {
By("acquiring the first lock on the file")
var err error
theFirstFileHandle, err = filelock.NewLocker(path).Open()
Expect(err).NotTo(HaveOccurred())
By("writing some data to the file")
_, err = theFirstFileHandle.Write([]byte("the first data"))
Expect(err).ToNot(HaveOccurred())
})
It("blocks the second open until the first one is closed", func() {
locker := filelock.NewLocker(path)
By("attempting to acquire another lock on the same file")
lockAcquiredChan := make(chan struct{})
var secondFileHandle filelock.LockedFile
done := make(chan struct{})
go func() {
defer GinkgoRecover()
var err error
secondFileHandle, err = locker.Open()
Expect(err).NotTo(HaveOccurred())
lockAcquiredChan <- struct{}{}
close(done)
}()
By("verifying that we cannot acquire the lock")
Consistently(lockAcquiredChan).ShouldNot(Receive())
By("releasing the first lock")
Expect(theFirstFileHandle.Close()).To(Succeed())
By("checking that we can now acquire the second lock on the file")
Eventually(lockAcquiredChan).Should(Receive())
By("validating the data written to the first lock")
Expect(io.ReadAll(secondFileHandle)).To(Equal([]byte("the first data")))
Expect(secondFileHandle.Close()).To(Succeed())
Eventually(done, 5*time.Second).Should(BeClosed())
})
})
Context("when the file is locked from a separate OS process", func() {
It("blocks the second file open until after the other process has released the lock", func() {
cmd := exec.Command(pathToBinary, path)
stdinPipe, err := cmd.StdinPipe()
Expect(err).NotTo(HaveOccurred())
By("starting an external process that will acquire the lock")
session, err := gexec.Start(cmd, GinkgoWriter, GinkgoWriter)
Expect(err).NotTo(HaveOccurred())
By("waiting for it to acquire the lock")
Eventually(session.Err).Should(gbytes.Say("done after"))
Consistently(session).ShouldNot(gexec.Exit())
_, err = stdinPipe.Write([]byte("some data from the external process"))
Expect(err).NotTo(HaveOccurred())
By("attempting to acquire the lock ourselves")
locker := filelock.NewLocker(path)
lockAcquiredChan := make(chan struct{})
done := make(chan struct{})
var file filelock.LockedFile
go func() {
defer GinkgoRecover()
var err error
file, err = locker.Open()
Expect(err).NotTo(HaveOccurred())
lockAcquiredChan <- struct{}{}
close(done)
}()
By("verifying that we cannot acquire the lock")
Consistently(lockAcquiredChan).ShouldNot(Receive())
By("signaling for the external process to release the lock")
Expect(stdinPipe.Close()).To(Succeed())
By("checking that we can now acquire the lock")
Eventually(lockAcquiredChan).Should(Receive())
By("validating the data written by the external process")
Expect(io.ReadAll(file)).To(Equal([]byte("some data from the external process")))
By("releasing the lock")
Expect(file.Close()).To(Succeed())
Eventually(done, 5*time.Second).Should(BeClosed())
})
})
})