fix: don't block in Send after shutdown

Send should block before a tea.Program has been started, but result
in a no-op when it has already been terminated.

Fixed godocs.
This commit is contained in:
Christian Muehlhaeuser
2022-10-15 04:04:06 +02:00
parent a520b7f4e1
commit 3609d87e70
2 changed files with 38 additions and 14 deletions

View File

@@ -149,3 +149,29 @@ func TestTeaSequenceMsg(t *testing.T) {
t.Fatalf("counter should be 2, got %d", m.counter.Load())
}
}
func TestTeaSend(t *testing.T) {
var buf bytes.Buffer
var in bytes.Buffer
m := &testModel{}
p := NewProgram(m, WithInput(&in), WithOutput(&buf))
// sending before the program is started is a blocking operation
go p.Send(Quit())
if _, err := p.Run(); err != nil {
t.Fatal(err)
}
// sending a message after program has quit is a no-op
p.Send(Quit())
}
func TestTeaNoRun(t *testing.T) {
var buf bytes.Buffer
var in bytes.Buffer
m := &testModel{}
NewProgram(m, WithInput(&in), WithOutput(&buf))
}