Commit 40ba0294 authored by Jeromy Johnson's avatar Jeromy Johnson Committed by GitHub

Merge pull request #2 from libp2p/fix/race-conditions

fix race conditions in dial_sync and limiter tests
parents 80858d6c ebce7de7
...@@ -81,7 +81,6 @@ func (ds *DialSync) DialLock(ctx context.Context, p peer.ID) (*Conn, error) { ...@@ -81,7 +81,6 @@ func (ds *DialSync) DialLock(ctx context.Context, p peer.ID) (*Conn, error) {
ad.conn, ad.err = ds.dialFunc(ctx, p) ad.conn, ad.err = ds.dialFunc(ctx, p)
close(ad.waitch) close(ad.waitch)
ad.cancel() ad.cancel()
ad.waitch = nil // to ensure nobody tries reusing this
}(ctx, p, actd) }(ctx, p, actd)
} }
......
...@@ -5,6 +5,7 @@ import ( ...@@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"math/rand" "math/rand"
"strconv" "strconv"
"sync"
"testing" "testing"
"time" "time"
...@@ -170,13 +171,17 @@ func TestFDLimiting(t *testing.T) { ...@@ -170,13 +171,17 @@ func TestFDLimiting(t *testing.T) {
} }
func TestTokenRedistribution(t *testing.T) { func TestTokenRedistribution(t *testing.T) {
var lk sync.Mutex
hangchs := make(map[peer.ID]chan struct{}) hangchs := make(map[peer.ID]chan struct{})
df := func(ctx context.Context, p peer.ID, a ma.Multiaddr) (iconn.Conn, error) { df := func(ctx context.Context, p peer.ID, a ma.Multiaddr) (iconn.Conn, error) {
if tcpPortOver(a, 10) { if tcpPortOver(a, 10) {
return (iconn.Conn)(nil), nil return (iconn.Conn)(nil), nil
} }
<-hangchs[p] lk.Lock()
ch := hangchs[p]
lk.Unlock()
<-ch
return nil, fmt.Errorf("test bad dial") return nil, fmt.Errorf("test bad dial")
} }
l := newDialLimiterWithParams(df, 8, 4) l := newDialLimiterWithParams(df, 8, 4)
...@@ -190,6 +195,9 @@ func TestTokenRedistribution(t *testing.T) { ...@@ -190,6 +195,9 @@ func TestTokenRedistribution(t *testing.T) {
// take all fd limit tokens with hang dials // take all fd limit tokens with hang dials
for _, pid := range pids { for _, pid := range pids {
hangchs[pid] = make(chan struct{}) hangchs[pid] = make(chan struct{})
}
for _, pid := range pids {
tryDialAddrs(ctx, l, pid, bads, resch) tryDialAddrs(ctx, l, pid, bads, resch)
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment