-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathlock_test.go
120 lines (105 loc) · 2.78 KB
/
lock_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
package main
import (
"os"
"path/filepath"
"testing"
"time"
"github.com/creativeprojects/resticprofile/lock"
"github.com/stretchr/testify/assert"
)
func TestLockRunWithNoLockfile(t *testing.T) {
called := 0
callback := func(setPID lock.SetPID) error {
called++
return nil
}
err := lockRun("", false, nil, nil, callback)
assert.NoError(t, err)
assert.Equal(t, 1, called)
}
func TestLockRunWithNoLock(t *testing.T) {
called := 0
callback := func(setPID lock.SetPID) error {
called++
return nil
}
lockfile := filepath.Join(t.TempDir(), "lockfile")
assert.NoFileExists(t, lockfile)
err := lockRun(lockfile, false, nil, nil, callback)
assert.NoError(t, err)
assert.Equal(t, 1, called)
assert.NoFileExists(t, lockfile)
}
func TestLockRunWithLock(t *testing.T) {
called := 0
callback := func(setPID lock.SetPID) error {
called++
return nil
}
lockfile := filepath.Join(t.TempDir(), "lockfile")
err := os.WriteFile(lockfile, []byte{}, 0o600)
assert.NoError(t, err)
assert.FileExists(t, lockfile)
err = lockRun(lockfile, false, nil, nil, callback)
assert.Error(t, err)
assert.Equal(t, 0, called)
assert.FileExists(t, lockfile)
}
func TestLockRunWithLockAndForce(t *testing.T) {
called := 0
callback := func(setPID lock.SetPID) error {
called++
return nil
}
lockfile := filepath.Join(t.TempDir(), "lockfile")
err := os.WriteFile(lockfile, []byte{}, 0o600)
assert.NoError(t, err)
assert.FileExists(t, lockfile)
err = lockRun(lockfile, true, nil, nil, callback)
assert.Error(t, err)
assert.Equal(t, 0, called)
assert.FileExists(t, lockfile)
}
func TestLockRunWithLockAndWait(t *testing.T) {
called := 0
callback := func(setPID lock.SetPID) error {
called++
return nil
}
lockfile := filepath.Join(t.TempDir(), "lockfile")
err := os.WriteFile(lockfile, []byte{}, 0o600)
assert.NoError(t, err)
assert.FileExists(t, lockfile)
// remove the lock after half a second
timer := time.AfterFunc(500*time.Millisecond, func() {
err := os.Remove(lockfile)
assert.NoError(t, err)
})
defer timer.Stop()
wait := 1 * time.Second
err = lockRun(lockfile, false, &wait, nil, callback)
assert.NoError(t, err)
assert.Equal(t, 1, called)
}
func TestLockRunWithLockAndCancel(t *testing.T) {
called := 0
callback := func(setPID lock.SetPID) error {
called++
return nil
}
lockfile := filepath.Join(t.TempDir(), "lockfile")
err := os.WriteFile(lockfile, []byte{}, 0o600)
assert.NoError(t, err)
assert.FileExists(t, lockfile)
sigChan := make(chan os.Signal, 1)
// cancel the wait after half a second
timer := time.AfterFunc(500*time.Millisecond, func() {
sigChan <- os.Interrupt
})
defer timer.Stop()
wait := 1 * time.Second
err = lockRun(lockfile, false, &wait, sigChan, callback)
assert.Error(t, err)
assert.Equal(t, 0, called)
assert.FileExists(t, lockfile)
}