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