-
Notifications
You must be signed in to change notification settings - Fork 4
/
gru_test.go
134 lines (112 loc) · 2.19 KB
/
gru_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
package scrape
import (
"fmt"
"net/url"
"sync"
"testing"
)
func Test_getIdleMinions(t *testing.T) {
g := &gru{
minions: []*minion{
{
name: "minion 1",
busy: true,
mu: &sync.RWMutex{},
},
{
name: "minion 2",
mu: &sync.RWMutex{},
},
{
name: "minion 3",
mu: &sync.RWMutex{},
},
{
name: "minion 4",
busy: true,
mu: &sync.RWMutex{},
},
},
}
expectedIdleMinions := map[string]bool{
"minion 2": true,
"minion 3": true,
}
idleMinions := getIdleMinions(g)
if len(idleMinions) != len(expectedIdleMinions) {
t.Fatalf("expected idle minions %d but got %d", len(expectedIdleMinions), len(idleMinions))
}
for _, m := range idleMinions {
_, ok := expectedIdleMinions[m.name]
if !ok {
t.Fatalf("expected %s to be idle but busy\n", m.name)
}
}
}
func Test_distributePayload(t *testing.T) {
tests := []struct {
minions int
busy int
urls int
}{
{
minions: 4,
busy: 4,
},
{
minions: 5,
urls: 4,
},
{
minions: 5,
busy: 1,
urls: 4,
},
{
minions: 5,
urls: 10,
},
}
minionCreateF := func(g *gru, total, busy int) (minions []*minion) {
for i := 0; i < total; i++ {
minions = append(minions, newMinion(fmt.Sprintf("minion %d", i), g.submitDumpCh))
}
for i := 0; i < busy; i++ {
minions[i].busy = true
}
return minions
}
urlCreateF := func(total int) (urls []*url.URL) {
for i := 0; i < total; i++ {
u, _ := url.Parse(fmt.Sprintf("http://test.com/%d", i))
urls = append(urls, u)
}
return urls
}
for _, c := range tests {
baseURL, _ := url.Parse("http://test.com")
g := newGru(baseURL, 1)
g.minions = minionCreateF(g, c.minions, c.busy)
urls := urlCreateF(c.urls)
testCh := make(chan int)
for _, m := range g.minions {
go func(m *minion) {
for mp := range m.payloadCh {
testCh <- len(mp.urls)
}
}(m)
}
distributePayload(g, 1, urls)
count := 0
busyMinions := c.urls
if busyMinions > (c.minions - c.busy) {
busyMinions = c.minions - c.busy
}
for i := 0; i < busyMinions; i++ {
count += <-testCh
}
if c.urls != count {
t.Fatalf("expected %d urls but got %d", c.urls, count)
}
}
}