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

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 return 4; // chosen by fair dice roll.
44 // guaranteed to be random.
mmenke 2017/01/30 19:49:30 I'd rather remove this, then - jokes not following
Julia Tuttle 2017/01/30 20:10:52 Fiiine, I'll follow the style guide for it. :)
mmenke 2017/01/30 20:15:29 Sorry to be so fixated on minor details, I've just
45 }
46
47 DummyObject dummy_;
48
49 DISALLOW_COPY_AND_ASSIGN(DummyRandIntCallback);
50 };
51
52 TEST(DummyRandIntCallbackTest, Referenced) {
mmenke 2017/01/30 19:49:30 Comment these? Looking at them, it's rather non-o
Julia Tuttle 2017/01/30 20:10:51 Done.
53 DummyRandIntCallback dummy;
54
55 RandIntCallback original = dummy.MakeCallback();
56 EXPECT_FALSE(dummy.HasRefs());
57 const RandIntCallback& reference = original;
58 EXPECT_TRUE(dummy.HasRefs());
59
60 EXPECT_EQ(4, reference.Run(0, 6));
61
62 original.Reset();
63 EXPECT_FALSE(dummy.HasRefs());
64 }
65
66 TEST(DummyRandIntCallbackTest, Copied) {
67 DummyRandIntCallback dummy;
68
69 RandIntCallback original = dummy.MakeCallback();
70 EXPECT_FALSE(dummy.HasRefs());
71 RandIntCallback copy = original;
72 EXPECT_TRUE(dummy.HasRefs());
73
74 EXPECT_EQ(4, copy.Run(0, 6));
75
76 original.Reset();
77 EXPECT_TRUE(dummy.HasRefs());
78 }
79
80 class DnsSocketPoolTest : public ::testing::Test {
81 protected:
82 DummyRandIntCallback dummy_;
83 std::unique_ptr<DnsSocketPool> pool_;
84 };
85
86 // Make sure that the DnsSocketPools returned by CreateDefault and CreateNull
87 // both retain (by copying the RandIntCallback object, instead of taking a
88 // reference) the RandIntCallback used for creating sockets.
89
90 TEST_F(DnsSocketPoolTest, DefaultCopiesCallback) {
91 pool_ = DnsSocketPool::CreateDefault(ClientSocketFactory::GetDefaultFactory(),
92 dummy_.MakeCallback());
93 EXPECT_TRUE(dummy_.HasRefs());
94 }
95
96 TEST_F(DnsSocketPoolTest, NullCopiesCallback) {
97 pool_ = DnsSocketPool::CreateNull(ClientSocketFactory::GetDefaultFactory(),
98 dummy_.MakeCallback());
99 EXPECT_TRUE(dummy_.HasRefs());
100 }
101
102 } // namespace
103 } // 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