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"
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"
23 using base::TimeDelta
;
25 class BlockingHttpPost
: public HttpPostProviderInterface
{
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
,
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
;
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
{
46 void Abort() override
{ wait_for_abort_
.Signal(); }
48 base::WaitableEvent wait_for_abort_
;
51 class BlockingHttpPostFactory
: public HttpPostProviderFactory
{
53 ~BlockingHttpPostFactory() override
{}
54 void Init(const std::string
& user_agent
,
55 const BindToTrackerCallback
& bind_to_tracker_callback
) override
{}
57 HttpPostProviderInterface
* Create() override
{
58 return new BlockingHttpPost();
60 void Destroy(HttpPostProviderInterface
* http
) override
{
61 delete static_cast<BlockingHttpPost
*>(http
);
67 // Ask the ServerConnectionManager to stop before it is created.
68 TEST(SyncAPIServerConnectionManagerTest
, VeryEarlyAbortPost
) {
69 CancelationSignal signal
;
71 SyncAPIServerConnectionManager
server(
72 "server", 0, true, new BlockingHttpPostFactory(), &signal
);
74 ServerConnectionManager::PostBufferParams params
;
75 ScopedServerStatusWatcher
watcher(&server
, ¶ms
.response
);
77 bool result
= server
.PostBufferToPath(
78 ¶ms
, "/testpath", "testauth", &watcher
);
81 EXPECT_EQ(HttpResponse::CONNECTION_UNAVAILABLE
,
82 params
.response
.server_status
);
85 // Ask the ServerConnectionManager to stop before its first request is made.
86 TEST(SyncAPIServerConnectionManagerTest
, EarlyAbortPost
) {
87 CancelationSignal signal
;
88 SyncAPIServerConnectionManager
server(
89 "server", 0, true, new BlockingHttpPostFactory(), &signal
);
91 ServerConnectionManager::PostBufferParams params
;
92 ScopedServerStatusWatcher
watcher(&server
, ¶ms
.response
);
95 bool result
= server
.PostBufferToPath(
96 ¶ms
, "/testpath", "testauth", &watcher
);
99 EXPECT_EQ(HttpResponse::CONNECTION_UNAVAILABLE
,
100 params
.response
.server_status
);
103 // Ask the ServerConnectionManager to stop during a request.
104 TEST(SyncAPIServerConnectionManagerTest
, AbortPost
) {
105 CancelationSignal signal
;
106 SyncAPIServerConnectionManager
server(
107 "server", 0, true, new BlockingHttpPostFactory(), &signal
);
109 ServerConnectionManager::PostBufferParams params
;
110 ScopedServerStatusWatcher
watcher(&server
, ¶ms
.response
);
112 base::Thread
abort_thread("Test_AbortThread");
113 ASSERT_TRUE(abort_thread
.Start());
114 abort_thread
.message_loop()->PostDelayedTask(
116 base::Bind(&CancelationSignal::Signal
,
117 base::Unretained(&signal
)),
118 TestTimeouts::tiny_timeout());
120 bool result
= server
.PostBufferToPath(
121 ¶ms
, "/testpath", "testauth", &watcher
);
123 EXPECT_FALSE(result
);
124 EXPECT_EQ(HttpResponse::CONNECTION_UNAVAILABLE
,
125 params
.response
.server_status
);
129 } // namespace syncer