|
| 1 | +// Copyright 2019 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "google/cloud/internal/background_threads_impl.h" |
| 16 | +#include <gmock/gmock.h> |
| 17 | + |
| 18 | +namespace google { |
| 19 | +namespace cloud { |
| 20 | +inline namespace GOOGLE_CLOUD_CPP_NS { |
| 21 | +namespace internal { |
| 22 | +namespace { |
| 23 | + |
| 24 | +/// @test Verify we can create and use a CustomerSuppliedBackgroundThreads |
| 25 | +/// without impacting the completion queue |
| 26 | +TEST(CustomerSuppliedBackgroundThreads, LifecycleNoShutdown) { |
| 27 | + CompletionQueue cq; |
| 28 | + promise<void> p; |
| 29 | + std::thread t([&cq, &p] { |
| 30 | + cq.Run(); |
| 31 | + p.set_value(); |
| 32 | + }); |
| 33 | + |
| 34 | + { CustomerSuppliedBackgroundThreads actual(cq); } |
| 35 | + |
| 36 | + using ms = std::chrono::milliseconds; |
| 37 | + |
| 38 | + auto has_shutdown = p.get_future(); |
| 39 | + EXPECT_NE(std::future_status::ready, has_shutdown.wait_for(ms(2))); |
| 40 | + |
| 41 | + auto expired = cq.MakeRelativeTimer(ms(0)); |
| 42 | + EXPECT_EQ(std::future_status::ready, expired.wait_for(ms(100))); |
| 43 | + |
| 44 | + cq.Shutdown(); |
| 45 | + EXPECT_EQ(std::future_status::ready, has_shutdown.wait_for(ms(100))); |
| 46 | + |
| 47 | + t.join(); |
| 48 | +} |
| 49 | + |
| 50 | +/// @test Verify that users can supply their own queue and threads. |
| 51 | +TEST(CustomerSuppliedBackgroundThreads, SharesCompletionQueue) { |
| 52 | + CompletionQueue cq; |
| 53 | + |
| 54 | + CustomerSuppliedBackgroundThreads actual(cq); |
| 55 | + |
| 56 | + using ms = std::chrono::milliseconds; |
| 57 | + // Verify the completion queue is shared. Scheduling work in actual.cq() works |
| 58 | + // once a thread is blocked in cq.Run(). Start that thread after scheduling |
| 59 | + // the work to avoid flaky failures where the timer expires immediately. |
| 60 | + future<std::thread::id> id = actual.cq().MakeRelativeTimer(ms(1)).then( |
| 61 | + [](future<StatusOr<std::chrono::system_clock::time_point>>) { |
| 62 | + return std::this_thread::get_id(); |
| 63 | + }); |
| 64 | + std::thread t([&cq] { cq.Run(); }); |
| 65 | + EXPECT_EQ(std::future_status::ready, id.wait_for(ms(100))); |
| 66 | + EXPECT_EQ(t.get_id(), id.get()); |
| 67 | + |
| 68 | + cq.Shutdown(); |
| 69 | + t.join(); |
| 70 | +} |
| 71 | + |
| 72 | +/// @test Verify that automatically created completion queues are usable. |
| 73 | +TEST(AutomaticallyCreatedBackgroundThreads, IsActive) { |
| 74 | + AutomaticallyCreatedBackgroundThreads actual; |
| 75 | + |
| 76 | + using ms = std::chrono::milliseconds; |
| 77 | + |
| 78 | + auto expired = actual.cq().MakeRelativeTimer(ms(0)); |
| 79 | + EXPECT_EQ(std::future_status::ready, expired.wait_for(ms(100))); |
| 80 | +} |
| 81 | + |
| 82 | +} // namespace |
| 83 | +} // namespace internal |
| 84 | +} // namespace GOOGLE_CLOUD_CPP_NS |
| 85 | +} // namespace cloud |
| 86 | +} // namespace google |
0 commit comments