From 7fe755ae53266b738f841915a22767bd0b13599e Mon Sep 17 00:00:00 2001 From: Tao He Date: Mon, 12 Oct 2020 13:19:23 +0800 Subject: [PATCH] Make sure a cancalable watch must be a successful watch. Signed-off-by: Tao He --- src/v3/AsyncWatchAction.cpp | 19 ++++++------------- tst/WatcherTest.cpp | 7 +++++++ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/v3/AsyncWatchAction.cpp b/src/v3/AsyncWatchAction.cpp index d5fa792..15ee5d1 100644 --- a/src/v3/AsyncWatchAction.cpp +++ b/src/v3/AsyncWatchAction.cpp @@ -36,12 +36,6 @@ etcdv3::AsyncWatchAction::AsyncWatchAction(etcdv3::ActionParameters param) } else { throw std::runtime_error("failed to create a watch connection"); } -} - -void etcdv3::AsyncWatchAction::waitForResponse() -{ - void* got_tag; - bool ok = false; // wait "write" (WatchCreateRequest) success, and start to read the first reply if (cq_.Next(&got_tag, &ok) && ok && got_tag == (void *)"write") { @@ -49,6 +43,12 @@ void etcdv3::AsyncWatchAction::waitForResponse() } else { throw std::runtime_error("failed to write WatchCreateRequest to server"); } +} + +void etcdv3::AsyncWatchAction::waitForResponse() +{ + void* got_tag; + bool ok = false; while(cq_.Next(&got_tag, &ok)) { @@ -102,13 +102,6 @@ void etcdv3::AsyncWatchAction::waitForResponse(std::functionRead(&reply, (void*)this); - } else { - throw std::runtime_error("failed to write WatchCreateRequest to server"); - } - while(cq_.Next(&got_tag, &ok)) { if(ok == false) diff --git a/tst/WatcherTest.cpp b/tst/WatcherTest.cpp index 7cf3609..4e6dea1 100644 --- a/tst/WatcherTest.cpp +++ b/tst/WatcherTest.cpp @@ -63,6 +63,13 @@ TEST_CASE("create watcher") etcd.rmdir("/test", true).error_code(); } +TEST_CASE("watch should exit normally") +{ + // cancal immediately after start watch. + etcd::Watcher watcher(etcd_uri, "/test", printResponse, true); + watcher.Cancel(); +} + // TEST_CASE("request cancellation") // { // etcd::Client etcd(etcd_uri);