1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-06-27 16:07:42 +08:00

Merge pull request #1501 from ipfs/fix/notif-test

fix race condition in notifications test
This commit is contained in:
Juan Benet
2015-07-23 15:10:39 -07:00
2 changed files with 43 additions and 21 deletions

View File

@ -7,6 +7,7 @@ import (
ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr" ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context" context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
inet "github.com/ipfs/go-ipfs/p2p/net" inet "github.com/ipfs/go-ipfs/p2p/net"
peer "github.com/ipfs/go-ipfs/p2p/peer"
) )
func TestNotifications(t *testing.T) { func TestNotifications(t *testing.T) {
@ -42,31 +43,43 @@ func TestNotifications(t *testing.T) {
// test everyone got the correct connection opened calls // test everyone got the correct connection opened calls
for i, s := range nets { for i, s := range nets {
n := notifiees[i] n := notifiees[i]
for _, s2 := range nets { notifs := make(map[peer.ID][]inet.Conn)
var actual []inet.Conn for j, s2 := range nets {
for len(s.ConnsToPeer(s2.LocalPeer())) != len(actual) { if i == j {
continue
}
// this feels a little sketchy, but its probably okay
for len(s.ConnsToPeer(s2.LocalPeer())) != len(notifs[s2.LocalPeer()]) {
select { select {
case c := <-n.connected: case c := <-n.connected:
actual = append(actual, c) nfp := notifs[c.RemotePeer()]
notifs[c.RemotePeer()] = append(nfp, c)
case <-time.After(timeout): case <-time.After(timeout):
t.Fatal("timeout") t.Fatal("timeout")
} }
} }
}
expect := s.ConnsToPeer(s2.LocalPeer()) for p, cons := range notifs {
for _, c1 := range actual { expect := s.ConnsToPeer(p)
found := false if len(expect) != len(cons) {
t.Fatal("got different number of connections")
}
for _, c := range cons {
var found bool
for _, c2 := range expect { for _, c2 := range expect {
if c1 == c2 { if c == c2 {
found = true found = true
break break
} }
} }
if !found { if !found {
t.Error("connection not found", c1, len(expect), len(actual)) t.Fatal("connection not found!")
} }
} }
} }
} }

View File

@ -8,6 +8,7 @@ import (
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context" context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
inet "github.com/ipfs/go-ipfs/p2p/net" inet "github.com/ipfs/go-ipfs/p2p/net"
peer "github.com/ipfs/go-ipfs/p2p/peer"
) )
func TestNotifications(t *testing.T) { func TestNotifications(t *testing.T) {
@ -37,35 +38,43 @@ func TestNotifications(t *testing.T) {
// test everyone got the correct connection opened calls // test everyone got the correct connection opened calls
for i, s := range swarms { for i, s := range swarms {
n := notifiees[i] n := notifiees[i]
for _, s2 := range swarms { notifs := make(map[peer.ID][]inet.Conn)
if s == s2 { for j, s2 := range swarms {
if i == j {
continue continue
} }
var actual []inet.Conn // this feels a little sketchy, but its probably okay
for len(s.ConnectionsToPeer(s2.LocalPeer())) != len(actual) { for len(s.ConnectionsToPeer(s2.LocalPeer())) != len(notifs[s2.LocalPeer()]) {
select { select {
case c := <-n.connected: case c := <-n.connected:
actual = append(actual, c) nfp := notifs[c.RemotePeer()]
notifs[c.RemotePeer()] = append(nfp, c)
case <-time.After(timeout): case <-time.After(timeout):
t.Fatal("timeout") t.Fatal("timeout")
} }
} }
}
expect := s.ConnectionsToPeer(s2.LocalPeer()) for p, cons := range notifs {
for _, c1 := range actual { expect := s.ConnectionsToPeer(p)
found := false if len(expect) != len(cons) {
t.Fatal("got different number of connections")
}
for _, c := range cons {
var found bool
for _, c2 := range expect { for _, c2 := range expect {
if c1 == c2 { if c == c2 {
found = true found = true
break break
} }
} }
if !found { if !found {
t.Error("connection not found") t.Fatal("connection not found!")
} }
} }
} }
} }