Repository navigation
Expand file tree
/
Copy pathterminal_input_test.go
More file actions
78 lines (70 loc) 路 1.98 KB
/
Copy pathterminal_input_test.go
File metadata and controls
78 lines (70 loc) 路 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package uv
import (
"fmt"
"testing"
"time"
)
// TestInputLoopDoesNotReuseTheReadBuffer is the regression test for #176. the
// input loop handed the event loop a slice of its own read buffer, and its
// next read could overwrite those bytes before the event loop had copied them,
// so one input sequence arrived as the bytes of the next.
//
// two different replies go in back to back, the way a terminal answers two
// queries, and every round must produce them in order.
func TestInputLoopDoesNotReuseTheReadBuffer(t *testing.T) {
const (
rounds = 2000
decrpm = "\x1b[?1016;2$y" // a mode report, eleven bytes
kitty = "\x1b_Gi=31;OK\x1b\\" // a kitty graphics reply; its first eleven bytes differ
)
con := newPipeConsole()
tm := NewTerminal(con, nil)
if err := tm.Start(); err != nil {
t.Fatalf("Start: %v", err)
}
defer stopAfterInputEnds(t, tm, con)
for i := range rounds {
con.send(decrpm)
con.send(kitty)
got := nextReplies(tm, 2)
if len(got) != 2 || !isModeReport(got[0]) || !isKittyReply(got[1]) {
t.Fatalf("round %d: replies arrived as %s, want [uv.ModeReportEvent uv.KittyGraphicsEvent]",
i, typesOf(got))
}
}
}
// nextReplies collects up to n events other than the window size Start
// reports on its own, and returns early with what it has if they stop
// coming. a reply the corruption swallowed shows up as a short result, not a
// hang.
func nextReplies(tm *Terminal, n int) []Event {
deadline := time.After(time.Second)
var got []Event
for len(got) < n {
select {
case ev := <-tm.Events():
if _, ok := ev.(WindowSizeEvent); ok {
continue
}
got = append(got, ev)
case <-deadline:
return got
}
}
return got
}
func isModeReport(ev Event) bool {
_, ok := ev.(ModeReportEvent)
return ok
}
func isKittyReply(ev Event) bool {
_, ok := ev.(KittyGraphicsEvent)
return ok
}
func typesOf(evs []Event) string {
names := make([]string, len(evs))
for i, ev := range evs {
names[i] = fmt.Sprintf("%T", ev)
}
return fmt.Sprint(names)
}