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 ServerConnectionManager::PostBufferParams params; |
| 80 ScopedServerStatusWatcher watcher(&server, ¶ms.response); |
| 81 |
| 82 server.TerminateAllIO(); |
| 83 bool result = server.PostBufferToPath( |
| 84 ¶ms, "/testpath", "testauth", &watcher); |
| 85 |
| 86 EXPECT_FALSE(result); |
| 87 EXPECT_EQ(HttpResponse::CONNECTION_UNAVAILABLE, |
| 88 params.response.server_status); |
| 89 } |
| 90 |
| 91 TEST(SyncAPIServerConnectionManagerTest, EarlyAbortCheckTime) { |
| 92 SyncAPIServerConnectionManager server( |
| 93 "server", 0, true, "1", new BlockingHttpPostFactory()); |
| 94 int32 time = 0; |
| 95 server.TerminateAllIO(); |
| 96 bool result = server.CheckTime(&time); |
| 97 EXPECT_FALSE(result); |
| 98 } |
| 99 |
| 100 TEST(SyncAPIServerConnectionManagerTest, AbortPost) { |
| 101 SyncAPIServerConnectionManager server( |
| 102 "server", 0, true, "1", new BlockingHttpPostFactory()); |
| 103 |
| 104 ServerConnectionManager::PostBufferParams params; |
| 105 ScopedServerStatusWatcher watcher(&server, ¶ms.response); |
| 106 |
| 107 base::Thread abort_thread("Test_AbortThread"); |
| 108 ASSERT_TRUE(abort_thread.Start()); |
| 109 abort_thread.message_loop()->PostDelayedTask( |
| 110 FROM_HERE, |
| 111 base::Bind(&ServerConnectionManager::TerminateAllIO, |
| 112 base::Unretained(&server)), |
| 113 TestTimeouts::tiny_timeout_ms()); |
| 114 |
| 115 bool result = server.PostBufferToPath( |
| 116 ¶ms, "/testpath", "testauth", &watcher); |
| 117 |
| 118 EXPECT_FALSE(result); |
| 119 EXPECT_EQ(HttpResponse::CONNECTION_UNAVAILABLE, |
| 120 params.response.server_status); |
| 121 abort_thread.Stop(); |
| 122 } |
| 123 |
| 124 TEST(SyncAPIServerConnectionManagerTest, AbortCheckTime) { |
| 125 SyncAPIServerConnectionManager server( |
| 126 "server", 0, true, "1", new BlockingHttpPostFactory()); |
| 127 |
| 128 base::Thread abort_thread("Test_AbortThread"); |
| 129 ASSERT_TRUE(abort_thread.Start()); |
| 130 abort_thread.message_loop()->PostDelayedTask( |
| 131 FROM_HERE, |
| 132 base::Bind(&ServerConnectionManager::TerminateAllIO, |
| 133 base::Unretained(&server)), |
| 134 TestTimeouts::tiny_timeout_ms()); |
| 135 |
| 136 int32 time = 0; |
| 137 bool result = server.CheckTime(&time); |
| 138 EXPECT_FALSE(result); |
| 139 abort_thread.Stop(); |
| 140 } |
| 141 |
| 142 } // namespace sync_api |
OLD | NEW |