|
| 1 | +package common |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "syscall" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "go.uber.org/zap" |
| 10 | +) |
| 11 | + |
| 12 | +func TestListenSysExit(t *testing.T) { |
| 13 | + // Create a logger for testing |
| 14 | + logger := zap.NewNop() |
| 15 | + |
| 16 | + // Test SIGTERM signal |
| 17 | + t.Run("SIGTERM signal", func(t *testing.T) { |
| 18 | + // Create a new context for this test |
| 19 | + testCtx, testCancel := context.WithCancel(context.Background()) |
| 20 | + defer testCancel() |
| 21 | + |
| 22 | + // Start ListenSysExit |
| 23 | + go ListenSysExit(logger, testCancel) |
| 24 | + |
| 25 | + // Give some time for the signal handler to be set up |
| 26 | + time.Sleep(100 * time.Millisecond) |
| 27 | + |
| 28 | + // Send SIGTERM signal to the current process |
| 29 | + err := syscall.Kill(syscall.Getpid(), syscall.SIGTERM) |
| 30 | + if err != nil { |
| 31 | + t.Fatalf("Failed to send SIGTERM signal: %v", err) |
| 32 | + } |
| 33 | + |
| 34 | + // Wait for the context to be cancelled |
| 35 | + select { |
| 36 | + case <-testCtx.Done(): |
| 37 | + // Context was cancelled as expected |
| 38 | + t.Log("Context cancelled successfully after SIGTERM") |
| 39 | + case <-time.After(2 * time.Second): |
| 40 | + t.Fatal("Context was not cancelled within 2 seconds after SIGTERM") |
| 41 | + } |
| 42 | + }) |
| 43 | + |
| 44 | + // Test SIGINT signal |
| 45 | + t.Run("SIGINT signal", func(t *testing.T) { |
| 46 | + // Create a new context for this test |
| 47 | + testCtx, testCancel := context.WithCancel(context.Background()) |
| 48 | + defer testCancel() |
| 49 | + |
| 50 | + // Start ListenSysExit |
| 51 | + go ListenSysExit(logger, testCancel) |
| 52 | + |
| 53 | + // Give some time for the signal handler to be set up |
| 54 | + time.Sleep(100 * time.Millisecond) |
| 55 | + |
| 56 | + // Send SIGINT signal to the current process |
| 57 | + err := syscall.Kill(syscall.Getpid(), syscall.SIGINT) |
| 58 | + if err != nil { |
| 59 | + t.Fatalf("Failed to send SIGINT signal: %v", err) |
| 60 | + } |
| 61 | + |
| 62 | + // Wait for the context to be cancelled |
| 63 | + select { |
| 64 | + case <-testCtx.Done(): |
| 65 | + // Context was cancelled as expected |
| 66 | + t.Log("Context cancelled successfully after SIGINT") |
| 67 | + case <-time.After(2 * time.Second): |
| 68 | + t.Fatal("Context was not cancelled within 2 seconds after SIGINT") |
| 69 | + } |
| 70 | + }) |
| 71 | + |
| 72 | + // Test that the function doesn't exit immediately |
| 73 | + t.Run("no signal sent", func(t *testing.T) { |
| 74 | + // Create a new context for this test |
| 75 | + testCtx, testCancel := context.WithCancel(context.Background()) |
| 76 | + defer testCancel() |
| 77 | + |
| 78 | + // Start ListenSysExit |
| 79 | + go ListenSysExit(logger, testCancel) |
| 80 | + |
| 81 | + // Wait a short time and verify context is not cancelled |
| 82 | + select { |
| 83 | + case <-testCtx.Done(): |
| 84 | + t.Fatal("Context was unexpectedly cancelled without signal") |
| 85 | + case <-time.After(500 * time.Millisecond): |
| 86 | + // Context was not cancelled as expected |
| 87 | + t.Log("Context remained active as expected when no signal was sent") |
| 88 | + } |
| 89 | + }) |
| 90 | +} |
| 91 | + |
| 92 | +// TestListenSysExitConcurrent tests that multiple calls to ListenSysExit work correctly |
| 93 | +func TestListenSysExitConcurrent(t *testing.T) { |
| 94 | + logger := zap.NewNop() |
| 95 | + ctx, cancel := context.WithCancel(context.Background()) |
| 96 | + defer cancel() |
| 97 | + |
| 98 | + // Start multiple ListenSysExit goroutines |
| 99 | + for i := 0; i < 3; i++ { |
| 100 | + go ListenSysExit(logger, cancel) |
| 101 | + } |
| 102 | + |
| 103 | + // Give some time for all signal handlers to be set up |
| 104 | + time.Sleep(100 * time.Millisecond) |
| 105 | + |
| 106 | + // Send SIGTERM signal |
| 107 | + err := syscall.Kill(syscall.Getpid(), syscall.SIGTERM) |
| 108 | + if err != nil { |
| 109 | + t.Fatalf("Failed to send SIGTERM signal: %v", err) |
| 110 | + } |
| 111 | + |
| 112 | + // Wait for the context to be cancelled |
| 113 | + select { |
| 114 | + case <-ctx.Done(): |
| 115 | + t.Log("Context cancelled successfully with multiple ListenSysExit goroutines") |
| 116 | + case <-time.After(2 * time.Second): |
| 117 | + t.Fatal("Context was not cancelled within 2 seconds") |
| 118 | + } |
| 119 | +} |
0 commit comments