Test etcd client from forked child process

Signed-off-by: Tao He <sighingnow@gmail.com>
This commit is contained in:
Tao He 2023-02-19 14:03:55 +08:00
parent 817153bcc9
commit 1e71b0b19d
2 changed files with 47 additions and 0 deletions

View File

@ -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

44
tst/ForkTest.cpp Normal file
View File

@ -0,0 +1,44 @@
#define CATCH_CONFIG_MAIN
#include <catch.hpp>
#include <unistd.h>
#include <atomic>
#include <chrono>
#include <cstdlib>
#include <iostream>
#include <thread>
#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;
}
}