| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium OS 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 <unistd.h> | 5 #include <unistd.h> |
| 6 #include <string> | 6 #include <string> |
| 7 #include <vector> | 7 #include <vector> |
| 8 #include <base/scoped_ptr.h> | 8 #include <base/scoped_ptr.h> |
| 9 #include <glib.h> | 9 #include <glib.h> |
| 10 #include <gtest/gtest.h> | 10 #include <gtest/gtest.h> |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 return new MockHttpFetcher("x", 1); | 54 return new MockHttpFetcher("x", 1); |
| 55 } | 55 } |
| 56 string BigUrl() const { | 56 string BigUrl() const { |
| 57 return "unused://unused"; | 57 return "unused://unused"; |
| 58 } | 58 } |
| 59 string SmallUrl() const { | 59 string SmallUrl() const { |
| 60 return "unused://unused"; | 60 return "unused://unused"; |
| 61 } | 61 } |
| 62 bool IsMock() const { return true; } | 62 bool IsMock() const { return true; } |
| 63 typedef NullHttpServer HttpServer; | 63 typedef NullHttpServer HttpServer; |
| 64 void IgnoreServerAborting(HttpServer* server) const {} |
| 64 }; | 65 }; |
| 65 | 66 |
| 66 class PythonHttpServer { | 67 class PythonHttpServer { |
| 67 public: | 68 public: |
| 68 PythonHttpServer() { | 69 PythonHttpServer() { |
| 69 char *argv[2] = {strdup("./test_http_server"), NULL}; | 70 char *argv[2] = {strdup("./test_http_server"), NULL}; |
| 70 GError *err; | 71 GError *err; |
| 71 started_ = false; | 72 started_ = false; |
| 73 validate_quit_ = true; |
| 72 if (!g_spawn_async(NULL, | 74 if (!g_spawn_async(NULL, |
| 73 argv, | 75 argv, |
| 74 NULL, | 76 NULL, |
| 75 G_SPAWN_DO_NOT_REAP_CHILD, | 77 G_SPAWN_DO_NOT_REAP_CHILD, |
| 76 NULL, | 78 NULL, |
| 77 NULL, | 79 NULL, |
| 78 &pid_, | 80 &pid_, |
| 79 &err)) { | 81 &err)) { |
| 80 return; | 82 return; |
| 81 } | 83 } |
| 82 int rc = 1; | 84 int rc = 1; |
| 83 while (0 != rc) { | 85 while (0 != rc) { |
| 84 rc = system((string("wget --output-document=/dev/null ") + | 86 rc = system((string("wget --output-document=/dev/null ") + |
| 85 LocalServerUrlForPath("/test")).c_str()); | 87 LocalServerUrlForPath("/test")).c_str()); |
| 86 usleep(10 * 1000); // 10 ms | 88 usleep(10 * 1000); // 10 ms |
| 87 } | 89 } |
| 88 started_ = true; | 90 started_ = true; |
| 89 free(argv[0]); | 91 free(argv[0]); |
| 90 return; | 92 return; |
| 91 } | 93 } |
| 92 ~PythonHttpServer() { | 94 ~PythonHttpServer() { |
| 93 if (!started_) | 95 if (!started_) |
| 94 return; | 96 return; |
| 95 // request that the server exit itself | 97 // request that the server exit itself |
| 96 system((string("wget --output-document=/dev/null ") + | 98 int rc = system((string("wget -t 1 --output-document=/dev/null ") + |
| 97 LocalServerUrlForPath("/quitquitquit")).c_str()); | 99 LocalServerUrlForPath("/quitquitquit")).c_str()); |
| 100 if (validate_quit_) |
| 101 EXPECT_EQ(0, rc); |
| 98 waitpid(pid_, NULL, 0); | 102 waitpid(pid_, NULL, 0); |
| 99 } | 103 } |
| 100 GPid pid_; | 104 GPid pid_; |
| 101 bool started_; | 105 bool started_; |
| 106 bool validate_quit_; |
| 102 }; | 107 }; |
| 103 | 108 |
| 104 template <> | 109 template <> |
| 105 class HttpFetcherTest<LibcurlHttpFetcher> : public ::testing::Test { | 110 class HttpFetcherTest<LibcurlHttpFetcher> : public ::testing::Test { |
| 106 public: | 111 public: |
| 107 HttpFetcher* NewLargeFetcher() { | 112 HttpFetcher* NewLargeFetcher() { |
| 108 LibcurlHttpFetcher *ret = new LibcurlHttpFetcher; | 113 LibcurlHttpFetcher *ret = new LibcurlHttpFetcher; |
| 109 ret->set_idle_ms(1); // speeds up test execution | 114 ret->set_idle_ms(1); // speeds up test execution |
| 110 return ret; | 115 return ret; |
| 111 } | 116 } |
| 112 HttpFetcher* NewSmallFetcher() { | 117 HttpFetcher* NewSmallFetcher() { |
| 113 return NewLargeFetcher(); | 118 return NewLargeFetcher(); |
| 114 } | 119 } |
| 115 string BigUrl() const { | 120 string BigUrl() const { |
| 116 return LocalServerUrlForPath("/big"); | 121 return LocalServerUrlForPath("/big"); |
| 117 } | 122 } |
| 118 string SmallUrl() const { | 123 string SmallUrl() const { |
| 119 return LocalServerUrlForPath("/foo"); | 124 return LocalServerUrlForPath("/foo"); |
| 120 } | 125 } |
| 121 bool IsMock() const { return false; } | 126 bool IsMock() const { return false; } |
| 122 typedef PythonHttpServer HttpServer; | 127 typedef PythonHttpServer HttpServer; |
| 128 void IgnoreServerAborting(HttpServer* server) const { |
| 129 PythonHttpServer *pyserver = reinterpret_cast<PythonHttpServer*>(server); |
| 130 pyserver->validate_quit_ = false; |
| 131 } |
| 123 }; | 132 }; |
| 124 | 133 |
| 125 typedef ::testing::Types<LibcurlHttpFetcher, MockHttpFetcher> | 134 typedef ::testing::Types<LibcurlHttpFetcher, MockHttpFetcher> |
| 126 HttpFetcherTestTypes; | 135 HttpFetcherTestTypes; |
| 127 TYPED_TEST_CASE(HttpFetcherTest, HttpFetcherTestTypes); | 136 TYPED_TEST_CASE(HttpFetcherTest, HttpFetcherTestTypes); |
| 128 | 137 |
| 129 namespace { | 138 namespace { |
| 130 class HttpFetcherTestDelegate : public HttpFetcherDelegate { | 139 class HttpFetcherTestDelegate : public HttpFetcherDelegate { |
| 131 public: | 140 public: |
| 132 virtual void ReceivedBytes(HttpFetcher* fetcher, | 141 virtual void ReceivedBytes(HttpFetcher* fetcher, |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 270 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); | 279 GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE); |
| 271 { | 280 { |
| 272 AbortingHttpFetcherTestDelegate delegate; | 281 AbortingHttpFetcherTestDelegate delegate; |
| 273 scoped_ptr<HttpFetcher> fetcher(this->NewLargeFetcher()); | 282 scoped_ptr<HttpFetcher> fetcher(this->NewLargeFetcher()); |
| 274 delegate.once_ = true; | 283 delegate.once_ = true; |
| 275 delegate.loop_ = loop; | 284 delegate.loop_ = loop; |
| 276 delegate.fetcher_ = fetcher.get(); | 285 delegate.fetcher_ = fetcher.get(); |
| 277 fetcher->set_delegate(&delegate); | 286 fetcher->set_delegate(&delegate); |
| 278 | 287 |
| 279 typename TestFixture::HttpServer server; | 288 typename TestFixture::HttpServer server; |
| 289 this->IgnoreServerAborting(&server); |
| 280 ASSERT_TRUE(server.started_); | 290 ASSERT_TRUE(server.started_); |
| 281 GSource* timeout_source_; | 291 GSource* timeout_source_; |
| 282 timeout_source_ = g_timeout_source_new(0); // ms | 292 timeout_source_ = g_timeout_source_new(0); // ms |
| 283 g_source_set_callback(timeout_source_, AbortingTimeoutCallback, &delegate, | 293 g_source_set_callback(timeout_source_, AbortingTimeoutCallback, &delegate, |
| 284 NULL); | 294 NULL); |
| 285 g_source_attach(timeout_source_, NULL); | 295 g_source_attach(timeout_source_, NULL); |
| 286 fetcher->BeginTransfer(this->BigUrl()); | 296 fetcher->BeginTransfer(this->BigUrl()); |
| 287 | 297 |
| 288 g_main_loop_run(loop); | 298 g_main_loop_run(loop); |
| 289 g_source_destroy(timeout_source_); | 299 g_source_destroy(timeout_source_); |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 331 ASSERT_EQ(100000, delegate.data.size()); | 341 ASSERT_EQ(100000, delegate.data.size()); |
| 332 for (int i = 0; i < 100000; i += 10) { | 342 for (int i = 0; i < 100000; i += 10) { |
| 333 // Assert so that we don't flood the screen w/ EXPECT errors on failure. | 343 // Assert so that we don't flood the screen w/ EXPECT errors on failure. |
| 334 ASSERT_EQ(delegate.data.substr(i, 10), "abcdefghij"); | 344 ASSERT_EQ(delegate.data.substr(i, 10), "abcdefghij"); |
| 335 } | 345 } |
| 336 } | 346 } |
| 337 g_main_loop_unref(loop); | 347 g_main_loop_unref(loop); |
| 338 } | 348 } |
| 339 | 349 |
| 340 } // namespace chromeos_update_engine | 350 } // namespace chromeos_update_engine |
| OLD | NEW |