示例#1
0
func (c *channel) copySender(val libchan.Sender) (libchan.Sender, error) {
	recv, send, err := c.CreateNestedReceiver()
	if err != nil {
		return nil, err
	}
	go func() {
		libchan.Copy(val, recv)
		val.Close()
	}()
	return send, nil
}
示例#2
0
func (c *channel) copyReceiver(val libchan.Receiver) (libchan.Receiver, error) {
	send, recv, err := c.CreateNestedSender()
	if err != nil {
		return nil, err
	}
	go func() {
		libchan.Copy(send, val)
		send.Close()
	}()
	return recv, nil
}
示例#3
0
func SpawnProxyTest(t *testing.T, client PipeSenderRoutine, server PipeReceiverRoutine, proxyCount int) {
	endClient := make(chan bool)
	endServer := make(chan bool)
	endProxy := make(chan bool)

	receiver1, sender1, err := Pipe()
	receiver2, sender2, err := Pipe()

	if err != nil {
		t.Fatalf("Error creating pipe: %s", err)
	}

	go func() {
		defer close(endProxy)
		n, err := libchan.Copy(sender2, receiver1)
		if err != nil {
			t.Errorf("Error proxying: %s", err)
		}
		err = sender2.Close()
		if err != nil {
			t.Errorf("Error closing sender: %s", err)
		}
		if n != proxyCount {
			t.Errorf("Wrong proxy count\n\tExpected: %d\n\tActual: %d", proxyCount, n)
		}
	}()

	go func() {
		defer close(endClient)
		client(t, sender1)
		err := sender1.Close()
		if err != nil {
			t.Errorf("Error closing sender: %s", err)
		}
	}()

	go func() {
		defer close(endServer)
		server(t, receiver2)
	}()

	timeout := time.After(ClientServerTimeout)

	for endClient != nil || endServer != nil || endProxy != nil {
		select {
		case <-endProxy:
			if t.Failed() {
				t.Fatal("Proxy failed")
			}
			endProxy = nil
		case <-endClient:
			if t.Failed() {
				t.Fatal("Client failed")
			}
			endClient = nil
		case <-endServer:
			if t.Failed() {
				t.Fatal("Server failed")
			}
			endServer = nil
		case <-timeout:
			if DumpStackOnTimeout {
				pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
			}
			t.Fatal("Timeout")
		}
	}
}