Skip to content

Commit

Permalink
Merge pull request #253 from coryschwartz/feat/fix-go-vet
Browse files Browse the repository at this point in the history
fix go vet
  • Loading branch information
Stebalien committed Apr 19, 2021
2 parents ce7c0bc + b00734a commit b556f82
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
27 changes: 23 additions & 4 deletions p2p/net/swarm/dial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,19 +583,23 @@ func TestDialSimultaneousJoin(t *testing.T) {
go acceptAndHang(s2silentListener)

connch := make(chan network.Conn, 512)
errs := make(chan error, 2)

// start a dial to s2 through the silent addr
go func() {
s1.Peerstore().AddAddrs(s2.LocalPeer(), s2silentAddrs, peerstore.PermanentAddrTTL)

c, err := s1.DialPeer(ctx, s2.LocalPeer())
if err != nil {
t.Fatal(err)
errs <- err
connch <- nil
return
}

t.Logf("first dial succedded; conn: %+v", c)

connch <- c
errs <- nil
}()

// wait a bit for the dial to take hold
Expand All @@ -605,18 +609,22 @@ func TestDialSimultaneousJoin(t *testing.T) {
go func() {
s2addrs, err := s2.InterfaceListenAddresses()
if err != nil {
t.Fatal(err)
errs <- err
return
}
s1.Peerstore().AddAddrs(s2.LocalPeer(), s2addrs[:1], peerstore.PermanentAddrTTL)

c, err := s1.DialPeer(ctx, s2.LocalPeer())
if err != nil {
t.Fatal(err)
errs <- err
connch <- nil
return
}

t.Logf("second dial succedded; conn: %+v", c)

connch <- c
errs <- nil
}()

// wait for the second dial to finish
Expand All @@ -626,16 +634,27 @@ func TestDialSimultaneousJoin(t *testing.T) {
go func() {
c, err := s1.DialPeer(ctx, s2.LocalPeer())
if err != nil {
t.Fatal(err)
errs <- err
connch <- nil
return
}

t.Logf("third dial succedded; conn: %+v", c)

connch <- c
errs <- nil
}()

c3 := <-connch

// raise any errors from the previous goroutines
for i := 0; i < 3; i++ {
err := <-errs
if err != nil {
t.Fatal(err)
}
}

if c2 != c3 {
t.Fatal("expected c2 and c3 to be the same")
}
Expand Down
16 changes: 14 additions & 2 deletions p2p/net/swarm/limiter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package swarm

import (
"context"
"errors"
"fmt"
"math/rand"
"strconv"
Expand Down Expand Up @@ -379,8 +380,12 @@ func TestFDLimitUnderflow(t *testing.T) {
addrs = append(addrs, addrWithPort(t, i))
}

wg := sync.WaitGroup{}
wg.Add(1000)
errs := make(chan error, 1000)
for i := 0; i < 1000; i++ {
go func(id peer.ID, i int) {
defer wg.Done()
ctx, cancel := context.WithCancel(context.Background())

resp := make(chan dialResult)
Expand All @@ -406,12 +411,19 @@ func TestFDLimitUnderflow(t *testing.T) {
if res.Err != nil {
return
}
t.Fatal("got dial res, shouldn't")
errs <- errors.New("got dial res, but shouldn't")
}
}(peer.ID(fmt.Sprintf("testpeer%d", i%20)), i)
}

time.Sleep(time.Second * 3)
go func() {
wg.Wait()
close(errs)
}()

for err := range errs {
t.Fatal(err)
}

l.lk.Lock()
fdConsuming := l.fdConsuming
Expand Down

0 comments on commit b556f82

Please sign in to comment.