| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "sync/internal_api/syncapi_server_connection_manager.h" | 5 #include "sync/internal_api/syncapi_server_connection_manager.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/synchronization/waitable_event.h" | 10 #include "base/synchronization/waitable_event.h" |
| 11 #include "base/test/test_timeouts.h" | 11 #include "base/test/test_timeouts.h" |
| 12 #include "base/threading/thread.h" | 12 #include "base/threading/thread.h" |
| 13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 14 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
| 15 #include "sync/internal_api/public/base/cancelation_signal.h" |
| 15 #include "sync/internal_api/public/http_post_provider_factory.h" | 16 #include "sync/internal_api/public/http_post_provider_factory.h" |
| 16 #include "sync/internal_api/public/http_post_provider_interface.h" | 17 #include "sync/internal_api/public/http_post_provider_interface.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 19 |
| 19 namespace syncer { | 20 namespace syncer { |
| 20 namespace { | 21 namespace { |
| 21 | 22 |
| 22 using base::TimeDelta; | 23 using base::TimeDelta; |
| 23 | 24 |
| 24 class BlockingHttpPost : public HttpPostProviderInterface { | 25 class BlockingHttpPost : public HttpPostProviderInterface { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 50 virtual void Abort() OVERRIDE { | 51 virtual void Abort() OVERRIDE { |
| 51 wait_for_abort_.Signal(); | 52 wait_for_abort_.Signal(); |
| 52 } | 53 } |
| 53 private: | 54 private: |
| 54 base::WaitableEvent wait_for_abort_; | 55 base::WaitableEvent wait_for_abort_; |
| 55 }; | 56 }; |
| 56 | 57 |
| 57 class BlockingHttpPostFactory : public HttpPostProviderFactory { | 58 class BlockingHttpPostFactory : public HttpPostProviderFactory { |
| 58 public: | 59 public: |
| 59 virtual ~BlockingHttpPostFactory() {} | 60 virtual ~BlockingHttpPostFactory() {} |
| 61 virtual void Init(const std::string& user_agent) OVERRIDE {} |
| 60 virtual HttpPostProviderInterface* Create() OVERRIDE { | 62 virtual HttpPostProviderInterface* Create() OVERRIDE { |
| 61 return new BlockingHttpPost(); | 63 return new BlockingHttpPost(); |
| 62 } | 64 } |
| 63 virtual void Destroy(HttpPostProviderInterface* http) OVERRIDE { | 65 virtual void Destroy(HttpPostProviderInterface* http) OVERRIDE { |
| 64 delete static_cast<BlockingHttpPost*>(http); | 66 delete static_cast<BlockingHttpPost*>(http); |
| 65 } | 67 } |
| 66 virtual void Shutdown() OVERRIDE {} | |
| 67 }; | 68 }; |
| 68 | 69 |
| 69 } // namespace | 70 } // namespace |
| 70 | 71 |
| 71 TEST(SyncAPIServerConnectionManagerTest, EarlyAbortPost) { | 72 // Ask the ServerConnectionManager to stop before it is created. |
| 73 TEST(SyncAPIServerConnectionManagerTest, VeryEarlyAbortPost) { |
| 74 CancelationSignal signal; |
| 75 signal.Signal(); |
| 72 SyncAPIServerConnectionManager server( | 76 SyncAPIServerConnectionManager server( |
| 73 "server", 0, true, false, new BlockingHttpPostFactory()); | 77 "server", 0, true, false, new BlockingHttpPostFactory(), &signal); |
| 74 | 78 |
| 75 ServerConnectionManager::PostBufferParams params; | 79 ServerConnectionManager::PostBufferParams params; |
| 76 ScopedServerStatusWatcher watcher(&server, ¶ms.response); | 80 ScopedServerStatusWatcher watcher(&server, ¶ms.response); |
| 77 | 81 |
| 78 server.TerminateAllIO(); | |
| 79 bool result = server.PostBufferToPath( | 82 bool result = server.PostBufferToPath( |
| 80 ¶ms, "/testpath", "testauth", &watcher); | 83 ¶ms, "/testpath", "testauth", &watcher); |
| 81 | 84 |
| 85 EXPECT_FALSE(result); |
| 86 EXPECT_EQ(HttpResponse::CONNECTION_UNAVAILABLE, |
| 87 params.response.server_status); |
| 88 } |
| 89 |
| 90 // Ask the ServerConnectionManager to stop before its first request is made. |
| 91 TEST(SyncAPIServerConnectionManagerTest, EarlyAbortPost) { |
| 92 CancelationSignal signal; |
| 93 SyncAPIServerConnectionManager server( |
| 94 "server", 0, true, false, new BlockingHttpPostFactory(), &signal); |
| 95 |
| 96 ServerConnectionManager::PostBufferParams params; |
| 97 ScopedServerStatusWatcher watcher(&server, ¶ms.response); |
| 98 |
| 99 signal.Signal(); |
| 100 bool result = server.PostBufferToPath( |
| 101 ¶ms, "/testpath", "testauth", &watcher); |
| 102 |
| 82 EXPECT_FALSE(result); | 103 EXPECT_FALSE(result); |
| 83 EXPECT_EQ(HttpResponse::CONNECTION_UNAVAILABLE, | 104 EXPECT_EQ(HttpResponse::CONNECTION_UNAVAILABLE, |
| 84 params.response.server_status); | 105 params.response.server_status); |
| 85 } | 106 } |
| 86 | 107 |
| 108 // Ask the ServerConnectionManager to stop during a request. |
| 87 TEST(SyncAPIServerConnectionManagerTest, AbortPost) { | 109 TEST(SyncAPIServerConnectionManagerTest, AbortPost) { |
| 110 CancelationSignal signal; |
| 88 SyncAPIServerConnectionManager server( | 111 SyncAPIServerConnectionManager server( |
| 89 "server", 0, true, false, new BlockingHttpPostFactory()); | 112 "server", 0, true, false, new BlockingHttpPostFactory(), &signal); |
| 90 | 113 |
| 91 ServerConnectionManager::PostBufferParams params; | 114 ServerConnectionManager::PostBufferParams params; |
| 92 ScopedServerStatusWatcher watcher(&server, ¶ms.response); | 115 ScopedServerStatusWatcher watcher(&server, ¶ms.response); |
| 93 | 116 |
| 94 base::Thread abort_thread("Test_AbortThread"); | 117 base::Thread abort_thread("Test_AbortThread"); |
| 95 ASSERT_TRUE(abort_thread.Start()); | 118 ASSERT_TRUE(abort_thread.Start()); |
| 96 abort_thread.message_loop()->PostDelayedTask( | 119 abort_thread.message_loop()->PostDelayedTask( |
| 97 FROM_HERE, | 120 FROM_HERE, |
| 98 base::Bind(&ServerConnectionManager::TerminateAllIO, | 121 base::Bind(&CancelationSignal::Signal, |
| 99 base::Unretained(&server)), | 122 base::Unretained(&signal)), |
| 100 TestTimeouts::tiny_timeout()); | 123 TestTimeouts::tiny_timeout()); |
| 101 | 124 |
| 102 bool result = server.PostBufferToPath( | 125 bool result = server.PostBufferToPath( |
| 103 ¶ms, "/testpath", "testauth", &watcher); | 126 ¶ms, "/testpath", "testauth", &watcher); |
| 104 | 127 |
| 105 EXPECT_FALSE(result); | 128 EXPECT_FALSE(result); |
| 106 EXPECT_EQ(HttpResponse::CONNECTION_UNAVAILABLE, | 129 EXPECT_EQ(HttpResponse::CONNECTION_UNAVAILABLE, |
| 107 params.response.server_status); | 130 params.response.server_status); |
| 108 abort_thread.Stop(); | 131 abort_thread.Stop(); |
| 109 } | 132 } |
| 110 | 133 |
| 111 } // namespace syncer | 134 } // namespace syncer |
| OLD | NEW |