obsaddr_test.go 2.45 KB
Newer Older
1
2
3
4
5
6
package identify

import (
	"testing"
	"time"

Can ZHANG's avatar
Can ZHANG committed
7
	net "github.com/libp2p/go-libp2p-net"
Jeromy's avatar
Jeromy committed
8
	ma "github.com/multiformats/go-multiaddr"
9
10
11
12
13
14
15
16
17
18
19
20
21
)

// TestObsAddrSet
func TestObsAddrSet(t *testing.T) {
	m := func(s string) ma.Multiaddr {
		m, err := ma.NewMultiaddr(s)
		if err != nil {
			t.Error(err)
		}
		return m
	}

	addrsMarch := func(a, b []ma.Multiaddr) bool {
22
23
24
25
		if len(a) != len(b) {
			return false
		}

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
		for _, aa := range a {
			found := false
			for _, bb := range b {
				if aa.Equal(bb) {
					found = true
					break
				}
			}
			if !found {
				return false
			}
		}
		return true
	}

	a1 := m("/ip4/1.2.3.4/tcp/1231")
	a2 := m("/ip4/1.2.3.4/tcp/1232")
	a3 := m("/ip4/1.2.3.4/tcp/1233")
44
45
	a4 := m("/ip4/1.2.3.4/tcp/1234")
	a5 := m("/ip4/1.2.3.4/tcp/1235")
46
47
48
49
50
51

	b1 := m("/ip4/1.2.3.6/tcp/1236")
	b2 := m("/ip4/1.2.3.7/tcp/1237")
	b3 := m("/ip4/1.2.3.8/tcp/1237")
	b4 := m("/ip4/1.2.3.9/tcp/1237")
	b5 := m("/ip4/1.2.3.10/tcp/1237")
52

Can ZHANG's avatar
Can ZHANG committed
53
	oas := &ObservedAddrSet{}
54
55
56
57
58

	if !addrsMarch(oas.Addrs(), nil) {
		t.Error("addrs should be empty")
	}

Can ZHANG's avatar
Can ZHANG committed
59
60
61
62
63
64
65
66
67
68
	add := func(oas *ObservedAddrSet, observed, observer ma.Multiaddr) {
		dummyLocal := m("/ip4/127.0.0.1/tcp/10086")
		dummyDirection := net.DirOutbound

		oas.Add(observed, dummyLocal, observer, dummyDirection)
	}

	add(oas, a1, a4)
	add(oas, a2, a4)
	add(oas, a3, a4)
69
70
71
72
73
74

	// these are all different so we should not yet get them.
	if !addrsMarch(oas.Addrs(), nil) {
		t.Error("addrs should _still_ be empty (once)")
	}

75
	// same observer, so should not yet get them.
Can ZHANG's avatar
Can ZHANG committed
76
77
78
	add(oas, a1, a4)
	add(oas, a2, a4)
	add(oas, a3, a4)
79
80
81
82
	if !addrsMarch(oas.Addrs(), nil) {
		t.Error("addrs should _still_ be empty (same obs)")
	}

83
	// different observer, but same observer group.
Can ZHANG's avatar
Can ZHANG committed
84
85
86
	add(oas, a1, a5)
	add(oas, a2, a5)
	add(oas, a3, a5)
87
88
89
90
	if !addrsMarch(oas.Addrs(), nil) {
		t.Error("addrs should _still_ be empty (same obs group)")
	}

Can ZHANG's avatar
Can ZHANG committed
91
92
93
	add(oas, a1, b1)
	add(oas, a1, b2)
	add(oas, a1, b3)
94
95
96
97
	if !addrsMarch(oas.Addrs(), []ma.Multiaddr{a1}) {
		t.Error("addrs should only have a1")
	}

Can ZHANG's avatar
Can ZHANG committed
98
99
100
101
102
103
104
105
106
107
108
	add(oas, a2, a5)
	add(oas, a1, a5)
	add(oas, a1, a5)
	add(oas, a2, b1)
	add(oas, a1, b1)
	add(oas, a1, b1)
	add(oas, a2, b2)
	add(oas, a1, b2)
	add(oas, a1, b2)
	add(oas, a2, b4)
	add(oas, a2, b5)
109
110
111
112
113
114
115
	if !addrsMarch(oas.Addrs(), []ma.Multiaddr{a1, a2}) {
		t.Error("addrs should only have a1, a2")
	}

	// change the timeout constant so we can time it out.
	oas.SetTTL(time.Millisecond * 200)
	<-time.After(time.Millisecond * 210)
116
	if !addrsMarch(oas.Addrs(), nil) {
117
118
119
		t.Error("addrs should have timed out")
	}
}