-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStopProgram_test.go
More file actions
288 lines (223 loc) · 6.64 KB
/
StopProgram_test.go
File metadata and controls
288 lines (223 loc) · 6.64 KB
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package gorun
import (
"os"
"strings"
"testing"
"time"
)
func TestStopProgram_Basic(t *testing.T) {
// Build test program
execPath := buildTestProgram(t, "simple_program")
defer os.Remove(execPath)
exitChan := make(chan bool)
buf, logger := createTestLogger()
config := &Config{
ExecProgramPath: execPath,
RunArguments: func() []string { return []string{} },
ExitChan: exitChan,
Logger: logger,
}
gr := New(config)
// Start the program
err := gr.RunProgram()
if err != nil {
t.Fatalf("RunProgram() failed: %v", err)
}
if !gr.IsRunning() {
t.Error("Program should be running")
}
// Stop the program
err = gr.StopProgram()
if err != nil {
t.Errorf("StopProgram() failed: %v", err)
}
// Wait for it to stop
time.Sleep(100 * time.Millisecond)
if gr.IsRunning() {
t.Error("Program should not be running after StopProgram()")
}
_ = buf // Use buf to avoid unused variable error
}
func TestStopProgram_WhenNotRunning(t *testing.T) {
exitChan := make(chan bool)
buf, logger := createTestLogger()
config := &Config{
ExecProgramPath: "test",
RunArguments: func() []string { return []string{} },
ExitChan: exitChan,
Logger: logger,
}
gr := New(config)
// Try to stop when not running
err := gr.StopProgram()
if err != nil {
t.Errorf("StopProgram() should not fail when not running: %v", err)
}
if gr.IsRunning() {
t.Error("Program should not be running")
}
_ = buf // Use buf to avoid unused variable error
}
func TestStopProgram_GracefulShutdown(t *testing.T) {
// Build test program that handles signals
execPath := buildTestProgram(t, "simple_program")
defer os.Remove(execPath)
exitChan := make(chan bool)
buf, logger := createTestLogger()
config := &Config{
ExecProgramPath: execPath,
RunArguments: func() []string { return []string{} },
ExitChan: exitChan,
Logger: logger,
}
gr := New(config)
// Start the program
err := gr.RunProgram()
if err != nil {
t.Fatalf("RunProgram() failed: %v", err)
}
// Wait for it to start
time.Sleep(50 * time.Millisecond)
// Stop the program
err = gr.StopProgram()
if err != nil && !strings.Contains(err.Error(), "no child processes") {
t.Errorf("StopProgram() failed with unexpected error: %v", err)
}
// Wait for it to process the signal
time.Sleep(200 * time.Millisecond)
output := gr.getOutput()
// Check that the program completed (either gracefully or was terminated)
// The specific signal handling may vary by OS and implementation
if !strings.Contains(output, "SIGNAL_RECEIVED") && !strings.Contains(output, "closed") {
t.Errorf("Expected either signal handling or program completion. Output: %s", output)
}
if gr.IsRunning() {
t.Error("Program should not be running after shutdown")
}
_ = buf // Use buf to avoid unused variable error
}
func TestStopProgram_ForcedTermination(t *testing.T) {
// Build a program that doesn't handle signals (should be force-killed)
execPath := buildTestProgram(t, "long_program")
defer os.Remove(execPath)
exitChan := make(chan bool)
buf, logger := createTestLogger()
config := &Config{
ExecProgramPath: execPath,
RunArguments: func() []string { return []string{} },
ExitChan: exitChan,
Logger: logger,
}
gr := New(config)
// Start the program
err := gr.RunProgram()
if err != nil {
t.Fatalf("RunProgram() failed: %v", err)
}
// Wait for it to start
time.Sleep(50 * time.Millisecond)
if !gr.IsRunning() {
t.Error("Program should be running")
}
// Stop the program - ignore "no child processes" error which can happen
// if the process exits on its own before we stop it
err = gr.StopProgram()
if err != nil && !strings.Contains(err.Error(), "no child processes") {
t.Errorf("StopProgram() failed with unexpected error: %v", err)
}
// Wait for the termination process to complete
// The implementation should try graceful first, then force kill
time.Sleep(300 * time.Millisecond)
if gr.IsRunning() {
t.Error("Program should not be running after forced termination")
}
_ = buf // Use buf to avoid unused variable error
}
func TestStopProgram_MultipleCalls(t *testing.T) {
// Build test program
execPath := buildTestProgram(t, "simple_program")
defer os.Remove(execPath)
exitChan := make(chan bool)
buf, logger := createTestLogger()
config := &Config{
ExecProgramPath: execPath,
RunArguments: func() []string { return []string{} },
ExitChan: exitChan,
Logger: logger,
}
gr := New(config)
// Start the program
err := gr.RunProgram()
if err != nil {
t.Fatalf("RunProgram() failed: %v", err)
}
// Call StopProgram multiple times
err1 := gr.StopProgram()
err2 := gr.StopProgram()
err3 := gr.StopProgram()
if err1 != nil {
t.Errorf("First StopProgram() failed: %v", err1)
}
// Subsequent calls should not fail
if err2 != nil {
t.Errorf("Second StopProgram() failed: %v", err2)
}
if err3 != nil {
t.Errorf("Third StopProgram() failed: %v", err3)
}
// Wait for termination
time.Sleep(100 * time.Millisecond)
if gr.IsRunning() {
t.Error("Program should not be running after multiple StopProgram calls")
}
_ = buf // Use buf to avoid unused variable error
}
func TestStopProgram_ConcurrentCalls(t *testing.T) {
// Build test program
execPath := buildTestProgram(t, "long_program")
defer os.Remove(execPath)
exitChan := make(chan bool)
buf, logger := createTestLogger()
config := &Config{
ExecProgramPath: execPath,
RunArguments: func() []string { return []string{} },
ExitChan: exitChan,
Logger: logger,
}
gr := New(config)
// Start the program
err := gr.RunProgram()
if err != nil {
t.Fatalf("RunProgram() failed: %v", err)
}
// Wait for it to start
time.Sleep(50 * time.Millisecond)
// Call StopProgram concurrently from multiple goroutines
done := make(chan error, 3)
for i := 0; i < 3; i++ {
go func() {
done <- gr.StopProgram()
}()
}
// Collect results
var unexpectedErrors []error
for i := 0; i < 3; i++ {
if err := <-done; err != nil {
// Ignore "no child processes" errors as they can happen when
// multiple calls try to stop an already stopped process
if !strings.Contains(err.Error(), "no child processes") {
unexpectedErrors = append(unexpectedErrors, err)
}
}
}
// Should not have unexpected errors (concurrent calls should be handled safely)
if len(unexpectedErrors) > 0 {
t.Errorf("Concurrent StopProgram calls produced unexpected errors: %v", unexpectedErrors)
}
// Wait for termination
time.Sleep(200 * time.Millisecond)
if gr.IsRunning() {
t.Error("Program should not be running after concurrent StopProgram calls")
}
_ = buf // Use buf to avoid unused variable error
}