OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 "chrome/browser/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/synchronization/waitable_event.h" |
| 11 #include "base/test/test_timeouts.h" |
| 12 #include "base/threading/thread.h" |
| 13 #include "base/time.h" |
| 14 #include "chrome/browser/sync/internal_api/http_post_provider_factory.h" |
| 15 #include "chrome/browser/sync/internal_api/http_post_provider_interface.h" |
| 16 #include "net/base/net_errors.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 |
| 19 using base::TimeDelta; |
| 20 using browser_sync::HttpResponse; |
| 21 using browser_sync::ServerConnectionManager; |
| 22 using browser_sync::ScopedServerStatusWatcher; |
| 23 |
| 24 namespace sync_api { |
| 25 namespace { |
| 26 |
| 27 class BlockingHttpPost : public HttpPostProviderInterface { |
| 28 public: |
| 29 BlockingHttpPost() : wait_for_abort_(false, false) {} |
| 30 virtual ~BlockingHttpPost() {} |
| 31 |
| 32 virtual void SetUserAgent(const char* user_agent) OVERRIDE {} |
| 33 virtual void SetExtraRequestHeaders(const char* headers) OVERRIDE {} |
| 34 virtual void SetURL(const char* url, int port) OVERRIDE {} |
| 35 virtual void SetPostPayload(const char* content_type, |
| 36 int content_length, |
| 37 const char* content) OVERRIDE {} |
| 38 virtual bool MakeSynchronousPost(int* os_error_code, int* response_code) |
| 39 OVERRIDE { |
| 40 wait_for_abort_.TimedWait(TimeDelta::FromMilliseconds( |
| 41 TestTimeouts::action_max_timeout_ms())); |
| 42 *os_error_code = net::ERR_ABORTED; |
| 43 return false; |
| 44 } |
| 45 virtual int GetResponseContentLength() const OVERRIDE { |
| 46 return 0; |
| 47 } |
| 48 virtual const char* GetResponseContent() const OVERRIDE { |
| 49 return ""; |
| 50 } |
| 51 virtual const std::string GetResponseHeaderValue( |
| 52 const std::string& name) const OVERRIDE { |
| 53 return ""; |
| 54 } |
| 55 virtual void Abort() OVERRIDE { |
| 56 wait_for_abort_.Signal(); |
| 57 } |
| 58 private: |
| 59 base::WaitableEvent wait_for_abort_; |
| 60 }; |
| 61 |
| 62 class BlockingHttpPostFactory : public HttpPostProviderFactory { |
| 63 public: |
| 64 virtual ~BlockingHttpPostFactory() {} |
| 65 virtual HttpPostProviderInterface* Create() OVERRIDE { |
| 66 return new BlockingHttpPost(); |
| 67 } |
| 68 virtual void Destroy(HttpPostProviderInterface* http) OVERRIDE { |
| 69 delete http; |
| 70 } |
| 71 }; |
| 72 |
| 73 } // namespace |
| 74 |
| 75 TEST(SyncAPIServerConnectionManagerTest, EarlyAbortPost) { |
| 76 SyncAPIServerConnectionManager server( |
| 77 "server", 0, true, "1", new BlockingHttpPostFactory()); |
| 78 |
| 79 std::string buffer_out; |
| 80 HttpResponse http_response; |
| 81 ServerConnectionManager::PostBufferParams params = { |
| 82 "testbuffer", &buffer_out, &http_response |
| 83 }; |
| 84 ScopedServerStatusWatcher watcher(&server, &http_response); |
| 85 |
| 86 server.TerminateAllIO(); |
| 87 bool result = server.PostBufferToPath( |
| 88 ¶ms, "/testpath", "testauth", &watcher); |
| 89 |
| 90 EXPECT_FALSE(result); |
| 91 EXPECT_EQ(HttpResponse::CONNECTION_UNAVAILABLE, |
| 92 params.response->server_status); |
| 93 } |
| 94 |
| 95 TEST(SyncAPIServerConnectionManagerTest, EarlyAbortCheckTime) { |
| 96 SyncAPIServerConnectionManager server( |
| 97 "server", 0, true, "1", new BlockingHttpPostFactory()); |
| 98 int32 time = 0; |
| 99 server.TerminateAllIO(); |
| 100 bool result = server.CheckTime(&time); |
| 101 EXPECT_FALSE(result); |
| 102 } |
| 103 |
| 104 TEST(SyncAPIServerConnectionManagerTest, AbortPost) { |
| 105 SyncAPIServerConnectionManager server( |
| 106 "server", 0, true, "1", new BlockingHttpPostFactory()); |
| 107 |
| 108 std::string buffer_out; |
| 109 HttpResponse http_response; |
| 110 ServerConnectionManager::PostBufferParams params = { |
| 111 "testbuffer", &buffer_out, &http_response |
| 112 }; |
| 113 ScopedServerStatusWatcher watcher(&server, &http_response); |
| 114 |
| 115 base::Thread abort_thread("Test_AbortThread"); |
| 116 ASSERT_TRUE(abort_thread.Start()); |
| 117 abort_thread.message_loop()->PostDelayedTask( |
| 118 FROM_HERE, |
| 119 base::Bind(&ServerConnectionManager::TerminateAllIO, |
| 120 base::Unretained(&server)), |
| 121 TestTimeouts::tiny_timeout_ms()); |
| 122 |
| 123 bool result = server.PostBufferToPath( |
| 124 ¶ms, "/testpath", "testauth", &watcher); |
| 125 |
| 126 EXPECT_FALSE(result); |
| 127 EXPECT_EQ(HttpResponse::CONNECTION_UNAVAILABLE, |
| 128 params.response->server_status); |
| 129 abort_thread.Stop(); |
| 130 } |
| 131 |
| 132 TEST(SyncAPIServerConnectionManagerTest, AbortCheckTime) { |
| 133 SyncAPIServerConnectionManager server( |
| 134 "server", 0, true, "1", new BlockingHttpPostFactory()); |
| 135 |
| 136 base::Thread abort_thread("Test_AbortThread"); |
| 137 ASSERT_TRUE(abort_thread.Start()); |
| 138 abort_thread.message_loop()->PostDelayedTask( |
| 139 FROM_HERE, |
| 140 base::Bind(&ServerConnectionManager::TerminateAllIO, |
| 141 base::Unretained(&server)), |
| 142 TestTimeouts::tiny_timeout_ms()); |
| 143 |
| 144 int32 time = 0; |
| 145 bool result = server.CheckTime(&time); |
| 146 EXPECT_FALSE(result); |
| 147 abort_thread.Stop(); |
| 148 } |
| 149 |
| 150 } // namespace sync_api |
OLD | NEW |