Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix flaky TestStreamsStress test #1288

Merged
merged 1 commit into from
Jan 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 25 additions & 32 deletions p2p/net/mock/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/protocol"

"github.com/libp2p/go-libp2p-testing/ci"
tnet "github.com/libp2p/go-libp2p-testing/net"
"github.com/libp2p/go-libp2p-testing/race"
Expand Down Expand Up @@ -330,11 +331,27 @@ func performPing(t *testing.T, st string, n int, s network.Stream) error {
return nil
}

func makePonger(t *testing.T, st string, errs chan<- error) func(network.Stream) {
t.Helper()
func TestStreamsStress(t *testing.T) {
ctx := context.Background()
nnodes := 100
if race.WithRace() {
nnodes = 30
}

return func(s network.Stream) {
go func() {
mn, err := FullMeshConnected(nnodes)
if err != nil {
t.Fatal(err)
}
defer mn.Close()

errs := make(chan error)

hosts := mn.Hosts()
var wg sync.WaitGroup
for _, h := range hosts {
h.SetStreamHandler(protocol.TestingID, func(s network.Stream) {
const st = "pingpong"
defer wg.Done()
defer s.Close()

for {
Expand All @@ -352,34 +369,11 @@ func makePonger(t *testing.T, st string, errs chan<- error) func(network.Stream)
errs <- err
}
}
}()
})
}
}

func TestStreamsStress(t *testing.T) {
ctx := context.Background()
nnodes := 100
if race.WithRace() {
nnodes = 30
}

mn, err := FullMeshConnected(nnodes)
if err != nil {
t.Fatal(err)
}
defer mn.Close()

errs := make(chan error)

hosts := mn.Hosts()
for _, h := range hosts {
ponger := makePonger(t, "pingpong", errs)
h.SetStreamHandler(protocol.TestingID, ponger)
}

var wg sync.WaitGroup
for i := 0; i < 1000; i++ {
wg.Add(1)
wg.Add(2)
go func(i int) {
defer wg.Done()
var from, to int
Expand All @@ -405,10 +399,9 @@ func TestStreamsStress(t *testing.T) {
}()

for err := range errs {
if err == nil {
continue
if err != nil {
t.Fatal(err)
}
t.Fatal(err)
}
}

Expand Down