Adding myself to authors file
[chromium-blink-merge.git] / sync / internal_api / syncapi_server_connection_manager_unittest.cc
blob10dc311832c23104d9dc10c2961467950ec0deda
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.
5 #include "sync/internal_api/syncapi_server_connection_manager.h"
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/base/cancelation_signal.h"
16 #include "sync/internal_api/public/http_post_provider_factory.h"
17 #include "sync/internal_api/public/http_post_provider_interface.h"
18 #include "testing/gtest/include/gtest/gtest.h"
20 namespace syncer {
21 namespace {
23 using base::TimeDelta;
25 class BlockingHttpPost : public HttpPostProviderInterface {
26 public:
27 BlockingHttpPost() : wait_for_abort_(false, false) {}
28 ~BlockingHttpPost() override {}
30 void SetExtraRequestHeaders(const char* headers) override {}
31 void SetURL(const char* url, int port) override {}
32 void SetPostPayload(const char* content_type,
33 int content_length,
34 const char* content) override {}
35 bool MakeSynchronousPost(int* error_code, int* response_code) override {
36 wait_for_abort_.TimedWait(TestTimeouts::action_max_timeout());
37 *error_code = net::ERR_ABORTED;
38 return false;
40 int GetResponseContentLength() const override { return 0; }
41 const char* GetResponseContent() const override { return ""; }
42 const std::string GetResponseHeaderValue(
43 const std::string& name) const override {
44 return std::string();
46 void Abort() override { wait_for_abort_.Signal(); }
47 private:
48 base::WaitableEvent wait_for_abort_;
51 class BlockingHttpPostFactory : public HttpPostProviderFactory {
52 public:
53 ~BlockingHttpPostFactory() override {}
54 void Init(const std::string& user_agent) override {}
55 HttpPostProviderInterface* Create() override {
56 return new BlockingHttpPost();
58 void Destroy(HttpPostProviderInterface* http) override {
59 delete static_cast<BlockingHttpPost*>(http);
63 } // namespace
65 // Ask the ServerConnectionManager to stop before it is created.
66 TEST(SyncAPIServerConnectionManagerTest, VeryEarlyAbortPost) {
67 CancelationSignal signal;
68 signal.Signal();
69 SyncAPIServerConnectionManager server(
70 "server", 0, true, new BlockingHttpPostFactory(), &signal);
72 ServerConnectionManager::PostBufferParams params;
73 ScopedServerStatusWatcher watcher(&server, &params.response);
75 bool result = server.PostBufferToPath(
76 &params, "/testpath", "testauth", &watcher);
78 EXPECT_FALSE(result);
79 EXPECT_EQ(HttpResponse::CONNECTION_UNAVAILABLE,
80 params.response.server_status);
83 // Ask the ServerConnectionManager to stop before its first request is made.
84 TEST(SyncAPIServerConnectionManagerTest, EarlyAbortPost) {
85 CancelationSignal signal;
86 SyncAPIServerConnectionManager server(
87 "server", 0, true, new BlockingHttpPostFactory(), &signal);
89 ServerConnectionManager::PostBufferParams params;
90 ScopedServerStatusWatcher watcher(&server, &params.response);
92 signal.Signal();
93 bool result = server.PostBufferToPath(
94 &params, "/testpath", "testauth", &watcher);
96 EXPECT_FALSE(result);
97 EXPECT_EQ(HttpResponse::CONNECTION_UNAVAILABLE,
98 params.response.server_status);
101 // Ask the ServerConnectionManager to stop during a request.
102 TEST(SyncAPIServerConnectionManagerTest, AbortPost) {
103 CancelationSignal signal;
104 SyncAPIServerConnectionManager server(
105 "server", 0, true, new BlockingHttpPostFactory(), &signal);
107 ServerConnectionManager::PostBufferParams params;
108 ScopedServerStatusWatcher watcher(&server, &params.response);
110 base::Thread abort_thread("Test_AbortThread");
111 ASSERT_TRUE(abort_thread.Start());
112 abort_thread.message_loop()->PostDelayedTask(
113 FROM_HERE,
114 base::Bind(&CancelationSignal::Signal,
115 base::Unretained(&signal)),
116 TestTimeouts::tiny_timeout());
118 bool result = server.PostBufferToPath(
119 &params, "/testpath", "testauth", &watcher);
121 EXPECT_FALSE(result);
122 EXPECT_EQ(HttpResponse::CONNECTION_UNAVAILABLE,
123 params.response.server_status);
124 abort_thread.Stop();
127 } // namespace syncer