| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "sync/internal_api/syncapi_server_connection_manager.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/bind_helpers.h" | |
| 9 #include "base/compiler_specific.h" | |
| 10 #include "base/single_thread_task_runner.h" | |
| 11 #include "base/synchronization/waitable_event.h" | |
| 12 #include "base/test/test_timeouts.h" | |
| 13 #include "base/threading/thread.h" | |
| 14 #include "base/time/time.h" | |
| 15 #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" | |
| 20 | |
| 21 namespace syncer { | |
| 22 namespace { | |
| 23 | |
| 24 using base::TimeDelta; | |
| 25 | |
| 26 class BlockingHttpPost : public HttpPostProviderInterface { | |
| 27 public: | |
| 28 BlockingHttpPost() | |
| 29 : wait_for_abort_(base::WaitableEvent::ResetPolicy::AUTOMATIC, | |
| 30 base::WaitableEvent::InitialState::NOT_SIGNALED) {} | |
| 31 ~BlockingHttpPost() override {} | |
| 32 | |
| 33 void SetExtraRequestHeaders(const char* headers) override {} | |
| 34 void SetURL(const char* url, int port) override {} | |
| 35 void SetPostPayload(const char* content_type, | |
| 36 int content_length, | |
| 37 const char* content) override {} | |
| 38 bool MakeSynchronousPost(int* error_code, int* response_code) override { | |
| 39 wait_for_abort_.TimedWait(TestTimeouts::action_max_timeout()); | |
| 40 *error_code = net::ERR_ABORTED; | |
| 41 return false; | |
| 42 } | |
| 43 int GetResponseContentLength() const override { return 0; } | |
| 44 const char* GetResponseContent() const override { return ""; } | |
| 45 const std::string GetResponseHeaderValue( | |
| 46 const std::string& name) const override { | |
| 47 return std::string(); | |
| 48 } | |
| 49 void Abort() override { wait_for_abort_.Signal(); } | |
| 50 private: | |
| 51 base::WaitableEvent wait_for_abort_; | |
| 52 }; | |
| 53 | |
| 54 class BlockingHttpPostFactory : public HttpPostProviderFactory { | |
| 55 public: | |
| 56 ~BlockingHttpPostFactory() override {} | |
| 57 void Init(const std::string& user_agent, | |
| 58 const BindToTrackerCallback& bind_to_tracker_callback) override {} | |
| 59 | |
| 60 HttpPostProviderInterface* Create() override { | |
| 61 return new BlockingHttpPost(); | |
| 62 } | |
| 63 void Destroy(HttpPostProviderInterface* http) override { | |
| 64 delete static_cast<BlockingHttpPost*>(http); | |
| 65 } | |
| 66 }; | |
| 67 | |
| 68 } // namespace | |
| 69 | |
| 70 // Ask the ServerConnectionManager to stop before it is created. | |
| 71 TEST(SyncAPIServerConnectionManagerTest, VeryEarlyAbortPost) { | |
| 72 CancelationSignal signal; | |
| 73 signal.Signal(); | |
| 74 SyncAPIServerConnectionManager server( | |
| 75 "server", 0, true, new BlockingHttpPostFactory(), &signal); | |
| 76 | |
| 77 ServerConnectionManager::PostBufferParams params; | |
| 78 | |
| 79 bool result = server.PostBufferToPath(¶ms, "/testpath", "testauth"); | |
| 80 | |
| 81 EXPECT_FALSE(result); | |
| 82 EXPECT_EQ(HttpResponse::CONNECTION_UNAVAILABLE, | |
| 83 params.response.server_status); | |
| 84 } | |
| 85 | |
| 86 // Ask the ServerConnectionManager to stop before its first request is made. | |
| 87 TEST(SyncAPIServerConnectionManagerTest, EarlyAbortPost) { | |
| 88 CancelationSignal signal; | |
| 89 SyncAPIServerConnectionManager server( | |
| 90 "server", 0, true, new BlockingHttpPostFactory(), &signal); | |
| 91 | |
| 92 ServerConnectionManager::PostBufferParams params; | |
| 93 | |
| 94 signal.Signal(); | |
| 95 bool result = server.PostBufferToPath(¶ms, "/testpath", "testauth"); | |
| 96 | |
| 97 EXPECT_FALSE(result); | |
| 98 EXPECT_EQ(HttpResponse::CONNECTION_UNAVAILABLE, | |
| 99 params.response.server_status); | |
| 100 } | |
| 101 | |
| 102 // Ask the ServerConnectionManager to stop during a request. | |
| 103 TEST(SyncAPIServerConnectionManagerTest, AbortPost) { | |
| 104 CancelationSignal signal; | |
| 105 SyncAPIServerConnectionManager server( | |
| 106 "server", 0, true, new BlockingHttpPostFactory(), &signal); | |
| 107 | |
| 108 ServerConnectionManager::PostBufferParams params; | |
| 109 | |
| 110 base::Thread abort_thread("Test_AbortThread"); | |
| 111 ASSERT_TRUE(abort_thread.Start()); | |
| 112 abort_thread.task_runner()->PostDelayedTask( | |
| 113 FROM_HERE, | |
| 114 base::Bind(&CancelationSignal::Signal, base::Unretained(&signal)), | |
| 115 TestTimeouts::tiny_timeout()); | |
| 116 | |
| 117 bool result = server.PostBufferToPath(¶ms, "/testpath", "testauth"); | |
| 118 | |
| 119 EXPECT_FALSE(result); | |
| 120 EXPECT_EQ(HttpResponse::CONNECTION_UNAVAILABLE, | |
| 121 params.response.server_status); | |
| 122 abort_thread.Stop(); | |
| 123 } | |
| 124 | |
| 125 namespace { | |
| 126 | |
| 127 class FailingHttpPost : public HttpPostProviderInterface { | |
| 128 public: | |
| 129 explicit FailingHttpPost(int error_code) : error_code_(error_code) {} | |
| 130 ~FailingHttpPost() override {} | |
| 131 | |
| 132 void SetExtraRequestHeaders(const char* headers) override {} | |
| 133 void SetURL(const char* url, int port) override {} | |
| 134 void SetPostPayload(const char* content_type, | |
| 135 int content_length, | |
| 136 const char* content) override {} | |
| 137 bool MakeSynchronousPost(int* error_code, int* response_code) override { | |
| 138 *error_code = error_code_; | |
| 139 return false; | |
| 140 } | |
| 141 int GetResponseContentLength() const override { return 0; } | |
| 142 const char* GetResponseContent() const override { return ""; } | |
| 143 const std::string GetResponseHeaderValue( | |
| 144 const std::string& name) const override { | |
| 145 return std::string(); | |
| 146 } | |
| 147 void Abort() override {} | |
| 148 | |
| 149 private: | |
| 150 int error_code_; | |
| 151 }; | |
| 152 | |
| 153 class FailingHttpPostFactory : public HttpPostProviderFactory { | |
| 154 public: | |
| 155 explicit FailingHttpPostFactory(int error_code) : error_code_(error_code) {} | |
| 156 ~FailingHttpPostFactory() override {} | |
| 157 void Init(const std::string& user_agent, | |
| 158 const BindToTrackerCallback& bind_to_tracker_callback) override {} | |
| 159 | |
| 160 HttpPostProviderInterface* Create() override { | |
| 161 return new FailingHttpPost(error_code_); | |
| 162 } | |
| 163 void Destroy(HttpPostProviderInterface* http) override { | |
| 164 delete static_cast<FailingHttpPost*>(http); | |
| 165 } | |
| 166 | |
| 167 private: | |
| 168 int error_code_; | |
| 169 }; | |
| 170 | |
| 171 } // namespace | |
| 172 | |
| 173 // Fail request with TIMED_OUT error. Make sure server status is | |
| 174 // CONNECTION_UNAVAILABLE and therefore request will be retried after network | |
| 175 // change. | |
| 176 TEST(SyncAPIServerConnectionManagerTest, FailPostWithTimedOut) { | |
| 177 CancelationSignal signal; | |
| 178 SyncAPIServerConnectionManager server( | |
| 179 "server", 0, true, new FailingHttpPostFactory(net::ERR_TIMED_OUT), | |
| 180 &signal); | |
| 181 | |
| 182 ServerConnectionManager::PostBufferParams params; | |
| 183 | |
| 184 bool result = server.PostBufferToPath(¶ms, "/testpath", "testauth"); | |
| 185 | |
| 186 EXPECT_FALSE(result); | |
| 187 EXPECT_EQ(HttpResponse::CONNECTION_UNAVAILABLE, | |
| 188 params.response.server_status); | |
| 189 } | |
| 190 | |
| 191 } // namespace syncer | |
| OLD | NEW |