Golang Pipe示例

说明

golang pipe示例是从最受好评的开源项目中提取的实现代码,你可以参考下面示例的使用方式。

编程语言: Golang

命名空间/包名称: io

示例#1
文件: process_chain.go 项目: Nitro/heka

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
文件: builder_test.go 项目: hortonworks/kubernetes-yarn

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
文件: protocol_test.go 项目: Zypan/syncthing

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
文件: rpc_test.go 项目: BobbWu/gorpc

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
文件: protocol_test.go 项目: ngpestelos/syncthing

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
文件: registry_internal_test.go 项目: dilgerma/scope

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
文件: protocol_test.go 项目: escribano/syncthing

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
文件: protocol_test.go 项目: hexuallyactive/syncthing

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
文件: rpc_test.go 项目: bubble66/skynet

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
文件: session_test.go 项目: sclevine/packer-bosh

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
文件: protocol_test.go 项目: hexuallyactive/syncthing

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
文件: hashcheck_test.go 项目: ntijanic/arvados

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
文件: container.go 项目: Empia/block

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
文件: childprocess.go 项目: ziscky/zist

//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
文件: roombasim.go 项目: xa4a/go-roomba

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
}

示例#16
文件: xmpp.go 项目: rdlaitila/xmppctl

func (cl *Client) startTransport() (io.Reader, io.WriteCloser) {
	inr, inw := io.Pipe()
	outr, outw := io.Pipe()
	go cl.readTransport(inw)
	go cl.writeTransport(outr)
	return inr, outw
}

示例#17
文件: multi_test.go 项目: vmware/vic

func TestMultiWrite(t *testing.T) {
	pipeAR, pipeAW := io.Pipe()
	pipeBR, pipeBW := io.Pipe()

	mwriter := MultiWriter(pipeAW, pipeBW)

	var bufA bytes.Buffer
	var bufB bytes.Buffer

	// set up a copy so we don't block writes
	go io.Copy(&bufA, pipeAR)
	go io.Copy(&bufB, pipeBR)

	// send the test string
	data := "verify base multiwriter function"
	_, err := mwriter.Write([]byte(data))
	if err != nil {
		t.Error(err)
		return
	}

	// compare the data
	if bufA.String() != data {
		t.Errorf("A: expected: %s, actual: %s", data, bufA.String())
		return
	}

	if bufB.String() != data {
		t.Errorf("B: expected: %s, actual: %s", data, bufB.String())
		return
	}
}

示例#18
文件: testtimercpu-tasker.go 项目: roo7break/cracklord

func newTestTimerTask(j common.Job) (common.Tasker, error) {
	log.Debug("Starting up a new example task plugin.")
	t := testTimerCPUTasker{}

	t.job = j
	t.job.CrackedHashes = 0
	t.job.PerformanceTitle = "Time data"

	var err error
	t.job.TotalHashes, err = strconv.ParseInt(j.Parameters["seconds"], 10, 0)
	if err != nil {
		return &t, errors.New("Unable to parse seconds.")
	}

	if j.Parameters["result"] == "Success" {
		t.success = true
	} else {
		t.success = false
	}

	t.stderr.R, t.stderr.W = io.Pipe()
	t.stdout.R, t.stdout.W = io.Pipe()
	t.stdin.R, t.stdin.W = io.Pipe()

	return &t, nil
}

示例#19
文件: rpc_client_test.go 项目: ZhiqinYang/go-plugin

func TestClient_syncStreams(t *testing.T) {
	client, server := TestPluginRPCConn(t, map[string]Plugin{})

	// Create streams for the server that we can talk to
	stdout_r, stdout_w := io.Pipe()
	stderr_r, stderr_w := io.Pipe()
	server.Stdout = stdout_r
	server.Stderr = stderr_r

	// Start the data copying
	var stdout_out, stderr_out bytes.Buffer
	stdout := bytes.NewBufferString("stdouttest")
	stderr := bytes.NewBufferString("stderrtest")
	go client.SyncStreams(&stdout_out, &stderr_out)
	go io.Copy(stdout_w, stdout)
	go io.Copy(stderr_w, stderr)

	// Unfortunately I can't think of a better way to make sure all the
	// copies above go through so let's just exit.
	time.Sleep(100 * time.Millisecond)

	// Close everything, and lets test the result
	client.Close()
	stdout_w.Close()
	stderr_w.Close()

	if v := stdout_out.String(); v != "stdouttest" {
		t.Fatalf("bad: %s", v)
	}
	if v := stderr_out.String(); v != "stderrtest" {
		t.Fatalf("bad: %s", v)
	}
}

