internal/gitutil/transport_test.go
33 lines · 1006 bytes
1package gitutil
2
3import (
4 "io"
5 "os/exec"
6 "strings"
7 "testing"
8 "time"
9)
10
11// Closing cancel kills the transport; it does not wait for the client
12// to hang up. Stdin ends only after the kill, as a cut connection's
13// does, so a clean exit here would mean the kill never happened.
14func TestTransportCancelKillsGit(t *testing.T) {
15 dir := t.TempDir()
16 if out, err := exec.Command("git", "init", "-q", "--bare", dir).CombinedOutput(); err != nil {
17 t.Fatalf("git init: %v\n%s", err, out)
18 }
19 in, w := io.Pipe()
20 cancel := make(chan struct{})
21 errc := make(chan error, 1)
22 go func() { errc <- Transport("git-upload-pack", dir, in, io.Discard, io.Discard, nil, 0, cancel) }()
23 close(cancel)
24 time.AfterFunc(500*time.Millisecond, func() { w.Close() })
25 select {
26 case err := <-errc:
27 if err == nil || !strings.Contains(err.Error(), "killed") {
28 t.Fatalf("Transport returned %v, want the process killed", err)
29 }
30 case <-time.After(5 * time.Second):
31 t.Fatal("Transport did not return after cancel")
32 }
33}