Skip to content

Commit

Permalink
fixed dht race ipfs#270
Browse files Browse the repository at this point in the history
  • Loading branch information
jbenet committed Nov 5, 2014
1 parent 03f48e9 commit a45511b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
4 changes: 2 additions & 2 deletions dht_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,14 +417,14 @@ func TestConnectCollision(t *testing.T) {

done := make(chan struct{})
go func() {
_, err = dhtA.Connect(ctx, peerB)
_, err := dhtA.Connect(ctx, peerB)
if err != nil {
t.Fatal(err)
}
done <- struct{}{}
}()
go func() {
_, err = dhtB.Connect(ctx, peerA)
_, err := dhtB.Connect(ctx, peerA)
if err != nil {
t.Fatal(err)
}
Expand Down
19 changes: 17 additions & 2 deletions ext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
pb "github.com/jbenet/go-ipfs/routing/dht/pb"
u "github.com/jbenet/go-ipfs/util"

"sync"
"time"
)

Expand All @@ -28,15 +29,24 @@ type mesHandleFunc func(msg.NetMessage) msg.NetMessage
// fauxNet is a standin for a swarm.Network in order to more easily recreate
// different testing scenarios
type fauxSender struct {
sync.Mutex
handlers []mesHandleFunc
}

func (f *fauxSender) AddHandler(fn func(msg.NetMessage) msg.NetMessage) {
f.Lock()
defer f.Unlock()

f.handlers = append(f.handlers, fn)
}

func (f *fauxSender) SendRequest(ctx context.Context, m msg.NetMessage) (msg.NetMessage, error) {
for _, h := range f.handlers {
f.Lock()
handlers := make([]mesHandleFunc, len(f.handlers))
copy(handlers, f.handlers)
f.Unlock()

for _, h := range handlers {
reply := h(m)
if reply != nil {
return reply, nil
Expand All @@ -52,7 +62,12 @@ func (f *fauxSender) SendRequest(ctx context.Context, m msg.NetMessage) (msg.Net
}

func (f *fauxSender) SendMessage(ctx context.Context, m msg.NetMessage) error {
for _, h := range f.handlers {
f.Lock()
handlers := make([]mesHandleFunc, len(f.handlers))
copy(handlers, f.handlers)
f.Unlock()

for _, h := range handlers {
reply := h(m)
if reply != nil {
return nil
Expand Down

0 comments on commit a45511b

Please sign in to comment.