|
| 1 | +// mcppide.jobs over the mock mcpp: a build streams its records and ends `ok`, a failed build ends |
| 2 | +// `failed` with its diagnostics, and a cancelled one ends `cancelled` with mcpp's record saying so. |
| 3 | +#include "support.hpp" |
| 4 | +import std; |
| 5 | +import nlohmann.json; |
| 6 | +import mcpplibs.os; |
| 7 | +import mcppide.mcpp; |
| 8 | +import mcppide.jobs; |
| 9 | + |
| 10 | +namespace jobs = mcppide::jobs; |
| 11 | +using Json = nlohmann::json; |
| 12 | + |
| 13 | +namespace { |
| 14 | +struct Seen { |
| 15 | + std::mutex lock; |
| 16 | + std::vector<Json> records; |
| 17 | + std::map<std::string, Json> finished; |
| 18 | +}; |
| 19 | + |
| 20 | +} // namespace |
| 21 | + |
| 22 | +int main() { |
| 23 | + const auto mock = testsupport::built("mcpp-ide-mock-mcpp"); |
| 24 | + CHECK(!mock.empty()); |
| 25 | + // The mock reads its fixtures and its behaviour from the environment of each run. |
| 26 | + const mcppide::mcpp::Environment fixtures{{"MCPPIDE_FIXTURES", testsupport::fixtures().generic_string()}}; |
| 27 | + auto protocol = mcppide::mcpp::handshake(mock, fixtures); |
| 28 | + CHECK(protocol.has_value() && protocol->contract); |
| 29 | + |
| 30 | + const auto root = std::filesystem::temp_directory_path() / std::format("mcppide-jobs-{}", mcpplibs::os::process::self()->id); |
| 31 | + std::filesystem::create_directories(root); |
| 32 | + Seen seen; |
| 33 | + jobs::Sink sink; |
| 34 | + sink.record = [&](std::string_view, const Json& r) { std::lock_guard g(seen.lock); seen.records.push_back(r); }; |
| 35 | + sink.finished = [&](std::string_view id, const Json& s) { std::lock_guard g(seen.lock); seen.finished[std::string(id)] = s; }; |
| 36 | + jobs::Jobs runner(mcppide::mcpp::Client(mock, *protocol), sink); |
| 37 | + |
| 38 | + jobs::Request build{.kind = jobs::Kind::build, .root = root, .args = {"build", "--message-format", "json"}, .mode = jobs::Mode::ndjson}; |
| 39 | + build.env = fixtures; |
| 40 | + const auto ok = runner.submit(build); |
| 41 | + runner.drain(); |
| 42 | + { |
| 43 | + std::lock_guard g(seen.lock); |
| 44 | + CHECK(seen.finished.contains(ok)); |
| 45 | + CHECK(seen.finished[ok]["status"] == "ok"); |
| 46 | + CHECK(!seen.records.empty()); |
| 47 | + CHECK(seen.finished[ok].contains("envelope")); |
| 48 | + } |
| 49 | + |
| 50 | + auto failing = build; |
| 51 | + failing.env.push_back({"MOCK_MCPP_FAIL", "build"}); |
| 52 | + const auto failed = runner.submit(failing); |
| 53 | + runner.drain(); |
| 54 | + { |
| 55 | + std::lock_guard g(seen.lock); |
| 56 | + CHECK(seen.finished[failed]["status"] == "failed"); |
| 57 | + } |
| 58 | + |
| 59 | + // A build that takes its time, cancelled while it runs. |
| 60 | + auto slowly = build; |
| 61 | + slowly.env.push_back({"MOCK_MCPP_SLOW_MS", "20000"}); |
| 62 | + const auto slow = runner.submit(slowly); |
| 63 | + std::this_thread::sleep_for(std::chrono::milliseconds(500)); |
| 64 | + CHECK(runner.cancel(slow)); |
| 65 | + runner.drain(); |
| 66 | + { |
| 67 | + std::lock_guard g(seen.lock); |
| 68 | + CHECK(seen.finished[slow]["status"] == "cancelled"); |
| 69 | + } |
| 70 | + const auto record = testsupport::read(root / "target" / ".last-result.json"); |
| 71 | + CHECK(record.find("\"cancelled\"") != std::string::npos); |
| 72 | + |
| 73 | + std::error_code ec; |
| 74 | + std::filesystem::remove_all(root, ec); |
| 75 | + std::println("test_jobs: ok"); |
| 76 | +} |
0 commit comments