-
Notifications
You must be signed in to change notification settings - Fork 0
/
pid_test.go
141 lines (116 loc) · 2.3 KB
/
pid_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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package tinyfile
import (
"os"
"path/filepath"
"strconv"
"testing"
)
func TestPidSet(t *testing.T) {
tmp, err := os.MkdirTemp("", "gotest")
if err != nil {
panic(err)
}
defer os.RemoveAll(tmp)
defer PidClean()
filename := filepath.Join(tmp, "test.pid")
err = PidSet(filename)
if err != nil {
t.Error(err)
}
b, err := os.ReadFile(filename)
if err != nil {
t.Error(err)
}
if string(b) != strconv.Itoa(os.Getpid()) {
t.Errorf("pid is not equal %s != %d", string(b), os.Getpid())
}
err = PidSet(filename)
if err != ErrAleadySetPid {
t.Errorf("invalid error")
}
}
func TestPidSetAleadyRunning(t *testing.T) {
tmp := os.TempDir()
tmp, err := os.MkdirTemp("", "gotest")
if err != nil {
panic(err)
}
defer os.RemoveAll(tmp)
defer PidClean()
filename := filepath.Join(tmp, "test.pid")
os.WriteFile(filename, []byte(""), 0664)
err = PidSet(filename)
if err != ErrAnotherProcessRunning {
t.Errorf("invalid error of %v", err)
}
}
func TestPidSetTwiceSet(t *testing.T) {
tmp, err := os.MkdirTemp("", "gotest")
if err != nil {
panic(err)
}
defer os.RemoveAll(tmp)
defer PidClean()
filename := filepath.Join(tmp, "test.pid")
filename2 := filepath.Join(tmp, "test2.pid")
err = PidSet(filename)
if err != nil {
t.Error(err)
}
err = PidSet(filename2)
if err != ErrAleadySetPid {
t.Errorf("invalid error of %s", err)
}
}
func TestPidClean(t *testing.T) {
err := PidClean()
if err != nil {
t.Error(err)
}
tmp, err := os.MkdirTemp("", "gotest")
if err != nil {
panic(err)
}
defer os.RemoveAll(tmp)
filename := filepath.Join(tmp, "test.pid")
err = PidSet(filename)
if err != nil {
t.Error(err)
}
_, err = os.ReadFile(filename)
if err != nil {
t.Error(err)
}
err = PidClean()
if err != nil {
t.Error(err)
}
_, err = os.ReadFile(filename)
if err == nil {
t.Error(err)
}
}
func TestFileExist(t *testing.T) {
f, err := os.CreateTemp(os.TempDir(), "gotest")
if err != nil {
panic(err)
}
defer os.Remove(f.Name())
if !FileExist(f.Name()) {
t.Errorf("file is exist")
}
os.Remove(f.Name())
if FileExist(f.Name()) {
t.Errorf("file is not exist")
}
}
func TestFileExistDirectory(t *testing.T) {
tmp, err := os.MkdirTemp("", "gotest")
if err != nil {
panic(err)
}
defer os.RemoveAll(tmp)
if FileExist(tmp) {
t.Errorf("this is directory")
}
}