Test etcd client from forked child process
Signed-off-by: Tao He <sighingnow@gmail.com>
This commit is contained in:
parent
817153bcc9
commit
1e71b0b19d
|
|
@ -226,6 +226,9 @@ jobs:
|
||||||
echo "Run the etcd election test ........................."
|
echo "Run the etcd election test ........................."
|
||||||
./build/bin/ElectionTest
|
./build/bin/ElectionTest
|
||||||
|
|
||||||
|
echo "Run the etcd with fork test ........................."
|
||||||
|
./build/bin/ForkTest
|
||||||
|
|
||||||
killall -TERM etcd || true
|
killall -TERM etcd || true
|
||||||
sleep 5
|
sleep 5
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue