OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chrome/browser/sync/internal_api/syncapi_server_connection_manager.h" | |
6 | |
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.h" | |
14 #include "chrome/browser/sync/internal_api/http_post_provider_factory.h" | |
15 #include "chrome/browser/sync/internal_api/http_post_provider_interface.h" | |
16 #include "net/base/net_errors.h" | |
17 #include "testing/gtest/include/gtest/gtest.h" | |
18 | |
19 using base::TimeDelta; | |
20 using browser_sync::HttpResponse; | |
21 using browser_sync::ServerConnectionManager; | |
22 using browser_sync::ScopedServerStatusWatcher; | |
23 | |
24 namespace sync_api { | |
25 namespace { | |
26 | |
27 class BlockingHttpPost : public HttpPostProviderInterface { | |
28 public: | |
29 BlockingHttpPost() : wait_for_abort_(false, false) {} | |
30 virtual ~BlockingHttpPost() {} | |
31 | |
32 virtual void SetUserAgent(const char* user_agent) OVERRIDE {} | |
33 virtual void SetExtraRequestHeaders(const char* headers) OVERRIDE {} | |
34 virtual void SetURL(const char* url, int port) OVERRIDE {} | |
35 virtual void SetPostPayload(const char* content_type, | |
36 int content_length, | |
37 const char* content) OVERRIDE {} | |
38 virtual bool MakeSynchronousPost(int* error_code, int* response_code) | |
39 OVERRIDE { | |
40 wait_for_abort_.TimedWait(TimeDelta::FromMilliseconds( | |
41 TestTimeouts::action_max_timeout_ms())); | |
42 *error_code = net::ERR_ABORTED; | |
43 return false; | |
44 } | |
45 virtual int GetResponseContentLength() const OVERRIDE { | |
46 return 0; | |
47 } | |
48 virtual const char* GetResponseContent() const OVERRIDE { | |
49 return ""; | |
50 } | |
51 virtual const std::string GetResponseHeaderValue( | |
52 const std::string& name) const OVERRIDE { | |
53 return ""; | |
54 } | |
55 virtual void Abort() OVERRIDE { | |
56 wait_for_abort_.Signal(); | |
57 } | |
58 private: | |
59 base::WaitableEvent wait_for_abort_; | |
60 }; | |
61 | |
62 class BlockingHttpPostFactory : public HttpPostProviderFactory { | |
63 public: | |
64 virtual ~BlockingHttpPostFactory() {} | |
65 virtual HttpPostProviderInterface* Create() OVERRIDE { | |
66 return new BlockingHttpPost(); | |
67 } | |
68 virtual void Destroy(HttpPostProviderInterface* http) OVERRIDE { | |
69 delete http; | |
70 } | |
71 }; | |
72 | |
73 } // namespace | |
74 | |
75 TEST(SyncAPIServerConnectionManagerTest, EarlyAbortPost) { | |
76 SyncAPIServerConnectionManager server( | |
77 "server", 0, true, "1", new BlockingHttpPostFactory()); | |
78 | |
79 ServerConnectionManager::PostBufferParams params; | |
80 ScopedServerStatusWatcher watcher(&server, ¶ms.response); | |
81 | |
82 server.TerminateAllIO(); | |
83 bool result = server.PostBufferToPath( | |
84 ¶ms, "/testpath", "testauth", &watcher); | |
85 | |
86 EXPECT_FALSE(result); | |
87 EXPECT_EQ(HttpResponse::CONNECTION_UNAVAILABLE, | |
88 params.response.server_status); | |
89 } | |
90 | |
91 TEST(SyncAPIServerConnectionManagerTest, AbortPost) { | |
92 SyncAPIServerConnectionManager server( | |
93 "server", 0, true, "1", new BlockingHttpPostFactory()); | |
94 | |
95 ServerConnectionManager::PostBufferParams params; | |
96 ScopedServerStatusWatcher watcher(&server, ¶ms.response); | |
97 | |
98 base::Thread abort_thread("Test_AbortThread"); | |
99 ASSERT_TRUE(abort_thread.Start()); | |
100 abort_thread.message_loop()->PostDelayedTask( | |
101 FROM_HERE, | |
102 base::Bind(&ServerConnectionManager::TerminateAllIO, | |
103 base::Unretained(&server)), | |
104 TestTimeouts::tiny_timeout()); | |
105 | |
106 bool result = server.PostBufferToPath( | |
107 ¶ms, "/testpath", "testauth", &watcher); | |
108 | |
109 EXPECT_FALSE(result); | |
110 EXPECT_EQ(HttpResponse::CONNECTION_UNAVAILABLE, | |
111 params.response.server_status); | |
112 abort_thread.Stop(); | |
113 } | |
114 | |
115 } // namespace sync_api | |
OLD | NEW |