-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
dht find connected peers #428
Changes from all commits
a123773
c5c0e7e
26e7656
94f04c7
ff1e672
f15ab8e
d06bb6d
e0f11df
6040a3e
93872a5
26a44fc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package dht | |
|
||
import ( | ||
"bytes" | ||
"sort" | ||
"testing" | ||
|
||
context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context" | ||
|
@@ -64,6 +65,14 @@ func setupDHTS(ctx context.Context, n int, t *testing.T) ([]ma.Multiaddr, []peer | |
return addrs, peers, dhts | ||
} | ||
|
||
func makePeerString(t *testing.T, addr string) peer.Peer { | ||
maddr, err := ma.NewMultiaddr(addr) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
return makePeer(maddr) | ||
} | ||
|
||
func makePeer(addr ma.Multiaddr) peer.Peer { | ||
sk, pk, err := ci.GenerateKeyPair(ci.RSA, 512) | ||
if err != nil { | ||
|
@@ -406,6 +415,100 @@ func TestFindPeer(t *testing.T) { | |
} | ||
} | ||
|
||
func TestFindPeersConnectedToPeer(t *testing.T) { | ||
if testing.Short() { | ||
t.SkipNow() | ||
} | ||
|
||
ctx := context.Background() | ||
u.Debug = false | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. prev := u.Debug
u.Debug = false
defer u.Debug = prev ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Woah what. sorry idk how that got in there. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. btw, lovely pattern. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. huh, it's all over the place in this file. dont remember why. @whyrusleeping any idea? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
lovely is this pattern >> http://commandcenter.blogspot.nl/2014/01/self-referential-functions-and-design.html There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. merging. we'll solve this later. #430 |
||
|
||
_, peers, dhts := setupDHTS(ctx, 4, t) | ||
defer func() { | ||
for i := 0; i < 4; i++ { | ||
dhts[i].Close() | ||
dhts[i].dialer.(inet.Network).Close() | ||
} | ||
}() | ||
|
||
// topology: | ||
// 0-1, 1-2, 1-3, 2-3 | ||
err := dhts[0].Connect(ctx, peers[1]) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
err = dhts[1].Connect(ctx, peers[2]) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
err = dhts[1].Connect(ctx, peers[3]) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
err = dhts[2].Connect(ctx, peers[3]) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// fmt.Println("0 is", peers[0]) | ||
// fmt.Println("1 is", peers[1]) | ||
// fmt.Println("2 is", peers[2]) | ||
// fmt.Println("3 is", peers[3]) | ||
|
||
ctxT, _ := context.WithTimeout(ctx, time.Second) | ||
pchan, err := dhts[0].FindPeersConnectedToPeer(ctxT, peers[2].ID()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// shouldFind := []peer.Peer{peers[1], peers[3]} | ||
found := []peer.Peer{} | ||
for nextp := range pchan { | ||
found = append(found, nextp) | ||
} | ||
|
||
// fmt.Printf("querying 0 (%s) FindPeersConnectedToPeer 2 (%s)\n", peers[0], peers[2]) | ||
// fmt.Println("should find 1, 3", shouldFind) | ||
// fmt.Println("found", found) | ||
|
||
// testPeerListsMatch(t, shouldFind, found) | ||
|
||
log.Warning("TestFindPeersConnectedToPeer is not quite correct") | ||
if len(found) == 0 { | ||
t.Fatal("didn't find any peers.") | ||
} | ||
} | ||
|
||
func testPeerListsMatch(t *testing.T, p1, p2 []peer.Peer) { | ||
|
||
if len(p1) != len(p2) { | ||
t.Fatal("did not find as many peers as should have", p1, p2) | ||
} | ||
|
||
ids1 := make([]string, len(p1)) | ||
ids2 := make([]string, len(p2)) | ||
|
||
for i, p := range p1 { | ||
ids1[i] = p.ID().Pretty() | ||
} | ||
|
||
for i, p := range p2 { | ||
ids2[i] = p.ID().Pretty() | ||
} | ||
|
||
sort.Sort(sort.StringSlice(ids1)) | ||
sort.Sort(sort.StringSlice(ids2)) | ||
|
||
for i := range ids1 { | ||
if ids1[i] != ids2[i] { | ||
t.Fatal("Didnt find expected peer", ids1[i], ids2) | ||
} | ||
} | ||
} | ||
|
||
func TestConnectCollision(t *testing.T) { | ||
if testing.Short() { | ||
t.SkipNow() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
err instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is programmer error. It's not an error that should be allowed at runtime / handled. variable addresses should be addressed long before we get here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
more generally, i'm coming to think that returning
err
all over the place can actually lead to harder to debug things than well understood panics. it treatserror
as a catch-all, instead of purposeful error handling of things known to go wrong. imagine if index errors returnederr
in slices? the stdlib panics all over the place to signal what we definitely shouldnt do.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's not the errors themselves that cause that. it's masking them behind log statements and
continuing
over them. those practices will certainly make it harder to debug behavior.i'm in complete disagreement regarding panics versus erroring. but whatever
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#412 documents a panic that takes down the daemon during
ipfs swarm connect
. i weep when i see more opportunities for panicking going into subsystems.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is a separate issue, and yes, if that's used incorrectly it's pretty bad. In servers though, logging the error often IS exactly what we want to do (think 404s, 500s, etc).
err
should be handled correctly OR gracefully bubble back to {exit the application in cli, exit the request in server}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean to call attention to the fact the this separate issue is the actual reason why one would begin to lose confidence in errors.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My point is that adding errors all over the place is bad practice itself. Look at the stdlib: https://gist.github.com/jbenet/9c20705c43a7ae47f9be Programmer error that is always incorrect, should not ship, should panic. We disagree on this and i dont think we'll reach consensus now.
And yeah, #412 is a problem that needs to be addressed.