当前位置: 首页>>代码示例>>Golang>>正文


Golang io.Pipe函数代码示例

本文整理汇总了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()
}
开发者ID:Nitro,项目名称:heka,代码行数:7,代码来源:process_chain.go

示例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())
	}
}
开发者ID:hortonworks,项目名称:kubernetes-yarn,代码行数:33,代码来源:builder_test.go

示例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)
			}
		}
	}
}
开发者ID:Zypan,项目名称:syncthing,代码行数:25,代码来源:protocol_test.go

示例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)
}
开发者ID:BobbWu,项目名称:gorpc,代码行数:32,代码来源:rpc_test.go

示例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")
	}
}
开发者ID:ngpestelos,项目名称:syncthing,代码行数:32,代码来源:protocol_test.go

示例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},
	}
}
开发者ID:dilgerma,项目名称:scope,代码行数:28,代码来源:registry_internal_test.go

示例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)
	}
}
开发者ID:escribano,项目名称:syncthing,代码行数:25,代码来源:protocol_test.go

示例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)
			}
		}
	}
}
开发者ID:hexuallyactive,项目名称:syncthing,代码行数:31,代码来源:protocol_test.go

示例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)
	}
}
开发者ID:bubble66,项目名称:skynet,代码行数:27,代码来源:rpc_test.go

示例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
}
开发者ID:sclevine,项目名称:packer-bosh,代码行数:7,代码来源:session_test.go

示例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")
	}
}
开发者ID:hexuallyactive,项目名称:syncthing,代码行数:26,代码来源:protocol_test.go

示例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)
	}
}
开发者ID:ntijanic,项目名称:arvados,代码行数:27,代码来源:hashcheck_test.go

示例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)
}
开发者ID:Empia,项目名称:block,代码行数:25,代码来源:container.go

示例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
}
开发者ID:ziscky,项目名称:zist,代码行数:32,代码来源:childprocess.go

示例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
}
开发者ID:xa4a,项目名称:go-roomba,代码行数:29,代码来源:roombasim.go


注:本文中的io.Pipe函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。