forked from rjkroege/edwood
-
Notifications
You must be signed in to change notification settings - Fork 0
/
acme_test.go
248 lines (218 loc) · 5.23 KB
/
acme_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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package main
import (
"context"
"fmt"
"github.com/rjkroege/edwood/file"
"os/exec"
"reflect"
"testing"
"time"
"github.com/rjkroege/edwood/edwoodtest"
)
func TestIsmtpt(t *testing.T) {
oldmtpt := mtpt
defer func() { mtpt = oldmtpt }()
*mtpt = "/mnt/acme"
testCases := []struct {
filename string
ok bool
}{
{"/mnt/acme", true},
{"/mnt/acme/", true},
{"/mnt/acme/new", true},
{"/mnt/acme/5/body", true},
{"/usr/../mnt/acme", true},
{"/usr/../mnt/acme/", true},
{"/usr/../mnt/acme/new", true},
{"/usr/../mnt/acme/5/body", true},
{"/", false},
}
for _, tc := range testCases {
ok := ismtpt(tc.filename)
if ok != tc.ok {
t.Errorf("ismtpt(%v) = %v; expected %v", tc.filename, ok, tc.ok)
}
}
}
func TestKillprocs(t *testing.T) {
cmd := exec.Command("sleep", "3600")
if err := cmd.Start(); err != nil {
t.Fatalf("failed start command: %v", err)
}
done := make(chan struct{})
go func() {
cmd.Wait()
close(done)
}()
command = []*Command{
{
proc: cmd.Process,
},
}
killprocs(nil)
timer := time.NewTimer(5 * time.Second)
select {
case <-done:
// Do nothing
case <-timer.C:
t.Errorf("killprocs did not kill command in time")
}
}
// TestWaithreadCommandCycle tests that we don't create a cycle in the command linked list
// (regression test for https://github.com/rjkroege/edwood/issues/279).
func TestWaitthreadCommandCycle(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
done := startMockWaitthread(ctx)
defer func() {
cancel() // Ask waithtread to finish up.
<-done // Wait for waithtread to return and finish clean up.
}()
var (
c [4]*Command
w [4]*mockProcessState
)
for i := 0; i < len(c); i++ {
c[i] = &Command{
pid: i,
name: fmt.Sprintf("proc%v", i),
}
w[i] = &mockProcessState{
pid: i,
success: true,
}
}
ccommand <- c[3]
ccommand <- c[2]
ccommand <- c[1]
ccommand <- c[0]
waitthreadSync()
// command is 0 -> 1 -> 2 -> 3
if got, want := len(command), 4; got != want {
t.Errorf("command is length is %v; want %v", got, want)
}
cwait <- w[2] // delete 2, command is 0 -> 1 -> 3, lc = 1 -> 3
cwait <- w[0] // delete 0, command is 1 -> 1 -> 1 -> ...
cwait <- w[3] // try to delete 2: infinite loop
cwait <- w[1]
waitthreadSync()
if got, want := len(command), 0; got != want {
t.Errorf("command is length is %v; want %v", got, want)
}
}
func TestWaitthreadEarlyExit(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
done := startMockWaitthread(ctx)
defer func() {
cancel() // Ask waithtread to finish up.
<-done // Wait for waithtread to return and finish clean up.
}()
c := &Command{
pid: 42,
name: "proc42",
iseditcommand: true,
}
w := &mockProcessState{
pid: 42,
success: true,
}
// simulate command exit before adding it to command list
cwait <- w
waitthreadSync()
ccommand <- c
<-cedit
waitthreadSync()
if got, want := len(command), 0; got != want {
t.Errorf("command is length is %v; want %v", got, want)
}
row.lk.Lock()
got := string(warnings[0].buf)
row.lk.Unlock()
want := "pid 42, success true\n"
if got != want {
t.Fatalf("warnings is %q; want %q", got, want)
}
}
func TestWaitthreadKill(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
done := startMockWaitthread(ctx)
defer func() {
cancel() // Ask waithtread to finish up.
<-done // Wait for waitthread to return and finish clean up.
}()
cmd := exec.Command("sleep", "3600")
if err := cmd.Start(); err != nil {
t.Fatalf("failed start command: %v", err)
}
waitDone := make(chan struct{})
go func() {
cmd.Wait()
cwait <- cmd.ProcessState
waitthreadSync()
close(waitDone)
}()
c := &Command{
pid: cmd.Process.Pid,
proc: cmd.Process,
name: "sleep ",
}
ccommand <- c
waitthreadSync()
if got, want := command, []*Command{c}; !reflect.DeepEqual(got, want) {
t.Errorf("command is %v; want %v", got, want)
}
ckill <- "unknown_cmd"
waitthreadSync()
row.lk.Lock()
got := string(warnings[0].buf)
row.lk.Unlock()
want := "Kill: no process unknown_cmd\n"
if got != want {
t.Fatalf("warnings is %q; want %q", got, want)
}
ckill <- "sleep"
waitthreadSync()
<-waitDone
if got, want := len(command), 0; got != want {
t.Errorf("command is length is %v; want %v", got, want)
}
}
func startMockWaitthread(ctx context.Context) (done <-chan struct{}) {
ccommand = make(chan *Command)
cwait = make(chan ProcessState)
ckill = make(chan string)
command = nil
cerr = make(chan error)
cedit = make(chan int)
warnings = nil
row = Row{
display: edwoodtest.NewDisplay(),
tag: Text{
file: file.MakeObservableEditableBuffer("", nil),
},
}
ch := make(chan struct{})
go func() {
waitthread(ctx)
ccommand = nil
cwait = nil
ckill = nil
command = nil
cerr = nil
cedit = nil
warnings = nil
row = Row{}
close(ch)
}()
return ch
}
// waitthreadSync waits until a select case in waitthread finishes.
func waitthreadSync() { cerr <- fmt.Errorf("") }
type mockProcessState struct {
pid int
success bool
}
func (ps *mockProcessState) Pid() int { return ps.pid }
func (ps *mockProcessState) String() string {
return fmt.Sprintf("pid %v, success %v", ps.pid, ps.success)
}
func (ps *mockProcessState) Success() bool { return ps.success }