-
Notifications
You must be signed in to change notification settings - Fork 3
/
locking_test.go
67 lines (50 loc) · 1.65 KB
/
locking_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 prekeyserver
import (
"encoding/binary"
"io/ioutil"
"os"
"path"
"time"
. "gopkg.in/check.v1"
)
func (s *GenericServerSuite) Test_fileStorage_lockDir_unlockDir_worksCorrectlyInSimpleCase(c *C) {
os.Mkdir(testDir, 0700)
defer os.RemoveAll(testDir)
t1 := lockDir(testDir)
c.Assert(entryExists(path.Join(testDir, ".lock")), Equals, true)
b, _ := ioutil.ReadFile(path.Join(testDir, ".lock"))
c.Assert(binary.BigEndian.Uint64(b), Equals, t1)
unlockDir(testDir, t1)
c.Assert(entryExists(path.Join(testDir, ".lock")), Equals, false)
}
func (s *GenericServerSuite) Test_fileStorage_lockDir_waitsUntilAbleToLockIfSomeoneAlreadyLockedFile(c *C) {
os.Mkdir(testDir, 0700)
defer os.RemoveAll(testDir)
lfile := path.Join(testDir, ".lock")
ioutil.WriteFile(lfile, []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}, 0600)
var ackLock time.Time
wait := make(chan bool)
go func() {
t1 := lockDir(testDir)
ackLock = time.Now()
c.Assert(entryExists(lfile), Equals, true)
b, _ := ioutil.ReadFile(lfile)
c.Assert(binary.BigEndian.Uint64(b), Equals, t1)
unlockDir(testDir, t1)
c.Assert(entryExists(lfile), Equals, false)
wait <- true
}()
before := time.Now()
time.Sleep(time.Duration(100) * time.Millisecond)
os.Remove(lfile)
<-wait
c.Assert(before.Before(ackLock), Equals, true)
}
func (s *GenericServerSuite) Test_fileStorage_unlockDir_doesntRemoveALockForAnotherToken(c *C) {
os.Mkdir(testDir, 0700)
defer os.RemoveAll(testDir)
lfile := path.Join(testDir, ".lock")
ioutil.WriteFile(lfile, []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}, 0600)
unlockDir(testDir, 0x4242424212341123)
c.Assert(entryExists(lfile), Equals, true)
}