Chromium Code Reviews| 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 {} | |
|
akalin
2011/08/31 21:10:03
#include "base/compiler_specific.h" for 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, EarlyAbortCheckTime) { | |
| 98 SyncAPIServerConnectionManager server( | |
| 99 "server", 0, true, "1", new BlockingHttpPostFactory()); | |
| 100 int32 time = 0; | |
| 101 server.TerminateAllIO(); | |
| 102 bool result = server.CheckTime(&time); | |
| 103 EXPECT_FALSE(result); | |
| 104 } | |
| 105 | |
| 106 TEST(SyncAPIServerConnectionManagerTest, AbortPost) { | |
| 107 SyncAPIServerConnectionManager server( | |
| 108 "server", 0, true, "1", new BlockingHttpPostFactory()); | |
| 109 | |
| 110 std::string buffer_out; | |
| 111 HttpResponse http_response; | |
| 112 ServerConnectionManager::PostBufferParams params = { | |
| 113 "testbuffer", &buffer_out, &http_response | |
| 114 }; | |
| 115 ScopedServerStatusWatcher watcher(&server, &http_response); | |
| 116 | |
| 117 base::Thread abort_thread("Test_AbortThread"); | |
| 118 ASSERT_TRUE(abort_thread.Start()); | |
| 119 abort_thread.message_loop()->PostDelayedTask( | |
| 120 FROM_HERE, | |
| 121 base::Bind(&CallTerminateAllIO, &server), | |
|
akalin
2011/08/31 21:10:03
why not base::Bind(&ServerConnectionManager::Termi
| |
| 122 TestTimeouts::tiny_timeout_ms()); | |
| 123 | |
| 124 bool result = server.PostBufferToPath( | |
| 125 ¶ms, "/testpath", "testauth", &watcher); | |
| 126 | |
| 127 EXPECT_FALSE(result); | |
| 128 EXPECT_EQ(HttpResponse::CONNECTION_UNAVAILABLE, | |
| 129 params.response->server_status); | |
| 130 abort_thread.Stop(); | |
| 131 } | |
| 132 | |
| 133 TEST(SyncAPIServerConnectionManagerTest, AbortCheckTime) { | |
| 134 SyncAPIServerConnectionManager server( | |
| 135 "server", 0, true, "1", new BlockingHttpPostFactory()); | |
| 136 | |
| 137 base::Thread abort_thread("Test_AbortThread"); | |
| 138 ASSERT_TRUE(abort_thread.Start()); | |
| 139 abort_thread.message_loop()->PostDelayedTask( | |
| 140 FROM_HERE, | |
| 141 base::Bind(&CallTerminateAllIO, &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 |