-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
218 lines (193 loc) · 4.91 KB
/
main_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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package main
import (
"context"
"flag"
"fmt"
"testing"
"time"
"github.com/chromedp/chromedp"
amqp "github.com/rabbitmq/amqp091-go"
)
func TestWorker(t *testing.T) {
if testing.Short() {
t.Skip()
}
var queue_host string
var queue_user string
var queue_password string
var queue_port string
var queue_name string
var concurreny_limit int
var cache_ttl int
flag.StringVar(&queue_host, "queue_host", "localhost", "Hostname for rabbitmq")
flag.StringVar(&queue_user, "queue_user", "guest", "User for rabbitmq")
flag.StringVar(&queue_password, "queue_password", "guest", "Password for rabbitmq")
flag.StringVar(&queue_port, "queue_port", "5672", "Port for rabbitmq")
flag.StringVar(&queue_name, "queue_name", "busyboi", "Queue name for rabbitmq")
flag.IntVar(&concurreny_limit, "concurreny_limit", 10, "Limits how many crawling jobs can run simultaneously")
flag.IntVar(&cache_ttl, "cache_ttl", 900, "How long should cache be stored (in seconds). Default is 15 minutes.")
flag.Parse()
bb := &Busyboi{
queueMsgs: make(chan amqp.Delivery),
mq: &Rabbitmq{
hostname: queue_host,
user: queue_user,
password: queue_password,
port: queue_port,
queue: queue_name,
},
cache: NewCache(time.Second * time.Duration(cache_ttl)),
}
go bb.cache.Invalidator()
go testserver()
urls := []string{
"http://localhost:8080",
}
for i, k := range urls {
bb.mq.RabbitMqAddMessages(JobConfig{
Collection: "products",
Url: k,
Fields: []JobConfigField{
{
Name: "Images",
Selector: ".images",
Attr: "src",
Children: []JobConfigField{
{
Name: "Image 1",
Selector: "body .product_image",
Attr: "src",
Regex: "(\\.jpg)",
},
{
Name: "Image 2",
Selector: "body .product_image",
Attr: "src",
},
},
Regex: "([a-z]+\\.jpg)",
},
{
Name: "Title",
Selector: "body .product_title",
},
{
Name: "Price",
Selector: "body .product_price",
Regex: "(\\.[0-9]+)",
},
{
Name: "Some url",
Selector: "body a[href]",
Attr: "href",
},
},
})
fmt.Printf("Added job # %d\n", i)
}
go bb.mq.RabbitMqGetMessages(bb)
opts := []chromedp.ExecAllocatorOption{
chromedp.UserAgent("Busyboi"),
chromedp.WindowSize(1920, 1080),
chromedp.NoFirstRun,
chromedp.NoDefaultBrowserCheck,
chromedp.Headless,
chromedp.DisableGPU,
}
ctx, cancel := chromedp.NewExecAllocator(context.Background(), opts...)
defer cancel()
limiter := make(chan uint, concurreny_limit)
for m := range bb.queueMsgs {
limiter <- 1
go func(m amqp.Delivery) {
worker(m.Body, ctx, bb)
<-limiter
}(m)
}
close(bb.queueMsgs)
}
func TestThrottle(t *testing.T) {
if testing.Short() {
t.Skip()
}
var queue_host string
var queue_user string
var queue_password string
var queue_port string
var queue_name string
var concurreny_limit int
var cache_ttl int
flag.StringVar(&queue_host, "queue_host", "localhost", "Hostname for rabbitmq")
flag.StringVar(&queue_user, "queue_user", "guest", "User for rabbitmq")
flag.StringVar(&queue_password, "queue_password", "guest", "Password for rabbitmq")
flag.StringVar(&queue_port, "queue_port", "5672", "Port for rabbitmq")
flag.StringVar(&queue_name, "queue_name", "busyboi", "Queue name for rabbitmq")
flag.IntVar(&concurreny_limit, "concurreny_limit", 10, "Limits how many crawling jobs can run simultaneously")
flag.IntVar(&cache_ttl, "cache_ttl", 900, "How long should cache be stored (in seconds). Default is 15 minutes.")
flag.Parse()
bb := &Busyboi{
queueMsgs: make(chan amqp.Delivery),
mq: &Rabbitmq{
hostname: queue_host,
user: queue_user,
password: queue_password,
port: queue_port,
queue: queue_name,
},
cache: NewCache(time.Second * time.Duration(cache_ttl)),
}
go bb.cache.Invalidator()
urls := []string{
"http://localhost:8080",
}
for i := 0; i < 50; i++ {
for i, k := range urls {
bb.mq.RabbitMqAddMessages(JobConfig{
Collection: "products",
Url: k,
Fields: []JobConfigField{
{
Name: "Images",
Selector: ".images",
Attr: "src",
Children: []JobConfigField{
{
Name: "Image 1",
Selector: "body .product_image",
Attr: "src",
Regex: "(\\.jpg)",
},
{
Name: "Image 2",
Selector: "body .product_image",
Attr: "src",
},
},
Regex: "([a-z]+\\.jpg)",
},
{
Name: "Title",
Selector: "body .product_title",
},
{
Name: "Price",
Selector: "body .product_price",
Regex: "(\\.[0-9]+)",
},
{
Name: "Some url",
Selector: "body a[href]",
Attr: "href",
},
},
})
fmt.Printf("Added job # %d\n", i)
}
}
}
func TestTestServer(t *testing.T) {
if testing.Short() {
t.Skip()
}
testserver()
}