mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
p2p/simulations/adapters: fix staticcheck warnings
This commit is contained in:
parent
c5c5e0dbe8
commit
563410573d
2 changed files with 112 additions and 171 deletions
|
|
@ -19,7 +19,6 @@ package adapters
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"crypto/ecdsa"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
@ -146,7 +145,6 @@ type ExecNode struct {
|
||||||
client *rpc.Client
|
client *rpc.Client
|
||||||
wsAddr string
|
wsAddr string
|
||||||
newCmd func() *exec.Cmd
|
newCmd func() *exec.Cmd
|
||||||
key *ecdsa.PrivateKey
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Addr returns the node's enode URL
|
// Addr returns the node's enode URL
|
||||||
|
|
|
||||||
|
|
@ -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,43 +32,27 @@ func TestTCPPipe(t *testing.T) {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
done := make(chan struct{})
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
msgs := 50
|
msgs := 50
|
||||||
size := 1024
|
size := 1024
|
||||||
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 {
|
||||||
_, err := c1.Write(msg)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
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)
|
||||||
_, err := c2.Read(out)
|
if _, err := c2.Read(out); err != nil {
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !bytes.Equal(msg, out) {
|
if !bytes.Equal(msg, out) {
|
||||||
t.Fatalf("expected %#v, got %#v", 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")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestTCPPipeBidirections(t *testing.T) {
|
func TestTCPPipeBidirections(t *testing.T) {
|
||||||
|
|
@ -77,26 +61,19 @@ func TestTCPPipeBidirections(t *testing.T) {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
done := make(chan struct{})
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
msgs := 50
|
msgs := 50
|
||||||
size := 7
|
size := 7
|
||||||
for i := 0; i < msgs; i++ {
|
for i := 0; i < msgs; i++ {
|
||||||
msg := []byte(fmt.Sprintf("ping %02d", i))
|
msg := []byte(fmt.Sprintf("ping %02d", i))
|
||||||
|
if _, err := c1.Write(msg); err != nil {
|
||||||
_, err := c1.Write(msg)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
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)
|
||||||
_, err := c2.Read(out)
|
if _, err := c2.Read(out); err != nil {
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -104,8 +81,7 @@ func TestTCPPipeBidirections(t *testing.T) {
|
||||||
t.Fatalf("expected %#v, got %#v", out, expected)
|
t.Fatalf("expected %#v, got %#v", out, expected)
|
||||||
} else {
|
} else {
|
||||||
msg := []byte(fmt.Sprintf("pong %02d", i))
|
msg := []byte(fmt.Sprintf("pong %02d", i))
|
||||||
_, err := c2.Write(msg)
|
if _, err := c2.Write(msg); err != nil {
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -113,25 +89,14 @@ func TestTCPPipeBidirections(t *testing.T) {
|
||||||
|
|
||||||
for i := 0; i < msgs; i++ {
|
for i := 0; i < msgs; i++ {
|
||||||
expected := []byte(fmt.Sprintf("pong %02d", i))
|
expected := []byte(fmt.Sprintf("pong %02d", i))
|
||||||
|
|
||||||
out := make([]byte, size)
|
out := make([]byte, size)
|
||||||
_, err := c1.Read(out)
|
if _, err := c1.Read(out); err != nil {
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !bytes.Equal(expected, out) {
|
if !bytes.Equal(expected, out) {
|
||||||
t.Fatalf("expected %#v, got %#v", out, expected)
|
t.Fatalf("expected %#v, got %#v", out, expected)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
done <- struct{}{}
|
|
||||||
}()
|
|
||||||
|
|
||||||
select {
|
|
||||||
case <-done:
|
|
||||||
case <-time.After(5 * time.Second):
|
|
||||||
t.Fatal("test timeout")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNetPipe(t *testing.T) {
|
func TestNetPipe(t *testing.T) {
|
||||||
|
|
@ -140,47 +105,36 @@ func TestNetPipe(t *testing.T) {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
done := make(chan struct{})
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
msgs := 50
|
msgs := 50
|
||||||
size := 1024
|
size := 1024
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
defer wg.Wait()
|
||||||
|
|
||||||
// netPipe is blocking, so writes are emitted asynchronously
|
// netPipe is blocking, so writes are emitted asynchronously
|
||||||
|
wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
|
||||||
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 {
|
||||||
_, err := c1.Write(msg)
|
t.Error(err)
|
||||||
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)
|
||||||
_, err := c2.Read(out)
|
if _, err := c2.Read(out); err != nil {
|
||||||
if err != nil {
|
t.Error(err)
|
||||||
t.Fatal(err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if !bytes.Equal(msg, out) {
|
if !bytes.Equal(msg, out) {
|
||||||
t.Fatalf("expected %#v, got %#v", msg, out)
|
t.Errorf("expected %#v, got %#v", msg, out)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
done <- struct{}{}
|
|
||||||
}()
|
|
||||||
|
|
||||||
select {
|
|
||||||
case <-done:
|
|
||||||
case <-time.After(5 * time.Second):
|
|
||||||
t.Fatal("test timeout")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNetPipeBidirections(t *testing.T) {
|
func TestNetPipeBidirections(t *testing.T) {
|
||||||
|
|
@ -189,43 +143,41 @@ func TestNetPipeBidirections(t *testing.T) {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
done := make(chan struct{})
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
msgs := 1000
|
msgs := 1000
|
||||||
size := 8
|
size := 8
|
||||||
pingTemplate := "ping %03d"
|
pingTemplate := "ping %03d"
|
||||||
pongTemplate := "pong %03d"
|
pongTemplate := "pong %03d"
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
defer wg.Wait()
|
||||||
|
|
||||||
// netPipe is blocking, so writes are emitted asynchronously
|
// netPipe is blocking, so writes are emitted asynchronously
|
||||||
|
wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
|
||||||
for i := 0; i < msgs; i++ {
|
for i := 0; i < msgs; i++ {
|
||||||
msg := []byte(fmt.Sprintf(pingTemplate, i))
|
msg := []byte(fmt.Sprintf(pingTemplate, i))
|
||||||
|
if _, err := c1.Write(msg); err != nil {
|
||||||
_, err := c1.Write(msg)
|
t.Error(err)
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// netPipe is blocking, so reads for pong are emitted asynchronously
|
// netPipe is blocking, so reads for pong are emitted asynchronously
|
||||||
|
wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
|
defer wg.Done()
|
||||||
|
|
||||||
for i := 0; i < msgs; i++ {
|
for i := 0; i < msgs; i++ {
|
||||||
expected := []byte(fmt.Sprintf(pongTemplate, i))
|
expected := []byte(fmt.Sprintf(pongTemplate, i))
|
||||||
|
|
||||||
out := make([]byte, size)
|
out := make([]byte, size)
|
||||||
_, err := c1.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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
done <- struct{}{}
|
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// expect to read pings, and respond with pongs to the alternate connection
|
// expect to read pings, and respond with pongs to the alternate connection
|
||||||
|
|
@ -239,21 +191,12 @@ func TestNetPipeBidirections(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
} else {
|
||||||
msg := []byte(fmt.Sprintf(pongTemplate, i))
|
msg := []byte(fmt.Sprintf(pongTemplate, i))
|
||||||
|
if _, err := c2.Write(msg); err != nil {
|
||||||
_, err := c2.Write(msg)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
|
||||||
|
|
||||||
select {
|
|
||||||
case <-done:
|
|
||||||
case <-time.After(5 * time.Second):
|
|
||||||
t.Fatal("test timeout")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue