2014-07-12 02:02:55 +08:00
|
|
|
package syncpipe
|
2014-06-27 02:50:22 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2014-08-07 09:00:52 +08:00
|
|
|
type testStruct struct {
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
2014-06-27 02:50:22 +08:00
|
|
|
func TestSendErrorFromChild(t *testing.T) {
|
|
|
|
pipe, err := NewSyncPipe()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := pipe.Close(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
expected := "something bad happened"
|
|
|
|
|
|
|
|
pipe.ReportChildError(fmt.Errorf(expected))
|
|
|
|
|
|
|
|
childError := pipe.ReadFromChild()
|
|
|
|
if childError == nil {
|
|
|
|
t.Fatal("expected an error to be returned but did not receive anything")
|
|
|
|
}
|
|
|
|
|
|
|
|
if childError.Error() != expected {
|
|
|
|
t.Fatalf("expected %q but received error message %q", expected, childError.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSendPayloadToChild(t *testing.T) {
|
|
|
|
pipe, err := NewSyncPipe()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err := pipe.Close(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
expected := "libcontainer"
|
|
|
|
|
2014-08-07 09:00:52 +08:00
|
|
|
if err := pipe.SendToChild(testStruct{Name: expected}); err != nil {
|
2014-06-27 02:50:22 +08:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-08-07 09:00:52 +08:00
|
|
|
var s *testStruct
|
|
|
|
if err := pipe.ReadFromParent(&s); err != nil {
|
2014-06-27 02:50:22 +08:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-08-07 09:00:52 +08:00
|
|
|
if s.Name != expected {
|
|
|
|
t.Fatalf("expected name %q but received %q", expected, s.Name)
|
2014-06-27 02:50:22 +08:00
|
|
|
}
|
|
|
|
}
|