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

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

Issue 2660253003: Unit test that DnsSocketPool copies RandIntCallback (Closed)
Patch Set: Make requested changes. Created 3 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
« no previous file with comments | « net/BUILD.gn ('k') | no next file » | 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 2017 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/dns_socket_pool.h"
6
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "base/memory/weak_ptr.h"
10 #include "net/base/rand_callback.h"
11 #include "net/socket/client_socket_factory.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 namespace net {
15 namespace {
16
17 class DummyObject {
18 public:
19 DummyObject() : weak_factory_(this) {}
20
21 base::WeakPtr<DummyObject> GetWeakPtr() { return weak_factory_.GetWeakPtr(); }
22
23 bool HasWeakPtrs() const { return weak_factory_.HasWeakPtrs(); }
24
25 private:
26 base::WeakPtrFactory<DummyObject> weak_factory_;
27
28 DISALLOW_COPY_AND_ASSIGN(DummyObject);
29 };
30
31 class DummyRandIntCallback {
32 public:
33 DummyRandIntCallback() {}
34
35 RandIntCallback MakeCallback() {
36 return base::Bind(&DummyRandIntCallback::GetRandInt, dummy_.GetWeakPtr());
37 }
38
39 bool HasRefs() const { return dummy_.HasWeakPtrs(); }
40
41 private:
42 static int GetRandInt(base::WeakPtr<DummyObject> dummy, int from, int to) {
43 // Chosen by fair dice roll. Guaranteed to be random.
44 return 4;
45 }
46
47 DummyObject dummy_;
48
49 DISALLOW_COPY_AND_ASSIGN(DummyRandIntCallback);
50 };
51
52 // Before the below tests rely upon it, make sure that DummyRandIntCallback
mmenke 2017/01/30 20:15:29 nit: Before -> Because? Tests aren't guaranteed
Julia Tuttle 2017/01/30 20:42:42 Done.
53 // can reliably tell whether there are other refs to the callback it returns.
54
55 // A const reference to the callback shouldn't keep the callback referenced.
56 TEST(DummyRandIntCallbackTest, Referenced) {
57 DummyRandIntCallback dummy;
58
59 RandIntCallback original = dummy.MakeCallback();
60 EXPECT_FALSE(dummy.HasRefs());
61 const RandIntCallback& reference = original;
62 EXPECT_TRUE(dummy.HasRefs());
63
64 EXPECT_EQ(4, reference.Run(0, 6));
65
66 original.Reset();
67 EXPECT_FALSE(dummy.HasRefs());
68 }
69
70 // A copy of the callback should keep the callback referenced.
71 TEST(DummyRandIntCallbackTest, Copied) {
72 DummyRandIntCallback dummy;
73
74 RandIntCallback original = dummy.MakeCallback();
75 EXPECT_FALSE(dummy.HasRefs());
76 RandIntCallback copy = original;
77 EXPECT_TRUE(dummy.HasRefs());
78
79 EXPECT_EQ(4, copy.Run(0, 6));
80
81 original.Reset();
82 EXPECT_TRUE(dummy.HasRefs());
83 }
84
85 class DnsSocketPoolTest : public ::testing::Test {
86 protected:
87 DummyRandIntCallback dummy_;
88 std::unique_ptr<DnsSocketPool> pool_;
89 };
90
91 // Make sure that the DnsSocketPools returned by CreateDefault and CreateNull
92 // both retain (by copying the RandIntCallback object, instead of taking a
93 // reference) the RandIntCallback used for creating sockets.
94
95 TEST_F(DnsSocketPoolTest, DefaultCopiesCallback) {
96 pool_ = DnsSocketPool::CreateDefault(ClientSocketFactory::GetDefaultFactory(),
97 dummy_.MakeCallback());
98 EXPECT_TRUE(dummy_.HasRefs());
99 }
100
101 TEST_F(DnsSocketPoolTest, NullCopiesCallback) {
102 pool_ = DnsSocketPool::CreateNull(ClientSocketFactory::GetDefaultFactory(),
103 dummy_.MakeCallback());
104 EXPECT_TRUE(dummy_.HasRefs());
105 }
106
107 } // namespace
108 } // namespace net
OLDNEW
« no previous file with comments | « net/BUILD.gn ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698