| 1 | package cage |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | "time" |
| 6 | ) |
| 7 | |
| 8 | func TestJobStopCoversLateAndRunningHostagents(t *testing.T) { |
| 9 | profile := t.TempDir() |
| 10 | id := "0123456789ab" |
| 11 | called := make(chan struct{}, 1) |
| 12 | stop, err := WatchJobStop(profile, id, func() { called <- struct{}{} }) |
| 13 | if err != nil { |
| 14 | t.Fatal(err) |
| 15 | } |
| 16 | defer stop() |
| 17 | if err = RequestJobStop(profile, id); err != nil { |
| 18 | t.Fatal(err) |
| 19 | } |
| 20 | select { |
| 21 | case <-called: |
| 22 | case <-time.After(2 * time.Second): |
| 23 | t.Fatal("running hostagent did not observe cancellation") |
| 24 | } |
| 25 | if _, err = WatchJobStop(profile, id, func() { t.Error("late startup callback") }); err == nil { |
| 26 | t.Fatal("cancelled job could start a detached hostagent") |
| 27 | } |
| 28 | fresh, err := WatchJobStop(profile, "abcdef012345", func() { t.Error("new job was cancelled") }) |
| 29 | if err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | fresh() |
| 33 | if err = RequestJobStop(profile, "../../unrelated"); err == nil { |
| 34 | t.Fatal("accepted a path as job ID") |
| 35 | } |
| 36 | } |