2016-08-28 17:21:57 +00:00
|
|
|
// Copyright 2016 syzkaller project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
|
|
|
|
|
2017-06-02 18:09:00 +00:00
|
|
|
package vmimpl
|
2016-08-28 17:21:57 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
|
|
|
"time"
|
2017-06-02 18:09:00 +00:00
|
|
|
|
|
|
|
"github.com/google/syzkaller/pkg/osutil"
|
2016-08-28 17:21:57 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestMerger(t *testing.T) {
|
|
|
|
tee := new(bytes.Buffer)
|
|
|
|
merger := NewOutputMerger(tee)
|
|
|
|
|
2017-06-02 18:09:00 +00:00
|
|
|
rp1, wp1, err := osutil.LongPipe()
|
2016-08-28 17:21:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer wp1.Close()
|
2016-12-08 19:54:09 +00:00
|
|
|
merger.Add("pipe1", rp1)
|
2016-08-28 17:21:57 +00:00
|
|
|
|
2017-06-02 18:09:00 +00:00
|
|
|
rp2, wp2, err := osutil.LongPipe()
|
2016-08-28 17:21:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer wp2.Close()
|
2016-12-08 19:54:09 +00:00
|
|
|
merger.Add("pipe2", rp2)
|
2016-08-28 17:21:57 +00:00
|
|
|
|
|
|
|
wp1.Write([]byte("111"))
|
|
|
|
select {
|
|
|
|
case <-merger.Output:
|
|
|
|
t.Fatalf("merger produced incomplete line")
|
|
|
|
case <-time.After(10 * time.Millisecond):
|
|
|
|
}
|
|
|
|
|
|
|
|
wp2.Write([]byte("222"))
|
|
|
|
select {
|
|
|
|
case <-merger.Output:
|
|
|
|
t.Fatalf("merger produced incomplete line")
|
|
|
|
case <-time.After(10 * time.Millisecond):
|
|
|
|
}
|
|
|
|
|
|
|
|
wp1.Write([]byte("333\n444"))
|
|
|
|
got := string(<-merger.Output)
|
|
|
|
if want := "111333\n"; got != want {
|
|
|
|
t.Fatalf("bad line: '%s', want '%s'", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
wp2.Write([]byte("555\n666\n777"))
|
|
|
|
got = string(<-merger.Output)
|
|
|
|
if want := "222555\n666\n"; got != want {
|
|
|
|
t.Fatalf("bad line: '%s', want '%s'", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
wp1.Close()
|
|
|
|
got = string(<-merger.Output)
|
|
|
|
if want := "444\n"; got != want {
|
|
|
|
t.Fatalf("bad line: '%s', want '%s'", got, want)
|
|
|
|
}
|
|
|
|
|
2016-12-08 19:54:09 +00:00
|
|
|
if err := <-merger.Err; err == nil || err.Error() != "failed to read from pipe1: EOF" {
|
|
|
|
t.Fatalf("merger did not produce io.EOF: %v", err)
|
|
|
|
}
|
|
|
|
|
2016-08-28 17:21:57 +00:00
|
|
|
wp2.Close()
|
|
|
|
got = string(<-merger.Output)
|
|
|
|
if want := "777\n"; got != want {
|
|
|
|
t.Fatalf("bad line: '%s', want '%s'", got, want)
|
|
|
|
}
|
|
|
|
|
|
|
|
merger.Wait()
|
|
|
|
want := "111333\n222555\n666\n444\n777\n"
|
|
|
|
if got := string(tee.Bytes()); got != want {
|
|
|
|
t.Fatalf("bad tee: '%s', want '%s'", got, want)
|
|
|
|
}
|
|
|
|
}
|