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

Side by Side Diff: net/dns/single_request_host_resolver_unittest.cc

Issue 2212873002: Remove SingleHostResolver completely. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@SingleRemoveCallers
Patch Set: rebased to fix remote refs Created 4 years, 4 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
« no previous file with comments | « net/dns/single_request_host_resolver.cc ('k') | net/net.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2012 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/dns/single_request_host_resolver.h"
6
7 #include "base/macros.h"
8 #include "net/base/address_list.h"
9 #include "net/base/net_errors.h"
10 #include "net/base/test_completion_callback.h"
11 #include "net/dns/mock_host_resolver.h"
12 #include "net/log/net_log.h"
13 #include "net/test/gtest_util.h"
14 #include "testing/gmock/include/gmock/gmock.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16
17 using net::test::IsError;
18 using net::test::IsOk;
19
20 namespace net {
21
22 namespace {
23
24 // Helper class used by SingleRequestHostResolverTest.Cancel test.
25 // It checks that only one request is outstanding at a time, and that
26 // it is cancelled before the class is destroyed.
27 class HangingHostResolver : public HostResolver {
28 private:
29 class RequestImpl;
30
31 public:
32 HangingHostResolver() : outstanding_request_(NULL) {}
33
34 ~HangingHostResolver() override { EXPECT_TRUE(!has_outstanding_request()); }
35
36 bool has_outstanding_request() const {
37 return outstanding_request_ != NULL;
38 }
39
40 int Resolve(const RequestInfo& info,
41 RequestPriority priority,
42 AddressList* addresses,
43 const CompletionCallback& callback,
44 std::unique_ptr<Request>* request,
45 const BoundNetLog& net_log) override {
46 EXPECT_FALSE(has_outstanding_request());
47 outstanding_request_ = new RequestImpl(this);
48 request->reset(outstanding_request_);
49
50 // Never complete this request! Caller is expected to cancel it
51 // before destroying the resolver.
52 return ERR_IO_PENDING;
53 }
54
55 int ResolveFromCache(const RequestInfo& info,
56 AddressList* addresses,
57 const BoundNetLog& net_log) override {
58 NOTIMPLEMENTED();
59 return ERR_UNEXPECTED;
60 }
61
62 void RemoveRequest(RequestImpl* request) {
63 EXPECT_TRUE(has_outstanding_request());
64 EXPECT_EQ(outstanding_request_, request);
65 outstanding_request_ = nullptr;
66 }
67
68 private:
69 class RequestImpl : public HostResolver::Request {
70 public:
71 explicit RequestImpl(HangingHostResolver* resolver) : resolver_(resolver) {}
72
73 ~RequestImpl() override {
74 DCHECK(resolver_);
75 resolver_->RemoveRequest(this);
76 }
77
78 void ChangeRequestPriority(RequestPriority priority) override {}
79
80 private:
81 HangingHostResolver* resolver_;
82 };
83
84 HostResolver::Request* outstanding_request_;
85
86 DISALLOW_COPY_AND_ASSIGN(HangingHostResolver);
87 };
88
89 // Test that a regular end-to-end lookup returns the expected result.
90 TEST(SingleRequestHostResolverTest, NormalResolve) {
91 // Create a host resolver dependency that returns address "199.188.1.166"
92 // for resolutions of "watsup".
93 MockHostResolver resolver;
94 resolver.rules()->AddIPLiteralRule("watsup", "199.188.1.166", std::string());
95
96 SingleRequestHostResolver single_request_resolver(&resolver);
97
98 // Resolve "watsup:90" using our SingleRequestHostResolver.
99 AddressList addrlist;
100 TestCompletionCallback callback;
101 HostResolver::RequestInfo request(HostPortPair("watsup", 90));
102 int rv = single_request_resolver.Resolve(
103 request, DEFAULT_PRIORITY, &addrlist, callback.callback(), BoundNetLog());
104 EXPECT_THAT(rv, IsError(ERR_IO_PENDING));
105 EXPECT_THAT(callback.WaitForResult(), IsOk());
106
107 // Verify that the result is what we specified in the MockHostResolver.
108 ASSERT_FALSE(addrlist.empty());
109 EXPECT_EQ("199.188.1.166", addrlist.front().ToStringWithoutPort());
110 }
111
112 // Test that the Cancel() method cancels any outstanding request.
113 TEST(SingleRequestHostResolverTest, Cancel) {
114 HangingHostResolver resolver;
115
116 {
117 SingleRequestHostResolver single_request_resolver(&resolver);
118
119 // Resolve "watsup:90" using our SingleRequestHostResolver.
120 AddressList addrlist;
121 TestCompletionCallback callback;
122 HostResolver::RequestInfo request(HostPortPair("watsup", 90));
123 int rv = single_request_resolver.Resolve(request,
124 DEFAULT_PRIORITY,
125 &addrlist,
126 callback.callback(),
127 BoundNetLog());
128 EXPECT_THAT(rv, IsError(ERR_IO_PENDING));
129 EXPECT_TRUE(resolver.has_outstanding_request());
130 }
131
132 // Now that the SingleRequestHostResolver has been destroyed, the
133 // in-progress request should have been aborted.
134 EXPECT_FALSE(resolver.has_outstanding_request());
135 }
136
137 // Test that the Cancel() method is a no-op when there is no outstanding
138 // request.
139 TEST(SingleRequestHostResolverTest, CancelWhileNoPendingRequest) {
140 HangingHostResolver resolver;
141 SingleRequestHostResolver single_request_resolver(&resolver);
142 single_request_resolver.Cancel();
143
144 // To pass, HangingHostResolver should not have received a cancellation
145 // request (since there is nothing to cancel). If it does, it will crash.
146 }
147
148 } // namespace
149
150 } // namespace net
OLDNEW
« no previous file with comments | « net/dns/single_request_host_resolver.cc ('k') | net/net.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698