diff options
author | Klaus Aehlig <klaus.aehlig@huawei.com> | 2022-02-22 17:03:21 +0100 |
---|---|---|
committer | Klaus Aehlig <klaus.aehlig@huawei.com> | 2022-02-22 17:03:21 +0100 |
commit | 619def44c1cca9f3cdf63544d5f24f2c7a7d9b77 (patch) | |
tree | 01868de723cb82c86842f33743fa7b14e24c1fa3 /test/buildtool/multithreading/async_map_node.test.cpp | |
download | justbuild-619def44c1cca9f3cdf63544d5f24f2c7a7d9b77.tar.gz |
Initial self-hosting commit
This is the initial version of our tool that is able to
build itself. In can be bootstrapped by
./bin/bootstrap.py
Co-authored-by: Oliver Reiche <oliver.reiche@huawei.com>
Co-authored-by: Victor Moreno <victor.moreno1@huawei.com>
Diffstat (limited to 'test/buildtool/multithreading/async_map_node.test.cpp')
-rw-r--r-- | test/buildtool/multithreading/async_map_node.test.cpp | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/test/buildtool/multithreading/async_map_node.test.cpp b/test/buildtool/multithreading/async_map_node.test.cpp new file mode 100644 index 00000000..9377e7f2 --- /dev/null +++ b/test/buildtool/multithreading/async_map_node.test.cpp @@ -0,0 +1,93 @@ +#include <mutex> +#include <string> +#include <thread> + +#include "catch2/catch.hpp" +#include "src/buildtool/multithreading/async_map_node.hpp" +#include "src/buildtool/multithreading/task_system.hpp" + +TEST_CASE("No task is queued if the node is never ready", "[async_map_node]") { + std::vector<int> tasks; + std::mutex m; + AsyncMapNode<int, bool> node_never_ready{0}; + { + TaskSystem ts; + CHECK_FALSE( + node_never_ready.AddOrQueueAwaitingTask(&ts, [&tasks, &m]() { + std::unique_lock l{m}; + // NOLINTNEXTLINE(readability-magic-numbers,cppcoreguidelines-avoid-magic-numbers) + tasks.push_back(0); + })); + CHECK_FALSE( + node_never_ready.AddOrQueueAwaitingTask(&ts, [&tasks, &m]() { + std::unique_lock l{m}; + // NOLINTNEXTLINE(readability-magic-numbers,cppcoreguidelines-avoid-magic-numbers) + tasks.push_back(1); + })); + CHECK_FALSE( + node_never_ready.AddOrQueueAwaitingTask(&ts, [&tasks, &m]() { + std::unique_lock l{m}; + // NOLINTNEXTLINE(readability-magic-numbers,cppcoreguidelines-avoid-magic-numbers) + tasks.push_back(2); + })); + } + CHECK(tasks.empty()); +} + +TEST_CASE("Value is set correctly", "[async_map_node]") { + AsyncMapNode<int, bool> node{0}; + { + TaskSystem ts; + node.SetAndQueueAwaitingTasks(&ts, true); + } + CHECK(node.GetValue()); +} + +TEST_CASE("Tasks are queued correctly", "[async_map_node]") { + AsyncMapNode<int, std::string> node{0}; + std::vector<int> tasks; + std::mutex m; + { + TaskSystem ts; + CHECK_FALSE(node.AddOrQueueAwaitingTask(&ts, [&tasks, &m]() { + std::unique_lock l{m}; + // NOLINTNEXTLINE(readability-magic-numbers,cppcoreguidelines-avoid-magic-numbers) + tasks.push_back(0); + })); + CHECK_FALSE(node.AddOrQueueAwaitingTask(&ts, [&tasks, &m]() { + std::unique_lock l{m}; + // NOLINTNEXTLINE(readability-magic-numbers,cppcoreguidelines-avoid-magic-numbers) + tasks.push_back(1); + })); + CHECK_FALSE(node.AddOrQueueAwaitingTask(&ts, [&tasks, &m]() { + std::unique_lock l{m}; + // NOLINTNEXTLINE(readability-magic-numbers,cppcoreguidelines-avoid-magic-numbers) + tasks.push_back(2); + })); + + { + std::unique_lock l{m}; + CHECK(tasks.empty()); + } + node.SetAndQueueAwaitingTasks(&ts, "ready"); + CHECK(node.AddOrQueueAwaitingTask(&ts, [&tasks, &m]() { + std::unique_lock l{m}; + // NOLINTNEXTLINE(readability-magic-numbers,cppcoreguidelines-avoid-magic-numbers) + tasks.push_back(3); + })); + CHECK(node.AddOrQueueAwaitingTask(&ts, [&tasks, &m]() { + std::unique_lock l{m}; + // NOLINTNEXTLINE(readability-magic-numbers,cppcoreguidelines-avoid-magic-numbers) + tasks.push_back(4); + })); + CHECK(node.AddOrQueueAwaitingTask(&ts, [&tasks, &m]() { + std::unique_lock l{m}; + // NOLINTNEXTLINE(readability-magic-numbers,cppcoreguidelines-avoid-magic-numbers) + tasks.push_back(5); + })); + } + CHECK(node.GetValue() == "ready"); + CHECK_THAT( + tasks, + Catch::Matchers::UnorderedEquals(std::vector<int>{0, 1, 2, 3, 4, 5})); +} |