| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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/base/socket_performance_watcher.h" |
| 6 |
| 7 #include <stddef.h> |
| 8 |
| 9 #include "base/macros.h" |
| 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/memory/weak_ptr.h" |
| 12 #include "base/run_loop.h" |
| 13 #include "base/thread_task_runner_handle.h" |
| 14 #include "base/time/time.h" |
| 15 #include "net/base/socket_performance_watcher_factory.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 |
| 18 namespace net { |
| 19 |
| 20 namespace { |
| 21 |
| 22 class TestSocketPerformanceWatcherFactory |
| 23 : public SocketPerformanceWatcherFactory { |
| 24 public: |
| 25 TestSocketPerformanceWatcherFactory() |
| 26 : rtt_notification_received_count_(0), weak_ptr_factory_(this) {} |
| 27 |
| 28 ~TestSocketPerformanceWatcherFactory() override {} |
| 29 |
| 30 // SocketPerformanceWatcherFactory implementation: |
| 31 scoped_ptr<SocketPerformanceWatcher> CreateSocketPerformanceWatcher( |
| 32 const Protocol protocol) override { |
| 33 return scoped_ptr<SocketPerformanceWatcher>(new SocketPerformanceWatcher( |
| 34 protocol, GetWeakPtr(), base::ThreadTaskRunnerHandle::Get())); |
| 35 } |
| 36 |
| 37 void OnUpdatedRTTAvailable(const Protocol protocol, |
| 38 const base::TimeDelta& rtt) override { |
| 39 rtt_notification_received_count_++; |
| 40 } |
| 41 |
| 42 void OnWatcherReset() override {} |
| 43 |
| 44 size_t rtt_notification_received_count() const { |
| 45 return rtt_notification_received_count_; |
| 46 } |
| 47 |
| 48 base::WeakPtr<TestSocketPerformanceWatcherFactory> GetWeakPtr() { |
| 49 return weak_ptr_factory_.GetWeakPtr(); |
| 50 } |
| 51 |
| 52 private: |
| 53 size_t rtt_notification_received_count_; |
| 54 |
| 55 base::WeakPtrFactory<TestSocketPerformanceWatcherFactory> weak_ptr_factory_; |
| 56 |
| 57 DISALLOW_COPY_AND_ASSIGN(TestSocketPerformanceWatcherFactory); |
| 58 }; |
| 59 |
| 60 // Tests if SocketPerformanceWatcher computes ShouldNotifyUpdatedRTT() |
| 61 // correctly. |
| 62 // TODO(tbansal): crbug.com/590300: Tighten up this test once |
| 63 // ShouldNotifyUpdatedRTT() is tuned. |
| 64 TEST(SocketPerformanceWatcherTest, TestCanNotifyRTT) { |
| 65 TestSocketPerformanceWatcherFactory socket_performance_watcher_factory; |
| 66 scoped_ptr<SocketPerformanceWatcher> watcher = |
| 67 socket_performance_watcher_factory.CreateSocketPerformanceWatcher( |
| 68 SocketPerformanceWatcherFactory::PROTOCOL_TCP); |
| 69 watcher->OnUpdatedRTTAvailable(base::TimeDelta::FromSeconds(1)); |
| 70 base::RunLoop().RunUntilIdle(); |
| 71 EXPECT_EQ( |
| 72 1u, socket_performance_watcher_factory.rtt_notification_received_count()); |
| 73 EXPECT_FALSE(watcher->ShouldNotifyUpdatedRTT()); |
| 74 watcher->Reset(); |
| 75 EXPECT_TRUE(watcher->ShouldNotifyUpdatedRTT()); |
| 76 } |
| 77 |
| 78 } // namespace |
| 79 |
| 80 } // namespace net |
| OLD | NEW |