|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "os" |
| 6 | + "sync" |
| 7 | + "syscall" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | +) |
| 13 | + |
| 14 | +func Test_signalHandler_Signal(t *testing.T) { |
| 15 | + // Create a context with cancel function |
| 16 | + ctx, cancel := context.WithCancel(context.Background()) |
| 17 | + defer cancel() |
| 18 | + |
| 19 | + // Create a waitgroup to synchronize the test |
| 20 | + var wg sync.WaitGroup |
| 21 | + wg.Add(1) |
| 22 | + |
| 23 | + // Track if cleanup function was called |
| 24 | + cleanupCalled := false |
| 25 | + cleanupError := "" |
| 26 | + |
| 27 | + // Mock cleanup function |
| 28 | + cleanup := func(ctx context.Context, err error) { |
| 29 | + cleanupCalled = true |
| 30 | + if err != nil { |
| 31 | + cleanupError = err.Error() |
| 32 | + } |
| 33 | + wg.Done() |
| 34 | + } |
| 35 | + |
| 36 | + // Save original os.Exit and restore after test |
| 37 | + originalOsExit := osExit |
| 38 | + defer func() { osExit = originalOsExit }() |
| 39 | + |
| 40 | + exitCode := 0 |
| 41 | + osExit = func(code int) { |
| 42 | + exitCode = code |
| 43 | + // Instead of exiting, just cancel the context |
| 44 | + cancel() |
| 45 | + } |
| 46 | + |
| 47 | + // Set up the signal handler |
| 48 | + signalHandler(ctx, cancel, cleanup) |
| 49 | + |
| 50 | + // Send a signal to trigger the handler |
| 51 | + p, err := os.FindProcess(os.Getpid()) |
| 52 | + if err != nil { |
| 53 | + t.Fatalf("Failed to find process: %v", err) |
| 54 | + } |
| 55 | + |
| 56 | + // Send SIGINT to trigger the handler |
| 57 | + err = p.Signal(syscall.SIGINT) |
| 58 | + if err != nil { |
| 59 | + t.Fatalf("Failed to send signal: %v", err) |
| 60 | + } |
| 61 | + |
| 62 | + // Wait for cleanup to be called with a timeout |
| 63 | + waitCh := make(chan struct{}) |
| 64 | + go func() { |
| 65 | + wg.Wait() |
| 66 | + close(waitCh) |
| 67 | + }() |
| 68 | + |
| 69 | + select { |
| 70 | + case <-waitCh: |
| 71 | + // Success - cleanup was called |
| 72 | + case <-time.After(1 * time.Second): |
| 73 | + t.Fatal("Timed out waiting for cleanup function to be called") |
| 74 | + } |
| 75 | + |
| 76 | + // Verify cleanup was called with the expected error |
| 77 | + assert.True(t, cleanupCalled, "Cleanup function should have been called") |
| 78 | + assert.Contains(t, cleanupError, "command interrupted by signal: interrupt") |
| 79 | + assert.Equal(t, 1, exitCode, "Exit code should be 1") |
| 80 | +} |
| 81 | + |
| 82 | +func Test_signalHandler_ContextDone(t *testing.T) { |
| 83 | + // Create a context with cancel function |
| 84 | + ctx, cancel := context.WithCancel(context.Background()) |
| 85 | + |
| 86 | + // We expect cleanup NOT to be called when context is cancelled |
| 87 | + cleanupCalled := false |
| 88 | + |
| 89 | + cleanup := func(ctx context.Context, err error) { |
| 90 | + cleanupCalled = true |
| 91 | + } |
| 92 | + |
| 93 | + // Set up the signal handler |
| 94 | + signalHandler(ctx, cancel, cleanup) |
| 95 | + |
| 96 | + // Cancel the context |
| 97 | + cancel() |
| 98 | + |
| 99 | + // Give some time for any handlers to run |
| 100 | + time.Sleep(100 * time.Millisecond) |
| 101 | + |
| 102 | + // Verify cleanup was NOT called |
| 103 | + assert.False(t, cleanupCalled, "Cleanup function should not have been called when context is done") |
| 104 | +} |
0 commit comments