Skip to content

Commit

Permalink
Merge pull request #2 from libp2p/fix/race-conditions
Browse files Browse the repository at this point in the history
fix race conditions in dial_sync and limiter tests
  • Loading branch information
whyrusleeping authored Oct 25, 2016
2 parents ba53315 + 1fc8868 commit c71f968
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
1 change: 0 additions & 1 deletion p2p/net/swarm/dial_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ func (ds *DialSync) DialLock(ctx context.Context, p peer.ID) (*Conn, error) {
ad.conn, ad.err = ds.dialFunc(ctx, p)
close(ad.waitch)
ad.cancel()
ad.waitch = nil // to ensure nobody tries reusing this
}(ctx, p, actd)
}

Expand Down
10 changes: 9 additions & 1 deletion p2p/net/swarm/limiter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"math/rand"
"strconv"
"sync"
"testing"
"time"

Expand Down Expand Up @@ -170,13 +171,17 @@ func TestFDLimiting(t *testing.T) {
}

func TestTokenRedistribution(t *testing.T) {
var lk sync.Mutex
hangchs := make(map[peer.ID]chan struct{})
df := func(ctx context.Context, p peer.ID, a ma.Multiaddr) (iconn.Conn, error) {
if tcpPortOver(a, 10) {
return (iconn.Conn)(nil), nil
}

<-hangchs[p]
lk.Lock()
ch := hangchs[p]
lk.Unlock()
<-ch
return nil, fmt.Errorf("test bad dial")
}
l := newDialLimiterWithParams(df, 8, 4)
Expand All @@ -190,6 +195,9 @@ func TestTokenRedistribution(t *testing.T) {
// take all fd limit tokens with hang dials
for _, pid := range pids {
hangchs[pid] = make(chan struct{})
}

for _, pid := range pids {
tryDialAddrs(ctx, l, pid, bads, resch)
}

Expand Down

0 comments on commit c71f968

Please sign in to comment.