Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion shellterm.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,13 @@ func (t *Session) key(k lineedit.Key) {
}
return
}
ctx, cancel := context.WithTimeout(context.Background(), CompleteTimeout)
// Only Tab asks the completer, so only Tab needs a deadline: a context and
// a timer per keystroke cost four allocations and a runtime timer for
// nothing (wasmdesk/wasmbox#175).
ctx, cancel := context.Background(), context.CancelFunc(func() {})
if k.Code == lineedit.KeyTab {
ctx, cancel = context.WithTimeout(ctx, CompleteTimeout)
}
before := t.ed.State()
ev := t.ed.Key(ctx, k)
cancel()
Expand Down
25 changes: 25 additions & 0 deletions shellterm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,28 @@ func TestSessionPromptHook(t *testing.T) {
r.ses.Input("z\r")
r.until("after SetShell", func() bool { return !r.running() && r.line() == "swapped $" })
}

// A completion runs under CompleteTimeout; the keys that do not complete get no
// timer (wasmdesk/wasmbox#175).
func TestTabCompletionHasADeadline(t *testing.T) {
r := newRig(t, Options{})
var mu sync.Mutex
var deadlines []bool
r.ses.SetShell(&recShell{}, func(ctx context.Context, _ []string, _ string) ([]lineedit.Candidate, bool) {
_, ok := ctx.Deadline()
mu.Lock()
deadlines = append(deadlines, ok)
mu.Unlock()
return nil, false
})
r.attach()
r.ses.Input("a")
r.ses.Input("\t")
r.settle()
r.until("the completer ran", func() bool { mu.Lock(); defer mu.Unlock(); return len(deadlines) == 1 })
mu.Lock()
defer mu.Unlock()
if !deadlines[0] {
t.Error("Tab's context has no deadline")
}
}
Loading