示例#20
文件: protocol_test.go 项目: tomschlenkhoff/syncthing

func TestPingErr(t *testing.T) {
	e := errors.New("something broke")

	for i := 0; i < 16; i++ {
		for j := 0; j < 16; 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)
			NewConnection(c1ID, br, eaw, m1, "name", CompressAlways)

			res := c0.ping()
			if (i < 8 || j < 8) && res {
				t.Errorf("Unexpected ping success; i=%d, j=%d", i, j)
			} else if (i >= 12 && j >= 12) && !res {
				t.Errorf("Unexpected ping fail; i=%d, j=%d", i, j)
			}
		}
	}
}

示例#21
文件: protocol_test.go 项目: escribano/syncthing

func TestClose(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{})

	c0.close(errors.New("manual close"))

	<-c0.closed
	if err := m0.closedError(); err == nil || !strings.Contains(err.Error(), "manual close") {
		t.Fatal("Connection should be closed")
	}

	// None of these should panic, some should return an error

	if c0.ping() {
		t.Error("Ping should not return true")
	}

	c0.Index("default", nil, 0, nil)
	c0.Index("default", nil, 0, nil)

	if _, err := c0.Request("default", "foo", 0, 0, nil, 0, nil); err == nil {
		t.Error("Request should return an error")
	}
}

示例#22
文件: universal_collector.go 项目: stigkj/nomad

// LaunchCollector launches a new syslog server and starts writing log lines to
// files and rotates them
func (s *SyslogCollector) LaunchCollector(ctx *LogCollectorContext) (*SyslogCollectorState, error) {
	addr, err := s.getFreePort(ctx.PortLowerBound, ctx.PortUpperBound)
	if err != nil {
		return nil, err
	}
	s.logger.Printf("[DEBUG] sylog-server: launching syslog server on addr: %v", addr)
	s.ctx = ctx
	// configuring the task dir
	if err := s.configureTaskDir(); err != nil {
		return nil, err
	}

	channel := make(syslog.LogPartsChannel)
	handler := syslog.NewChannelHandler(channel)

	s.server = syslog.NewServer()
	s.server.SetFormat(&CustomParser{logger: s.logger})
	s.server.SetHandler(handler)
	s.server.ListenTCP(addr.String())
	if err := s.server.Boot(); err != nil {
		return nil, err
	}
	logFileSize := int64(ctx.LogConfig.MaxFileSizeMB * 1024 * 1024)

	ro, wo := io.Pipe()
	lro, err := logrotator.NewLogRotator(filepath.Join(s.taskDir, allocdir.TaskLocal),
		fmt.Sprintf("%v.stdout", ctx.TaskName), ctx.LogConfig.MaxFiles,
		logFileSize, s.logger)
	if err != nil {
		return nil, err
	}
	s.lro = lro
	go lro.Start(ro)

	re, we := io.Pipe()
	lre, err := logrotator.NewLogRotator(filepath.Join(s.taskDir, allocdir.TaskLocal),
		fmt.Sprintf("%v.stderr", ctx.TaskName), ctx.LogConfig.MaxFiles,
		logFileSize, s.logger)
	if err != nil {
		return nil, err
	}
	s.lre = lre
	go lre.Start(re)

	go func(channel syslog.LogPartsChannel) {
		for logParts := range channel {
			// If the severity of the log line is err then we write to stderr
			// otherwise all messages go to stdout
			s := logParts["severity"].(Priority)
			if s.Severity == s1.LOG_ERR {
				we.Write(logParts["content"].([]byte))
			} else {
				wo.Write(logParts["content"].([]byte))
			}
			wo.Write([]byte("\n"))
		}
	}(channel)
	go s.server.Wait()
	return &SyslogCollectorState{Addr: addr.String()}, nil
}

