From 1e71b0b19df5f934ae2c3d07dd552d3b43e9da78 Mon Sep 17 00:00:00 2001 From: Tao He Date: Sun, 19 Feb 2023 14:03:55 +0800 Subject: [PATCH] Test etcd client from forked child process Signed-off-by: Tao He --- .github/workflows/build-test.yml | 3 +++ tst/ForkTest.cpp | 44 ++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 tst/ForkTest.cpp diff --git a/.github/workflows/build-test.yml b/.github/workflows/build-test.yml index e82a19b..f51437f 100644 --- a/.github/workflows/build-test.yml +++ b/.github/workflows/build-test.yml @@ -226,6 +226,9 @@ jobs: echo "Run the etcd election test ........................." ./build/bin/ElectionTest + echo "Run the etcd with fork test ........................." + ./build/bin/ForkTest + killall -TERM etcd || true sleep 5 diff --git a/tst/ForkTest.cpp b/tst/ForkTest.cpp new file mode 100644 index 0000000..1cf0dbe --- /dev/null +++ b/tst/ForkTest.cpp @@ -0,0 +1,44 @@ +#define CATCH_CONFIG_MAIN +#include + +#include + +#include +#include +#include +#include +#include + +#include "etcd/Client.hpp" +#include "etcd/KeepAlive.hpp" + +static const std::string etcd_url("http://127.0.0.1:2379"); + +TEST_CASE("fork: set in child and get from self") +{ + pid_t pid = fork(); + REQUIRE(pid >= 0); + + if (pid == 0) { + // child + etcd::Client etcd(etcd_url); + etcd.set("/test/fork-key1", "fork: abcdefgh").wait(); + std::cout << "child: set finished ..." << std::endl; + } else { + // self + etcd::Client etcd(etcd_url); + size_t check = 0; + while (check < 10) { + auto resp = etcd.get("/test/fork-key1").get(); + if (!resp.is_ok()) { + check += 1; + std::this_thread::sleep_for(std::chrono::seconds(1)); + continue; + } else { + CHECK(resp.value().as_string() == "fork: abcdefgh"); + break; + } + } + std::cout << "self: get finished ..." << std::endl; + } +}