| 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/ptr_util.h" |
| 12 #include "base/memory/ref_counted_memory.h" | 12 #include "base/memory/ref_counted_memory.h" |
| 13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 14 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
| 15 #include "base/sequence_checker.h" |
| 15 #include "base/single_thread_task_runner.h" | 16 #include "base/single_thread_task_runner.h" |
| 16 #include "base/synchronization/lock.h" | 17 #include "base/synchronization/lock.h" |
| 17 #include "base/synchronization/waitable_event.h" | 18 #include "base/synchronization/waitable_event.h" |
| 18 #include "base/threading/non_thread_safe.h" | |
| 19 #include "base/threading/thread.h" | 19 #include "base/threading/thread.h" |
| 20 #include "base/threading/thread_task_runner_handle.h" | 20 #include "base/threading/thread_task_runner_handle.h" |
| 21 #include "components/sync/base/model_type.h" | 21 #include "components/sync/base/model_type.h" |
| 22 #include "components/sync/protocol/sync.pb.h" | 22 #include "components/sync/protocol/sync.pb.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 24 | 24 |
| 25 namespace syncer { | 25 namespace syncer { |
| 26 | 26 |
| 27 // A stub implementation of AttachmentService that counts the number of times | 27 // A stub implementation of AttachmentService that counts the number of times |
| 28 // its methods are invoked. | 28 // its methods are invoked. |
| 29 class StubAttachmentService : public AttachmentService, | 29 class StubAttachmentService : public AttachmentService { |
| 30 public base::NonThreadSafe { | |
| 31 public: | 30 public: |
| 32 StubAttachmentService() : call_count_(0), weak_ptr_factory_(this) { | 31 StubAttachmentService() : call_count_(0), weak_ptr_factory_(this) { |
| 33 // DetachFromThread because we will be constructed in one thread and | 32 // DetachFromThread because we will be constructed in one thread and |
| 34 // used/destroyed in another. | 33 // used/destroyed in another. |
| 35 DetachFromThread(); | 34 DETACH_FROM_SEQUENCE(sequence_checker_); |
| 36 } | 35 } |
| 37 | 36 |
| 38 ~StubAttachmentService() override {} | 37 ~StubAttachmentService() override { |
| 38 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 39 } |
| 39 | 40 |
| 40 void GetOrDownloadAttachments( | 41 void GetOrDownloadAttachments( |
| 41 const AttachmentIdList& attachment_ids, | 42 const AttachmentIdList& attachment_ids, |
| 42 const GetOrDownloadCallback& callback) override { | 43 const GetOrDownloadCallback& callback) override { |
| 43 CalledOnValidThread(); | 44 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 44 Increment(); | 45 Increment(); |
| 45 std::unique_ptr<AttachmentMap> attachments(new AttachmentMap()); | 46 std::unique_ptr<AttachmentMap> attachments(new AttachmentMap()); |
| 46 base::ThreadTaskRunnerHandle::Get()->PostTask( | 47 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 47 FROM_HERE, | 48 FROM_HERE, |
| 48 base::Bind(callback, AttachmentService::GET_UNSPECIFIED_ERROR, | 49 base::Bind(callback, AttachmentService::GET_UNSPECIFIED_ERROR, |
| 49 base::Passed(&attachments))); | 50 base::Passed(&attachments))); |
| 50 } | 51 } |
| 51 | 52 |
| 52 void UploadAttachments(const AttachmentIdList& attachments_ids) override { | 53 void UploadAttachments(const AttachmentIdList& attachments_ids) override { |
| 53 CalledOnValidThread(); | 54 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 54 Increment(); | 55 Increment(); |
| 55 } | 56 } |
| 56 | 57 |
| 57 virtual base::WeakPtr<AttachmentService> AsWeakPtr() { | 58 virtual base::WeakPtr<AttachmentService> AsWeakPtr() { |
| 58 return weak_ptr_factory_.GetWeakPtr(); | 59 return weak_ptr_factory_.GetWeakPtr(); |
| 59 } | 60 } |
| 60 | 61 |
| 61 // Return the number of method invocations. | 62 // Return the number of method invocations. |
| 62 int GetCallCount() const { | 63 int GetCallCount() const { |
| 63 base::AutoLock lock(mutex_); | 64 base::AutoLock lock(mutex_); |
| 64 return call_count_; | 65 return call_count_; |
| 65 } | 66 } |
| 66 | 67 |
| 67 private: | 68 private: |
| 68 // Protects call_count_. | 69 // Protects call_count_. |
| 69 mutable base::Lock mutex_; | 70 mutable base::Lock mutex_; |
| 70 int call_count_; | 71 int call_count_; |
| 71 | 72 |
| 72 // Must be last data member. | 73 // Must be last data member. |
| 73 base::WeakPtrFactory<AttachmentService> weak_ptr_factory_; | 74 base::WeakPtrFactory<AttachmentService> weak_ptr_factory_; |
| 74 | 75 |
| 75 void Increment() { | 76 void Increment() { |
| 76 base::AutoLock lock(mutex_); | 77 base::AutoLock lock(mutex_); |
| 77 ++call_count_; | 78 ++call_count_; |
| 78 } | 79 } |
| 80 |
| 81 SEQUENCE_CHECKER(sequence_checker_); |
| 79 }; | 82 }; |
| 80 | 83 |
| 81 class AttachmentServiceProxyTest : public testing::Test, | 84 class AttachmentServiceProxyTest : public testing::Test { |
| 82 public base::NonThreadSafe { | |
| 83 protected: | 85 protected: |
| 84 AttachmentServiceProxyTest() {} | 86 AttachmentServiceProxyTest() {} |
| 85 | 87 |
| 86 void SetUp() override { | 88 void SetUp() override { |
| 87 CalledOnValidThread(); | 89 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 88 stub_thread_ = | 90 stub_thread_ = |
| 89 base::MakeUnique<base::Thread>("attachment service stub thread"); | 91 base::MakeUnique<base::Thread>("attachment service stub thread"); |
| 90 stub_thread_->Start(); | 92 stub_thread_->Start(); |
| 91 stub_ = base::MakeUnique<StubAttachmentService>(); | 93 stub_ = base::MakeUnique<StubAttachmentService>(); |
| 92 proxy_ = base::MakeUnique<AttachmentServiceProxy>( | 94 proxy_ = base::MakeUnique<AttachmentServiceProxy>( |
| 93 stub_thread_->task_runner(), stub_->AsWeakPtr()); | 95 stub_thread_->task_runner(), stub_->AsWeakPtr()); |
| 94 | 96 |
| 95 callback_get_or_download_ = | 97 callback_get_or_download_ = |
| 96 base::Bind(&AttachmentServiceProxyTest::IncrementGetOrDownload, | 98 base::Bind(&AttachmentServiceProxyTest::IncrementGetOrDownload, |
| 97 base::Unretained(this)); | 99 base::Unretained(this)); |
| 98 count_callback_get_or_download_ = 0; | 100 count_callback_get_or_download_ = 0; |
| 99 } | 101 } |
| 100 | 102 |
| 101 void TearDown() override { | 103 void TearDown() override { |
| 102 // We must take care to call the stub's destructor on the stub_thread_ | 104 // We must take care to call the stub's destructor on the stub_thread_ |
| 103 // because that's the thread to which its WeakPtrs are bound. | 105 // because that's the thread to which its WeakPtrs are bound. |
| 104 if (stub_) { | 106 if (stub_) { |
| 105 stub_thread_->task_runner()->DeleteSoon(FROM_HERE, stub_.release()); | 107 stub_thread_->task_runner()->DeleteSoon(FROM_HERE, stub_.release()); |
| 106 WaitForStubThread(); | 108 WaitForStubThread(); |
| 107 } | 109 } |
| 108 stub_thread_->Stop(); | 110 stub_thread_->Stop(); |
| 109 } | 111 } |
| 110 | 112 |
| 111 // a GetOrDownloadCallback | 113 // a GetOrDownloadCallback |
| 112 void IncrementGetOrDownload(const AttachmentService::GetOrDownloadResult&, | 114 void IncrementGetOrDownload(const AttachmentService::GetOrDownloadResult&, |
| 113 std::unique_ptr<AttachmentMap>) { | 115 std::unique_ptr<AttachmentMap>) { |
| 114 CalledOnValidThread(); | 116 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 115 ++count_callback_get_or_download_; | 117 ++count_callback_get_or_download_; |
| 116 } | 118 } |
| 117 | 119 |
| 118 void WaitForStubThread() { | 120 void WaitForStubThread() { |
| 119 base::WaitableEvent done(base::WaitableEvent::ResetPolicy::AUTOMATIC, | 121 base::WaitableEvent done(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 120 base::WaitableEvent::InitialState::NOT_SIGNALED); | 122 base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 121 stub_thread_->task_runner()->PostTask( | 123 stub_thread_->task_runner()->PostTask( |
| 122 FROM_HERE, | 124 FROM_HERE, |
| 123 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&done))); | 125 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&done))); |
| 124 done.Wait(); | 126 done.Wait(); |
| 125 } | 127 } |
| 126 | 128 |
| 127 base::MessageLoop loop_; | 129 base::MessageLoop loop_; |
| 128 std::unique_ptr<base::Thread> stub_thread_; | 130 std::unique_ptr<base::Thread> stub_thread_; |
| 129 std::unique_ptr<StubAttachmentService> stub_; | 131 std::unique_ptr<StubAttachmentService> stub_; |
| 130 std::unique_ptr<AttachmentServiceProxy> proxy_; | 132 std::unique_ptr<AttachmentServiceProxy> proxy_; |
| 131 | 133 |
| 132 AttachmentService::GetOrDownloadCallback callback_get_or_download_; | 134 AttachmentService::GetOrDownloadCallback callback_get_or_download_; |
| 133 | 135 |
| 134 // number of times callback_get_or_download_ was invoked | 136 // number of times callback_get_or_download_ was invoked |
| 135 int count_callback_get_or_download_; | 137 int count_callback_get_or_download_; |
| 138 |
| 139 SEQUENCE_CHECKER(sequence_checker_); |
| 136 }; | 140 }; |
| 137 | 141 |
| 138 // Verify that each of AttachmentServiceProxy's methods are invoked on the stub. | 142 // Verify that each of AttachmentServiceProxy's methods are invoked on the stub. |
| 139 // Verify that the methods that take callbacks invoke passed callbacks on this | 143 // Verify that the methods that take callbacks invoke passed callbacks on this |
| 140 // thread. | 144 // thread. |
| 141 TEST_F(AttachmentServiceProxyTest, MethodsAreProxied) { | 145 TEST_F(AttachmentServiceProxyTest, MethodsAreProxied) { |
| 142 proxy_->GetOrDownloadAttachments(AttachmentIdList(), | 146 proxy_->GetOrDownloadAttachments(AttachmentIdList(), |
| 143 callback_get_or_download_); | 147 callback_get_or_download_); |
| 144 proxy_->UploadAttachments(AttachmentIdList()); | 148 proxy_->UploadAttachments(AttachmentIdList()); |
| 145 // Wait for the posted calls to execute in the stub thread. | 149 // Wait for the posted calls to execute in the stub thread. |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 // don't crash and the count remains the same. | 181 // don't crash and the count remains the same. |
| 178 proxy_->GetOrDownloadAttachments(AttachmentIdList(), | 182 proxy_->GetOrDownloadAttachments(AttachmentIdList(), |
| 179 callback_get_or_download_); | 183 callback_get_or_download_); |
| 180 WaitForStubThread(); | 184 WaitForStubThread(); |
| 181 WaitForStubThread(); | 185 WaitForStubThread(); |
| 182 base::RunLoop().RunUntilIdle(); | 186 base::RunLoop().RunUntilIdle(); |
| 183 EXPECT_EQ(1, count_callback_get_or_download_); | 187 EXPECT_EQ(1, count_callback_get_or_download_); |
| 184 } | 188 } |
| 185 | 189 |
| 186 } // namespace syncer | 190 } // namespace syncer |
| OLD | NEW |