From 79ba610d21eb5213906e959dc63d4bdf0b771ded Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Thu, 8 Nov 2018 11:08:38 -0800 Subject: [PATCH] mocknet: create a connection on NewStream if we need one That's what the Swarm does and that's what the function's documentation says it does. --- p2p/net/mock/mock_peernet.go | 22 +++------------------- 1 file changed, 3 insertions(+), 19 deletions(-) diff --git a/p2p/net/mock/mock_peernet.go b/p2p/net/mock/mock_peernet.go index 5233101..35c9bbe 100644 --- a/p2p/net/mock/mock_peernet.go +++ b/p2p/net/mock/mock_peernet.go @@ -330,26 +330,10 @@ func (pn *peernet) Connectedness(p peer.ID) inet.Connectedness { // NewStream returns a new stream to given peer p. // If there is no connection to p, attempts to create one. func (pn *peernet) NewStream(ctx context.Context, p peer.ID) (inet.Stream, error) { - pn.Lock() - cs, found := pn.connsByPeer[p] - if !found || len(cs) < 1 { - pn.Unlock() - return nil, fmt.Errorf("no connection to peer") + c, err := pn.DialPeer(ctx, p) + if err != nil { + return nil, err } - - // if many conns are found, how do we select? for now, randomly... - // this would be an interesting place to test logic that can measure - // links (network interfaces) and select properly - n := rand.Intn(len(cs)) - var c *conn - for c = range cs { - if n == 0 { - break - } - n-- - } - pn.Unlock() - return c.NewStream() } -- GitLab