OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "sync/internal_api/syncapi_server_connection_manager.h" | 5 #include "components/sync/core_impl/syncapi_server_connection_manager.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 #include "base/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
11 #include "base/synchronization/waitable_event.h" | 11 #include "base/synchronization/waitable_event.h" |
12 #include "base/test/test_timeouts.h" | 12 #include "base/test/test_timeouts.h" |
13 #include "base/threading/thread.h" | 13 #include "base/threading/thread.h" |
14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 15 #include "components/sync/base/cancelation_signal.h" |
| 16 #include "components/sync/core/http_post_provider_factory.h" |
| 17 #include "components/sync/core/http_post_provider_interface.h" |
15 #include "net/base/net_errors.h" | 18 #include "net/base/net_errors.h" |
16 #include "sync/internal_api/public/base/cancelation_signal.h" | |
17 #include "sync/internal_api/public/http_post_provider_factory.h" | |
18 #include "sync/internal_api/public/http_post_provider_interface.h" | |
19 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
20 | 20 |
21 namespace syncer { | 21 namespace syncer { |
22 namespace { | 22 namespace { |
23 | 23 |
24 using base::TimeDelta; | 24 using base::TimeDelta; |
25 | 25 |
26 class BlockingHttpPost : public HttpPostProviderInterface { | 26 class BlockingHttpPost : public HttpPostProviderInterface { |
27 public: | 27 public: |
28 BlockingHttpPost() | 28 BlockingHttpPost() |
(...skipping 11 matching lines...) Expand all Loading... |
40 *error_code = net::ERR_ABORTED; | 40 *error_code = net::ERR_ABORTED; |
41 return false; | 41 return false; |
42 } | 42 } |
43 int GetResponseContentLength() const override { return 0; } | 43 int GetResponseContentLength() const override { return 0; } |
44 const char* GetResponseContent() const override { return ""; } | 44 const char* GetResponseContent() const override { return ""; } |
45 const std::string GetResponseHeaderValue( | 45 const std::string GetResponseHeaderValue( |
46 const std::string& name) const override { | 46 const std::string& name) const override { |
47 return std::string(); | 47 return std::string(); |
48 } | 48 } |
49 void Abort() override { wait_for_abort_.Signal(); } | 49 void Abort() override { wait_for_abort_.Signal(); } |
| 50 |
50 private: | 51 private: |
51 base::WaitableEvent wait_for_abort_; | 52 base::WaitableEvent wait_for_abort_; |
52 }; | 53 }; |
53 | 54 |
54 class BlockingHttpPostFactory : public HttpPostProviderFactory { | 55 class BlockingHttpPostFactory : public HttpPostProviderFactory { |
55 public: | 56 public: |
56 ~BlockingHttpPostFactory() override {} | 57 ~BlockingHttpPostFactory() override {} |
57 void Init(const std::string& user_agent, | 58 void Init(const std::string& user_agent, |
58 const BindToTrackerCallback& bind_to_tracker_callback) override {} | 59 const BindToTrackerCallback& bind_to_tracker_callback) override {} |
59 | 60 |
60 HttpPostProviderInterface* Create() override { | 61 HttpPostProviderInterface* Create() override { |
61 return new BlockingHttpPost(); | 62 return new BlockingHttpPost(); |
62 } | 63 } |
63 void Destroy(HttpPostProviderInterface* http) override { | 64 void Destroy(HttpPostProviderInterface* http) override { |
64 delete static_cast<BlockingHttpPost*>(http); | 65 delete static_cast<BlockingHttpPost*>(http); |
65 } | 66 } |
66 }; | 67 }; |
67 | 68 |
68 } // namespace | 69 } // namespace |
69 | 70 |
70 // Ask the ServerConnectionManager to stop before it is created. | 71 // Ask the ServerConnectionManager to stop before it is created. |
71 TEST(SyncAPIServerConnectionManagerTest, VeryEarlyAbortPost) { | 72 TEST(SyncAPIServerConnectionManagerTest, VeryEarlyAbortPost) { |
72 CancelationSignal signal; | 73 CancelationSignal signal; |
73 signal.Signal(); | 74 signal.Signal(); |
74 SyncAPIServerConnectionManager server( | 75 SyncAPIServerConnectionManager server("server", 0, true, |
75 "server", 0, true, new BlockingHttpPostFactory(), &signal); | 76 new BlockingHttpPostFactory(), &signal); |
76 | 77 |
77 ServerConnectionManager::PostBufferParams params; | 78 ServerConnectionManager::PostBufferParams params; |
78 | 79 |
79 bool result = server.PostBufferToPath(¶ms, "/testpath", "testauth"); | 80 bool result = server.PostBufferToPath(¶ms, "/testpath", "testauth"); |
80 | 81 |
81 EXPECT_FALSE(result); | 82 EXPECT_FALSE(result); |
82 EXPECT_EQ(HttpResponse::CONNECTION_UNAVAILABLE, | 83 EXPECT_EQ(HttpResponse::CONNECTION_UNAVAILABLE, |
83 params.response.server_status); | 84 params.response.server_status); |
84 } | 85 } |
85 | 86 |
86 // Ask the ServerConnectionManager to stop before its first request is made. | 87 // Ask the ServerConnectionManager to stop before its first request is made. |
87 TEST(SyncAPIServerConnectionManagerTest, EarlyAbortPost) { | 88 TEST(SyncAPIServerConnectionManagerTest, EarlyAbortPost) { |
88 CancelationSignal signal; | 89 CancelationSignal signal; |
89 SyncAPIServerConnectionManager server( | 90 SyncAPIServerConnectionManager server("server", 0, true, |
90 "server", 0, true, new BlockingHttpPostFactory(), &signal); | 91 new BlockingHttpPostFactory(), &signal); |
91 | 92 |
92 ServerConnectionManager::PostBufferParams params; | 93 ServerConnectionManager::PostBufferParams params; |
93 | 94 |
94 signal.Signal(); | 95 signal.Signal(); |
95 bool result = server.PostBufferToPath(¶ms, "/testpath", "testauth"); | 96 bool result = server.PostBufferToPath(¶ms, "/testpath", "testauth"); |
96 | 97 |
97 EXPECT_FALSE(result); | 98 EXPECT_FALSE(result); |
98 EXPECT_EQ(HttpResponse::CONNECTION_UNAVAILABLE, | 99 EXPECT_EQ(HttpResponse::CONNECTION_UNAVAILABLE, |
99 params.response.server_status); | 100 params.response.server_status); |
100 } | 101 } |
101 | 102 |
102 // Ask the ServerConnectionManager to stop during a request. | 103 // Ask the ServerConnectionManager to stop during a request. |
103 TEST(SyncAPIServerConnectionManagerTest, AbortPost) { | 104 TEST(SyncAPIServerConnectionManagerTest, AbortPost) { |
104 CancelationSignal signal; | 105 CancelationSignal signal; |
105 SyncAPIServerConnectionManager server( | 106 SyncAPIServerConnectionManager server("server", 0, true, |
106 "server", 0, true, new BlockingHttpPostFactory(), &signal); | 107 new BlockingHttpPostFactory(), &signal); |
107 | 108 |
108 ServerConnectionManager::PostBufferParams params; | 109 ServerConnectionManager::PostBufferParams params; |
109 | 110 |
110 base::Thread abort_thread("Test_AbortThread"); | 111 base::Thread abort_thread("Test_AbortThread"); |
111 ASSERT_TRUE(abort_thread.Start()); | 112 ASSERT_TRUE(abort_thread.Start()); |
112 abort_thread.task_runner()->PostDelayedTask( | 113 abort_thread.task_runner()->PostDelayedTask( |
113 FROM_HERE, | 114 FROM_HERE, |
114 base::Bind(&CancelationSignal::Signal, base::Unretained(&signal)), | 115 base::Bind(&CancelationSignal::Signal, base::Unretained(&signal)), |
115 TestTimeouts::tiny_timeout()); | 116 TestTimeouts::tiny_timeout()); |
116 | 117 |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 ServerConnectionManager::PostBufferParams params; | 183 ServerConnectionManager::PostBufferParams params; |
183 | 184 |
184 bool result = server.PostBufferToPath(¶ms, "/testpath", "testauth"); | 185 bool result = server.PostBufferToPath(¶ms, "/testpath", "testauth"); |
185 | 186 |
186 EXPECT_FALSE(result); | 187 EXPECT_FALSE(result); |
187 EXPECT_EQ(HttpResponse::CONNECTION_UNAVAILABLE, | 188 EXPECT_EQ(HttpResponse::CONNECTION_UNAVAILABLE, |
188 params.response.server_status); | 189 params.response.server_status); |
189 } | 190 } |
190 | 191 |
191 } // namespace syncer | 192 } // namespace syncer |
OLD | NEW |