diff --git a/commands/legacy/legacy_test.go b/commands/legacy/legacy_test.go index 3e650cb6915..cb0de7692cb 100644 --- a/commands/legacy/legacy_test.go +++ b/commands/legacy/legacy_test.go @@ -114,7 +114,7 @@ func TestNewCommand(t *testing.T) { } func TestPipePair(t *testing.T) { - cmd := &cmds.Command{Type: "string"} + cmd := NewCommand(&oldcmds.Command{Type: "string"}) req, err := cmds.NewRequest(context.TODO(), nil, nil, nil, nil, cmd) if err != nil { @@ -134,6 +134,11 @@ func TestPipePair(t *testing.T) { t.Fatal(err) } + err = re.Close() + if err != nil { + t.Fatal(err) + } + close(wait) }() @@ -149,6 +154,57 @@ func TestPipePair(t *testing.T) { t.Fatalf("expected value %#v but got %#v", expect, v) } + _, err = res.Next() + if err != io.EOF { + t.Fatal("expected io.EOF, got:", err) + } + <-wait +} + +func TestChanPair(t *testing.T) { + cmd := NewCommand(&oldcmds.Command{Type: "string"}) + + req, err := cmds.NewRequest(context.TODO(), nil, nil, nil, nil, cmd) + if err != nil { + t.Fatal(err) + } + + re, res := cmds.NewChanResponsePair(req) + wait := make(chan interface{}) + + expect := "abc" + go func() { + err := re.Emit(expect) + if err != nil { + t.Fatal(err) + } + + err = re.Close() + if err != nil { + t.Fatal(err) + } + + close(wait) + }() + + v, err := res.Next() + if err != nil { + t.Fatal(err) + } + str, ok := v.(string) + if !ok { + t.Fatalf("expected type %T but got %T", expect, v) + } + if str != expect { + t.Fatalf("expected value %#v but got %#v", expect, v) + } + + _, err = res.Next() + if err != io.EOF { + t.Fatal("expected io.EOF, got:", err) + } + + <-wait } diff --git a/commands/legacy/response.go b/commands/legacy/response.go index 1ee87c4b8ff..f2b8a5a44d8 100644 --- a/commands/legacy/response.go +++ b/commands/legacy/response.go @@ -123,16 +123,13 @@ func (r *fakeResponse) Send(errCh chan<- error) { defer close(errCh) out := r.Output() - if out == nil { - return - } - if ch, ok := out.(chan interface{}); ok { - out = (<-chan interface{})(ch) + // don't emit nil or Single{nil} + if out == nil || out == (cmds.Single{nil}) { + return } - err := r.re.Emit(out) - errCh <- err + errCh <- r.re.Emit(out) return }