|
|
@ -20,8 +20,8 @@ import ( |
|
|
|
"bytes" |
|
|
|
"bytes" |
|
|
|
"encoding/binary" |
|
|
|
"encoding/binary" |
|
|
|
"fmt" |
|
|
|
"fmt" |
|
|
|
|
|
|
|
"sync" |
|
|
|
"testing" |
|
|
|
"testing" |
|
|
|
"time" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/simulations/pipes" |
|
|
|
"github.com/ethereum/go-ethereum/p2p/simulations/pipes" |
|
|
|
) |
|
|
|
) |
|
|
@ -32,42 +32,26 @@ func TestTCPPipe(t *testing.T) { |
|
|
|
t.Fatal(err) |
|
|
|
t.Fatal(err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
done := make(chan struct{}) |
|
|
|
msgs := 50 |
|
|
|
|
|
|
|
size := 1024 |
|
|
|
go func() { |
|
|
|
for i := 0; i < msgs; i++ { |
|
|
|
msgs := 50 |
|
|
|
msg := make([]byte, size) |
|
|
|
size := 1024 |
|
|
|
binary.PutUvarint(msg, uint64(i)) |
|
|
|
for i := 0; i < msgs; i++ { |
|
|
|
if _, err := c1.Write(msg); err != nil { |
|
|
|
msg := make([]byte, size) |
|
|
|
t.Fatal(err) |
|
|
|
_ = binary.PutUvarint(msg, uint64(i)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_, err := c1.Write(msg) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
t.Fatal(err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
for i := 0; i < msgs; i++ { |
|
|
|
for i := 0; i < msgs; i++ { |
|
|
|
msg := make([]byte, size) |
|
|
|
msg := make([]byte, size) |
|
|
|
_ = binary.PutUvarint(msg, uint64(i)) |
|
|
|
binary.PutUvarint(msg, uint64(i)) |
|
|
|
|
|
|
|
out := make([]byte, size) |
|
|
|
out := make([]byte, size) |
|
|
|
if _, err := c2.Read(out); err != nil { |
|
|
|
_, err := c2.Read(out) |
|
|
|
t.Fatal(err) |
|
|
|
if err != nil { |
|
|
|
} |
|
|
|
t.Fatal(err) |
|
|
|
if !bytes.Equal(msg, out) { |
|
|
|
} |
|
|
|
t.Fatalf("expected %#v, got %#v", msg, out) |
|
|
|
|
|
|
|
|
|
|
|
if !bytes.Equal(msg, out) { |
|
|
|
|
|
|
|
t.Fatalf("expected %#v, got %#v", msg, out) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
done <- struct{}{} |
|
|
|
|
|
|
|
}() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
select { |
|
|
|
|
|
|
|
case <-done: |
|
|
|
|
|
|
|
case <-time.After(5 * time.Second): |
|
|
|
|
|
|
|
t.Fatal("test timeout") |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -77,60 +61,41 @@ func TestTCPPipeBidirections(t *testing.T) { |
|
|
|
t.Fatal(err) |
|
|
|
t.Fatal(err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
done := make(chan struct{}) |
|
|
|
msgs := 50 |
|
|
|
|
|
|
|
size := 7 |
|
|
|
go func() { |
|
|
|
for i := 0; i < msgs; i++ { |
|
|
|
msgs := 50 |
|
|
|
msg := []byte(fmt.Sprintf("ping %02d", i)) |
|
|
|
size := 7 |
|
|
|
if _, err := c1.Write(msg); err != nil { |
|
|
|
for i := 0; i < msgs; i++ { |
|
|
|
t.Fatal(err) |
|
|
|
msg := []byte(fmt.Sprintf("ping %02d", i)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_, err := c1.Write(msg) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
t.Fatal(err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
for i := 0; i < msgs; i++ { |
|
|
|
for i := 0; i < msgs; i++ { |
|
|
|
expected := []byte(fmt.Sprintf("ping %02d", i)) |
|
|
|
expected := []byte(fmt.Sprintf("ping %02d", i)) |
|
|
|
|
|
|
|
out := make([]byte, size) |
|
|
|
out := make([]byte, size) |
|
|
|
if _, err := c2.Read(out); err != nil { |
|
|
|
_, err := c2.Read(out) |
|
|
|
t.Fatal(err) |
|
|
|
if err != nil { |
|
|
|
|
|
|
|
t.Fatal(err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if !bytes.Equal(expected, out) { |
|
|
|
|
|
|
|
t.Fatalf("expected %#v, got %#v", out, expected) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
msg := []byte(fmt.Sprintf("pong %02d", i)) |
|
|
|
|
|
|
|
_, err := c2.Write(msg) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
t.Fatal(err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
for i := 0; i < msgs; i++ { |
|
|
|
if !bytes.Equal(expected, out) { |
|
|
|
expected := []byte(fmt.Sprintf("pong %02d", i)) |
|
|
|
t.Fatalf("expected %#v, got %#v", out, expected) |
|
|
|
|
|
|
|
} else { |
|
|
|
out := make([]byte, size) |
|
|
|
msg := []byte(fmt.Sprintf("pong %02d", i)) |
|
|
|
_, err := c1.Read(out) |
|
|
|
if _, err := c2.Write(msg); err != nil { |
|
|
|
if err != nil { |
|
|
|
|
|
|
|
t.Fatal(err) |
|
|
|
t.Fatal(err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if !bytes.Equal(expected, out) { |
|
|
|
|
|
|
|
t.Fatalf("expected %#v, got %#v", out, expected) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
done <- struct{}{} |
|
|
|
} |
|
|
|
}() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
select { |
|
|
|
for i := 0; i < msgs; i++ { |
|
|
|
case <-done: |
|
|
|
expected := []byte(fmt.Sprintf("pong %02d", i)) |
|
|
|
case <-time.After(5 * time.Second): |
|
|
|
out := make([]byte, size) |
|
|
|
t.Fatal("test timeout") |
|
|
|
if _, err := c1.Read(out); err != nil { |
|
|
|
|
|
|
|
t.Fatal(err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if !bytes.Equal(expected, out) { |
|
|
|
|
|
|
|
t.Fatalf("expected %#v, got %#v", out, expected) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -140,46 +105,35 @@ func TestNetPipe(t *testing.T) { |
|
|
|
t.Fatal(err) |
|
|
|
t.Fatal(err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
done := make(chan struct{}) |
|
|
|
msgs := 50 |
|
|
|
|
|
|
|
size := 1024 |
|
|
|
|
|
|
|
var wg sync.WaitGroup |
|
|
|
|
|
|
|
defer wg.Wait() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// netPipe is blocking, so writes are emitted asynchronously
|
|
|
|
|
|
|
|
wg.Add(1) |
|
|
|
go func() { |
|
|
|
go func() { |
|
|
|
msgs := 50 |
|
|
|
defer wg.Done() |
|
|
|
size := 1024 |
|
|
|
|
|
|
|
// netPipe is blocking, 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++ { |
|
|
|
for i := 0; i < msgs; i++ { |
|
|
|
msg := make([]byte, size) |
|
|
|
msg := make([]byte, size) |
|
|
|
_ = binary.PutUvarint(msg, uint64(i)) |
|
|
|
binary.PutUvarint(msg, uint64(i)) |
|
|
|
|
|
|
|
if _, err := c1.Write(msg); err != nil { |
|
|
|
out := make([]byte, size) |
|
|
|
t.Error(err) |
|
|
|
_, err := c2.Read(out) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
t.Fatal(err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if !bytes.Equal(msg, out) { |
|
|
|
|
|
|
|
t.Fatalf("expected %#v, got %#v", msg, out) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
done <- struct{}{} |
|
|
|
|
|
|
|
}() |
|
|
|
}() |
|
|
|
|
|
|
|
|
|
|
|
select { |
|
|
|
for i := 0; i < msgs; i++ { |
|
|
|
case <-done: |
|
|
|
msg := make([]byte, size) |
|
|
|
case <-time.After(5 * time.Second): |
|
|
|
binary.PutUvarint(msg, uint64(i)) |
|
|
|
t.Fatal("test timeout") |
|
|
|
out := make([]byte, size) |
|
|
|
|
|
|
|
if _, err := c2.Read(out); err != nil { |
|
|
|
|
|
|
|
t.Error(err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if !bytes.Equal(msg, out) { |
|
|
|
|
|
|
|
t.Errorf("expected %#v, got %#v", msg, out) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -189,71 +143,60 @@ func TestNetPipeBidirections(t *testing.T) { |
|
|
|
t.Fatal(err) |
|
|
|
t.Fatal(err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
done := make(chan struct{}) |
|
|
|
msgs := 1000 |
|
|
|
|
|
|
|
size := 8 |
|
|
|
|
|
|
|
pingTemplate := "ping %03d" |
|
|
|
|
|
|
|
pongTemplate := "pong %03d" |
|
|
|
|
|
|
|
var wg sync.WaitGroup |
|
|
|
|
|
|
|
defer wg.Wait() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// netPipe is blocking, so writes are emitted asynchronously
|
|
|
|
|
|
|
|
wg.Add(1) |
|
|
|
go func() { |
|
|
|
go func() { |
|
|
|
msgs := 1000 |
|
|
|
defer wg.Done() |
|
|
|
size := 8 |
|
|
|
|
|
|
|
pingTemplate := "ping %03d" |
|
|
|
|
|
|
|
pongTemplate := "pong %03d" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// netPipe is blocking, so writes are emitted asynchronously
|
|
|
|
|
|
|
|
go func() { |
|
|
|
|
|
|
|
for i := 0; i < msgs; i++ { |
|
|
|
|
|
|
|
msg := []byte(fmt.Sprintf(pingTemplate, i)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_, err := c1.Write(msg) |
|
|
|
for i := 0; i < msgs; i++ { |
|
|
|
if err != nil { |
|
|
|
msg := []byte(fmt.Sprintf(pingTemplate, i)) |
|
|
|
t.Fatal(err) |
|
|
|
if _, err := c1.Write(msg); err != nil { |
|
|
|
} |
|
|
|
t.Error(err) |
|
|
|
} |
|
|
|
|
|
|
|
}() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// netPipe is blocking, so reads for pong are emitted asynchronously
|
|
|
|
|
|
|
|
go func() { |
|
|
|
|
|
|
|
for i := 0; i < msgs; i++ { |
|
|
|
|
|
|
|
expected := []byte(fmt.Sprintf(pongTemplate, i)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
out := make([]byte, size) |
|
|
|
|
|
|
|
_, err := c1.Read(out) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
t.Fatal(err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if !bytes.Equal(expected, out) { |
|
|
|
|
|
|
|
t.Fatalf("expected %#v, got %#v", expected, out) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}() |
|
|
|
|
|
|
|
|
|
|
|
done <- struct{}{} |
|
|
|
// netPipe is blocking, so reads for pong are emitted asynchronously
|
|
|
|
}() |
|
|
|
wg.Add(1) |
|
|
|
|
|
|
|
go func() { |
|
|
|
|
|
|
|
defer wg.Done() |
|
|
|
|
|
|
|
|
|
|
|
// expect to read pings, and respond with pongs to the alternate connection
|
|
|
|
|
|
|
|
for i := 0; i < msgs; i++ { |
|
|
|
for i := 0; i < msgs; i++ { |
|
|
|
expected := []byte(fmt.Sprintf(pingTemplate, i)) |
|
|
|
expected := []byte(fmt.Sprintf(pongTemplate, i)) |
|
|
|
|
|
|
|
|
|
|
|
out := make([]byte, size) |
|
|
|
out := make([]byte, size) |
|
|
|
_, err := c2.Read(out) |
|
|
|
if _, err := c1.Read(out); err != nil { |
|
|
|
if err != nil { |
|
|
|
t.Error(err) |
|
|
|
t.Fatal(err) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if !bytes.Equal(expected, out) { |
|
|
|
if !bytes.Equal(expected, out) { |
|
|
|
t.Fatalf("expected %#v, got %#v", expected, out) |
|
|
|
t.Errorf("expected %#v, got %#v", expected, out) |
|
|
|
} else { |
|
|
|
|
|
|
|
msg := []byte(fmt.Sprintf(pongTemplate, i)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_, err := c2.Write(msg) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
t.Fatal(err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
}() |
|
|
|
}() |
|
|
|
|
|
|
|
|
|
|
|
select { |
|
|
|
// expect to read pings, and respond with pongs to the alternate connection
|
|
|
|
case <-done: |
|
|
|
for i := 0; i < msgs; i++ { |
|
|
|
case <-time.After(5 * time.Second): |
|
|
|
expected := []byte(fmt.Sprintf(pingTemplate, i)) |
|
|
|
t.Fatal("test timeout") |
|
|
|
|
|
|
|
|
|
|
|
out := make([]byte, size) |
|
|
|
|
|
|
|
_, err := c2.Read(out) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
t.Fatal(err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if !bytes.Equal(expected, out) { |
|
|
|
|
|
|
|
t.Errorf("expected %#v, got %#v", expected, out) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
msg := []byte(fmt.Sprintf(pongTemplate, i)) |
|
|
|
|
|
|
|
if _, err := c2.Write(msg); err != nil { |
|
|
|
|
|
|
|
t.Fatal(err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|