Chromium Code Reviews| Index: net/dns/dns_socket_pool_unittest.cc |
| diff --git a/net/dns/dns_socket_pool_unittest.cc b/net/dns/dns_socket_pool_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..71f613a4e9c596b35bcd6dbb1678b7108bb5beae |
| --- /dev/null |
| +++ b/net/dns/dns_socket_pool_unittest.cc |
| @@ -0,0 +1,103 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "net/dns/dns_socket_pool.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/callback.h" |
| +#include "base/memory/weak_ptr.h" |
| +#include "net/base/rand_callback.h" |
| +#include "net/socket/client_socket_factory.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace net { |
| +namespace { |
| + |
| +class DummyObject { |
| + public: |
| + DummyObject() : weak_factory_(this) {} |
| + |
| + base::WeakPtr<DummyObject> GetWeakPtr() { return weak_factory_.GetWeakPtr(); } |
| + |
| + bool HasWeakPtrs() const { return weak_factory_.HasWeakPtrs(); } |
| + |
| + private: |
| + base::WeakPtrFactory<DummyObject> weak_factory_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(DummyObject); |
| +}; |
| + |
| +class DummyRandIntCallback { |
| + public: |
| + DummyRandIntCallback() {} |
| + |
| + RandIntCallback MakeCallback() { |
| + return base::Bind(&DummyRandIntCallback::GetRandInt, dummy_.GetWeakPtr()); |
| + } |
| + |
| + bool HasRefs() const { return dummy_.HasWeakPtrs(); } |
| + |
| + private: |
| + static int GetRandInt(base::WeakPtr<DummyObject> dummy, int from, int to) { |
| + return 4; // chosen by fair dice roll. |
| + // 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
|
| + } |
| + |
| + DummyObject dummy_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(DummyRandIntCallback); |
| +}; |
| + |
| +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.
|
| + DummyRandIntCallback dummy; |
| + |
| + RandIntCallback original = dummy.MakeCallback(); |
| + EXPECT_FALSE(dummy.HasRefs()); |
| + const RandIntCallback& reference = original; |
| + EXPECT_TRUE(dummy.HasRefs()); |
| + |
| + EXPECT_EQ(4, reference.Run(0, 6)); |
| + |
| + original.Reset(); |
| + EXPECT_FALSE(dummy.HasRefs()); |
| +} |
| + |
| +TEST(DummyRandIntCallbackTest, Copied) { |
| + DummyRandIntCallback dummy; |
| + |
| + RandIntCallback original = dummy.MakeCallback(); |
| + EXPECT_FALSE(dummy.HasRefs()); |
| + RandIntCallback copy = original; |
| + EXPECT_TRUE(dummy.HasRefs()); |
| + |
| + EXPECT_EQ(4, copy.Run(0, 6)); |
| + |
| + original.Reset(); |
| + EXPECT_TRUE(dummy.HasRefs()); |
| +} |
| + |
| +class DnsSocketPoolTest : public ::testing::Test { |
| + protected: |
| + DummyRandIntCallback dummy_; |
| + std::unique_ptr<DnsSocketPool> pool_; |
| +}; |
| + |
| +// Make sure that the DnsSocketPools returned by CreateDefault and CreateNull |
| +// both retain (by copying the RandIntCallback object, instead of taking a |
| +// reference) the RandIntCallback used for creating sockets. |
| + |
| +TEST_F(DnsSocketPoolTest, DefaultCopiesCallback) { |
| + pool_ = DnsSocketPool::CreateDefault(ClientSocketFactory::GetDefaultFactory(), |
| + dummy_.MakeCallback()); |
| + EXPECT_TRUE(dummy_.HasRefs()); |
| +} |
| + |
| +TEST_F(DnsSocketPoolTest, NullCopiesCallback) { |
| + pool_ = DnsSocketPool::CreateNull(ClientSocketFactory::GetDefaultFactory(), |
| + dummy_.MakeCallback()); |
| + EXPECT_TRUE(dummy_.HasRefs()); |
| +} |
| + |
| +} // namespace |
| +} // namespace net |