|
6 | 6 | package provider |
7 | 7 |
|
8 | 8 | import ( |
| 9 | + "context" |
9 | 10 | "testing" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/microsoft/retina/pkg/capture/file" |
| 14 | + "github.com/microsoft/retina/pkg/log" |
| 15 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
10 | 16 | ) |
11 | 17 |
|
12 | 18 | func TestValidateNetshFilter(t *testing.T) { |
@@ -114,3 +120,37 @@ func TestValidateNetshFilter(t *testing.T) { |
114 | 120 | }) |
115 | 121 | } |
116 | 122 | } |
| 123 | + |
| 124 | +// TestStopNetworkCapture_ContextIndependence verifies stopNetworkCapture creates its own context |
| 125 | +func TestStopNetworkCapture_ContextIndependence(t *testing.T) { |
| 126 | + now := metav1.Now() |
| 127 | + ncp := &NetworkCaptureProvider{ |
| 128 | + NetworkCaptureProviderCommon: NetworkCaptureProviderCommon{ |
| 129 | + TmpCaptureDir: t.TempDir(), |
| 130 | + l: log.Logger().Named("test-capture"), |
| 131 | + }, |
| 132 | + Filename: file.CaptureFilename{ |
| 133 | + CaptureName: "test-capture", |
| 134 | + NodeHostname: "test-node", |
| 135 | + StartTimestamp: &now, |
| 136 | + }, |
| 137 | + } |
| 138 | + |
| 139 | + // Create an expired context (simulating capture duration ending) |
| 140 | + parentCtx, cancel := context.WithTimeout(context.Background(), 1*time.Nanosecond) |
| 141 | + time.Sleep(10 * time.Millisecond) |
| 142 | + defer cancel() |
| 143 | + |
| 144 | + if parentCtx.Err() == nil { |
| 145 | + t.Fatal("Setup error: parent context should be expired") |
| 146 | + } |
| 147 | + |
| 148 | + // Call StopNetworkCapture - should NOT return "context deadline exceeded" |
| 149 | + err := ncp.stopNetworkCapture() |
| 150 | + |
| 151 | + if err != nil && err.Error() == "context deadline exceeded" { |
| 152 | + t.Fatal("StopNetworkCapture returned 'context deadline exceeded' - bug reintroduced") |
| 153 | + } |
| 154 | + |
| 155 | + t.Logf("StopNetworkCapture uses independent context (netsh error expected: %v)", err) |
| 156 | +} |
0 commit comments