-
Notifications
You must be signed in to change notification settings - Fork 0
/
crawlplan_test.go
152 lines (136 loc) · 5.82 KB
/
crawlplan_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package crawlrate
import (
"net"
"testing"
"time"
"fmt"
)
var bottomTests = []struct {
keywordCount, proxyCount int
pulse *Pulse
out CrawlPlan
}{
{
1, 1, &Pulse{1, time.Duration(60) * time.Second, time.Duration(60) * time.Second},
CrawlPlan{
{time.Duration(0) * time.Second, net.ParseIP("127.0.0.0"), 0, "keyword-0"},
},
},
{
2, 1, &Pulse{1, time.Duration(60) * time.Second, time.Duration(120) * time.Second},
CrawlPlan{
{time.Duration(0) * time.Second, net.ParseIP("127.0.0.0"), 0, "keyword-0"},
{time.Duration(60) * time.Second, net.ParseIP("127.0.0.0"), 0, "keyword-1"},
},
},
{
2, 1, &Pulse{2, time.Duration(60) * time.Second, time.Duration(60) * time.Second},
CrawlPlan{
{time.Duration(0) * time.Second, net.ParseIP("127.0.0.0"), 0, "keyword-0"},
{time.Duration(0) * time.Second, net.ParseIP("127.0.0.0"), 1, "keyword-1"},
},
},
{
3, 1, &Pulse{1, time.Duration(60) * time.Second, time.Duration(180) * time.Second},
CrawlPlan{
{time.Duration(0) * time.Second, net.ParseIP("127.0.0.0"), 0, "keyword-0"},
{time.Duration(60) * time.Second, net.ParseIP("127.0.0.0"), 0, "keyword-1"},
{time.Duration(120) * time.Second, net.ParseIP("127.0.0.0"), 0, "keyword-2"},
},
},
{
3, 1, &Pulse{2, time.Duration(60) * time.Second, time.Duration(120) * time.Second},
CrawlPlan{
{time.Duration(0) * time.Second, net.ParseIP("127.0.0.0"), 0, "keyword-0"},
{time.Duration(0) * time.Second, net.ParseIP("127.0.0.0"), 1, "keyword-1"},
{time.Duration(60) * time.Second, net.ParseIP("127.0.0.0"), 0, "keyword-2"},
},
},
{
3, 1, &Pulse{3, time.Duration(60) * time.Second, time.Duration(60) * time.Second},
CrawlPlan{
{time.Duration(0) * time.Second, net.ParseIP("127.0.0.0"), 0, "keyword-0"},
{time.Duration(0) * time.Second, net.ParseIP("127.0.0.0"), 1, "keyword-1"},
{time.Duration(0) * time.Second, net.ParseIP("127.0.0.0"), 2, "keyword-2"},
},
},
{
15, 3, &Pulse{2, time.Duration(60) * time.Second, time.Duration(180) * time.Second},
CrawlPlan{
{time.Duration(0) * time.Second, net.ParseIP("127.0.0.0"), 0, "keyword-0"},
{time.Duration(0) * time.Second, net.ParseIP("127.0.0.0"), 1, "keyword-1"},
{time.Duration(0) * time.Second, net.ParseIP("127.0.0.1"), 0, "keyword-2"},
{time.Duration(0) * time.Second, net.ParseIP("127.0.0.1"), 1, "keyword-3"},
{time.Duration(0) * time.Second, net.ParseIP("127.0.0.2"), 0, "keyword-4"},
{time.Duration(0) * time.Second, net.ParseIP("127.0.0.2"), 1, "keyword-5"},
{time.Duration(60) * time.Second, net.ParseIP("127.0.0.0"), 0, "keyword-6"},
{time.Duration(60) * time.Second, net.ParseIP("127.0.0.0"), 1, "keyword-7"},
{time.Duration(60) * time.Second, net.ParseIP("127.0.0.1"), 0, "keyword-8"},
{time.Duration(60) * time.Second, net.ParseIP("127.0.0.1"), 1, "keyword-9"},
{time.Duration(60) * time.Second, net.ParseIP("127.0.0.2"), 0, "keyword-10"},
{time.Duration(60) * time.Second, net.ParseIP("127.0.0.2"), 1, "keyword-11"},
{time.Duration(120) * time.Second, net.ParseIP("127.0.0.0"), 0, "keyword-12"},
{time.Duration(120) * time.Second, net.ParseIP("127.0.0.0"), 1, "keyword-13"},
{time.Duration(120) * time.Second, net.ParseIP("127.0.0.1"), 0, "keyword-14"},
},
},
}
func Test_BottomDistribution(t *testing.T) {
for k, tt := range bottomTests {
cp := New(generateLists("keyword-", tt.keywordCount), generateLists("127.0.0.", tt.proxyCount), tt.pulse)
if len(cp) != len(tt.out) {
t.Errorf("Test %d: Non-equal slice lengths. Got: %d Expected: %d\n", k, len(cp), len(tt.out))
}
for n:=0; n<len(cp); n++ {
if cp[n].Time != tt.out[n].Time {
t.Errorf("Test %d: Time not equal. Got: %d Expected: %d\n", k, cp[n].Time, tt.out[n].Time)
}
if cp[n].Proxy.String() != tt.out[n].Proxy.String() {
t.Errorf("Test %d: Proxy not equal. Got: %s Expected: %s\n", k, cp[n].Proxy.String(), tt.out[n].Proxy.String())
}
if cp[n].Conn != tt.out[n].Conn {
t.Errorf("Test %d: Connection count not equal. Got: %d Expected: %d\n", k, cp[n].Conn, tt.out[n].Conn)
}
if cp[n].Conn != tt.out[n].Conn {
t.Errorf("Test %d: Connection count not equal. Got: %d Expected: %d\n", k, cp[n].Conn, tt.out[n].Conn)
}
if cp[n].Keyword != tt.out[n].Keyword {
t.Errorf("Test %d: Keyword not equal. Got: %d Expected: %d\n", k, cp[n].Keyword, tt.out[n].Keyword)
}
}
}
}
func Test_Distinct(t *testing.T) {
var tableCr = CrawlPlan{
{time.Duration(0) * time.Second, net.ParseIP("127.0.0.0"), 0, "keyword-0"},
{time.Duration(60) * time.Second, net.ParseIP("127.0.0.0"), 0, "keyword-1"},
{time.Duration(120) * time.Second, net.ParseIP("127.0.0.0"), 0, "keyword-2"},
{time.Duration(0) * time.Second, net.ParseIP("127.0.0.0"), 0, "keyword-0"},
{time.Duration(60) * time.Second, net.ParseIP("127.0.0.0"), 0, "keyword-1"},
{time.Duration(120) * time.Second, net.ParseIP("127.0.0.0"), 0, "keyword-2"},
{time.Duration(0) * time.Second, net.ParseIP("127.0.0.0"), 0, "keyword-0"},
{time.Duration(60) * time.Second, net.ParseIP("127.0.0.0"), 0, "keyword-1"},
{time.Duration(120) * time.Second, net.ParseIP("127.0.0.0"), 0, "keyword-2"},
}
dist := tableCr.Distinct()
distinctCount := len(dist)
if distinctCount != 3 {
t.Errorf("Distinct count wrong. Got: %d Expected: %d\n", distinctCount, 3)
}
if dist[0] != 0 {
t.Errorf("Distinct item 0 is wrong. Got: %d Expected: %d\n", dist[0], 0)
}
if dist[1] != 60 {
t.Errorf("Distinct item 0 is wrong. Got: %d Expected: %d\n", dist[1], 60)
}
if dist[2] != 120 {
t.Errorf("Distinct item 0 is wrong. Got: %d Expected: %d\n", dist[2], 120)
}
}
func generateLists(prefix string, count int) []string {
var out []string
for n:=0; n< count; n++ {
out = append(out, fmt.Sprintf("%s%d", prefix, n))
}
return out
}