示例#23
文件: demultiplex.go 项目: ericcapricorn/flynn

func Streams(r io.Reader) (stdout, stderr io.Reader) {
	outr, outw := io.Pipe()
	errr, errw := io.Pipe()
	go func() {
		read := frameReader(r)
		for {
			typ, data, err := read()
			if typ == frameTypeStderr {
				if _, err := errw.Write(data); err != nil {
					outw.Close()
					return
				}
			} else {
				if _, err := outw.Write(data); err != nil {
					errw.Close()
					return
				}
			}
			if err != nil {
				outw.CloseWithError(err)
				errw.CloseWithError(err)
				return
			}
		}
	}()
	return outr, errr
}

示例#24
文件: protocol_test.go 项目: weaveworks/mesh

func connPair() (protocolIntroConn, protocolIntroConn) {
	a := testConn{}
	b := testConn{}
	a.Reader, b.Writer = io.Pipe()
	b.Reader, a.Writer = io.Pipe()
	return &a, &b
}

示例#25
文件: commands_test.go 项目: huangjun-pg/docker

// TestAttachDetachTruncatedID checks that attach in tty mode can be detached
func TestAttachDetachTruncatedID(t *testing.T) {
	stdin, stdinPipe := io.Pipe()
	stdout, stdoutPipe := io.Pipe()

	cli := NewDockerCli(stdin, stdoutPipe, ioutil.Discard, testDaemonProto, testDaemonAddr)
	defer cleanup(globalRuntime)

	go stdout.Read(make([]byte, 1024))
	setTimeout(t, "Starting container timed out", 2*time.Second, func() {
		if err := cli.CmdRun("-i", "-t", "-d", unitTestImageID, "cat"); err != nil {
			t.Fatal(err)
		}
	})

	container := globalRuntime.List()[0]

	stdin, stdinPipe = io.Pipe()
	stdout, stdoutPipe = io.Pipe()
	cli = NewDockerCli(stdin, stdoutPipe, ioutil.Discard, testDaemonProto, testDaemonAddr)

	ch := make(chan struct{})
	go func() {
		defer close(ch)
		if err := cli.CmdAttach(utils.TruncateID(container.ID)); err != nil {
			if err != io.ErrClosedPipe {
				t.Fatal(err)
			}
		}
	}()

	setTimeout(t, "First read/write assertion timed out", 2*time.Second, func() {
		if err := assertPipe("hello\n", "hello", stdout, stdinPipe, 15); err != nil {
			if err != io.ErrClosedPipe {
				t.Fatal(err)
			}
		}
	})

	setTimeout(t, "Escape sequence timeout", 5*time.Second, func() {
		stdinPipe.Write([]byte{16, 17})
		if err := stdinPipe.Close(); err != nil {
			t.Fatal(err)
		}
	})
	closeWrap(stdin, stdinPipe, stdout, stdoutPipe)

	// wait for CmdRun to return
	setTimeout(t, "Waiting for CmdAttach timed out", 15*time.Second, func() {
		<-ch
	})

	time.Sleep(500 * time.Millisecond)
	if !container.State.Running {
		t.Fatal("The detached container should be still running")
	}

	setTimeout(t, "Waiting for container to die timedout", 5*time.Second, func() {
		container.Kill()
	})
}

示例#26
文件: attacher.go 项目: kevinsu1989/eru-agent

func (m *AttachManager) Attach(app *defines.Meta) {
	// Not Thread Safe
	if m.Attached(app.ID) {
		return
	}
	outrd, outwr := io.Pipe()
	errrd, errwr := io.Pipe()
	go func() {
		err := g.Docker.AttachToContainer(docker.AttachToContainerOptions{
			Container:    app.ID,
			OutputStream: outwr,
			ErrorStream:  errwr,
			Stdin:        false,
			Stdout:       true,
			Stderr:       true,
			Stream:       true,
		})
		outwr.Close()
		errwr.Close()
		logs.Debug("Lenz Attach", app.ID[:12], "finished")
		if err != nil {
			logs.Debug("Lenz Attach", app.ID, "failure:", err)
		}
		m.send(&defines.AttachEvent{Type: "detach", App: app})
		m.Lock()
		defer m.Unlock()
		delete(m.attached, app.ID)
	}()
	m.Lock()
	m.attached[app.ID] = NewLogPump(outrd, errrd, app)
	m.Unlock()
	m.send(&defines.AttachEvent{Type: "attach", App: app})
	logs.Debug("Lenz Attach", app.ID[:12], "success")
}

