Chromium Code Reviews| OLD | NEW |
|---|---|
| (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_; | |
|
mmenke
2017/01/30 19:14:42
DISALLOW_COPY_AND_ASSIGN
Julia Tuttle
2017/01/30 19:35:04
Done.
| |
| 27 }; | |
| 28 | |
| 29 class DummyRandIntCallback { | |
| 30 public: | |
| 31 RandIntCallback MakeCallback() { | |
| 32 return base::Bind(&DummyRandIntCallback::GetRandInt, dummy_.GetWeakPtr()); | |
| 33 } | |
| 34 | |
| 35 bool HasRefs() const { return dummy_.HasWeakPtrs(); } | |
| 36 | |
| 37 private: | |
| 38 static int GetRandInt(base::WeakPtr<DummyObject> dummy, int from, int to) { | |
| 39 return 4; // chosen by fair dice roll. | |
| 40 // guaranteed to be random. | |
|
mmenke
2017/01/30 19:14:42
nit: Chosen / Guaranteed.
I'd also suggest putti
Julia Tuttle
2017/01/30 19:35:04
It's acceptable per Randall Munroe:
https://xkcd.
mmenke
2017/01/30 19:49:30
I'm pretty sure the very name "xkcd" violates the
asanka
2017/01/30 20:17:18
http://dilbert.com/strip/2001-10-25 mandates that
mmenke
2017/01/30 20:19:31
Only if it's a sufficiently large 4, though.
| |
| 41 } | |
| 42 | |
| 43 DummyObject dummy_; | |
|
mmenke
2017/01/30 19:14:42
DISALLOW_COPY_AND_ASSIGN
Julia Tuttle
2017/01/30 19:35:04
Done.
| |
| 44 }; | |
|
mmenke
2017/01/30 18:18:44
Is all this needed, or does the following work:
i
mmenke
2017/01/30 18:25:03
Actually, would probably have to be "int SillyRand
Julia Tuttle
2017/01/30 19:06:44
It's not *needed*, but I think it's clearer to enc
| |
| 45 | |
| 46 TEST(DummyRandIntCallbackTest, Referenced) { | |
|
mmenke
2017/01/30 19:14:42
I think that all 4 tests are sufficiently weird th
Julia Tuttle
2017/01/30 19:35:04
I'll comment them. The first two are mostly to ens
| |
| 47 DummyRandIntCallback dummy; | |
| 48 | |
| 49 RandIntCallback original = dummy.MakeCallback(); | |
| 50 const RandIntCallback& reference = original; | |
| 51 EXPECT_EQ(4, reference.Run(0, 6)); | |
|
mmenke
2017/01/30 19:14:42
EXPECT_TRUE(dummy.HasRefs());?
Julia Tuttle
2017/01/30 19:35:04
Oh, sure.
| |
| 52 | |
| 53 original.Reset(); | |
| 54 EXPECT_FALSE(dummy.HasRefs()); | |
| 55 } | |
| 56 | |
| 57 TEST(DummyRandIntCallbackTest, Copied) { | |
| 58 DummyRandIntCallback dummy; | |
| 59 | |
| 60 RandIntCallback original = dummy.MakeCallback(); | |
| 61 RandIntCallback copy = original; | |
| 62 EXPECT_EQ(4, copy.Run(0, 6)); | |
| 63 | |
| 64 original.Reset(); | |
| 65 EXPECT_TRUE(dummy.HasRefs()); | |
| 66 } | |
| 67 | |
| 68 class DnsSocketPoolTest : public ::testing::Test { | |
| 69 protected: | |
| 70 DummyRandIntCallback dummy_; | |
| 71 std::unique_ptr<DnsSocketPool> pool_; | |
| 72 }; | |
| 73 | |
| 74 TEST_F(DnsSocketPoolTest, DefaultCopiesCallback) { | |
| 75 pool_ = DnsSocketPool::CreateDefault(ClientSocketFactory::GetDefaultFactory(), | |
| 76 dummy_.MakeCallback()); | |
| 77 EXPECT_TRUE(dummy_.HasRefs()); | |
| 78 } | |
| 79 | |
| 80 TEST_F(DnsSocketPoolTest, NullCopiesCallback) { | |
| 81 pool_ = DnsSocketPool::CreateNull(ClientSocketFactory::GetDefaultFactory(), | |
| 82 dummy_.MakeCallback()); | |
| 83 EXPECT_TRUE(dummy_.HasRefs()); | |
| 84 } | |
| 85 | |
| 86 } // namespace | |
| 87 } // namespace net | |
| OLD | NEW |