@cryptotaxi247 / kubo / commits / 47cd70fa7

fix same test in swarm

License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>

Jeromy committed Jul 20, 2015 at 16:29 UTC 47cd70fa7dffc14e2658e55336719b760d5dea42
2 files changed +49 -26
p2p/net/mock/mock_notif_test.go
+29 -15
@@ -43,28 +43,42 @@ func TestNotifications(t *testing.T) {
43 // test everyone got the correct connection opened calls
44 for i, s := range nets {
45 n := notifiees[i]
46 - notifs := make(map[peer.ID]inet.Conn)
47 - for j := 0; j < len(nets)-1; j++ {
48 - select {
49 - case c := <-n.connected:
50 - _, ok := notifs[c.RemotePeer()]
51 - if ok {
52 - t.Fatal("shouldnt have received more than one connection per peer")
46 + notifs := make(map[peer.ID][]inet.Conn)
47 + for j, s2 := range nets {
48 + if i == j {
49 + continue
50 + }
51 +
52 + // this feels a little sketchy, but its probably okay
53 + for len(s.ConnsToPeer(s2.LocalPeer())) != len(notifs[s2.LocalPeer()]) {
54 + select {
55 + case c := <-n.connected:
56 + nfp := notifs[c.RemotePeer()]
57 + notifs[c.RemotePeer()] = append(nfp, c)
58 + case <-time.After(timeout):
59 + t.Fatal("timeout")
60 }
54 - notifs[c.RemotePeer()] = c
55 - case <-time.After(timeout):
56 - t.Fatal("timeout")
61 }
62 }
63
60 - for p, con := range notifs {
64 + for p, cons := range notifs {
65 expect := s.ConnsToPeer(p)
62 - if len(expect) != 1 {
63 - t.Fatal("got more than one connection, not supposed to happen")
66 + if len(expect) != len(cons) {
67 + t.Fatal("got different number of connections")
68 }
69
66 - if expect[0] != con {
67 - t.Fatal("got different connection than we expected")
70 + for _, c := range cons {
71 + var found bool
72 + for _, c2 := range expect {
73 + if c == c2 {
74 + found = true
75 + break
76 + }
77 + }
78 +
79 + if !found {
80 + t.Fatal("connection not found!")
81 + }
82 }
83 }
84 }
p2p/net/swarm/swarm_notif_test.go
+20 -11
@@ -8,6 +8,7 @@ import (
8 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
9
10 inet "github.com/ipfs/go-ipfs/p2p/net"
11 + peer "github.com/ipfs/go-ipfs/p2p/peer"
12 )
13
14 func TestNotifications(t *testing.T) {
@@ -37,35 +38,43 @@ func TestNotifications(t *testing.T) {
38 // test everyone got the correct connection opened calls
39 for i, s := range swarms {
40 n := notifiees[i]
40 - for _, s2 := range swarms {
41 - if s == s2 {
41 + notifs := make(map[peer.ID][]inet.Conn)
42 + for j, s2 := range swarms {
43 + if i == j {
44 continue
45 }
46
45 - var actual []inet.Conn
46 - for len(s.ConnectionsToPeer(s2.LocalPeer())) != len(actual) {
47 + // this feels a little sketchy, but its probably okay
48 + for len(s.ConnectionsToPeer(s2.LocalPeer())) != len(notifs[s2.LocalPeer()]) {
49 select {
50 case c := <-n.connected:
49 - actual = append(actual, c)
51 + nfp := notifs[c.RemotePeer()]
52 + notifs[c.RemotePeer()] = append(nfp, c)
53 case <-time.After(timeout):
54 t.Fatal("timeout")
55 }
56 }
57 + }
58 +
59 + for p, cons := range notifs {
60 + expect := s.ConnectionsToPeer(p)
61 + if len(expect) != len(cons) {
62 + t.Fatal("got different number of connections")
63 + }
64
55 - expect := s.ConnectionsToPeer(s2.LocalPeer())
56 - for _, c1 := range actual {
57 - found := false
65 + for _, c := range cons {
66 + var found bool
67 for _, c2 := range expect {
59 - if c1 == c2 {
68 + if c == c2 {
69 found = true
70 break
71 }
72 }
73 +
74 if !found {
65 - t.Error("connection not found")
75 + t.Fatal("connection not found!")
76 }
77 }
68 -
78 }
79 }
80