| 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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 virtual HttpPostProviderInterface* Create() OVERRIDE { | 61 virtual HttpPostProviderInterface* Create() OVERRIDE { |
| 61 return new BlockingHttpPost(); | 62 return new BlockingHttpPost(); |
| 62 } | 63 } |
| 63 virtual void Destroy(HttpPostProviderInterface* http) OVERRIDE { | 64 virtual void Destroy(HttpPostProviderInterface* http) OVERRIDE { |
| 64 delete static_cast<BlockingHttpPost*>(http); | 65 delete static_cast<BlockingHttpPost*>(http); |
| 65 } | 66 } |
| 66 }; | 67 }; |
| 67 | 68 |
| 68 } // namespace | 69 } // namespace |
| 69 | 70 |
| 70 TEST(SyncAPIServerConnectionManagerTest, EarlyAbortPost) { | 71 // Ask the ServerConnectionManager to stop before it is created. |
| 72 TEST(SyncAPIServerConnectionManagerTest, VeryEarlyAbortPost) { |
| 73 CancelationSignal signal; |
| 74 signal.RequestStop(); |
| 71 SyncAPIServerConnectionManager server( | 75 SyncAPIServerConnectionManager server( |
| 72 "server", 0, true, false, new BlockingHttpPostFactory()); | 76 "server", 0, true, false, new BlockingHttpPostFactory(), &signal); |
| 73 | 77 |
| 74 ServerConnectionManager::PostBufferParams params; | 78 ServerConnectionManager::PostBufferParams params; |
| 75 ScopedServerStatusWatcher watcher(&server, ¶ms.response); | 79 ScopedServerStatusWatcher watcher(&server, ¶ms.response); |
| 76 | 80 |
| 77 server.TerminateAllIO(); | |
| 78 bool result = server.PostBufferToPath( | 81 bool result = server.PostBufferToPath( |
| 79 ¶ms, "/testpath", "testauth", &watcher); | 82 ¶ms, "/testpath", "testauth", &watcher); |
| 80 | 83 |
| 84 EXPECT_FALSE(result); |
| 85 EXPECT_EQ(HttpResponse::CONNECTION_UNAVAILABLE, |
| 86 params.response.server_status); |
| 87 } |
| 88 |
| 89 // Ask the ServerConnectionManager to stop before its first request is made. |
| 90 TEST(SyncAPIServerConnectionManagerTest, EarlyAbortPost) { |
| 91 CancelationSignal signal; |
| 92 SyncAPIServerConnectionManager server( |
| 93 "server", 0, true, false, new BlockingHttpPostFactory(), &signal); |
| 94 |
| 95 ServerConnectionManager::PostBufferParams params; |
| 96 ScopedServerStatusWatcher watcher(&server, ¶ms.response); |
| 97 |
| 98 signal.RequestStop(); |
| 99 bool result = server.PostBufferToPath( |
| 100 ¶ms, "/testpath", "testauth", &watcher); |
| 101 |
| 81 EXPECT_FALSE(result); | 102 EXPECT_FALSE(result); |
| 82 EXPECT_EQ(HttpResponse::CONNECTION_UNAVAILABLE, | 103 EXPECT_EQ(HttpResponse::CONNECTION_UNAVAILABLE, |
| 83 params.response.server_status); | 104 params.response.server_status); |
| 84 } | 105 } |
| 85 | 106 |
| 107 // Ask the ServerConnectionManager to stop during a request. |
| 86 TEST(SyncAPIServerConnectionManagerTest, AbortPost) { | 108 TEST(SyncAPIServerConnectionManagerTest, AbortPost) { |
| 109 CancelationSignal signal; |
| 87 SyncAPIServerConnectionManager server( | 110 SyncAPIServerConnectionManager server( |
| 88 "server", 0, true, false, new BlockingHttpPostFactory()); | 111 "server", 0, true, false, new BlockingHttpPostFactory(), &signal); |
| 89 | 112 |
| 90 ServerConnectionManager::PostBufferParams params; | 113 ServerConnectionManager::PostBufferParams params; |
| 91 ScopedServerStatusWatcher watcher(&server, ¶ms.response); | 114 ScopedServerStatusWatcher watcher(&server, ¶ms.response); |
| 92 | 115 |
| 93 base::Thread abort_thread("Test_AbortThread"); | 116 base::Thread abort_thread("Test_AbortThread"); |
| 94 ASSERT_TRUE(abort_thread.Start()); | 117 ASSERT_TRUE(abort_thread.Start()); |
| 95 abort_thread.message_loop()->PostDelayedTask( | 118 abort_thread.message_loop()->PostDelayedTask( |
| 96 FROM_HERE, | 119 FROM_HERE, |
| 97 base::Bind(&ServerConnectionManager::TerminateAllIO, | 120 base::Bind(&CancelationSignal::RequestStop, |
| 98 base::Unretained(&server)), | 121 base::Unretained(&signal)), |
| 99 TestTimeouts::tiny_timeout()); | 122 TestTimeouts::tiny_timeout()); |
| 100 | 123 |
| 101 bool result = server.PostBufferToPath( | 124 bool result = server.PostBufferToPath( |
| 102 ¶ms, "/testpath", "testauth", &watcher); | 125 ¶ms, "/testpath", "testauth", &watcher); |
| 103 | 126 |
| 104 EXPECT_FALSE(result); | 127 EXPECT_FALSE(result); |
| 105 EXPECT_EQ(HttpResponse::CONNECTION_UNAVAILABLE, | 128 EXPECT_EQ(HttpResponse::CONNECTION_UNAVAILABLE, |
| 106 params.response.server_status); | 129 params.response.server_status); |
| 107 abort_thread.Stop(); | 130 abort_thread.Stop(); |
| 108 } | 131 } |
| 109 | 132 |
| 110 } // namespace syncer | 133 } // namespace syncer |
| OLD | NEW |