fix: avoid blocking prints after program exit (#1758)
Co-authored-by: Ricardo-M-L <XuthodoreveraynWX61@elvisfan.com> Co-authored-by: Drew Smirnoff <drew@charm.land>
This commit is contained in:
parent
418cada888
commit
7a446b73f6
2 changed files with 49 additions and 4 deletions
10
tea.go
10
tea.go
|
|
@ -1474,10 +1474,11 @@ func (p *Program) RestoreTerminal() error {
|
|||
// and will persist across renders by the Program.
|
||||
//
|
||||
// If the altscreen is active no output will be printed.
|
||||
// If the program has already exited this is a no-op.
|
||||
func (p *Program) Println(args ...any) {
|
||||
p.msgs <- printLineMessage{
|
||||
p.Send(printLineMessage{
|
||||
messageBody: fmt.Sprint(args...),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Printf prints above the Program. It takes a format template followed by
|
||||
|
|
@ -1488,10 +1489,11 @@ func (p *Program) Println(args ...any) {
|
|||
// its own line.
|
||||
//
|
||||
// If the altscreen is active no output will be printed.
|
||||
// If the program has already exited this is a no-op.
|
||||
func (p *Program) Printf(template string, args ...any) {
|
||||
p.msgs <- printLineMessage{
|
||||
p.Send(printLineMessage{
|
||||
messageBody: fmt.Sprintf(template, args...),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// startRenderer starts the renderer.
|
||||
|
|
|
|||
43
tea_test.go
43
tea_test.go
|
|
@ -560,6 +560,49 @@ func TestTeaSend(t *testing.T) {
|
|||
p.Send(Quit())
|
||||
}
|
||||
|
||||
func TestProgramPrintAfterExit(t *testing.T) {
|
||||
t.Parallel()
|
||||
var buf bytes.Buffer
|
||||
var in bytes.Buffer
|
||||
|
||||
p := NewProgram(&testModel{},
|
||||
WithInput(&in),
|
||||
WithOutput(&buf),
|
||||
)
|
||||
go p.Send(Quit())
|
||||
if _, err := p.Run(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
print func()
|
||||
}{
|
||||
{"Println", func() { p.Println("after exit") }},
|
||||
{"Printf", func() { p.Printf("after %s", "exit") }},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
tt.print()
|
||||
close(done)
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-done:
|
||||
case <-time.After(100 * time.Millisecond):
|
||||
// Receive only after detecting the block. This releases the
|
||||
// direct channel send used before this regression was fixed,
|
||||
// so the test does not leak a goroutine against the old code.
|
||||
<-p.msgs
|
||||
<-done
|
||||
t.Fatal("print blocked after the program exited")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTeaNoRun(t *testing.T) {
|
||||
t.Parallel()
|
||||
var buf bytes.Buffer
|
||||
|
|
|
|||
Loading…
Reference in a new issue