hdfs_ioservice_test.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "hdfspp/ioservice.h"
  19. #include <future>
  20. #include <functional>
  21. #include <thread>
  22. #include <string>
  23. #include <google/protobuf/stubs/common.h>
  24. #include <gmock/gmock.h>
  25. using ::testing::_;
  26. using ::testing::InvokeArgument;
  27. using ::testing::Return;
  28. using namespace hdfs;
  29. // Make sure IoService spins up specified number of threads
  30. TEST(IoServiceTest, InitThreads) {
  31. #ifndef DISABLE_CONCURRENT_WORKERS
  32. std::shared_ptr<IoService> service = IoService::MakeShared();
  33. EXPECT_NE(service, nullptr);
  34. unsigned int thread_count = 4;
  35. unsigned int result_thread_count = service->InitWorkers(thread_count);
  36. EXPECT_EQ(thread_count, result_thread_count);
  37. service->Stop();
  38. #else
  39. #pragma message("DISABLE_CONCURRENT_WORKERS is defined so hdfs_ioservice_test will compile out the InitThreads test")
  40. #endif
  41. }
  42. // Make sure IoService defaults to logical thread count
  43. TEST(IoServiceTest, InitDefaultThreads) {
  44. #ifndef DISABLE_CONCURRENT_WORKERS
  45. std::shared_ptr<IoService> service = IoService::MakeShared();
  46. EXPECT_NE(service, nullptr);
  47. unsigned int thread_count = std::thread::hardware_concurrency();
  48. unsigned int result_thread_count = service->InitDefaultWorkers();
  49. EXPECT_EQ(thread_count, result_thread_count);
  50. service->Stop();
  51. #else
  52. #pragma message("DISABLE_CONCURRENT_WORKERS is defined so hdfs_ioservice_test will compile out the InitDefaultThreads test")
  53. #endif
  54. }
  55. // Check IoService::PostTask
  56. TEST(IoServiceTest, SimplePost) {
  57. std::shared_ptr<IoService> service = IoService::MakeShared();
  58. EXPECT_NE(service, nullptr);
  59. unsigned int thread_count = std::thread::hardware_concurrency();
  60. unsigned int result_thread_count = service->InitDefaultWorkers();
  61. #ifndef DISABLE_CONCURRENT_WORKERS
  62. EXPECT_EQ(thread_count, result_thread_count);
  63. #else
  64. (void)thread_count;
  65. (void)result_thread_count;
  66. #endif
  67. // Like with the C synchronous shims a promise/future is needed to block until the async call completes.
  68. auto promise = std::make_shared<std::promise<std::string>>();
  69. std::future<std::string> future = promise->get_future();
  70. // this will get invoked on a worker thread
  71. std::function<void()> example_callback = [promise](){
  72. promise->set_value("hello from IoService");
  73. };
  74. service->PostTask(example_callback);
  75. // block until worker thread finishes
  76. std::string result = future.get();
  77. EXPECT_EQ(result, "hello from IoService");
  78. service->Stop();
  79. }
  80. int main(int argc, char *argv[]) {
  81. // The following line must be executed to initialize Google Mock
  82. // (and Google Test) before running the tests.
  83. ::testing::InitGoogleMock(&argc, argv);
  84. int exit_code = RUN_ALL_TESTS();
  85. google::protobuf::ShutdownProtobufLibrary();
  86. return exit_code;
  87. }