Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(30)

Side by Side Diff: sync/internal_api/attachments/attachment_service_proxy_unittest.cc

Issue 2130453004: [Sync] Move //sync to //components/sync. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase. Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "sync/internal_api/public/attachments/attachment_service_proxy.h"
6
7 #include <memory>
8
9 #include "base/bind.h"
10 #include "base/location.h"
11 #include "base/memory/ref_counted_memory.h"
12 #include "base/run_loop.h"
13 #include "base/single_thread_task_runner.h"
14 #include "base/synchronization/lock.h"
15 #include "base/synchronization/waitable_event.h"
16 #include "base/threading/non_thread_safe.h"
17 #include "base/threading/thread.h"
18 #include "base/threading/thread_task_runner_handle.h"
19 #include "sync/api/attachments/attachment.h"
20 #include "sync/internal_api/public/attachments/attachment_service.h"
21 #include "sync/internal_api/public/base/model_type.h"
22 #include "sync/protocol/sync.pb.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24
25 namespace syncer {
26
27 // A stub implementation of AttachmentService that counts the number of times
28 // its methods are invoked.
29 class StubAttachmentService : public AttachmentService,
30 public base::NonThreadSafe {
31 public:
32 StubAttachmentService() : call_count_(0), weak_ptr_factory_(this) {
33 // DetachFromThread because we will be constructed in one thread and
34 // used/destroyed in another.
35 DetachFromThread();
36 }
37
38 ~StubAttachmentService() override {}
39
40 void GetOrDownloadAttachments(
41 const AttachmentIdList& attachment_ids,
42 const GetOrDownloadCallback& callback) override {
43 CalledOnValidThread();
44 Increment();
45 std::unique_ptr<AttachmentMap> attachments(new AttachmentMap());
46 base::ThreadTaskRunnerHandle::Get()->PostTask(
47 FROM_HERE,
48 base::Bind(callback, AttachmentService::GET_UNSPECIFIED_ERROR,
49 base::Passed(&attachments)));
50 }
51
52 void UploadAttachments(const AttachmentIdList& attachments_ids) override {
53 CalledOnValidThread();
54 Increment();
55 }
56
57 virtual base::WeakPtr<AttachmentService> AsWeakPtr() {
58 return weak_ptr_factory_.GetWeakPtr();
59 }
60
61 // Return the number of method invocations.
62 int GetCallCount() const {
63 base::AutoLock lock(mutex_);
64 return call_count_;
65 }
66
67 private:
68 // Protects call_count_.
69 mutable base::Lock mutex_;
70 int call_count_;
71
72 // Must be last data member.
73 base::WeakPtrFactory<AttachmentService> weak_ptr_factory_;
74
75 void Increment() {
76 base::AutoLock lock(mutex_);
77 ++call_count_;
78 }
79 };
80
81 class AttachmentServiceProxyTest : public testing::Test,
82 public base::NonThreadSafe {
83 protected:
84 AttachmentServiceProxyTest() {}
85
86 void SetUp() override {
87 CalledOnValidThread();
88 stub_thread.reset(new base::Thread("attachment service stub thread"));
89 stub_thread->Start();
90 stub.reset(new StubAttachmentService);
91 proxy.reset(new AttachmentServiceProxy(stub_thread->task_runner(),
92 stub->AsWeakPtr()));
93
94 callback_get_or_download =
95 base::Bind(&AttachmentServiceProxyTest::IncrementGetOrDownload,
96 base::Unretained(this));
97 count_callback_get_or_download = 0;
98 }
99
100 void TearDown() override {
101 // We must take care to call the stub's destructor on the stub_thread
102 // because that's the thread to which its WeakPtrs are bound.
103 if (stub) {
104 stub_thread->task_runner()->DeleteSoon(FROM_HERE, stub.release());
105 WaitForStubThread();
106 }
107 stub_thread->Stop();
108 }
109
110 // a GetOrDownloadCallback
111 void IncrementGetOrDownload(const AttachmentService::GetOrDownloadResult&,
112 std::unique_ptr<AttachmentMap>) {
113 CalledOnValidThread();
114 ++count_callback_get_or_download;
115 }
116
117 void WaitForStubThread() {
118 base::WaitableEvent done(base::WaitableEvent::ResetPolicy::AUTOMATIC,
119 base::WaitableEvent::InitialState::NOT_SIGNALED);
120 stub_thread->task_runner()->PostTask(
121 FROM_HERE,
122 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&done)));
123 done.Wait();
124 }
125
126 base::MessageLoop loop;
127 std::unique_ptr<base::Thread> stub_thread;
128 std::unique_ptr<StubAttachmentService> stub;
129 std::unique_ptr<AttachmentServiceProxy> proxy;
130
131 AttachmentService::GetOrDownloadCallback callback_get_or_download;
132
133 // number of times callback_get_or_download was invoked
134 int count_callback_get_or_download;
135 };
136
137 // Verify that each of AttachmentServiceProxy's methods are invoked on the stub.
138 // Verify that the methods that take callbacks invoke passed callbacks on this
139 // thread.
140 TEST_F(AttachmentServiceProxyTest, MethodsAreProxied) {
141 proxy->GetOrDownloadAttachments(AttachmentIdList(), callback_get_or_download);
142 proxy->UploadAttachments(AttachmentIdList());
143 // Wait for the posted calls to execute in the stub thread.
144 WaitForStubThread();
145 EXPECT_EQ(2, stub->GetCallCount());
146 // At this point the stub thread has finished executed the calls. However, the
147 // result callbacks it has posted may not have executed yet. Wait a second
148 // time to ensure the stub thread has executed the posted result callbacks.
149 WaitForStubThread();
150
151 base::RunLoop().RunUntilIdle();
152 EXPECT_EQ(1, count_callback_get_or_download);
153 }
154
155 // Verify that it's safe to use an AttachmentServiceProxy even after its wrapped
156 // AttachmentService has been destroyed.
157 TEST_F(AttachmentServiceProxyTest, WrappedIsDestroyed) {
158 proxy->GetOrDownloadAttachments(AttachmentIdList(), callback_get_or_download);
159 // Wait for the posted calls to execute in the stub thread.
160 WaitForStubThread();
161 EXPECT_EQ(1, stub->GetCallCount());
162 // Wait a second time ensure the stub thread has executed the posted result
163 // callbacks.
164 WaitForStubThread();
165
166 base::RunLoop().RunUntilIdle();
167 EXPECT_EQ(1, count_callback_get_or_download);
168
169 // Destroy the stub and call GetOrDownloadAttachments again.
170 stub_thread->task_runner()->DeleteSoon(FROM_HERE, stub.release());
171 WaitForStubThread();
172
173 // Now that the wrapped object has been destroyed, call again and see that we
174 // don't crash and the count remains the same.
175 proxy->GetOrDownloadAttachments(AttachmentIdList(), callback_get_or_download);
176 WaitForStubThread();
177 WaitForStubThread();
178 base::RunLoop().RunUntilIdle();
179 EXPECT_EQ(1, count_callback_get_or_download);
180 }
181
182 } // namespace syncer
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698