forked from sjdirect/abot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCrawlSiteSimulator.cs
253 lines (210 loc) · 15.6 KB
/
CrawlSiteSimulator.cs
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
using Abot.Core;
using Abot.Crawler;
using Abot.Poco;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
namespace Abot.Tests.Integration
{
[TestFixture]
public class CrawlSiteSimulator : CrawlTestBase
{
public CrawlSiteSimulator()
:base(new Uri("http://localhost.fiddler:1111/"), 25)
{
}
[Test]
public void Crawl_VerifyCrawlResultIsAsExpected()
{
new PageRequester(new CrawlConfiguration{ UserAgentString = "aaa" }).MakeRequest(new Uri("http://localhost.fiddler:1111/PageGenerator/ClearCounters"));
base.CrawlAndAssert(new PoliteWebCrawler());
}
[Test]
public void Crawl_MaxPagesTo5_OnlyCrawls5Pages()
{
new PageRequester(new CrawlConfiguration{ UserAgentString = "aaa" }).MakeRequest(new Uri("http://localhost.fiddler:1111/PageGenerator/ClearCounters"));
CrawlConfiguration configuration = new CrawlConfiguration();
configuration.MaxPagesToCrawl = 5;
int pagesCrawledCount = 0;
PoliteWebCrawler crawler = new PoliteWebCrawler(configuration, null, null, null, null, null, null, null, null);
crawler.PageCrawlCompletedAsync += (a, b) => pagesCrawledCount++;
crawler.Crawl(new Uri("http://localhost.fiddler:1111/"));
Assert.AreEqual(5, pagesCrawledCount);
}
[Test]
public void Crawl_MaxPagesTo25_OnlyCrawls25Pages()
{
new PageRequester(new CrawlConfiguration { UserAgentString = "aaa" }).MakeRequest(new Uri("http://localhost.fiddler:1111/PageGenerator/ClearCounters"));
CrawlConfiguration configuration = new CrawlConfiguration();
configuration.MaxPagesToCrawl = 25;
int pagesCrawledCount = 0;
PoliteWebCrawler crawler = new PoliteWebCrawler(configuration, null, null, null, null, null, null, null, null);
crawler.PageCrawlCompletedAsync += (a, b) => pagesCrawledCount++;
crawler.Crawl(new Uri("http://localhost.fiddler:1111/"));
Assert.AreEqual(25, pagesCrawledCount);
}
[Test]
public void Crawl_MaxPagesTo5_WithCrawlDelay_OnlyCrawls5Pages()
{
new PageRequester(new CrawlConfiguration{ UserAgentString = "aaa" }).MakeRequest(new Uri("http://localhost.fiddler:1111/PageGenerator/ClearCounters"));
CrawlConfiguration configuration = new CrawlConfiguration();
configuration.MinCrawlDelayPerDomainMilliSeconds = 1000; //adding delay since it increases the chance of issues with abot crawling more than MaxPagesToCrawl.
configuration.MaxPagesToCrawl = 5;
int pagesCrawledCount = 0;
PoliteWebCrawler crawler = new PoliteWebCrawler(configuration, null, null, null, null, null, null, null, null);
crawler.PageCrawlCompletedAsync += (a, b) => pagesCrawledCount++;
crawler.Crawl(new Uri("http://localhost.fiddler:1111/"));
Assert.AreEqual(5, pagesCrawledCount);
}
[Test]
public void Crawl_CrawlTimeoutIs1Sec_TimesOut()
{
CrawlConfiguration configuration = new CrawlConfiguration();
configuration.CrawlTimeoutSeconds = 2;
int pagesCrawledCount = 0;
PoliteWebCrawler crawler = new PoliteWebCrawler(configuration, null, null, null, null, null, null, null, null);
crawler.PageCrawlCompletedAsync += (a, b) => pagesCrawledCount++;
CrawlResult result = crawler.Crawl(new Uri("http://localhost.fiddler:1111/"));
Assert.IsFalse(result.ErrorOccurred);
Assert.IsTrue(result.Elapsed.TotalSeconds < 8, "Took more than 8 seconds");
Assert.IsTrue(pagesCrawledCount < 2, "Crawled more than 2 pages");
}
[Test]
public void Crawl_Synchronous_CancellationTokenCancelled_StopsCrawl()
{
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
System.Timers.Timer timer = new System.Timers.Timer(800);
timer.Elapsed += (o, e) =>
{
cancellationTokenSource.Cancel();
timer.Stop();
timer.Dispose();
};
timer.Start();
PoliteWebCrawler crawler = new PoliteWebCrawler();
CrawlResult result = crawler.Crawl(new Uri("http://localhost.fiddler:1111/"), cancellationTokenSource);
Assert.IsTrue(result.ErrorOccurred);
Assert.IsTrue(result.ErrorException is OperationCanceledException);
}
[Test]
public void Crawl_Asynchronous_CancellationTokenCancelled_StopsCrawl()
{
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
System.Timers.Timer timer = new System.Timers.Timer(800);
timer.Elapsed += (o, e) =>
{
cancellationTokenSource.Cancel();
timer.Stop();
timer.Dispose();
};
timer.Start();
PoliteWebCrawler crawler = new PoliteWebCrawler();
Task<CrawlResult> task = Task.Factory.StartNew<CrawlResult>(() => crawler.Crawl(new Uri("http://localhost.fiddler:1111/"), cancellationTokenSource));
CrawlResult result = task.Result;
Assert.IsTrue(result.ErrorOccurred);
Assert.IsTrue(result.ErrorException is OperationCanceledException);
}
[Test]
public void Crawl_IsRateLimited()
{
new PageRequester(new CrawlConfiguration { UserAgentString = "aaa" }).MakeRequest(new Uri("http://localhost.fiddler:1111/PageGenerator/ClearCounters"));
CrawlConfiguration configuration = new CrawlConfiguration();
configuration.MaxPagesToCrawl = 3;
configuration.MinCrawlDelayPerDomainMilliSeconds = 1000; // 1 second * 2 pages = 2 (or more) seconds
int pagesCrawledCount = 0;
var crawler = new PoliteWebCrawler(configuration);
crawler.PageCrawlCompletedAsync += (a, b) => pagesCrawledCount++;
var uriToCrawl = new Uri("http://localhost.fiddler:1111/");
var start = DateTime.Now;
crawler.Crawl(uriToCrawl);
var elapsed = DateTime.Now - start;
Assert.GreaterOrEqual(elapsed.TotalMilliseconds, 2000);
Assert.AreEqual(3, pagesCrawledCount);
}
[Test]
public void Crawl_RetryEnabled_VerifyCrawlResultIsAsExpected()
{
new PageRequester(new CrawlConfiguration { UserAgentString = "aaa" }).MakeRequest(new Uri("http://localhost.fiddler:1111/PageGenerator/ClearCounters"));
CrawlConfiguration configuration = AbotConfigurationSectionHandler.LoadFromXml().Convert();
configuration.MaxRetryCount = 3;
configuration.MinRetryDelayInMilliseconds = 2000;
base.CrawlAndAssert(new PoliteWebCrawler(configuration));
}
protected override List<PageResult> GetExpectedCrawlResult()
{
List<PageResult> expectedCrawlResult = new List<PageResult>
{
new PageResult { Url = "http://localhost.fiddler:1111/", HttpStatusCode = 200},
new PageResult { Url = "http://localhost.fiddler:1111/PageGenerator/Generate?Status200Count=5&Status403Count=1&Status404Count=2&Status500Count=3&Status503Count=4&Page=1", HttpStatusCode = 200},
new PageResult { Url = "http://localhost.fiddler:1111/PageGenerator/Generate?Status200Count=5&Status403Count=1&Status404Count=2&Status500Count=3&Status503Count=4&Page=3", HttpStatusCode = 200},
new PageResult { Url = "http://localhost.fiddler:1111/PageGenerator/Generate?Status200Count=5&Status403Count=1&Status404Count=2&Status500Count=3&Status503Count=4&Page=2", HttpStatusCode = 200},
new PageResult { Url = "http://localhost.fiddler:1111/PageGenerator/Generate?Status200Count=5&Status403Count=1&Status404Count=2&Status500Count=3&Status503Count=4&Page=4", HttpStatusCode = 200},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status200/Page1", HttpStatusCode = 200},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status200/Page3", HttpStatusCode = 200},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status200/Page2", HttpStatusCode = 200},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status200/Page4", HttpStatusCode = 200},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status200/Page5", HttpStatusCode = 200},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status403/Page1", HttpStatusCode = 403},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status404/Page2", HttpStatusCode = 404},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status404/Page1", HttpStatusCode = 404},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status500/Page1", HttpStatusCode = 500},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status500/Page3", HttpStatusCode = 500},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status500/Page2", HttpStatusCode = 500},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status503/Page2", HttpStatusCode = 503},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status503/Page1", HttpStatusCode = 503},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status200/Page12", HttpStatusCode = 200},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status200/Page11", HttpStatusCode = 200},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status503/Page3", HttpStatusCode = 503},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status503/Page4", HttpStatusCode = 503},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status200/Page13", HttpStatusCode = 200},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status200/Page14", HttpStatusCode = 200},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status200/Page15", HttpStatusCode = 200},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status403/Page3", HttpStatusCode = 403},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status404/Page5", HttpStatusCode = 404},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status404/Page6", HttpStatusCode = 404},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status500/Page7", HttpStatusCode = 500},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status500/Page8", HttpStatusCode = 500},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status200/Page6", HttpStatusCode = 200},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status503/Page9", HttpStatusCode = 503},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status500/Page9", HttpStatusCode = 500},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status200/Page7", HttpStatusCode = 200},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status503/Page12", HttpStatusCode = 503},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status503/Page10", HttpStatusCode = 503},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status503/Page11", HttpStatusCode = 503},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status200/Page8", HttpStatusCode = 200},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status200/Page9", HttpStatusCode = 200},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status200/Page10", HttpStatusCode = 200},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status403/Page2", HttpStatusCode = 403},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status404/Page3", HttpStatusCode = 404},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status404/Page4", HttpStatusCode = 404},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status500/Page4", HttpStatusCode = 500},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status500/Page5", HttpStatusCode = 500},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status200/Page16", HttpStatusCode = 200},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status500/Page6", HttpStatusCode = 500},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status503/Page6", HttpStatusCode = 503},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status503/Page5", HttpStatusCode = 503},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status200/Page17", HttpStatusCode = 200},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status200/Page18", HttpStatusCode = 200},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status200/Page19", HttpStatusCode = 200},
new PageResult { Url = "http://yahoo.com/", HttpStatusCode = 200},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status503/Page8", HttpStatusCode = 503},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status200/Page20", HttpStatusCode = 200},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status503/Page7", HttpStatusCode = 503},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status403/Page4", HttpStatusCode = 403},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status404/Page7", HttpStatusCode = 404},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status500/Page11", HttpStatusCode = 500},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status500/Page10", HttpStatusCode = 500},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status404/Page8", HttpStatusCode = 404},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status503/Page13", HttpStatusCode = 503},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status500/Page12", HttpStatusCode = 500},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status503/Page14", HttpStatusCode = 503},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status503/Page15", HttpStatusCode = 503},
new PageResult { Url = "http://localhost.fiddler:1111/HttpResponse/Status503/Page16", HttpStatusCode = 503},
new PageResult { Url = "http://zoogle.com/", HttpStatusCode = 200}
};
return expectedCrawlResult;
}
}
}