fix race condition in notifications test
License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
Jeromy committed
Jul 20, 2015 at 15:46 UTC
b60f494c1e07a6fdf39863fe05dde56a766b0021
1 file changed
+19
-20
p2p/net/mock/mock_notif_test.go
+19
-20
@@ -7,6 +7,7 @@ import (
7
ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
8
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
9
inet "github.com/ipfs/go-ipfs/p2p/net"
10
+ peer "github.com/ipfs/go-ipfs/p2p/peer"
11
)
12
13
func TestNotifications(t *testing.T) {
@@ -42,31 +43,29 @@ func TestNotifications(t *testing.T) {
43
// test everyone got the correct connection opened calls
44
for i, s := range nets {
45
n := notifiees[i]
45
- for _, s2 := range nets {
46
- var actual []inet.Conn
47
- for len(s.ConnsToPeer(s2.LocalPeer())) != len(actual) {
48
- select {
49
- case c := <-n.connected:
50
- actual = append(actual, c)
51
- case <-time.After(timeout):
52
- t.Fatal("timeout")
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")
53
}
54
+ notifs[c.RemotePeer()] = c
55
+ case <-time.After(timeout):
56
+ t.Fatal("timeout")
57
}
58
+ }
59
56
- expect := s.ConnsToPeer(s2.LocalPeer())
57
- for _, c1 := range actual {
58
- found := false
59
- for _, c2 := range expect {
60
- if c1 == c2 {
61
- found = true
62
- break
63
- }
64
- }
65
- if !found {
66
- t.Error("connection not found", c1, len(expect), len(actual))
67
- }
60
+ for p, con := range notifs {
61
+ expect := s.ConnsToPeer(p)
62
+ if len(expect) != 1 {
63
+ t.Fatal("got more than one connection, not supposed to happen")
64
}
65
66
+ if expect[0] != con {
67
+ t.Fatal("got different connection than we expected")
68
+ }
69
}
70
}
71