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