net/id_test: refactor tests into own func
Juan Batiz-Benet committed
Dec 22, 2014 at 20:09 UTC
d9961893a216a65d0b19ca666bfe976d2782fdd0
1 file changed
+39
-38
net/id_test.go
+39
-38
@@ -39,60 +39,61 @@ func TestIDService(t *testing.T) {
39
n1 := GenNetwork(t, ctx)
40
n2 := GenNetwork(t, ctx)
41
42
- testKnowsAddrs := func(n inet.Network, p peer.ID, expected []ma.Multiaddr) {
43
- actual := n.Peerstore().Addresses(p)
44
-
45
- if len(actual) != len(expected) {
46
- t.Error("dont have the same addresses")
47
- }
48
-
49
- have := map[string]struct{}{}
50
- for _, addr := range actual {
51
- have[addr.String()] = struct{}{}
52
- }
53
- for _, addr := range expected {
54
- if _, found := have[addr.String()]; !found {
55
- t.Errorf("%s did not have addr for %s: %s", n.LocalPeer(), p, addr)
56
- panic("ahhhhhhh")
57
- }
58
- }
59
- }
60
-
61
- testHasProtocolVersions := func(n inet.Network, p peer.ID) {
62
- v, err := n.Peerstore().Get(p, "ProtocolVersion")
63
- if v.(string) != handshake.IpfsVersion.String() {
64
- t.Fatal("protocol mismatch", err)
65
- }
66
- v, err = n.Peerstore().Get(p, "AgentVersion")
67
- if v.(string) != handshake.ClientVersion {
68
- t.Fatal("agent version mismatch", err)
69
- }
70
- }
71
-
42
n1p := n1.LocalPeer()
43
n2p := n2.LocalPeer()
44
75
- testKnowsAddrs(n1, n2p, []ma.Multiaddr{}) // nothing
76
- testKnowsAddrs(n2, n1p, []ma.Multiaddr{}) // nothing
45
+ testKnowsAddrs(t, n1, n2p, []ma.Multiaddr{}) // nothing
46
+ testKnowsAddrs(t, n2, n1p, []ma.Multiaddr{}) // nothing
47
48
// have n2 tell n1, so we can dial...
49
DivulgeAddresses(n2, n1)
50
81
- testKnowsAddrs(n1, n2p, n2.Peerstore().Addresses(n2p)) // has them
82
- testKnowsAddrs(n2, n1p, []ma.Multiaddr{}) // nothing
51
+ testKnowsAddrs(t, n1, n2p, n2.Peerstore().Addresses(n2p)) // has them
52
+ testKnowsAddrs(t, n2, n1p, []ma.Multiaddr{}) // nothing
53
54
if err := n1.DialPeer(ctx, n2p); err != nil {
55
t.Fatalf("Failed to dial:", err)
56
}
57
58
+ // this is shitty. dial should wait for connecting to end
59
<-time.After(100 * time.Millisecond)
60
61
// the IDService should be opened automatically, by the network.
62
// what we should see now is that both peers know about each others listen addresses.
92
- testKnowsAddrs(n1, n2p, n2.Peerstore().Addresses(n2p)) // has them
93
- testKnowsAddrs(n2, n1p, n1.Peerstore().Addresses(n1p)) // has them
63
+ testKnowsAddrs(t, n1, n2p, n2.Peerstore().Addresses(n2p)) // has them
64
+ testKnowsAddrs(t, n2, n1p, n1.Peerstore().Addresses(n1p)) // has them
65
66
// and the protocol versions.
96
- testHasProtocolVersions(n1, n2p)
97
- testHasProtocolVersions(n2, n1p)
67
+ testHasProtocolVersions(t, n1, n2p)
68
+ testHasProtocolVersions(t, n2, n1p)
69
+}
70
+
71
+func testKnowsAddrs(t *testing.T, n inet.Network, p peer.ID, expected []ma.Multiaddr) {
72
+ actual := n.Peerstore().Addresses(p)
73
+
74
+ if len(actual) != len(expected) {
75
+ t.Error("dont have the same addresses")
76
+ }
77
+
78
+ have := map[string]struct{}{}
79
+ for _, addr := range actual {
80
+ have[addr.String()] = struct{}{}
81
+ }
82
+ for _, addr := range expected {
83
+ if _, found := have[addr.String()]; !found {
84
+ t.Errorf("%s did not have addr for %s: %s", n.LocalPeer(), p, addr)
85
+ panic("ahhhhhhh")
86
+ }
87
+ }
88
+}
89
+
90
+func testHasProtocolVersions(t *testing.T, n inet.Network, p peer.ID) {
91
+ v, err := n.Peerstore().Get(p, "ProtocolVersion")
92
+ if v.(string) != handshake.IpfsVersion.String() {
93
+ t.Fatal("protocol mismatch", err)
94
+ }
95
+ v, err = n.Peerstore().Get(p, "AgentVersion")
96
+ if v.(string) != handshake.ClientVersion {
97
+ t.Fatal("agent version mismatch", err)
98
+ }
99
}