本文整理汇总了Golang中io.Pipe函数的典型用法代码示例。如果您正苦于以下问题:Golang Pipe函数的具体用法?Golang Pipe怎么用?Golang Pipe使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Pipe函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Start
func (mc *ManagedCmd) Start(pipeOutput bool) (err error) {
if pipeOutput {
mc.Stdout_r, mc.Stdout = io.Pipe()
mc.Stderr_r, mc.Stderr = io.Pipe()
}
return mc.Cmd.Start()
}
示例2: TestIgnoreStreamErrors
func TestIgnoreStreamErrors(t *testing.T) {
pods, svc := testData()
r, w := io.Pipe()
go func() {
defer w.Close()
w.Write([]byte(`{}`))
w.Write([]byte(runtime.EncodeOrDie(latest.Codec, &pods.Items[0])))
}()
r2, w2 := io.Pipe()
go func() {
defer w2.Close()
w2.Write([]byte(`{}`))
w2.Write([]byte(runtime.EncodeOrDie(latest.Codec, &svc.Items[0])))
}()
b := NewBuilder(latest.RESTMapper, api.Scheme, fakeClient()).
ContinueOnError(). // TODO: order seems bad, but allows clients to determine what they want...
Stream(r, "1").Stream(r2, "2")
test := &testVisitor{}
singular := false
err := b.Do().IntoSingular(&singular).Visit(test.Handle)
if err != nil || singular || len(test.Infos) != 2 {
t.Fatalf("unexpected response: %v %f %#v", err, singular, test.Infos)
}
if !reflect.DeepEqual([]runtime.Object{&pods.Items[0], &svc.Items[0]}, test.Objects()) {
t.Errorf("unexpected visited objects: %#v", test.Objects())
}
}
示例3: TestPingErr
func TestPingErr(t *testing.T) {
e := errors.New("something broke")
for i := 0; i < 12; i++ {
for j := 0; j < 12; j++ {
m0 := newTestModel()
m1 := newTestModel()
ar, aw := io.Pipe()
br, bw := io.Pipe()
eaw := &ErrPipe{PipeWriter: *aw, max: i, err: e}
ebw := &ErrPipe{PipeWriter: *bw, max: j, err: e}
c0 := NewConnection("c0", ar, ebw, m0).(wireFormatConnection).next.(*rawConnection)
NewConnection("c1", br, eaw, m1)
res := c0.ping()
if (i < 4 || j < 4) && res {
t.Errorf("Unexpected ping success; i=%d, j=%d", i, j)
} else if (i >= 8 && j >= 8) && !res {
t.Errorf("Unexpected ping fail; i=%d, j=%d", i, j)
}
}
}
}
示例4: TestCustomTransport
func TestCustomTransport(t *testing.T) {
rc, ws := io.Pipe()
rs, wc := io.Pipe()
s := &Server{
Listener: newCustomListener("foobar", rs, ws),
Handler: func(clientAddr string, request interface{}) interface{} {
if clientAddr != "foobar" {
t.Fatalf("Unexpected client address: [%s]. Expected [foobar]", clientAddr)
}
return request
},
}
if err := s.Start(); err != nil {
t.Fatalf("Server.Start() failed: [%s]", err)
}
defer s.Stop()
c := &Client{
Conns: 1,
Dial: func(addr string) (conn io.ReadWriteCloser, err error) {
return &customConn{
r: rc,
w: wc,
}, nil
},
}
c.Start()
defer c.Stop()
testIntClient(t, c)
}
示例5: TestClose
func TestClose(t *testing.T) {
m0 := &TestModel{}
m1 := &TestModel{}
ar, aw := io.Pipe()
br, bw := io.Pipe()
c0 := NewConnection("c0", ar, bw, m0, nil)
NewConnection("c1", br, aw, m1, nil)
c0.close(nil)
ok := c0.isClosed()
if !ok {
t.Fatal("Connection should be closed")
}
// None of these should panic, some should return an error
ok = c0.ping()
if ok {
t.Error("Ping should not return true")
}
c0.Index("default", nil)
c0.Index("default", nil)
_, err := c0.Request("default", "foo", 0, 0)
if err == nil {
t.Error("Request should return an error")
}
}
示例6: file
func (p mockPlugin) file() fs.File {
incomingR, incomingW := io.Pipe()
outgoingR, outgoingW := io.Pipe()
// TODO: This is a terrible hack of a little http server. Really, we should
// implement some sort of fs.File -> net.Listener bridge and run an net/http
// server on that.
go func() {
for {
conn := httputil.NewServerConn(&ionet.Conn{R: incomingR, W: outgoingW}, nil)
req, err := conn.Read()
if err == io.EOF {
outgoingW.Close()
return
} else if err != nil {
p.t.Fatal(err)
}
resp := httptest.NewRecorder()
p.Handler.ServeHTTP(resp, req)
fmt.Fprintf(outgoingW, "HTTP/1.1 %d %s\nContent-Length: %d\n\n%s", resp.Code, http.StatusText(resp.Code), resp.Body.Len(), resp.Body.String())
}
}()
return fs.File{
FName: p.base(),
FWriter: incomingW,
FReader: outgoingR,
FStat: syscall.Stat_t{Mode: syscall.S_IFSOCK},
}
}
示例7: TestTypeErr
func TestTypeErr(t *testing.T) {
m0 := newTestModel()
m1 := newTestModel()
ar, aw := io.Pipe()
br, bw := io.Pipe()
c0 := NewConnection(c0ID, ar, bw, m0, "name", CompressAlways).(wireFormatConnection).Connection.(*rawConnection)
c0.Start()
c1 := NewConnection(c1ID, br, aw, m1, "name", CompressAlways)
c1.Start()
c0.ClusterConfig(ClusterConfigMessage{})
c1.ClusterConfig(ClusterConfigMessage{})
w := xdr.NewWriter(c0.cw)
timeoutWriteHeader(w, header{
version: 0,
msgID: 0,
msgType: 42, // unknown type
})
if err := m1.closedError(); err == nil || !strings.Contains(err.Error(), "unknown message type") {
t.Error("Connection should close due to unknown message type, not", err)
}
}
示例8: TestPingErr
func TestPingErr(t *testing.T) {
e := errors.New("something broke")
for i := 0; i < 32; i++ {
for j := 0; j < 32; j++ {
m0 := newTestModel()
m1 := newTestModel()
ar, aw := io.Pipe()
br, bw := io.Pipe()
eaw := &ErrPipe{PipeWriter: *aw, max: i, err: e}
ebw := &ErrPipe{PipeWriter: *bw, max: j, err: e}
c0 := NewConnection(c0ID, ar, ebw, m0, "name", CompressAlways).(wireFormatConnection).next.(*rawConnection)
c0.Start()
c1 := NewConnection(c1ID, br, eaw, m1, "name", CompressAlways)
c1.Start()
c0.ClusterConfig(ClusterConfigMessage{})
c1.ClusterConfig(ClusterConfigMessage{})
res := c0.ping()
if (i < 8 || j < 8) && res {
// This should have resulted in failure, as there is no way an empty ClusterConfig plus a Ping message fits in eight bytes.
t.Errorf("Unexpected ping success; i=%d, j=%d", i, j)
} else if (i >= 28 && j >= 28) && !res {
// This should have worked though, as 28 bytes is plenty for both.
t.Errorf("Unexpected ping fail; i=%d, j=%d", i, j)
}
}
}
}
示例9: TestBasicClientServer
func TestBasicClientServer(t *testing.T) {
toServer, fromClient := io.Pipe()
toClient, fromServer := io.Pipe()
s := rpc.NewServer()
var ts Test
s.Register(&ts)
go s.ServeCodec(NewServerCodec(duplex{toServer, fromServer}))
cl := NewClient(duplex{toClient, fromClient})
var tp TestParam
tp.Val1 = "Hello "
tp.Val2 = 10
err := cl.Call("Test.Foo", tp, &tp)
if err != nil {
t.Error(err)
return
}
if tp.Val1 != "Hello world!" {
t.Errorf("tp.Val2: expected %q, got %q", "Hello world!", tp.Val1)
}
if tp.Val2 != 15 {
t.Errorf("tp.Val2: expected 15, got %d", tp.Val2)
}
}
示例10: testConn
func testConn() (io.ReadWriteCloser, io.ReadWriteCloser) {
read1, write1 := io.Pipe()
read2, write2 := io.Pipe()
conn1 := &pipeConn{reader: read1, writer: write2}
conn2 := &pipeConn{reader: read2, writer: write1}
return conn1, conn2
}
示例11: TestTypeErr
func TestTypeErr(t *testing.T) {
m0 := newTestModel()
m1 := newTestModel()
ar, aw := io.Pipe()
br, bw := io.Pipe()
c0 := NewConnection(c0ID, ar, bw, m0, "name", CompressAlways).(wireFormatConnection).next.(*rawConnection)
c0.Start()
c1 := NewConnection(c1ID, br, aw, m1, "name", CompressAlways)
c1.Start()
c0.ClusterConfig(ClusterConfigMessage{})
c1.ClusterConfig(ClusterConfigMessage{})
w := xdr.NewWriter(c0.cw)
w.WriteUint32(encodeHeader(header{
version: 0,
msgID: 0,
msgType: 42,
}))
w.WriteUint32(0) // Avoids reader closing due to EOF
if !m1.isClosed() {
t.Error("Connection should close due to unknown message type")
}
}
示例12: TestRead
func (h *HashcheckSuiteSuite) TestRead(c *C) {
hash := fmt.Sprintf("%x", md5.Sum([]byte("foo")))
{
r, w := io.Pipe()
hcr := HashCheckingReader{r, md5.New(), hash}
go func() {
w.Write([]byte("foo"))
w.Close()
}()
p, err := ioutil.ReadAll(hcr)
c.Check(len(p), Equals, 3)
c.Check(err, Equals, nil)
}
{
r, w := io.Pipe()
hcr := HashCheckingReader{r, md5.New(), hash}
go func() {
w.Write([]byte("bar"))
w.Close()
}()
p, err := ioutil.ReadAll(hcr)
c.Check(len(p), Equals, 3)
c.Check(err, Equals, BadChecksum)
}
}
示例13: notmain
func notmain() {
client, err := dcli.NewClient("http://127.0.0.1:4243")
if err != nil {
panic(err)
}
outReader, outWriter := io.Pipe()
errReader, errWriter := io.Pipe()
runner := NewRunner(client, "ruby", "puts \"yo i'm rubby #{7*7}\"")
runner.OutStream = outWriter
runner.ErrStream = errWriter
go tailOutput("stdout", outReader)
go tailOutput("stderr", errReader)
log.Println("Running code...")
if _, err := runner.Run(10000); err != nil {
panic(err)
}
outReader.Close()
errReader.Close()
time.Sleep(1e9)
}
示例14: Initialize
//Initialize creates the process instance
//redirects stdout and stderr to internal pipes
//starts the process
func (cp *ChildProcess) Initialize(ps Job, numrestarts int) error {
wd, bname := getWD(ps.Path)
if err := os.Chdir(wd); err != nil {
return err
}
if len(ps.Args) < 1 {
cp.Proc = exec.Command(wd + bname)
} else {
log.Println(ps.Args)
cp.Proc = exec.Command(wd+bname, ps.Args)
}
cp.StdOutR, cp.StdOutWr = io.Pipe()
cp.StdErrR, cp.StdErrWr = io.Pipe()
cp.Proc.Stdout = cp.StdOutWr
cp.Proc.Stderr = cp.StdErrWr
cp.RestartCount += numrestarts
if ps.Workingdir != "" {
cp.Proc.Dir = ps.Workingdir
} else {
cp.Proc.Dir = wd
}
if err := cp.Proc.Start(); err != nil {
return err
}
cp.PID = cp.Proc.Process.Pid
cp.Timestamp = time.Now()
cp.IsAlive = true
return nil
}
示例15: MakeRoombaSim
func MakeRoombaSim() (*RoombaSimulator, *readWriter) {
// Input: driver writes, simulator reads.
inp_r, inp_w := io.Pipe()
// Ouput: simulator writes, driver reads.
out_r, out_w := io.Pipe()
readBytes := &bytes.Buffer{}
writtenBytes := &bytes.Buffer{}
sim := &RoombaSimulator{
rw: &readWriter{
// Log all read bytes to ReadBytes.
io.TeeReader(inp_r, readBytes),
// Log all written bytes to writtenBytes.
io.MultiWriter(out_w, writtenBytes),
},
writeQ: make(chan []byte, 15),
ReadBytes: *readBytes,
RequestedRadius: []byte{0, 0},
RequestedVelocity: []byte{0, 0},
}
go sim.serve()
rw := &readWriter{out_r, inp_w}
return sim, rw
}