|
| 1 | +/* |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include "velox/common/base/AsyncSource.h" |
| 18 | +#include <fmt/format.h> |
| 19 | +#include <folly/Random.h> |
| 20 | +#include <folly/Synchronized.h> |
| 21 | +#include <gtest/gtest.h> |
| 22 | +#include <thread> |
| 23 | + |
| 24 | +using namespace facebook::velox; |
| 25 | + |
| 26 | +// A sample class to be constructed via AsyncSource. |
| 27 | +struct Gizmo { |
| 28 | + explicit Gizmo(int32_t _id) : id(_id) {} |
| 29 | + |
| 30 | + const int32_t id; |
| 31 | +}; |
| 32 | + |
| 33 | +TEST(AsyncSourceTest, basic) { |
| 34 | + AsyncSource<Gizmo> gizmo([]() { return std::make_unique<Gizmo>(11); }); |
| 35 | + EXPECT_FALSE(gizmo.hasValue()); |
| 36 | + gizmo.prepare(); |
| 37 | + EXPECT_TRUE(gizmo.hasValue()); |
| 38 | + auto value = gizmo.move(); |
| 39 | + EXPECT_FALSE(gizmo.hasValue()); |
| 40 | + EXPECT_EQ(11, value->id); |
| 41 | +} |
| 42 | + |
| 43 | +TEST(AsyncSourceTest, threads) { |
| 44 | + constexpr int32_t kNumThreads = 10; |
| 45 | + constexpr int32_t kNumGizmos = 2000; |
| 46 | + folly::Synchronized<std::unordered_set<int32_t>> results; |
| 47 | + std::vector<std::shared_ptr<AsyncSource<Gizmo>>> gizmos; |
| 48 | + for (auto i = 0; i < kNumGizmos; ++i) { |
| 49 | + gizmos.push_back(std::make_shared<AsyncSource<Gizmo>>([i]() { |
| 50 | + std::this_thread::sleep_for(std::chrono::milliseconds(1)); // NOLINT |
| 51 | + return std::make_unique<Gizmo>(i); |
| 52 | + })); |
| 53 | + } |
| 54 | + |
| 55 | + std::vector<std::thread> threads; |
| 56 | + threads.reserve(kNumThreads); |
| 57 | + for (int32_t threadIndex = 0; threadIndex < kNumThreads; ++threadIndex) { |
| 58 | + threads.push_back(std::thread([threadIndex, &gizmos, &results]() { |
| 59 | + if (threadIndex < kNumThreads / 2) { |
| 60 | + // The first half of the threads prepare Gizmos in the background. |
| 61 | + for (auto i = 0; i < kNumGizmos; ++i) { |
| 62 | + gizmos[i]->prepare(); |
| 63 | + } |
| 64 | + } else { |
| 65 | + // The rest of the threads first get random Gizmos and then do a pass |
| 66 | + // over all the Gizmos to make sure all get collected. We assert that |
| 67 | + // each Gizmo is obtained once. |
| 68 | + folly::Random::DefaultGenerator rng; |
| 69 | + for (auto i = 0; i < kNumGizmos / 3; ++i) { |
| 70 | + auto gizmo = |
| 71 | + gizmos[folly::Random::rand32(rng) % gizmos.size()]->move(); |
| 72 | + if (gizmo) { |
| 73 | + results.withWLock([&](auto& set) { |
| 74 | + EXPECT_TRUE(set.find(gizmo->id) == set.end()); |
| 75 | + set.insert(gizmo->id); |
| 76 | + }); |
| 77 | + } |
| 78 | + } |
| 79 | + for (auto i = 0; i < gizmos.size(); ++i) { |
| 80 | + auto gizmo = gizmos[i]->move(); |
| 81 | + if (gizmo) { |
| 82 | + results.withWLock([&](auto& set) { |
| 83 | + EXPECT_TRUE(set.find(gizmo->id) == set.end()); |
| 84 | + set.insert(gizmo->id); |
| 85 | + }); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + })); |
| 90 | + } |
| 91 | + for (auto& thread : threads) { |
| 92 | + thread.join(); |
| 93 | + } |
| 94 | + results.withRLock([&](auto& set) { |
| 95 | + for (auto i = 0; i < kNumGizmos; ++i) { |
| 96 | + EXPECT_TRUE(set.find(i) != set.end()); |
| 97 | + } |
| 98 | + }); |
| 99 | +} |
0 commit comments