2013-12-08 21:20:27 -05:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func readStream(t *testing.T, s io.Reader) string {
|
|
|
|
var data [1024]byte
|
|
|
|
n, err := s.Read(data[:])
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(data[0:n])
|
|
|
|
}
|
|
|
|
|
2013-12-08 21:30:29 -05:00
|
|
|
func testMux(t *testing.T) (client *MuxConn, server *MuxConn) {
|
2013-12-08 21:20:27 -05:00
|
|
|
l, err := net.Listen("tcp", ":0")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2013-12-08 21:30:29 -05:00
|
|
|
// Server side
|
2013-12-08 21:20:27 -05:00
|
|
|
doneCh := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
defer close(doneCh)
|
|
|
|
conn, err := l.Accept()
|
|
|
|
l.Close()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2013-12-08 21:30:29 -05:00
|
|
|
server = NewMuxConn(conn)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Client side
|
|
|
|
conn, err := net.Dial("tcp", l.Addr().String())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
client = NewMuxConn(conn)
|
|
|
|
|
|
|
|
// Wait for the server
|
|
|
|
<-doneCh
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMuxConn(t *testing.T) {
|
|
|
|
client, server := testMux(t)
|
|
|
|
defer client.Close()
|
|
|
|
defer server.Close()
|
|
|
|
|
|
|
|
// When the server is done
|
|
|
|
doneCh := make(chan struct{})
|
|
|
|
|
|
|
|
// The server side
|
|
|
|
go func() {
|
|
|
|
defer close(doneCh)
|
|
|
|
|
2013-12-09 17:24:55 -05:00
|
|
|
s0, err := server.Accept(0)
|
2013-12-08 21:20:27 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2013-12-09 17:24:55 -05:00
|
|
|
s1, err := server.Dial(1)
|
2013-12-08 21:20:27 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(2)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
data := readStream(t, s1)
|
|
|
|
if data != "another" {
|
|
|
|
t.Fatalf("bad: %#v", data)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
data := readStream(t, s0)
|
|
|
|
if data != "hello" {
|
|
|
|
t.Fatalf("bad: %#v", data)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}()
|
|
|
|
|
2013-12-09 17:24:55 -05:00
|
|
|
s0, err := client.Dial(0)
|
2013-12-08 21:20:27 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2013-12-09 17:24:55 -05:00
|
|
|
s1, err := client.Accept(1)
|
2013-12-08 21:20:27 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := s0.Write([]byte("hello")); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if _, err := s1.Write([]byte("another")); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the server to be done
|
|
|
|
<-doneCh
|
|
|
|
}
|
2013-12-08 21:30:29 -05:00
|
|
|
|
2013-12-10 19:49:14 -05:00
|
|
|
func TestMuxConn_socketClose(t *testing.T) {
|
|
|
|
client, server := testMux(t)
|
|
|
|
defer client.Close()
|
|
|
|
defer server.Close()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
_, err := server.Accept(0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
server.rwc.Close()
|
|
|
|
}()
|
|
|
|
|
|
|
|
s0, err := client.Dial(0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var data [1024]byte
|
|
|
|
_, err = s0.Read(data[:])
|
|
|
|
if err != io.EOF {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-08 21:30:29 -05:00
|
|
|
func TestMuxConn_clientClosesStreams(t *testing.T) {
|
|
|
|
client, server := testMux(t)
|
|
|
|
defer client.Close()
|
|
|
|
defer server.Close()
|
2013-12-10 18:51:22 -05:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
conn, err := server.Accept(0)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
conn.Close()
|
|
|
|
}()
|
2013-12-08 21:30:29 -05:00
|
|
|
|
2013-12-09 17:24:55 -05:00
|
|
|
s0, err := client.Dial(0)
|
2013-12-08 21:30:29 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var data [1024]byte
|
|
|
|
_, err = s0.Read(data[:])
|
|
|
|
if err != io.EOF {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMuxConn_serverClosesStreams(t *testing.T) {
|
|
|
|
client, server := testMux(t)
|
|
|
|
defer client.Close()
|
|
|
|
defer server.Close()
|
2013-12-09 17:24:55 -05:00
|
|
|
go server.Accept(0)
|
2013-12-08 21:30:29 -05:00
|
|
|
|
2013-12-09 17:24:55 -05:00
|
|
|
s0, err := client.Dial(0)
|
2013-12-08 21:30:29 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := server.Close(); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// This should block forever since we never write onto this stream.
|
|
|
|
var data [1024]byte
|
|
|
|
_, err = s0.Read(data[:])
|
|
|
|
if err != io.EOF {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
2013-12-10 13:34:35 -05:00
|
|
|
|
|
|
|
func TestMuxConnNextId(t *testing.T) {
|
|
|
|
client, server := testMux(t)
|
|
|
|
defer client.Close()
|
|
|
|
defer server.Close()
|
|
|
|
|
|
|
|
a := client.NextId()
|
|
|
|
b := client.NextId()
|
|
|
|
|
|
|
|
if a != 0 || b != 1 {
|
|
|
|
t.Fatalf("IDs should increment")
|
|
|
|
}
|
|
|
|
}
|