-
Notifications
You must be signed in to change notification settings - Fork 0
/
lock_unix_test.go
53 lines (46 loc) · 893 Bytes
/
lock_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
// +build darwin linux
package wal
import (
"io/ioutil"
"os"
"testing"
)
func TestLockAndUnlock(t *testing.T) {
f, err := ioutil.TempFile("", "lock")
if err != nil {
t.Fatal(err)
}
f.Close()
defer func() {
if err := os.Remove(f.Name()); err != nil {
t.Fatal(err)
}
}()
// lock
if f, err = os.OpenFile(f.Name(), os.O_WRONLY, 0600); err != nil {
t.Fatal(err)
}
if err := lockFileNonBlocking(f); err != nil {
t.Fatal(err)
}
// try locking with another file descriptor
f2, err := os.OpenFile(f.Name(), os.O_WRONLY, 0600)
if err != nil {
t.Fatal(err)
}
if err = lockFileNonBlocking(f2); err != errLocked {
t.Fatal(err)
}
// unlock
if err = f.Close(); err != nil {
t.Fatal(err)
}
// lock as another file descriptor
if err := lockFileNonBlocking(f2); err != nil {
t.Fatal(err)
}
// unlock
if err := f2.Close(); err != nil {
t.Fatal(err)
}
}