OLD | NEW |
| (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 "components/sync/api/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 "components/sync/base/model_type.h" | |
20 #include "components/sync/protocol/sync.pb.h" | |
21 #include "testing/gtest/include/gtest/gtest.h" | |
22 | |
23 namespace syncer { | |
24 | |
25 // A stub implementation of AttachmentService that counts the number of times | |
26 // its methods are invoked. | |
27 class StubAttachmentService : public AttachmentService, | |
28 public base::NonThreadSafe { | |
29 public: | |
30 StubAttachmentService() : call_count_(0), weak_ptr_factory_(this) { | |
31 // DetachFromThread because we will be constructed in one thread and | |
32 // used/destroyed in another. | |
33 DetachFromThread(); | |
34 } | |
35 | |
36 ~StubAttachmentService() override {} | |
37 | |
38 void GetOrDownloadAttachments( | |
39 const AttachmentIdList& attachment_ids, | |
40 const GetOrDownloadCallback& callback) override { | |
41 CalledOnValidThread(); | |
42 Increment(); | |
43 std::unique_ptr<AttachmentMap> attachments(new AttachmentMap()); | |
44 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
45 FROM_HERE, | |
46 base::Bind(callback, AttachmentService::GET_UNSPECIFIED_ERROR, | |
47 base::Passed(&attachments))); | |
48 } | |
49 | |
50 void UploadAttachments(const AttachmentIdList& attachments_ids) override { | |
51 CalledOnValidThread(); | |
52 Increment(); | |
53 } | |
54 | |
55 virtual base::WeakPtr<AttachmentService> AsWeakPtr() { | |
56 return weak_ptr_factory_.GetWeakPtr(); | |
57 } | |
58 | |
59 // Return the number of method invocations. | |
60 int GetCallCount() const { | |
61 base::AutoLock lock(mutex_); | |
62 return call_count_; | |
63 } | |
64 | |
65 private: | |
66 // Protects call_count_. | |
67 mutable base::Lock mutex_; | |
68 int call_count_; | |
69 | |
70 // Must be last data member. | |
71 base::WeakPtrFactory<AttachmentService> weak_ptr_factory_; | |
72 | |
73 void Increment() { | |
74 base::AutoLock lock(mutex_); | |
75 ++call_count_; | |
76 } | |
77 }; | |
78 | |
79 class AttachmentServiceProxyTest : public testing::Test, | |
80 public base::NonThreadSafe { | |
81 protected: | |
82 AttachmentServiceProxyTest() {} | |
83 | |
84 void SetUp() override { | |
85 CalledOnValidThread(); | |
86 stub_thread.reset(new base::Thread("attachment service stub thread")); | |
87 stub_thread->Start(); | |
88 stub.reset(new StubAttachmentService); | |
89 proxy.reset(new AttachmentServiceProxy(stub_thread->task_runner(), | |
90 stub->AsWeakPtr())); | |
91 | |
92 callback_get_or_download = | |
93 base::Bind(&AttachmentServiceProxyTest::IncrementGetOrDownload, | |
94 base::Unretained(this)); | |
95 count_callback_get_or_download = 0; | |
96 } | |
97 | |
98 void TearDown() override { | |
99 // 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. | |
101 if (stub) { | |
102 stub_thread->task_runner()->DeleteSoon(FROM_HERE, stub.release()); | |
103 WaitForStubThread(); | |
104 } | |
105 stub_thread->Stop(); | |
106 } | |
107 | |
108 // a GetOrDownloadCallback | |
109 void IncrementGetOrDownload(const AttachmentService::GetOrDownloadResult&, | |
110 std::unique_ptr<AttachmentMap>) { | |
111 CalledOnValidThread(); | |
112 ++count_callback_get_or_download; | |
113 } | |
114 | |
115 void WaitForStubThread() { | |
116 base::WaitableEvent done(base::WaitableEvent::ResetPolicy::AUTOMATIC, | |
117 base::WaitableEvent::InitialState::NOT_SIGNALED); | |
118 stub_thread->task_runner()->PostTask( | |
119 FROM_HERE, | |
120 base::Bind(&base::WaitableEvent::Signal, base::Unretained(&done))); | |
121 done.Wait(); | |
122 } | |
123 | |
124 base::MessageLoop loop; | |
125 std::unique_ptr<base::Thread> stub_thread; | |
126 std::unique_ptr<StubAttachmentService> stub; | |
127 std::unique_ptr<AttachmentServiceProxy> proxy; | |
128 | |
129 AttachmentService::GetOrDownloadCallback callback_get_or_download; | |
130 | |
131 // number of times callback_get_or_download was invoked | |
132 int count_callback_get_or_download; | |
133 }; | |
134 | |
135 // 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 | |
137 // thread. | |
138 TEST_F(AttachmentServiceProxyTest, MethodsAreProxied) { | |
139 proxy->GetOrDownloadAttachments(AttachmentIdList(), callback_get_or_download); | |
140 proxy->UploadAttachments(AttachmentIdList()); | |
141 // Wait for the posted calls to execute in the stub thread. | |
142 WaitForStubThread(); | |
143 EXPECT_EQ(2, stub->GetCallCount()); | |
144 // 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 | |
146 // time to ensure the stub thread has executed the posted result callbacks. | |
147 WaitForStubThread(); | |
148 | |
149 base::RunLoop().RunUntilIdle(); | |
150 EXPECT_EQ(1, count_callback_get_or_download); | |
151 } | |
152 | |
153 // Verify that it's safe to use an AttachmentServiceProxy even after its wrapped | |
154 // AttachmentService has been destroyed. | |
155 TEST_F(AttachmentServiceProxyTest, WrappedIsDestroyed) { | |
156 proxy->GetOrDownloadAttachments(AttachmentIdList(), callback_get_or_download); | |
157 // Wait for the posted calls to execute in the stub thread. | |
158 WaitForStubThread(); | |
159 EXPECT_EQ(1, stub->GetCallCount()); | |
160 // Wait a second time ensure the stub thread has executed the posted result | |
161 // callbacks. | |
162 WaitForStubThread(); | |
163 | |
164 base::RunLoop().RunUntilIdle(); | |
165 EXPECT_EQ(1, count_callback_get_or_download); | |
166 | |
167 // Destroy the stub and call GetOrDownloadAttachments again. | |
168 stub_thread->task_runner()->DeleteSoon(FROM_HERE, stub.release()); | |
169 WaitForStubThread(); | |
170 | |
171 // Now that the wrapped object has been destroyed, call again and see that we | |
172 // don't crash and the count remains the same. | |
173 proxy->GetOrDownloadAttachments(AttachmentIdList(), callback_get_or_download); | |
174 WaitForStubThread(); | |
175 WaitForStubThread(); | |
176 base::RunLoop().RunUntilIdle(); | |
177 EXPECT_EQ(1, count_callback_get_or_download); | |
178 } | |
179 | |
180 } // namespace syncer | |
OLD | NEW |