示例#27
文件: protocol_test.go 项目: Zypan/syncthing

func TestClose(t *testing.T) {
	m0 := newTestModel()
	m1 := newTestModel()

	ar, aw := io.Pipe()
	br, bw := io.Pipe()

	c0 := NewConnection("c0", ar, bw, m0).(wireFormatConnection).next.(*rawConnection)
	NewConnection("c1", br, aw, m1)

	c0.close(nil)

	<-c0.closed
	if !m0.isClosed() {
		t.Fatal("Connection should be closed")
	}

	// None of these should panic, some should return an error

	if c0.ping() {
		t.Error("Ping should not return true")
	}

	c0.Index("default", nil)
	c0.Index("default", nil)

	if _, err := c0.Request("default", "foo", 0, 0); err == nil {
		t.Error("Request should return an error")
	}
}

示例#28
文件: commands_test.go 项目: strawlab/docker

// TestRunHostname checks that 'docker run -h' correctly sets a custom hostname
func TestRunHostname(t *testing.T) {
	runtime, err := newTestRuntime()
	if err != nil {
		t.Fatal(err)
	}
	defer nuke(runtime)

	srv := &Server{runtime: runtime}

	stdin, _ := io.Pipe()
	stdout, stdoutPipe := io.Pipe()

	c := make(chan struct{})
	go func() {
		if err := srv.CmdRun(stdin, rcli.NewDockerLocalConn(stdoutPipe), "-h", "foobar", GetTestImage(runtime).Id, "hostname"); err != nil {
			t.Fatal(err)
		}
		close(c)
	}()
	cmdOutput, err := bufio.NewReader(stdout).ReadString('\n')
	if err != nil {
		t.Fatal(err)
	}
	if cmdOutput != "foobar\n" {
		t.Fatalf("'hostname' should display '%s', not '%s'", "foobar\n", cmdOutput)
	}

	setTimeout(t, "CmdRun timed out", 2*time.Second, func() {
		<-c
		cmdWait(srv, srv.runtime.List()[0])
	})

}

示例#29
文件: sequence.go 项目: 4shome/go.jiri

func (s *Sequence) initAndDefer() func() {
	if s.stdout == nil && s.stderr == nil {
		return func() {}
	}
	opts := s.GetOpts()
	rStdin, wStdin := io.Pipe()
	rStderr, wStderr := io.Pipe()
	opts.Stdout = wStdin
	opts.Stderr = wStderr
	s.setOpts(opts)
	var stdinCh, stderrCh chan error
	if s.stdout != nil {
		stdinCh = make(chan error)
		go copy(s.stdout, rStdin, stdinCh)
	}
	if s.stderr != nil {
		stderrCh = make(chan error)
		go copy(s.stderr, rStderr, stderrCh)
	}
	return func() {
		if err := s.done(wStdin, wStderr, stdinCh, stderrCh); err != nil && s.err == nil {
			s.err = err
		}
	}
}

示例#30
文件: client_server_test.go 项目: thriftrw/thriftrw-go

func TestClientServerHandleError(t *testing.T) {
	serverReader, clientWriter := io.Pipe()
	clientReader, serverWriter := io.Pipe()

	defer func() {
		assert.NoError(t, serverWriter.Close())
		assert.NoError(t, clientWriter.Close())
		assert.NoError(t, clientReader.Close())
		assert.NoError(t, serverReader.Close())
	}()

	server := NewServer(serverReader, serverWriter)
	client := NewClient(clientWriter, clientReader)

	go func() {
		err := server.Serve(handlerFunc(
			func([]byte) ([]byte, error) {
				return nil, errors.New("great sadness")
			},
		))
		assert.Error(t, err)
	}()

	_, err := client.Send([]byte("hello"))
	assert.Equal(t, io.EOF, err)
}

展开阅读全文