Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(450)

Side by Side Diff: net/proxy/proxy_resolver_v8_tracing_unittest.cc

Issue 11885009: Improve performance of proxy resolver by tracing DNS dependencies. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Address mmneke comments Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "net/proxy/proxy_resolver_v8_tracing.h"
6
7 #include "base/file_util.h"
8 #include "base/message_loop.h"
9 #include "base/path_service.h"
10 #include "base/stl_util.h"
11 #include "base/string_util.h"
12 #include "base/stringprintf.h"
13 #include "base/synchronization/waitable_event.h"
14 #include "base/threading/platform_thread.h"
15 #include "base/utf_string_conversions.h"
16 #include "base/values.h"
17 #include "googleurl/src/gurl.h"
18 #include "net/base/host_cache.h"
19 #include "net/base/mock_host_resolver.h"
20 #include "net/base/net_errors.h"
21 #include "net/base/net_log.h"
22 #include "net/base/net_log_unittest.h"
23 #include "net/base/test_completion_callback.h"
24 #include "net/proxy/proxy_info.h"
25 #include "net/proxy/proxy_resolver_error_observer.h"
26 #include "testing/gtest/include/gtest/gtest.h"
27
28 namespace net {
29
30 namespace {
31
32 class ProxyResolverV8TracingTest : public testing::Test {
33 public:
34 virtual void TearDown() OVERRIDE {
35 // Drain any pending messages, which may be left over from cancellation.
36 // This way they get reliably run as part of the current test, rather than
37 // spilling into the next test's execution.
38 MessageLoop::current()->RunUntilIdle();
39 }
40 };
41
42 scoped_refptr<ProxyResolverScriptData> LoadScriptData(const char* filename) {
43 FilePath path;
44 PathService::Get(base::DIR_SOURCE_ROOT, &path);
45 path = path.AppendASCII("net");
46 path = path.AppendASCII("data");
47 path = path.AppendASCII("proxy_resolver_v8_tracing_unittest");
48 path = path.AppendASCII(filename);
49
50 // Try to read the file from disk.
51 std::string file_contents;
52 bool ok = file_util::ReadFileToString(path, &file_contents);
53
54 // If we can't load the file from disk, something is misconfigured.
55 EXPECT_TRUE(ok) << "Failed to read file: " << path.value();
56
57 // Load the PAC script into the ProxyResolver.
58 return ProxyResolverScriptData::FromUTF8(file_contents);
59 }
60
61 void InitResolver(ProxyResolverV8Tracing* resolver, const char* filename) {
62 TestCompletionCallback callback;
63 int rv =
64 resolver->SetPacScript(LoadScriptData(filename), callback.callback());
65 EXPECT_EQ(ERR_IO_PENDING, rv);
66 EXPECT_EQ(OK, callback.WaitForResult());
67 }
68
69 class MockErrorObserver : public ProxyResolverErrorObserver {
70 public:
71 MockErrorObserver() : event_(true, false) {}
72
73 virtual void OnPACScriptError(int line_number,
74 const string16& error) OVERRIDE {
75 {
76 base::AutoLock l(lock_);
77 output += StringPrintf("Error: line %d: %s\n", line_number,
78 UTF16ToASCII(error).c_str());
79 }
80 event_.Signal();
81 }
82
83 std::string GetOutput() {
84 base::AutoLock l(lock_);
85 return output;
86 }
87
88 void WaitForOutput() {
89 event_.Wait();
90 }
91
92 private:
93 base::Lock lock_;
94 std::string output;
95
96 base::WaitableEvent event_;
97 };
98
99 TEST_F(ProxyResolverV8TracingTest, Simple) {
100 CapturingNetLog log;
101 CapturingBoundNetLog request_log;
102 MockCachingHostResolver host_resolver;
103 MockErrorObserver* error_observer = new MockErrorObserver;
104 ProxyResolverV8Tracing resolver(&host_resolver, error_observer, &log);
105
106 InitResolver(&resolver, "simple.js");
107
108 TestCompletionCallback callback;
109 ProxyInfo proxy_info;
110
111 int rv = resolver.GetProxyForURL(
112 GURL("http://foo/"), &proxy_info, callback.callback(),
113 NULL, request_log.bound());
114
115 EXPECT_EQ(ERR_IO_PENDING, rv);
116 EXPECT_EQ(OK, callback.WaitForResult());
117
118 EXPECT_EQ("foo:99", proxy_info.proxy_server().ToURI());
119
120 EXPECT_EQ(0u, host_resolver.num_resolve());
121
122 // There were no errors.
123 EXPECT_EQ("", error_observer->GetOutput());
124
125 // Check the NetLogs -- nothing was logged.
126 EXPECT_EQ(0u, log.GetSize());
127 EXPECT_EQ(0u, request_log.GetSize());
128 }
129
130 TEST_F(ProxyResolverV8TracingTest, JavascriptError) {
131 CapturingNetLog log;
132 CapturingBoundNetLog request_log;
133 MockCachingHostResolver host_resolver;
134 MockErrorObserver* error_observer = new MockErrorObserver;
135 ProxyResolverV8Tracing resolver(&host_resolver, error_observer, &log);
136
137 InitResolver(&resolver, "error.js");
138
139 TestCompletionCallback callback;
140 ProxyInfo proxy_info;
141
142 int rv = resolver.GetProxyForURL(
143 GURL("http://throw-an-error/"), &proxy_info, callback.callback(), NULL,
144 request_log.bound());
145
146 EXPECT_EQ(ERR_IO_PENDING, rv);
147 EXPECT_EQ(ERR_PAC_SCRIPT_FAILED, callback.WaitForResult());
148
149 EXPECT_EQ(0u, host_resolver.num_resolve());
150
151 EXPECT_EQ("Error: line 5: Uncaught TypeError: Cannot call method 'split' "
152 "of null\n", error_observer->GetOutput());
153
154 // Check the NetLogs -- there was 1 alert and 1 javascript error, and they
155 // were output to both the global log, and per-request log.
156 CapturingNetLog::CapturedEntryList entries_list[2];
157 log.GetEntries(&entries_list[0]);
158 request_log.GetEntries(&entries_list[1]);
159
160 for (size_t list_i = 0; list_i < arraysize(entries_list); list_i++) {
161 const CapturingNetLog::CapturedEntryList& entries = entries_list[list_i];
162 EXPECT_EQ(2u, entries.size());
163 EXPECT_TRUE(
164 LogContainsEvent(entries, 0, NetLog::TYPE_PAC_JAVASCRIPT_ALERT,
165 NetLog::PHASE_NONE));
166 EXPECT_TRUE(
167 LogContainsEvent(entries, 1, NetLog::TYPE_PAC_JAVASCRIPT_ERROR,
168 NetLog::PHASE_NONE));
169
170 EXPECT_EQ("{\"message\":\"Prepare to DIE!\"}", entries[0].GetParamsJson());
171 EXPECT_EQ("{\"line_number\":5,\"message\":\"Uncaught TypeError: Cannot "
172 "call method 'split' of null\"}", entries[1].GetParamsJson());
173 }
174 }
175
176 TEST_F(ProxyResolverV8TracingTest, TooManyAlerts) {
177 CapturingNetLog log;
178 CapturingBoundNetLog request_log;
179 MockCachingHostResolver host_resolver;
180 MockErrorObserver* error_observer = new MockErrorObserver;
181 ProxyResolverV8Tracing resolver(&host_resolver, error_observer, &log);
182
183 InitResolver(&resolver, "too_many_alerts.js");
184
185 TestCompletionCallback callback;
186 ProxyInfo proxy_info;
187
188 int rv = resolver.GetProxyForURL(
189 GURL("http://foo/"),
190 &proxy_info,
191 callback.callback(),
192 NULL,
193 request_log.bound());
194
195 EXPECT_EQ(ERR_IO_PENDING, rv);
196 EXPECT_EQ(OK, callback.WaitForResult());
197
198 // Iteration1 does a DNS resolve
199 // Iteration2 exceeds the alert buffer
200 // Iteration3 runs in blocking mode and completes
201 EXPECT_EQ("foo:3", proxy_info.proxy_server().ToURI());
202
203 EXPECT_EQ(1u, host_resolver.num_resolve());
204
205 // No errors.
206 EXPECT_EQ("", error_observer->GetOutput());
207
208 // Check the NetLogs -- the script generated 50 alerts, which were mirrored
209 // to both the global and per-request logs.
210 CapturingNetLog::CapturedEntryList entries_list[2];
211 log.GetEntries(&entries_list[0]);
212 request_log.GetEntries(&entries_list[1]);
213
214 for (size_t list_i = 0; list_i < arraysize(entries_list); list_i++) {
215 const CapturingNetLog::CapturedEntryList& entries = entries_list[list_i];
216 EXPECT_EQ(50u, entries.size());
217 for (size_t i = 0; i < entries.size(); ++i) {
218 ASSERT_TRUE(
219 LogContainsEvent(entries, i, NetLog::TYPE_PAC_JAVASCRIPT_ALERT,
220 NetLog::PHASE_NONE));
221 }
222 }
223 }
224
225 // Verify that buffered alerts cannot grow unboundedly, even when the message is
226 // empty string.
227 TEST_F(ProxyResolverV8TracingTest, TooManyEmptyAlerts) {
228 CapturingNetLog log;
229 CapturingBoundNetLog request_log;
230 MockCachingHostResolver host_resolver;
231 MockErrorObserver* error_observer = new MockErrorObserver;
232 ProxyResolverV8Tracing resolver(&host_resolver, error_observer, &log);
233
234 InitResolver(&resolver, "too_many_empty_alerts.js");
235
236 TestCompletionCallback callback;
237 ProxyInfo proxy_info;
238
239 int rv = resolver.GetProxyForURL(
240 GURL("http://foo/"),
241 &proxy_info,
242 callback.callback(),
243 NULL,
244 request_log.bound());
245
246 EXPECT_EQ(ERR_IO_PENDING, rv);
247 EXPECT_EQ(OK, callback.WaitForResult());
248
249 EXPECT_EQ("foo:3", proxy_info.proxy_server().ToURI());
250
251 EXPECT_EQ(1u, host_resolver.num_resolve());
252
253 // No errors.
254 EXPECT_EQ("", error_observer->GetOutput());
255
256 // Check the NetLogs -- the script generated 50 alerts, which were mirrored
257 // to both the global and per-request logs.
258 CapturingNetLog::CapturedEntryList entries_list[2];
259 log.GetEntries(&entries_list[0]);
260 request_log.GetEntries(&entries_list[1]);
261
262 for (size_t list_i = 0; list_i < arraysize(entries_list); list_i++) {
263 const CapturingNetLog::CapturedEntryList& entries = entries_list[list_i];
264 EXPECT_EQ(1000u, entries.size());
265 for (size_t i = 0; i < entries.size(); ++i) {
266 ASSERT_TRUE(
267 LogContainsEvent(entries, i, NetLog::TYPE_PAC_JAVASCRIPT_ALERT,
268 NetLog::PHASE_NONE));
269 }
270 }
271 }
272
273 // This test runs a PAC script that issues a sequence of DNS resolves. The test
274 // verifies the final result, and that the underlying DNS resolver received
275 // the correct set of queries.
276 TEST_F(ProxyResolverV8TracingTest, Dns) {
277 CapturingNetLog log;
278 CapturingBoundNetLog request_log;
279 MockCachingHostResolver host_resolver;
280 MockErrorObserver* error_observer = new MockErrorObserver;
281 ProxyResolverV8Tracing resolver(&host_resolver, error_observer, &log);
282
283 host_resolver.rules()->AddRule("host1", "166.155.144.11");
284 host_resolver.rules()->AddSimulatedFailure("host2");
285 host_resolver.rules()->AddRule("host3", "166.155.144.33");
286 host_resolver.rules()->AddRule("host4", "166.155.144.44");
287 host_resolver.rules()->AddRule("host5", "166.155.144.55");
288 host_resolver.rules()->AddRule("*", "122.133.144.155");
289
290 InitResolver(&resolver, "dns.js");
291
292 TestCompletionCallback callback;
293 ProxyInfo proxy_info;
294
295 int rv = resolver.GetProxyForURL(
296 GURL("http://foo/"),
297 &proxy_info,
298 callback.callback(),
299 NULL,
300 request_log.bound());
301
302 EXPECT_EQ(ERR_IO_PENDING, rv);
303 EXPECT_EQ(OK, callback.WaitForResult());
304
305 // The test does 11 DNS resolution, however only 5 of them are unique.
306 EXPECT_EQ(5u, host_resolver.num_resolve());
307
308 const char* kExpectedResult =
309 "122.133.144.155-" // myIpAddress()
310 "null-" // dnsResolve('')
311 "166.155.144.11-" // dnsResolve('host1')
312 "null-" // dnsResolve('host2')
313 "166.155.144.33-" // dnsResolve('host3')
314 "122.133.144.155-" // myIpAddress()
315 "166.155.144.33-" // dnsResolve('host3')
316 "166.155.144.11-" // dnsResolve('host1')
317 "122.133.144.155-" // myIpAddress()
318 "null-" // dnsResolve('host2')
319 "166.155.144.44" // dnsResolve('host4')
320 ":99";
321
322 EXPECT_EQ(kExpectedResult, proxy_info.proxy_server().ToURI());
323
324 // No errors.
325 EXPECT_EQ("", error_observer->GetOutput());
326
327 // Check the NetLogs -- the script generated 1 alert, mirrored to both
328 // the per-request and global logs.
329 CapturingNetLog::CapturedEntryList entries_list[2];
330 log.GetEntries(&entries_list[0]);
331 request_log.GetEntries(&entries_list[1]);
332
333 for (size_t list_i = 0; list_i < arraysize(entries_list); list_i++) {
334 const CapturingNetLog::CapturedEntryList& entries = entries_list[list_i];
335 EXPECT_EQ(1u, entries.size());
336 EXPECT_TRUE(
337 LogContainsEvent(entries, 0, NetLog::TYPE_PAC_JAVASCRIPT_ALERT,
338 NetLog::PHASE_NONE));
339 EXPECT_EQ("{\"message\":\"iteration: 5\"}", entries[0].GetParamsJson());
340 }
341 }
342
343 // This test runs a PAC script that does "myIpAddress()" followed by
344 // "dnsResolve()". This requires 2 restarts. However once the HostResolver's
345 // cache is warmed, subsequent calls should take 0 restarts.
346 TEST_F(ProxyResolverV8TracingTest, DnsChecksCache) {
347 CapturingNetLog log;
348 CapturingBoundNetLog request_log;
349 MockCachingHostResolver host_resolver;
350 MockErrorObserver* error_observer = new MockErrorObserver;
351 ProxyResolverV8Tracing resolver(&host_resolver, error_observer, &log);
352
353 host_resolver.rules()->AddRule("foopy", "166.155.144.11");
354 host_resolver.rules()->AddRule("*", "122.133.144.155");
355
356 InitResolver(&resolver, "simple_dns.js");
357
358 TestCompletionCallback callback1;
359 TestCompletionCallback callback2;
360 ProxyInfo proxy_info;
361
362 int rv = resolver.GetProxyForURL(
363 GURL("http://foopy/req1"),
364 &proxy_info,
365 callback1.callback(),
366 NULL,
367 request_log.bound());
368
369 EXPECT_EQ(ERR_IO_PENDING, rv);
370 EXPECT_EQ(OK, callback1.WaitForResult());
371
372 // The test does 2 DNS resolutions.
373 EXPECT_EQ(2u, host_resolver.num_resolve());
374
375 // The first request took 2 restarts, hence on g_iteration=3.
376 EXPECT_EQ("166.155.144.11:3", proxy_info.proxy_server().ToURI());
377
378 rv = resolver.GetProxyForURL(
379 GURL("http://foopy/req2"),
380 &proxy_info,
381 callback2.callback(),
382 NULL,
383 request_log.bound());
384
385 EXPECT_EQ(ERR_IO_PENDING, rv);
386 EXPECT_EQ(OK, callback2.WaitForResult());
387
388 EXPECT_EQ(4u, host_resolver.num_resolve());
389
390 // This time no restarts were required, so g_iteration incremented by 1.
391 EXPECT_EQ("166.155.144.11:4", proxy_info.proxy_server().ToURI());
392
393 // No errors.
394 EXPECT_EQ("", error_observer->GetOutput());
395
396 EXPECT_EQ(0u, log.GetSize());
397 EXPECT_EQ(0u, request_log.GetSize());
398 }
399
400 // This test runs a weird PAC script that was designed to defeat the DNS tracing
401 // optimization. The proxy resolver should detect the inconsistency and
402 // fall-back to synchronous mode execution.
403 TEST_F(ProxyResolverV8TracingTest, FallBackToSynchronous1) {
404 CapturingNetLog log;
405 CapturingBoundNetLog request_log;
406 MockCachingHostResolver host_resolver;
407 MockErrorObserver* error_observer = new MockErrorObserver;
408 ProxyResolverV8Tracing resolver(&host_resolver, error_observer, &log);
409
410 host_resolver.rules()->AddRule("host1", "166.155.144.11");
411 host_resolver.rules()->AddRule("crazy4", "133.199.111.4");
412 host_resolver.rules()->AddRule("*", "122.133.144.155");
413
414 InitResolver(&resolver, "global_sideffects1.js");
415
416 TestCompletionCallback callback;
417 ProxyInfo proxy_info;
418
419 int rv = resolver.GetProxyForURL(
420 GURL("http://foo/"), &proxy_info, callback.callback(), NULL,
421 request_log.bound());
422 EXPECT_EQ(ERR_IO_PENDING, rv);
423 EXPECT_EQ(OK, callback.WaitForResult());
424
425 // The script itself only does 2 DNS resolves per execution, however it
426 // constructs the hostname using a global counter which changes on each
427 // invocation.
428 EXPECT_EQ(3u, host_resolver.num_resolve());
429
430 EXPECT_EQ("166.155.144.11-133.199.111.4:100",
431 proxy_info.proxy_server().ToURI());
432
433 // No errors.
434 EXPECT_EQ("", error_observer->GetOutput());
435
436 // Check the NetLogs -- the script generated 1 alert, mirrored to both
437 // the per-request and global logs.
438 CapturingNetLog::CapturedEntryList entries_list[2];
439 log.GetEntries(&entries_list[0]);
440 request_log.GetEntries(&entries_list[1]);
441
442 for (size_t list_i = 0; list_i < arraysize(entries_list); list_i++) {
443 const CapturingNetLog::CapturedEntryList& entries = entries_list[list_i];
444 EXPECT_EQ(1u, entries.size());
445 EXPECT_TRUE(
446 LogContainsEvent(entries, 0, NetLog::TYPE_PAC_JAVASCRIPT_ALERT,
447 NetLog::PHASE_NONE));
448 EXPECT_EQ("{\"message\":\"iteration: 4\"}", entries[0].GetParamsJson());
449 }
450 }
451
452 // This test runs a weird PAC script that was designed to defeat the DNS tracing
453 // optimization. The proxy resolver should detect the inconsistency and
454 // fall-back to synchronous mode execution.
455 TEST_F(ProxyResolverV8TracingTest, FallBackToSynchronous2) {
456 CapturingNetLog log;
457 CapturingBoundNetLog request_log;
458 MockCachingHostResolver host_resolver;
459 MockErrorObserver* error_observer = new MockErrorObserver;
460 ProxyResolverV8Tracing resolver(&host_resolver, error_observer, &log);
461
462 host_resolver.rules()->AddRule("host1", "166.155.144.11");
463 host_resolver.rules()->AddRule("host2", "166.155.144.22");
464 host_resolver.rules()->AddRule("host3", "166.155.144.33");
465 host_resolver.rules()->AddRule("host4", "166.155.144.44");
466 host_resolver.rules()->AddRule("*", "122.133.144.155");
467
468 InitResolver(&resolver, "global_sideffects2.js");
469
470 TestCompletionCallback callback;
471 ProxyInfo proxy_info;
472
473 int rv = resolver.GetProxyForURL(
474 GURL("http://foo/"), &proxy_info, callback.callback(), NULL,
475 request_log.bound());
476 EXPECT_EQ(ERR_IO_PENDING, rv);
477 EXPECT_EQ(OK, callback.WaitForResult());
478
479 EXPECT_EQ(3u, host_resolver.num_resolve());
480
481 EXPECT_EQ("166.155.144.44:100", proxy_info.proxy_server().ToURI());
482
483 // No errors.
484 EXPECT_EQ("", error_observer->GetOutput());
485
486 // Check the NetLogs -- nothing was logged.
487 EXPECT_EQ(0u, log.GetSize());
488 EXPECT_EQ(0u, request_log.GetSize());
489 }
490
491 // This test runs a weird PAC script that yields a never ending sequence
492 // of DNS resolves when restarting. Running it will hit the maximum
493 // DNS resolves per request limit (20) after which every DNS resolve will
494 // fail.
495 TEST_F(ProxyResolverV8TracingTest, InfiniteDNSSequence) {
496 CapturingNetLog log;
497 CapturingBoundNetLog request_log;
498 MockCachingHostResolver host_resolver;
499 MockErrorObserver* error_observer = new MockErrorObserver;
500 ProxyResolverV8Tracing resolver(&host_resolver, error_observer, &log);
501
502 host_resolver.rules()->AddRule("host*", "166.155.144.11");
503 host_resolver.rules()->AddRule("*", "122.133.144.155");
504
505 InitResolver(&resolver, "global_sideffects3.js");
506
507 TestCompletionCallback callback;
508 ProxyInfo proxy_info;
509
510 int rv = resolver.GetProxyForURL(
511 GURL("http://foo/"), &proxy_info, callback.callback(), NULL,
512 request_log.bound());
513 EXPECT_EQ(ERR_IO_PENDING, rv);
514 EXPECT_EQ(OK, callback.WaitForResult());
515
516 EXPECT_EQ(20u, host_resolver.num_resolve());
517
518 EXPECT_EQ(
519 "166.155.144.11-166.155.144.11-166.155.144.11-166.155.144.11-"
520 "166.155.144.11-166.155.144.11-166.155.144.11-166.155.144.11-"
521 "166.155.144.11-166.155.144.11-166.155.144.11-166.155.144.11-"
522 "166.155.144.11-166.155.144.11-166.155.144.11-166.155.144.11-"
523 "166.155.144.11-166.155.144.11-166.155.144.11-166.155.144.11-"
524 "null:21", proxy_info.proxy_server().ToURI());
525
526 // No errors.
527 EXPECT_EQ("", error_observer->GetOutput());
528
529 // Check the NetLogs -- 1 alert was logged.
530 EXPECT_EQ(1u, log.GetSize());
531 EXPECT_EQ(1u, request_log.GetSize());
532 }
533
534 // This test runs a weird PAC script that yields a never ending sequence
535 // of DNS resolves when restarting. Running it will hit the maximum
536 // DNS resolves per request limit (20) after which every DNS resolve will
537 // fail.
538 TEST_F(ProxyResolverV8TracingTest, InfiniteDNSSequence2) {
539 CapturingNetLog log;
540 CapturingBoundNetLog request_log;
541 MockCachingHostResolver host_resolver;
542 MockErrorObserver* error_observer = new MockErrorObserver;
543 ProxyResolverV8Tracing resolver(&host_resolver, error_observer, &log);
544
545 host_resolver.rules()->AddRule("host*", "166.155.144.11");
546 host_resolver.rules()->AddRule("*", "122.133.144.155");
547
548 InitResolver(&resolver, "global_sideffects4.js");
549
550 TestCompletionCallback callback;
551 ProxyInfo proxy_info;
552
553 int rv = resolver.GetProxyForURL(
554 GURL("http://foo/"), &proxy_info, callback.callback(), NULL,
555 request_log.bound());
556 EXPECT_EQ(ERR_IO_PENDING, rv);
557 EXPECT_EQ(OK, callback.WaitForResult());
558
559 EXPECT_EQ(20u, host_resolver.num_resolve());
560
561 EXPECT_EQ("null21:34", proxy_info.proxy_server().ToURI());
562
563 // No errors.
564 EXPECT_EQ("", error_observer->GetOutput());
565
566 // Check the NetLogs -- 1 alert was logged.
567 EXPECT_EQ(1u, log.GetSize());
568 EXPECT_EQ(1u, request_log.GetSize());
569 }
570
571 // Tests a PAC script which does DNS resolves during initialization.
572 TEST_F(ProxyResolverV8TracingTest, DnsDuringInit) {
573 CapturingNetLog log;
574 CapturingBoundNetLog request_log;
575 MockCachingHostResolver host_resolver;
576 MockErrorObserver* error_observer = new MockErrorObserver;
577 ProxyResolverV8Tracing resolver(&host_resolver, error_observer, &log);
578
579 host_resolver.rules()->AddRule("host1", "91.13.12.1");
580 host_resolver.rules()->AddRule("host2", "91.13.12.2");
581
582 InitResolver(&resolver, "dns_during_init.js");
583
584 // Initialization did 2 dnsResolves.
585 EXPECT_EQ(2u, host_resolver.num_resolve());
586
587 host_resolver.rules()->ClearRules();
588 host_resolver.GetHostCache()->clear();
589
590 host_resolver.rules()->AddRule("host1", "145.88.13.3");
591 host_resolver.rules()->AddRule("host2", "137.89.8.45");
592
593 TestCompletionCallback callback;
594 ProxyInfo proxy_info;
595
596 int rv = resolver.GetProxyForURL(
597 GURL("http://foo/"), &proxy_info, callback.callback(), NULL,
598 request_log.bound());
599 EXPECT_EQ(ERR_IO_PENDING, rv);
600 EXPECT_EQ(OK, callback.WaitForResult());
601
602 // Fetched host1 and host2 again, since the ones done during initialization
603 // should not have been cached.
604 EXPECT_EQ(4u, host_resolver.num_resolve());
605
606 EXPECT_EQ("91.13.12.1-91.13.12.2-145.88.13.3-137.89.8.45:99",
607 proxy_info.proxy_server().ToURI());
608
609 // Check the NetLogs -- the script generated 2 alerts during initialization.
610 EXPECT_EQ(0u, request_log.GetSize());
611 CapturingNetLog::CapturedEntryList entries;
612 log.GetEntries(&entries);
613
614 ASSERT_EQ(2u, entries.size());
615 EXPECT_TRUE(
616 LogContainsEvent(entries, 0, NetLog::TYPE_PAC_JAVASCRIPT_ALERT,
617 NetLog::PHASE_NONE));
618 EXPECT_TRUE(
619 LogContainsEvent(entries, 1, NetLog::TYPE_PAC_JAVASCRIPT_ALERT,
620 NetLog::PHASE_NONE));
621
622 EXPECT_EQ("{\"message\":\"Watsup\"}", entries[0].GetParamsJson());
623 EXPECT_EQ("{\"message\":\"Watsup2\"}", entries[1].GetParamsJson());
624 }
625
626 void CrashCallback(int) {
627 // Be extra sure that if the callback ever gets invoked, the test will fail.
628 CHECK(false);
629 }
630
631 // Start some requests, cancel them all, and then destroy the resolver.
632 // Note the execution order for this test can vary. Since multiple
633 // threads are involved, the cancellation may be received a different
634 // times.
635 TEST_F(ProxyResolverV8TracingTest, CancelAll) {
636 MockCachingHostResolver host_resolver;
637 MockErrorObserver* error_observer = new MockErrorObserver;
638 ProxyResolverV8Tracing resolver(&host_resolver, error_observer, NULL);
639
640 host_resolver.rules()->AddSimulatedFailure("*");
641
642 InitResolver(&resolver, "dns.js");
643
644 const size_t kNumRequests = 5;
645 ProxyInfo proxy_info[kNumRequests];
646 ProxyResolver::RequestHandle request[kNumRequests];
647
648 for (size_t i = 0; i < kNumRequests; ++i) {
649 int rv = resolver.GetProxyForURL(
650 GURL("http://foo/"), &proxy_info[i],
651 base::Bind(&CrashCallback), &request[i], BoundNetLog());
652 EXPECT_EQ(ERR_IO_PENDING, rv);
653 }
654
655 for (size_t i = 0; i < kNumRequests; ++i) {
656 resolver.CancelRequest(request[i]);
657 }
658 }
659
660 // Note the execution order for this test can vary. Since multiple
661 // threads are involved, the cancellation may be received a different
662 // times.
663 TEST_F(ProxyResolverV8TracingTest, CancelSome) {
664 MockCachingHostResolver host_resolver;
665 MockErrorObserver* error_observer = new MockErrorObserver;
666 ProxyResolverV8Tracing resolver(&host_resolver, error_observer, NULL);
667
668 host_resolver.rules()->AddSimulatedFailure("*");
669
670 InitResolver(&resolver, "dns.js");
671
672 ProxyInfo proxy_info1;
673 ProxyInfo proxy_info2;
674 ProxyResolver::RequestHandle request1;
675 ProxyResolver::RequestHandle request2;
676 TestCompletionCallback callback;
677
678 int rv = resolver.GetProxyForURL(
679 GURL("http://foo/"), &proxy_info1,
680 base::Bind(&CrashCallback), &request1, BoundNetLog());
681 EXPECT_EQ(ERR_IO_PENDING, rv);
682
683 rv = resolver.GetProxyForURL(
684 GURL("http://foo/"), &proxy_info2,
685 callback.callback(), &request2, BoundNetLog());
686 EXPECT_EQ(ERR_IO_PENDING, rv);
687
688 resolver.CancelRequest(request1);
689
690 EXPECT_EQ(OK, callback.WaitForResult());
691 }
692
693 // Cancel a request after it has finished running on the worker thread, and has
694 // posted a task the completion task back to origin thread.
695 TEST_F(ProxyResolverV8TracingTest, CancelWhilePendingCompletionTask) {
696 MockCachingHostResolver host_resolver;
697 MockErrorObserver* error_observer = new MockErrorObserver;
698 ProxyResolverV8Tracing resolver(&host_resolver, error_observer, NULL);
699
700 host_resolver.rules()->AddSimulatedFailure("*");
701
702 InitResolver(&resolver, "error.js");
703
704 ProxyInfo proxy_info1;
705 ProxyInfo proxy_info2;
706 ProxyInfo proxy_info3;
707 ProxyResolver::RequestHandle request1;
708 ProxyResolver::RequestHandle request2;
709 ProxyResolver::RequestHandle request3;
710 TestCompletionCallback callback;
711
712 int rv = resolver.GetProxyForURL(
713 GURL("http://foo/"), &proxy_info1,
714 base::Bind(&CrashCallback), &request1, BoundNetLog());
715 EXPECT_EQ(ERR_IO_PENDING, rv);
716
717 rv = resolver.GetProxyForURL(
718 GURL("http://throw-an-error/"), &proxy_info2,
719 callback.callback(), &request2, BoundNetLog());
720 EXPECT_EQ(ERR_IO_PENDING, rv);
721
722 // Wait until the first request has finished running on the worker thread.
723 // (The second request will output an error).
724 error_observer->WaitForOutput();
725
726 // Cancel the first request, while it has a pending completion task on
727 // the origin thread.
728 resolver.CancelRequest(request1);
729
730 EXPECT_EQ(ERR_PAC_SCRIPT_FAILED, callback.WaitForResult());
731
732 // Start another request, to make sure it is able to complete.
733 rv = resolver.GetProxyForURL(
734 GURL("http://i-have-no-idea-what-im-doing/"), &proxy_info3,
735 callback.callback(), &request3, BoundNetLog());
736 EXPECT_EQ(ERR_IO_PENDING, rv);
737
738 EXPECT_EQ(OK, callback.WaitForResult());
739
740 EXPECT_EQ("i-approve-this-message:42",
741 proxy_info3.proxy_server().ToURI());
742 }
743
744 // This implementation of HostResolver allows blocking until a resolve request
745 // has been received. The resolve requests it receives will never be completed.
746 class BlockableHostResolver : public HostResolver {
747 public:
748 BlockableHostResolver()
749 : num_cancelled_requests_(0), waiting_for_resolve_(false) {}
750
751 virtual int Resolve(const RequestInfo& info,
752 AddressList* addresses,
753 const CompletionCallback& callback,
754 RequestHandle* out_req,
755 const BoundNetLog& net_log) OVERRIDE {
756 EXPECT_FALSE(callback.is_null());
757 EXPECT_TRUE(out_req);
758 *out_req = reinterpret_cast<RequestHandle*>(1); // Magic value.
759
760 if (!action_.is_null())
761 action_.Run();
762
763 // Indicate to the caller that a request was received.
764 EXPECT_TRUE(waiting_for_resolve_);
765 MessageLoop::current()->Quit();
766
767 // Return ERR_IO_PENDING as this request will NEVER be completed.
768 // Expectation is for the caller to later cancel the request.
769 return ERR_IO_PENDING;
770 }
771
772 virtual int ResolveFromCache(const RequestInfo& info,
773 AddressList* addresses,
774 const BoundNetLog& net_log) OVERRIDE {
775 NOTREACHED();
776 return ERR_DNS_CACHE_MISS;
777 }
778
779 virtual void CancelRequest(RequestHandle req) OVERRIDE {
780 EXPECT_EQ(reinterpret_cast<RequestHandle*>(1), req);
781 num_cancelled_requests_++;
782 }
783
784 void SetAction(const base::Callback<void(void)>& action) {
785 action_ = action;
786 }
787
788 // Waits until Resolve() has been called.
789 void WaitUntilRequestIsReceived() {
790 waiting_for_resolve_ = true;
791 MessageLoop::current()->Run();
792 DCHECK(waiting_for_resolve_);
793 waiting_for_resolve_ = false;
794 }
795
796 int num_cancelled_requests() const {
797 return num_cancelled_requests_;
798 }
799
800 private:
801 int num_cancelled_requests_;
802 bool waiting_for_resolve_;
803 base::Callback<void(void)> action_;
804 };
805
806 // This cancellation test exercises a more predictable cancellation codepath --
807 // when the request has an outstanding DNS request in flight.
808 TEST_F(ProxyResolverV8TracingTest, CancelWhileOutstandingNonBlockingDns) {
809 BlockableHostResolver host_resolver;
810 MockErrorObserver* error_observer = new MockErrorObserver;
811 ProxyResolverV8Tracing resolver(&host_resolver, error_observer, NULL);
812
813 InitResolver(&resolver, "dns.js");
814
815 ProxyInfo proxy_info1;
816 ProxyInfo proxy_info2;
817 ProxyResolver::RequestHandle request1;
818 ProxyResolver::RequestHandle request2;
819
820 int rv = resolver.GetProxyForURL(
821 GURL("http://foo/req1"), &proxy_info1,
822 base::Bind(&CrashCallback), &request1, BoundNetLog());
823
824 EXPECT_EQ(ERR_IO_PENDING, rv);
825
826 host_resolver.WaitUntilRequestIsReceived();
827
828 rv = resolver.GetProxyForURL(
829 GURL("http://foo/req2"), &proxy_info2,
830 base::Bind(&CrashCallback), &request2, BoundNetLog());
831
832 EXPECT_EQ(ERR_IO_PENDING, rv);
833
834 host_resolver.WaitUntilRequestIsReceived();
835
836 resolver.CancelRequest(request1);
837 resolver.CancelRequest(request2);
838
839 EXPECT_EQ(2, host_resolver.num_cancelled_requests());
840
841 // After leaving this scope, the ProxyResolver is destroyed.
842 // This should not cause any problems, as the outstanding work
843 // should have been cancelled.
844 }
845
846 // In non-blocking mode, the worker thread actually does block for
847 // a short time to see if the result is in the DNS cache. Test
848 // cancellation while the worker thread is waiting on this event.
849 TEST_F(ProxyResolverV8TracingTest, CancelWhileBlockedInNonBlockingDns) {
850 BlockableHostResolver host_resolver;
851 MockErrorObserver* error_observer = new MockErrorObserver;
852 ProxyResolverV8Tracing resolver(&host_resolver, error_observer, NULL);
853
854 InitResolver(&resolver, "dns.js");
855
856 ProxyInfo proxy_info;
857 ProxyResolver::RequestHandle request;
858
859 int rv = resolver.GetProxyForURL(
860 GURL("http://foo/"), &proxy_info,
861 base::Bind(&CrashCallback), &request, BoundNetLog());
862
863 EXPECT_EQ(ERR_IO_PENDING, rv);
864
865 host_resolver.SetAction(base::Bind(&ProxyResolverV8Tracing::CancelRequest,
866 base::Unretained(&resolver), request));
867
868 host_resolver.WaitUntilRequestIsReceived();
869
870 // At this point the host resolver ran Resolve(), and should have cancelled
871 // the request.
872
873 EXPECT_EQ(1, host_resolver.num_cancelled_requests());
874 }
875
876 // Cancel the request while there is a pending DNS request, however before
877 // the request is sent to the host resolver.
878 TEST_F(ProxyResolverV8TracingTest, CancelWhileBlockedInNonBlockingDns2) {
879 MockCachingHostResolver host_resolver;
880 MockErrorObserver* error_observer = new MockErrorObserver;
881 ProxyResolverV8Tracing resolver(&host_resolver, error_observer, NULL);
882
883 InitResolver(&resolver, "dns.js");
884
885 ProxyInfo proxy_info;
886 ProxyResolver::RequestHandle request;
887
888 int rv = resolver.GetProxyForURL(
889 GURL("http://foo/"), &proxy_info,
890 base::Bind(&CrashCallback), &request, BoundNetLog());
891
892 EXPECT_EQ(ERR_IO_PENDING, rv);
893
894 // Wait a bit, so the DNS task has hopefully been posted. The test will
895 // work whatever the delay is here, but it is most useful if the delay
896 // is large enough to allow a task to be posted back.
897 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10));
898 resolver.CancelRequest(request);
899
900 EXPECT_EQ(0u, host_resolver.num_resolve());
901 }
902
903 TEST_F(ProxyResolverV8TracingTest, CancelSetPacWhileOutstandingBlockingDns) {
904 BlockableHostResolver host_resolver;
905 MockErrorObserver* error_observer = new MockErrorObserver;
906
907 ProxyResolverV8Tracing resolver(&host_resolver, error_observer, NULL);
908
909 int rv =
910 resolver.SetPacScript(LoadScriptData("dns_during_init.js"),
911 base::Bind(&CrashCallback));
912 EXPECT_EQ(ERR_IO_PENDING, rv);
913
914 host_resolver.WaitUntilRequestIsReceived();
915
916 resolver.CancelSetPacScript();
917 EXPECT_EQ(1, host_resolver.num_cancelled_requests());
918 }
919
920 } // namespace
921
922 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698