| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/sync/model/attachments/attachment_service_proxy.h" | 5 #include "components/sync/model/attachments/attachment_service_proxy.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| 11 #include "base/memory/ptr_util.h" |
| 11 #include "base/memory/ref_counted_memory.h" | 12 #include "base/memory/ref_counted_memory.h" |
| 12 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 13 #include "base/single_thread_task_runner.h" | 14 #include "base/single_thread_task_runner.h" |
| 14 #include "base/synchronization/lock.h" | 15 #include "base/synchronization/lock.h" |
| 15 #include "base/synchronization/waitable_event.h" | 16 #include "base/synchronization/waitable_event.h" |
| 16 #include "base/threading/non_thread_safe.h" | 17 #include "base/threading/non_thread_safe.h" |
| 17 #include "base/threading/thread.h" | 18 #include "base/threading/thread.h" |
| 18 #include "base/threading/thread_task_runner_handle.h" | 19 #include "base/threading/thread_task_runner_handle.h" |
| 19 #include "components/sync/base/model_type.h" | 20 #include "components/sync/base/model_type.h" |
| 20 #include "components/sync/protocol/sync.pb.h" | 21 #include "components/sync/protocol/sync.pb.h" |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 } | 77 } |
| 77 }; | 78 }; |
| 78 | 79 |
| 79 class AttachmentServiceProxyTest : public testing::Test, | 80 class AttachmentServiceProxyTest : public testing::Test, |
| 80 public base::NonThreadSafe { | 81 public base::NonThreadSafe { |
| 81 protected: | 82 protected: |
| 82 AttachmentServiceProxyTest() {} | 83 AttachmentServiceProxyTest() {} |
| 83 | 84 |
| 84 void SetUp() override { | 85 void SetUp() override { |
| 85 CalledOnValidThread(); | 86 CalledOnValidThread(); |
| 86 stub_thread.reset(new base::Thread("attachment service stub thread")); | 87 stub_thread_ = |
| 87 stub_thread->Start(); | 88 base::MakeUnique<base::Thread>("attachment service stub thread"); |
| 88 stub.reset(new StubAttachmentService); | 89 stub_thread_->Start(); |
| 89 proxy.reset(new AttachmentServiceProxy(stub_thread->task_runner(), | 90 stub_ = base::MakeUnique<StubAttachmentService>(); |
| 90 stub->AsWeakPtr())); | 91 proxy_ = base::MakeUnique<AttachmentServiceProxy>( |
| 92 stub_thread_->task_runner(), stub_->AsWeakPtr()); |
| 91 | 93 |
| 92 callback_get_or_download = | 94 callback_get_or_download_ = |
| 93 base::Bind(&AttachmentServiceProxyTest::IncrementGetOrDownload, | 95 base::Bind(&AttachmentServiceProxyTest::IncrementGetOrDownload, |
| 94 base::Unretained(this)); | 96 base::Unretained(this)); |
| 95 count_callback_get_or_download = 0; | 97 count_callback_get_or_download_ = 0; |
| 96 } | 98 } |
| 97 | 99 |
| 98 void TearDown() override { | 100 void TearDown() override { |
| 99 // We must take care to call the stub's destructor on the stub_thread | 101 // We must take care to call the stub's destructor on the stub_thread_ |
| 100 // because that's the thread to which its WeakPtrs are bound. | 102 // because that's the thread to which its WeakPtrs are bound. |
| 101 if (stub) { | 103 if (stub_) { |
| 102 stub_thread->task_runner()->DeleteSoon(FROM_HERE, stub.release()); | 104 stub_thread_->task_runner()->DeleteSoon(FROM_HERE, stub_.release()); |
| 103 WaitForStubThread(); | 105 WaitForStubThread(); |
| 104 } | 106 } |
| 105 stub_thread->Stop(); | 107 stub_thread_->Stop(); |
| 106 } | 108 } |
| 107 | 109 |
| 108 // a GetOrDownloadCallback | 110 // a GetOrDownloadCallback |
| 109 void IncrementGetOrDownload(const AttachmentService::GetOrDownloadResult&, | 111 void IncrementGetOrDownload(const AttachmentService::GetOrDownloadResult&, |
| 110 std::unique_ptr<AttachmentMap>) { | 112 std::unique_ptr<AttachmentMap>) { |
| 111 CalledOnValidThread(); | 113 CalledOnValidThread(); |
| 112 ++count_callback_get_or_download; | 114 ++count_callback_get_or_download_; |
| 113 } | 115 } |
| 114 | 116 |
| 115 void WaitForStubThread() { | 117 void WaitForStubThread() { |
| 116 base::WaitableEvent done(base::WaitableEvent::ResetPolicy::AUTOMATIC, | 118 base::WaitableEvent done(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 117 base::WaitableEvent::InitialState::NOT_SIGNALED); | 119 base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 118 stub_thread->task_runner()->PostTask( | 120 stub_thread_->task_runner()->PostTask( |
| 119 FROM_HERE, | 121 FROM_HERE, |
| 120 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&done))); | 122 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&done))); |
| 121 done.Wait(); | 123 done.Wait(); |
| 122 } | 124 } |
| 123 | 125 |
| 124 base::MessageLoop loop; | 126 base::MessageLoop loop_; |
| 125 std::unique_ptr<base::Thread> stub_thread; | 127 std::unique_ptr<base::Thread> stub_thread_; |
| 126 std::unique_ptr<StubAttachmentService> stub; | 128 std::unique_ptr<StubAttachmentService> stub_; |
| 127 std::unique_ptr<AttachmentServiceProxy> proxy; | 129 std::unique_ptr<AttachmentServiceProxy> proxy_; |
| 128 | 130 |
| 129 AttachmentService::GetOrDownloadCallback callback_get_or_download; | 131 AttachmentService::GetOrDownloadCallback callback_get_or_download_; |
| 130 | 132 |
| 131 // number of times callback_get_or_download was invoked | 133 // number of times callback_get_or_download_ was invoked |
| 132 int count_callback_get_or_download; | 134 int count_callback_get_or_download_; |
| 133 }; | 135 }; |
| 134 | 136 |
| 135 // Verify that each of AttachmentServiceProxy's methods are invoked on the stub. | 137 // Verify that each of AttachmentServiceProxy's methods are invoked on the stub. |
| 136 // Verify that the methods that take callbacks invoke passed callbacks on this | 138 // Verify that the methods that take callbacks invoke passed callbacks on this |
| 137 // thread. | 139 // thread. |
| 138 TEST_F(AttachmentServiceProxyTest, MethodsAreProxied) { | 140 TEST_F(AttachmentServiceProxyTest, MethodsAreProxied) { |
| 139 proxy->GetOrDownloadAttachments(AttachmentIdList(), callback_get_or_download); | 141 proxy_->GetOrDownloadAttachments(AttachmentIdList(), |
| 140 proxy->UploadAttachments(AttachmentIdList()); | 142 callback_get_or_download_); |
| 143 proxy_->UploadAttachments(AttachmentIdList()); |
| 141 // Wait for the posted calls to execute in the stub thread. | 144 // Wait for the posted calls to execute in the stub thread. |
| 142 WaitForStubThread(); | 145 WaitForStubThread(); |
| 143 EXPECT_EQ(2, stub->GetCallCount()); | 146 EXPECT_EQ(2, stub_->GetCallCount()); |
| 144 // At this point the stub thread has finished executed the calls. However, the | 147 // At this point the stub thread has finished executed the calls. However, the |
| 145 // result callbacks it has posted may not have executed yet. Wait a second | 148 // result callbacks it has posted may not have executed yet. Wait a second |
| 146 // time to ensure the stub thread has executed the posted result callbacks. | 149 // time to ensure the stub thread has executed the posted result callbacks. |
| 147 WaitForStubThread(); | 150 WaitForStubThread(); |
| 148 | 151 |
| 149 base::RunLoop().RunUntilIdle(); | 152 base::RunLoop().RunUntilIdle(); |
| 150 EXPECT_EQ(1, count_callback_get_or_download); | 153 EXPECT_EQ(1, count_callback_get_or_download_); |
| 151 } | 154 } |
| 152 | 155 |
| 153 // Verify that it's safe to use an AttachmentServiceProxy even after its wrapped | 156 // Verify that it's safe to use an AttachmentServiceProxy even after its wrapped |
| 154 // AttachmentService has been destroyed. | 157 // AttachmentService has been destroyed. |
| 155 TEST_F(AttachmentServiceProxyTest, WrappedIsDestroyed) { | 158 TEST_F(AttachmentServiceProxyTest, WrappedIsDestroyed) { |
| 156 proxy->GetOrDownloadAttachments(AttachmentIdList(), callback_get_or_download); | 159 proxy_->GetOrDownloadAttachments(AttachmentIdList(), |
| 160 callback_get_or_download_); |
| 157 // Wait for the posted calls to execute in the stub thread. | 161 // Wait for the posted calls to execute in the stub thread. |
| 158 WaitForStubThread(); | 162 WaitForStubThread(); |
| 159 EXPECT_EQ(1, stub->GetCallCount()); | 163 EXPECT_EQ(1, stub_->GetCallCount()); |
| 160 // Wait a second time ensure the stub thread has executed the posted result | 164 // Wait a second time ensure the stub thread has executed the posted result |
| 161 // callbacks. | 165 // callbacks. |
| 162 WaitForStubThread(); | 166 WaitForStubThread(); |
| 163 | 167 |
| 164 base::RunLoop().RunUntilIdle(); | 168 base::RunLoop().RunUntilIdle(); |
| 165 EXPECT_EQ(1, count_callback_get_or_download); | 169 EXPECT_EQ(1, count_callback_get_or_download_); |
| 166 | 170 |
| 167 // Destroy the stub and call GetOrDownloadAttachments again. | 171 // Destroy the stub and call GetOrDownloadAttachments again. |
| 168 stub_thread->task_runner()->DeleteSoon(FROM_HERE, stub.release()); | 172 stub_thread_->task_runner()->DeleteSoon(FROM_HERE, stub_.release()); |
| 169 WaitForStubThread(); | 173 WaitForStubThread(); |
| 170 | 174 |
| 171 // Now that the wrapped object has been destroyed, call again and see that we | 175 // Now that the wrapped object has been destroyed, call again and see that we |
| 172 // don't crash and the count remains the same. | 176 // don't crash and the count remains the same. |
| 173 proxy->GetOrDownloadAttachments(AttachmentIdList(), callback_get_or_download); | 177 proxy_->GetOrDownloadAttachments(AttachmentIdList(), |
| 178 callback_get_or_download_); |
| 174 WaitForStubThread(); | 179 WaitForStubThread(); |
| 175 WaitForStubThread(); | 180 WaitForStubThread(); |
| 176 base::RunLoop().RunUntilIdle(); | 181 base::RunLoop().RunUntilIdle(); |
| 177 EXPECT_EQ(1, count_callback_get_or_download); | 182 EXPECT_EQ(1, count_callback_get_or_download_); |
| 178 } | 183 } |
| 179 | 184 |
| 180 } // namespace syncer | 185 } // namespace syncer |
| OLD | NEW |