Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

do we have an example of syn watch? #8

Closed
danielzhanghl opened this issue Jul 10, 2017 · 2 comments
Closed

do we have an example of syn watch? #8

danielzhanghl opened this issue Jul 10, 2017 · 2 comments
Labels

Comments

@danielzhanghl
Copy link

hi, all the watch operation examples are asynchronous, do we have a way to sync the watch?
I tried below, but it return right way, instead of block there.
thanks for any suggestions!

static std::string etcd_uri("http: 127.0.0.1:2379");
etcd::Client etcd(etcd_uri);
etcd::Response response;
pplx::tasketcd::Response response_task = etcd.watch("/test/key1",true);
try
{
std::cout << "start watch" << std::endl;
response = response_task.get(); // can throw
if (response.is_ok())
std::cout << "successful action is: "<<response.action()<<" value= " << response.value().as_string() << std::endl;
else
std::cout << "operation failed, details: " << response.error_message();
}
catch (std::exception const & ex)
{
std::cerr << "communication problem, details: " << ex.what();
}


result is as below:

start watch
successful action is: value=

@danielzhanghl
Copy link
Author

after some dig into the code, it seems the watch can not be cancelled, that is a bug.

@sighingnow
Copy link
Member

Hi @danielzhanghl,

You could find sync-watch examples in

TEST_CASE("wait for a value change")
{
etcd::SyncClient etcd(etcd_uri);
etcd.set("/test/key1", "42");
std::thread watch_thrd([&]() {
etcd::Response res = etcd.watch("/test/key1");
REQUIRE("set" == res.action());
CHECK("43" == res.value().as_string());
});
sleep(1);
etcd.set("/test/key1", "43");
watch_thrd.join();
REQUIRE(0 == etcd.rmdir("/test", true).error_code());
}
TEST_CASE("wait for a directory change")
{
etcd::SyncClient etcd(etcd_uri);
std::thread watch_thrd1([&]() {
etcd::Response res = etcd.watch("/test", true);
CHECK("create" == res.action());
CHECK("44" == res.value().as_string());
});
sleep(1);
etcd.add("/test/key4", "44");
watch_thrd1.join();
std::thread watch_thrd2([&]() {
etcd::Response res2 = etcd.watch("/test", true);
CHECK("set" == res2.action());
CHECK("45" == res2.value().as_string());
});
sleep(1);
etcd.set("/test/key4", "45");
watch_thrd2.join();
REQUIRE(0 == etcd.rmdir("/test", true).error_code());
}
TEST_CASE("watch changes in the past")
{
etcd::SyncClient etcd(etcd_uri);
int index = etcd.set("/test/key1", "42").index();
etcd.set("/test/key1", "43");
etcd.set("/test/key1", "44");
etcd.set("/test/key1", "45");
etcd::Response res = etcd.watch("/test/key1", ++index);
CHECK("set" == res.action());
CHECK("43" == res.value().as_string());
res = etcd.watch("/test/key1", ++index);
CHECK("set" == res.action());
CHECK("44" == res.value().as_string());
res = etcd.watch("/test", ++index, true);
CHECK("set" == res.action());
CHECK("45" == res.value().as_string());
REQUIRE(0 == etcd.rmdir("/test", true).error_code());
}
.

sighingnow added a commit that referenced this issue May 25, 2021
As we have noticed a random "Segmentation fault" error with the following backtrace:

(gdb) bt
#0  _int_free (av=0x7f3db4000020, p=0x7f3db40018a0, have_lock=0) at malloc.c:4199
#1  0x0000557164b22982 in Catch::AssertionInfo::~AssertionInfo (this=0x7f3c32fcc3c8, __in_chrg=<optimized out>) at /home/gsbot/hetao/libvineyard/thirdparty/etcd-cpp-apiv3/catch.hpp:827
#2  0x0000557164b0afd6 in Catch::AssertionResult::~AssertionResult (this=0x7f3c32fcc3c8, __in_chrg=<optimized out>) at /home/gsbot/hetao/libvineyard/thirdparty/etcd-cpp-apiv3/catch.hpp:7275
#3  0x0000557164b117f4 in Catch::AssertionStats::~AssertionStats (this=0x7f3c32fcc3c0, __in_chrg=<optimized out>) at /home/gsbot/hetao/libvineyard/thirdparty/etcd-cpp-apiv3/catch.hpp:10254
#4  0x0000557164b26d8c in Catch::RunContext::assertionEnded (this=0x7ffc893f2e40, result=...) at /home/gsbot/hetao/libvineyard/thirdparty/etcd-cpp-apiv3/catch.hpp:5981
#5  0x0000557164b0f77f in Catch::ResultBuilder::handleResult (this=0x7f3c32fcca00, result=...) at /home/gsbot/hetao/libvineyard/thirdparty/etcd-cpp-apiv3/catch.hpp:8271
#6  0x0000557164b0f6ee in Catch::ResultBuilder::captureExpression (this=0x7f3c32fcca00) at /home/gsbot/hetao/libvineyard/thirdparty/etcd-cpp-apiv3/catch.hpp:8267
#7  0x0000557164b0f2ca in Catch::ResultBuilder::endExpression (this=0x7f3c32fcca00) at /home/gsbot/hetao/libvineyard/thirdparty/etcd-cpp-apiv3/catch.hpp:8229
#8  0x0000557164b4637c in Catch::ExpressionLhs<bool>::endExpression (this=0x7f3c32fcc6f0) at /home/gsbot/hetao/libvineyard/thirdparty/etcd-cpp-apiv3/catch.hpp:1850
#9  0x0000557164b176de in <lambda(const string&, size_t)>::operator()(const std::string &, size_t) const (__closure=0x557164dcb6b0, key="/test/test_key", index=112)
    at /home/gsbot/hetao/libvineyard/thirdparty/etcd-cpp-apiv3/tst/LockTest.cpp:184

Signed-off-by: Tao He <sighingnow@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants