forked from mtusi2026/mtusi2026lab5
lab5 na4
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
package images
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestImageSubprocessDeadlineKillsChild(t *testing.T) {
|
||||
if _, err := exec.LookPath("python3"); err != nil {
|
||||
t.Skip("python3 is required for subprocess test")
|
||||
}
|
||||
script := filepath.Join(t.TempDir(), "slow.py")
|
||||
if err := os.WriteFile(script, []byte("import time\ntime.sleep(60)\n"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Setenv("IMAGE_SCRIPT", script)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
|
||||
defer cancel()
|
||||
start := time.Now()
|
||||
_, err := Run(ctx, "process", "unused", "unused")
|
||||
if !errors.Is(err, context.DeadlineExceeded) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if time.Since(start) > 3*time.Second {
|
||||
t.Fatal("child was not killed within deadline")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user