Skip to content

Commit

Permalink
fix goroutine leaks and close channels in window tests
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed Oct 17, 2024
1 parent a54c66a commit d6dc8e2
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 87 deletions.
128 changes: 83 additions & 45 deletions window/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,15 @@ func TestManagerOpenEmpty(t *testing.T) {
eventCh, redrawCh := make(chan event.Event), make(chan struct{})
wm.Init(eventCh, redrawCh)
go func() {
<-redrawCh
defer close(eventCh)
defer close(redrawCh)
ev := <-eventCh
if ev.Type != event.Error {
t.Errorf("event type should be %d but got: %d", event.Error, ev.Type)
}
if expected := "no file name"; ev.Error.Error() != expected {
t.Errorf("err should be %q but got: %v", expected, ev.Error)
}
}()
wm.SetSize(110, 20)
if err := wm.Open(""); err != nil {
Expand Down Expand Up @@ -62,21 +70,18 @@ func TestManagerOpenEmpty(t *testing.T) {
if err != nil {
t.Errorf("err should be nil but got: %v", err)
}
go wm.Emit(event.Event{Type: event.Write})
ev := <-eventCh
if ev.Type != event.Error {
t.Errorf("event type should be %d but got: %d", event.Error, ev.Type)
}
if expected := "no file name"; ev.Error.Error() != expected {
t.Errorf("err should be %q but got: %v", expected, ev.Error)
}
wm.Emit(event.Event{Type: event.Write})
wm.Close()
}

func TestManagerOpenStates(t *testing.T) {
wm := NewManager()
eventCh, redrawCh := make(chan event.Event), make(chan struct{})
wm.Init(eventCh, redrawCh)
go func() {
defer close(eventCh)
defer close(redrawCh)
}()
wm.SetSize(110, 20)
str := "Hello, world! こんにちは、世界!"
f, err := createTemp(t.TempDir(), str)
Expand Down Expand Up @@ -117,11 +122,13 @@ func TestManagerOpenNonExistsWrite(t *testing.T) {
eventCh, redrawCh := make(chan event.Event), make(chan struct{})
wm.Init(eventCh, redrawCh)
go func() {
for {
select {
case <-eventCh:
case <-redrawCh:
}
defer close(eventCh)
defer close(redrawCh)
for range 16 {
<-redrawCh
}
if ev := <-eventCh; ev.Type != event.QuitAll {
t.Errorf("event type should be %d but got: %d", event.QuitAll, ev.Type)
}
}()
wm.SetSize(110, 20)
Expand Down Expand Up @@ -175,6 +182,10 @@ func TestManagerOpenExpandBacktick(t *testing.T) {
wm := NewManager()
eventCh, redrawCh := make(chan event.Event), make(chan struct{})
wm.Init(eventCh, redrawCh)
go func() {
defer close(eventCh)
defer close(redrawCh)
}()
wm.SetSize(110, 20)
cmd, name := "`which ls`", "ls"
if runtime.GOOS == "windows" {
Expand Down Expand Up @@ -210,6 +221,10 @@ func TestEditorOpenExpandHomedir(t *testing.T) {
wm := NewManager()
eventCh, redrawCh := make(chan event.Event), make(chan struct{})
wm.Init(eventCh, redrawCh)
go func() {
defer close(eventCh)
defer close(redrawCh)
}()
wm.SetSize(110, 20)
str := "Hello, world!"
f, err := createTemp(t.TempDir(), str)
Expand Down Expand Up @@ -248,19 +263,43 @@ func TestManagerOpenChdirWrite(t *testing.T) {
wm := NewManager()
eventCh, redrawCh := make(chan event.Event), make(chan struct{})
wm.Init(eventCh, redrawCh)
go func() {
for {
select {
case <-eventCh:
case <-redrawCh:
}
}
}()
wm.SetSize(110, 20)
f, err := createTemp(t.TempDir(), "Hello")
if err != nil {
t.Fatalf("err should be nil but got: %v", err)
}
go func() {
defer close(eventCh)
defer close(redrawCh)
ev := <-eventCh
if ev.Type != event.Info {
t.Errorf("event type should be %d but got: %d", event.Info, ev.Type)
}
dir, err := filepath.EvalSymlinks(filepath.Dir(f.Name()))
if err != nil {
t.Errorf("err should be nil but got: %v", err)
}
if expected := dir; ev.Error.Error() != expected {
t.Errorf("err should be %q but got: %v", expected, ev.Error)
}
ev = <-eventCh
if ev.Type != event.Info {
t.Errorf("event type should be %d but got: %d", event.Info, ev.Type)
}
if expected := filepath.Dir(dir); ev.Error.Error() != expected {
t.Errorf("err should be %q but got: %v", expected, ev.Error)
}
for range 11 {
<-redrawCh
}
ev = <-eventCh
if ev.Type != event.Info {
t.Errorf("event type should be %d but got: %d", event.Info, ev.Type)
}
if expected := "13 (0xd) bytes written"; !strings.HasSuffix(ev.Error.Error(), expected) {
t.Errorf("err should be %q but got: %v", expected, ev.Error)
}
}()
wm.SetSize(110, 20)
wm.Emit(event.Event{Type: event.Chdir, Arg: filepath.Dir(f.Name())})
if err := wm.Open(filepath.Base(f.Name())); err != nil {
t.Fatalf("err should be nil but got: %v", err)
Expand Down Expand Up @@ -292,12 +331,8 @@ func TestManagerOpenDirectory(t *testing.T) {
eventCh, redrawCh := make(chan event.Event), make(chan struct{})
wm.Init(eventCh, redrawCh)
go func() {
for {
select {
case <-eventCh:
case <-redrawCh:
}
}
defer close(eventCh)
defer close(redrawCh)
}()
wm.SetSize(110, 20)
dir := t.TempDir()
Expand All @@ -315,6 +350,10 @@ func TestManagerRead(t *testing.T) {
wm := NewManager()
eventCh, redrawCh := make(chan event.Event), make(chan struct{})
wm.Init(eventCh, redrawCh)
go func() {
defer close(eventCh)
defer close(redrawCh)
}()
wm.SetSize(110, 20)
r := strings.NewReader("Hello, world!")
if err := wm.Read(r); err != nil {
Expand Down Expand Up @@ -348,11 +387,10 @@ func TestManagerOnly(t *testing.T) {
eventCh, redrawCh := make(chan event.Event), make(chan struct{})
wm.Init(eventCh, redrawCh)
go func() {
for {
select {
case <-eventCh:
case <-redrawCh:
}
defer close(eventCh)
defer close(redrawCh)
for range 4 {
<-eventCh
}
}()
wm.SetSize(110, 20)
Expand Down Expand Up @@ -387,11 +425,10 @@ func TestManagerAlternative(t *testing.T) {
eventCh, redrawCh := make(chan event.Event), make(chan struct{})
wm.Init(eventCh, redrawCh)
go func() {
for {
select {
case <-eventCh:
case <-redrawCh:
}
defer close(eventCh)
defer close(redrawCh)
for range 9 {
<-eventCh
}
}()
wm.SetSize(110, 20)
Expand Down Expand Up @@ -483,11 +520,10 @@ func TestManagerWincmd(t *testing.T) {
eventCh, redrawCh := make(chan event.Event), make(chan struct{})
wm.Init(eventCh, redrawCh)
go func() {
for {
select {
case <-eventCh:
case <-redrawCh:
}
defer close(eventCh)
defer close(redrawCh)
for range 17 {
<-eventCh
}
}()
wm.SetSize(110, 20)
Expand Down Expand Up @@ -556,6 +592,9 @@ func TestManagerCopyCutPaste(t *testing.T) {
}
_, _, _, _ = wm.State()
go func() {
defer close(eventCh)
defer close(redrawCh)
defer close(waitCh)
<-redrawCh
<-redrawCh
<-redrawCh
Expand Down Expand Up @@ -620,7 +659,6 @@ func TestManagerCopyCutPaste(t *testing.T) {
if expected := "Hefoobarfoobarfoobarlrld!"; !strings.HasPrefix(string(ws.Bytes), expected) {
t.Errorf("Bytes should start with %q but got %q", expected, string(ws.Bytes))
}
close(waitCh)
}()
wm.Emit(event.Event{Type: event.CursorNext, Mode: mode.Normal, Count: 3})
wm.Emit(event.Event{Type: event.StartVisual})
Expand Down
Loading

0 comments on commit d6dc8e2

Please sign in to comment.