-
Notifications
You must be signed in to change notification settings - Fork 0
/
all_unix_test.go
59 lines (50 loc) · 1.25 KB
/
all_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
/*
* * Copyright (c) 2023 guojia99 All rights reserved.
* * Created: 2023/2/26 下午5:22.
* * Author: guojia(https://github.com/guojia99)
*/
package flock
import (
"fmt"
"os"
"testing"
"time"
)
const fileNameFormat = "./test/test_file_%d.test"
func init() {
_ = os.MkdirAll("test", os.ModeDir|os.ModePerm)
}
func Test_lockFile(t *testing.T) {
t.Run("lock and unlock", func(t *testing.T) {
fileName := fmt.Sprintf(fileNameFormat, time.Now().Unix())
fl := openLockFile(fileName)
if err := fl.Lock(); err != nil {
t.Fatalf("lock error %s", err)
}
if err := fl.Unlock(); err != nil {
t.Fatalf("unlock error %s", err)
}
})
t.Run("try lock", func(t *testing.T) {
fileName := fmt.Sprintf(fileNameFormat, time.Now().Unix())
fl := openLockFile(fileName)
if ok := fl.TryLock(); !ok {
t.Fatalf("can not lock")
}
if err := fl.Unlock(); err != nil {
t.Fatalf("unlock error %s", err)
}
})
t.Run("double lock", func(t *testing.T) {
fileName := fmt.Sprintf(fileNameFormat, time.Now().Unix())
fl := openLockFile(fileName)
defer fl.Unlock()
if err := fl.Lock(); err != nil {
t.Fatalf("lock error %s", err)
}
fl2 := openLockFile(fileName)
if err := fl2.Lock(); err != nil {
t.Logf("lock error %s", err)
}
})
}