p2p/sim: update socket pipe to use the default available buffer from OS

This commit is contained in:
Anton Evangelatov 2018-02-15 18:23:32 +01:00
parent da310f9ad1
commit 443ff80036
3 changed files with 25 additions and 32 deletions

View file

@ -34,11 +34,6 @@ import (
"github.com/ethereum/go-ethereum/rpc"
)
const (
socketReadBuffer = 5000 * 1024
socketWriteBuffer = 5000 * 1024
)
// SimAdapter is a NodeAdapter which creates in-memory simulation nodes and
// connects them using net.Pipe or OS socket connections
type SimAdapter struct {
@ -378,20 +373,10 @@ func socketPipe() (net.Conn, net.Conn, error) {
return nil, nil, err
}
err = setSocketBuffer(pipe1)
if err != nil {
return nil, nil, err
}
err = setSocketBuffer(pipe2)
if err != nil {
return nil, nil, err
}
return pipe1, pipe2, nil
}
func setSocketBuffer(conn net.Conn) error {
func setSocketBuffer(conn net.Conn, socketReadBuffer int, socketWriteBuffer int) error {
switch v := conn.(type) {
case *net.UnixConn:
err := v.SetReadBuffer(socketReadBuffer)

View file

@ -27,7 +27,7 @@ import (
func TestSocketPipe(t *testing.T) {
c1, c2, err := socketPipe()
if err != nil {
t.Skip(err)
t.Fatal(err)
}
done := make(chan struct{})
@ -35,15 +35,19 @@ func TestSocketPipe(t *testing.T) {
go func() {
msgs := 20
size := 8
for i := 0; i < msgs; i++ {
msg := make([]byte, size)
_ = binary.PutUvarint(msg, uint64(i))
_, err := c1.Write(msg)
if err != nil {
t.Fatal(err)
// OS socket pipe is blocking (depending on buffer size on OS), so writes are emitted asynchronously
go func() {
for i := 0; i < msgs; i++ {
msg := make([]byte, size)
_ = binary.PutUvarint(msg, uint64(i))
_, err := c1.Write(msg)
if err != nil {
t.Fatal(err)
}
}
}
}()
for i := 0; i < msgs; i++ {
msg := make([]byte, size)
@ -72,7 +76,7 @@ func TestSocketPipe(t *testing.T) {
func TestSocketPipeBidirections(t *testing.T) {
c1, c2, err := socketPipe()
if err != nil {
t.Skip(err)
t.Fatal(err)
}
done := make(chan struct{})
@ -80,14 +84,18 @@ func TestSocketPipeBidirections(t *testing.T) {
go func() {
msgs := 100
size := 4
for i := 0; i < msgs; i++ {
msg := []byte(`ping`)
_, err := c1.Write(msg)
if err != nil {
t.Fatal(err)
// OS socket pipe is blocking (depending on buffer size on OS), so writes are emitted asynchronously
go func() {
for i := 0; i < msgs; i++ {
msg := []byte(`ping`)
_, err := c1.Write(msg)
if err != nil {
t.Fatal(err)
}
}
}
}()
for i := 0; i < msgs; i++ {
out := make([]byte, size)

View file

@ -99,7 +99,7 @@ func testDiscoverySimulationExecAdapter(t *testing.T, nodes, conns int) {
testDiscoverySimulation(t, nodes, conns, adapters.NewExecAdapter(baseDir))
}
func XTestDiscoverySimulationSocketAdapter(t *testing.T) {
func TestDiscoverySimulationSocketAdapter(t *testing.T) {
testDiscoverySimulationSocketAdapter(t, *nodeCount, *initCount)
}