-
Notifications
You must be signed in to change notification settings - Fork 12
/
lock_test.go
55 lines (44 loc) · 1.07 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
package singleinstance
import (
"io/ioutil"
"os"
"testing"
)
func createTestLock(t *testing.T) *os.File {
tmpfile, err := ioutil.TempFile("", "singleinstance")
if err != nil {
t.Fatal("Cannot create temporary file:", err)
}
tmpfile.Close()
f, err := CreateLockFile(tmpfile.Name())
if err != nil {
t.Fatal("Expected no error while creating lock, got:", err)
}
return f
}
func TestCreateLockFile(t *testing.T) {
f := createTestLock(t)
defer os.Remove(f.Name())
_, err := CreateLockFile(f.Name())
if err == nil {
t.Fatal("Expected an error while trying to lock, got:", err)
}
f.Close() // Remove the lock
f, err = CreateLockFile(f.Name())
if err != nil {
t.Fatal("Expected no error while trying to lock, got:", err)
}
f.Close()
}
func TestGetLockFilePid(t *testing.T) {
f := createTestLock(t)
defer os.Remove(f.Name())
defer f.Close()
pid, err := GetLockFilePid(f.Name())
if err != nil {
t.Fatal("Expected no error while getting PID, got:", err)
}
if pid != os.Getpid() {
t.Errorf("Invalid PID: expected %v but got %v", os.Getpid(), pid)
}
}