-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess_id_comprehensive_test.go
More file actions
498 lines (402 loc) · 13.3 KB
/
process_id_comprehensive_test.go
File metadata and controls
498 lines (402 loc) · 13.3 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
//go:build integration
// +build integration
package comet
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"syscall"
"testing"
"time"
"unsafe"
)
// TestGetProcessID_ExhaustSlots tests behavior when all slots are taken
func TestGetProcessID_ExhaustSlots(t *testing.T) {
if testing.Short() {
t.Skip("skipping slot exhaustion test in short mode")
}
// Check if we're the parent or child process
if workerID := os.Getenv("COMET_EXHAUST_WORKER"); workerID != "" {
runExhaustWorker(t, workerID)
return
}
if os.Getenv("GO_TEST_SUBPROCESS") == "1" {
t.Skip("Skipping test in subprocess to prevent recursion")
return
}
shmFile := t.TempDir() + "/worker-slots-exhaust"
maxWorkers := runtime.NumCPU()
t.Logf("Testing slot exhaustion with %d max workers", maxWorkers)
// Start maxWorkers + 2 processes (more than available slots)
var processes []*exec.Cmd
numProcesses := maxWorkers + 2
for i := 0; i < numProcesses; i++ {
cmd := exec.Command(os.Args[0], "-test.run=TestGetProcessID_ExhaustSlots", "-test.v")
cmd.Env = append(os.Environ(),
fmt.Sprintf("COMET_EXHAUST_WORKER=%d", i),
fmt.Sprintf("COMET_SHM_FILE=%s", shmFile),
"GO_TEST_SUBPROCESS=1",
)
if err := cmd.Start(); err != nil {
t.Fatalf("Failed to start process %d: %v", i, err)
}
processes = append(processes, cmd)
// Small delay to stagger startup
time.Sleep(50 * time.Millisecond)
}
// Wait for all processes
successCount := 0
failureCount := 0
for i, cmd := range processes {
err := cmd.Wait()
if err != nil {
// Check if it's our expected "failed to acquire process ID" error
if exitError, ok := err.(*exec.ExitError); ok {
if exitError.ExitCode() == 42 { // Our custom exit code for "no slots available"
failureCount++
t.Logf("Process %d correctly failed to acquire slot (expected)", i)
} else {
t.Errorf("Process %d failed unexpectedly: %v", i, err)
}
} else {
t.Errorf("Process %d failed with non-exit error: %v", i, err)
}
} else {
successCount++
}
}
t.Logf("Results: %d successful acquisitions, %d expected failures", successCount, failureCount)
// Should have exactly maxWorkers successful and rest failed
if successCount != maxWorkers {
t.Errorf("Expected %d successful acquisitions, got %d", maxWorkers, successCount)
}
if failureCount != (numProcesses - maxWorkers) {
t.Errorf("Expected %d failures, got %d", numProcesses-maxWorkers, failureCount)
}
}
func runExhaustWorker(t *testing.T, workerID string) {
shmFile := os.Getenv("COMET_SHM_FILE")
processID := GetProcessID(shmFile)
t.Logf("Exhaust worker %s: processID = %d (PID: %d)", workerID, processID, os.Getpid())
if processID < 0 {
t.Logf("Worker %s failed to acquire process ID (expected for some workers)", workerID)
os.Exit(42) // Custom exit code to indicate "no slots available"
}
// Hold the slot for a while to test exhaustion
time.Sleep(3 * time.Second)
t.Logf("Worker %s completed successfully with process ID %d", workerID, processID)
}
// TestGetProcessID_FileSystemErrors tests various file system error conditions
func TestGetProcessID_FileSystemErrors(t *testing.T) {
if testing.Short() {
t.Skip("skipping filesystem error test in short mode")
}
t.Run("ReadOnlyDirectory", func(t *testing.T) {
// Create a read-only directory
readOnlyDir := t.TempDir() + "/readonly"
err := os.Mkdir(readOnlyDir, 0755)
if err != nil {
t.Fatal(err)
}
// Make it read-only
err = os.Chmod(readOnlyDir, 0555)
if err != nil {
t.Fatal(err)
}
defer os.Chmod(readOnlyDir, 0755) // Restore for cleanup
shmFile := readOnlyDir + "/worker-slots"
processID := GetProcessID(shmFile)
if processID != -1 {
t.Errorf("Expected failure in read-only directory, got processID %d", processID)
}
})
t.Run("InvalidPath", func(t *testing.T) {
// Try to create file in non-existent deeply nested path
shmFile := "/non/existent/very/deep/path/worker-slots"
processID := GetProcessID(shmFile)
if processID != -1 {
t.Errorf("Expected failure with invalid path, got processID %d", processID)
}
})
t.Run("CorruptedFile", func(t *testing.T) {
shmFile := t.TempDir() + "/corrupted-slots"
// Create a file with wrong size/content
file, err := os.Create(shmFile)
if err != nil {
t.Fatal(err)
}
// Write garbage data
file.WriteString("this is not a valid shared memory file")
file.Close()
processID := GetProcessID(shmFile)
// Should still work because we truncate to correct size
if processID < 0 {
t.Errorf("Expected success with corrupted file (should truncate), got %d", processID)
}
})
}
// TestGetProcessID_RaceConditions tests concurrent access from multiple goroutines
func TestGetProcessID_RaceConditions(t *testing.T) {
if testing.Short() {
t.Skip("skipping race condition test in short mode")
}
// Check if we're the parent or child process
if workerID := os.Getenv("COMET_RACE_WORKER"); workerID != "" {
runRaceWorker(t, workerID)
return
}
if os.Getenv("GO_TEST_SUBPROCESS") == "1" {
t.Skip("Skipping test in subprocess to prevent recursion")
return
}
shmFile := t.TempDir() + "/worker-slots-race"
// Start multiple processes simultaneously to test race conditions
numProcesses := 8
maxSlots := runtime.NumCPU()
expectedSlots := numProcesses
if maxSlots < numProcesses {
expectedSlots = maxSlots
t.Logf("System has %d CPU cores, test wants %d processes. Only %d will get slots.", maxSlots, numProcesses, maxSlots)
}
var processes []*exec.Cmd
// Start all processes at nearly the same time
for i := 0; i < numProcesses; i++ {
cmd := exec.Command(os.Args[0], "-test.run=TestGetProcessID_RaceConditions", "-test.v")
cmd.Env = append(os.Environ(),
fmt.Sprintf("COMET_RACE_WORKER=%d", i),
fmt.Sprintf("COMET_SHM_FILE=%s", shmFile),
"GO_TEST_SUBPROCESS=1",
)
if err := cmd.Start(); err != nil {
t.Fatalf("Failed to start process %d: %v", i, err)
}
processes = append(processes, cmd)
}
// Wait for all processes
for i, cmd := range processes {
err := cmd.Wait()
if err != nil {
if exitError, ok := err.(*exec.ExitError); ok && exitError.ExitCode() == 42 {
// Expected for processes that couldn't get slots
continue
}
t.Errorf("Process %d failed: %v", i, err)
}
}
// Verify final state of shared memory
verifyUniqueSlotAssignment(t, shmFile, expectedSlots)
}
func runRaceWorker(t *testing.T, workerID string) {
shmFile := os.Getenv("COMET_SHM_FILE")
// All processes try to acquire at exactly the same time
processID := GetProcessID(shmFile)
if processID < 0 {
t.Logf("Race worker %s failed to acquire process ID", workerID)
os.Exit(42)
}
t.Logf("Race worker %s acquired process ID %d", workerID, processID)
// Hold briefly then release
time.Sleep(1 * time.Second)
}
// TestGetProcessID_MemoryPressure tests behavior under memory pressure
func TestGetProcessID_MemoryPressure(t *testing.T) {
if testing.Short() {
t.Skip("skipping memory pressure test in short mode")
}
shmFile := t.TempDir() + "/worker-slots-memory"
// Allocate a lot of memory to create pressure
bigAllocs := make([][]byte, 0, 100)
for i := 0; i < 50; i++ {
// Allocate 10MB chunks
bigAllocs = append(bigAllocs, make([]byte, 10*1024*1024))
}
t.Logf("Allocated ~500MB memory, testing process ID acquisition")
processID := GetProcessID(shmFile)
if processID < 0 {
t.Error("Failed to acquire process ID under memory pressure")
}
// Force garbage collection
runtime.GC()
runtime.GC()
// Try again after GC
processID2 := GetProcessID(shmFile)
if processID2 != processID {
t.Errorf("Process ID changed after GC: %d -> %d", processID, processID2)
}
// Keep references to prevent premature GC
_ = bigAllocs
}
// TestGetProcessID_SignalInterruption tests behavior when interrupted by signals
func TestGetProcessID_SignalInterruption(t *testing.T) {
if testing.Short() {
t.Skip("skipping signal interruption test in short mode")
}
// Check if we're the parent or child process
if os.Getenv("COMET_SIGNAL_WORKER") != "" {
runSignalWorker(t)
return
}
if os.Getenv("GO_TEST_SUBPROCESS") == "1" {
t.Skip("Skipping test in subprocess to prevent recursion")
return
}
shmFile := t.TempDir() + "/worker-slots-signal"
// Start a process
cmd := exec.Command(os.Args[0], "-test.run=TestGetProcessID_SignalInterruption", "-test.v")
cmd.Env = append(os.Environ(),
"COMET_SIGNAL_WORKER=1",
fmt.Sprintf("COMET_SHM_FILE=%s", shmFile),
"GO_TEST_SUBPROCESS=1",
)
if err := cmd.Start(); err != nil {
t.Fatalf("Failed to start signal worker: %v", err)
}
// Give it time to acquire a slot
time.Sleep(500 * time.Millisecond)
// Send SIGTERM
if err := cmd.Process.Signal(syscall.SIGTERM); err != nil {
t.Fatalf("Failed to send SIGTERM: %v", err)
}
// Wait for it to exit
err := cmd.Wait()
if err == nil {
t.Error("Expected process to exit with error after SIGTERM")
}
// Check that slot was not properly released (since it was killed)
// A new process should detect this and reclaim the slot
processID := GetProcessID(shmFile)
if processID < 0 {
t.Error("Failed to acquire process ID after previous process was killed")
}
// Verify the slot was reclaimed from the dead process
if processID != 0 {
t.Logf("Note: Got slot %d, might have gotten the reclaimed slot from killed process", processID)
}
}
func runSignalWorker(t *testing.T) {
shmFile := os.Getenv("COMET_SHM_FILE")
processID := GetProcessID(shmFile)
if processID < 0 {
t.Fatal("Signal worker failed to acquire process ID")
}
t.Logf("Signal worker acquired process ID %d, waiting for signal...", processID)
// Wait indefinitely (will be killed by signal)
for {
time.Sleep(100 * time.Millisecond)
}
}
// TestGetProcessID_DiskSpace tests behavior when disk space is exhausted
func TestGetProcessID_DiskSpace(t *testing.T) {
if testing.Short() {
t.Skip("skipping disk space test in short mode")
}
// Create a very small tmpfs to simulate disk exhaustion
// Note: This test might not work on all systems
tmpDir := t.TempDir()
// Try to fill up available space by creating large files
largeFile := filepath.Join(tmpDir, "large-file")
file, err := os.Create(largeFile)
if err != nil {
t.Skip("Cannot create large file for disk space test")
}
// Try to write a large amount of data
data := make([]byte, 1024*1024) // 1MB
for i := 0; i < 1000; i++ { // Try to write 1GB
_, err := file.Write(data)
if err != nil {
break // Disk full or other error
}
}
file.Close()
// Now try to create shared memory file
shmFile := filepath.Join(tmpDir, "worker-slots")
processID := GetProcessID(shmFile)
// Should either succeed or fail gracefully
if processID < 0 {
t.Log("Process ID acquisition failed due to disk space (expected)")
} else {
t.Logf("Process ID acquisition succeeded despite disk pressure: %d", processID)
}
}
// TestGetProcessID_LongRunning tests long-running behavior and slot persistence
func TestGetProcessID_LongRunning(t *testing.T) {
if testing.Short() {
t.Skip("skipping long-running test in short mode")
}
shmFile := t.TempDir() + "/worker-slots-longrun"
// Acquire a process ID
processID1 := GetProcessID(shmFile)
if processID1 < 0 {
t.Fatal("Failed to acquire initial process ID")
}
// Simulate long-running process by holding slot and checking periodically
for i := 0; i < 10; i++ {
time.Sleep(200 * time.Millisecond)
// Re-acquire should return same ID (cached)
processID2 := GetProcessID(shmFile)
if processID2 != processID1 {
t.Errorf("Process ID changed during long run: %d -> %d", processID1, processID2)
}
// Verify slot is still ours in shared memory
maxWorkers := runtime.NumCPU()
slotSize := 8
file, err := os.OpenFile(shmFile, os.O_RDONLY, 0644)
if err != nil {
t.Fatalf("Failed to open shared memory file: %v", err)
}
data, err := syscall.Mmap(int(file.Fd()), 0, maxWorkers*slotSize,
syscall.PROT_READ, syscall.MAP_SHARED)
if err != nil {
file.Close()
t.Fatalf("Failed to mmap file: %v", err)
}
offset := processID1 * slotSize
pid := *(*uint32)(unsafe.Pointer(&data[offset]))
myPID := uint32(os.Getpid())
syscall.Munmap(data)
file.Close()
if pid != myPID {
t.Errorf("Slot %d hijacked: expected PID %d, got %d", processID1, myPID, pid)
}
}
t.Logf("Long-running test completed successfully, held slot %d", processID1)
}
// TestGetProcessID_Cleanup tests proper cleanup behavior
func TestGetProcessID_Cleanup(t *testing.T) {
shmFile := t.TempDir() + "/worker-slots-cleanup"
// Acquire and release multiple times
for i := 0; i < 5; i++ {
processID := GetProcessID(shmFile)
if processID < 0 {
t.Fatalf("Failed to acquire process ID on iteration %d", i)
}
// Should get slot 0 each time since we're releasing
if processID != 0 {
t.Errorf("Expected slot 0 on iteration %d, got %d", i, processID)
}
// Release it
ReleaseProcessID(shmFile)
// Verify slot was cleared
maxWorkers := runtime.NumCPU()
slotSize := 8
file, err := os.OpenFile(shmFile, os.O_RDONLY, 0644)
if err != nil {
t.Fatalf("Failed to open shared memory file: %v", err)
}
data, err := syscall.Mmap(int(file.Fd()), 0, maxWorkers*slotSize,
syscall.PROT_READ, syscall.MAP_SHARED)
if err != nil {
file.Close()
t.Fatalf("Failed to mmap file: %v", err)
}
offset := processID * slotSize
pid := *(*uint32)(unsafe.Pointer(&data[offset]))
syscall.Munmap(data)
file.Close()
if pid != 0 {
t.Errorf("Slot %d not cleared after release: PID %d", processID, pid)
}
}
}