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