| 1 | |
| 2 | |
| 3 | package workbench |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "errors" |
| 8 | "net" |
| 9 | "os/exec" |
| 10 | "testing" |
| 11 | ) |
| 12 | |
| 13 | func TestDesktopViewerConnectionReplacementEndsTheOneHoldingInput(t *testing.T) { |
| 14 | cat, err := exec.LookPath("cat") |
| 15 | if err != nil { |
| 16 | t.Skip("needs cat as a stand-in relay") |
| 17 | } |
| 18 | open := func() (*nativeWindowConnection, error) { |
| 19 | ctx, cancel := context.WithCancel(context.Background()) |
| 20 | return startWindowConnection(exec.CommandContext(ctx, cat), cancel) |
| 21 | } |
| 22 | first, err := open() |
| 23 | if err != nil { |
| 24 | t.Fatal(err) |
| 25 | } |
| 26 | connection := &desktopViewerConnection{current: first} |
| 27 | if err := connection.request(map[string]any{"action": "frame"}, nil); err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | if err := connection.replace(open); err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | |
| 34 | if first.cmd.ProcessState == nil { |
| 35 | t.Fatal("the connection holding input is still open") |
| 36 | } |
| 37 | if connection.current == first || connection.request(map[string]any{"action": "frame"}, nil) != nil { |
| 38 | t.Fatal("the viewer has no working connection") |
| 39 | } |
| 40 | failure := errors.New("relay unavailable") |
| 41 | if err := connection.replace(func() (*nativeWindowConnection, error) { return nil, failure }); err != failure { |
| 42 | t.Fatalf("failed replacement hidden: %v", err) |
| 43 | } |
| 44 | if connection.request(map[string]any{"action": "frame"}, nil) != nil { |
| 45 | t.Fatal("a failed replacement closed the working connection") |
| 46 | } |
| 47 | second := connection.current |
| 48 | connection.Close() |
| 49 | var late *nativeWindowConnection |
| 50 | err = connection.replace(func() (*nativeWindowConnection, error) { late, err = open(); return late, err }) |
| 51 | if !errors.Is(err, net.ErrClosed) || second.cmd.ProcessState == nil || late == nil || late.cmd.ProcessState == nil { |
| 52 | t.Fatalf("a closed viewer kept a guest connection: %v", err) |
| 53 | } |
| 54 | } |