Watch on range by specifying `rang_end`.
Follow-up work on #51, and fixes #50. Signed-off-by: Tao He <sighingnow@gmail.com>
This commit is contained in:
parent
1b24751b9d
commit
b2ec43e73a
|
|
@ -13,20 +13,40 @@ namespace etcd
|
|||
public:
|
||||
Watcher(Client const &client, std::string const & key,
|
||||
std::function<void(Response)> callback, bool recursive=false);
|
||||
Watcher(Client const &client, std::string const & key,
|
||||
std::string const &range_end,
|
||||
std::function<void(Response)> callback);
|
||||
Watcher(Client const &client, std::string const & key, int fromIndex,
|
||||
std::function<void(Response)> callback, bool recursive=false);
|
||||
Watcher(Client const &client, std::string const & key,
|
||||
std::string const &range_end, int fromIndex,
|
||||
std::function<void(Response)> callback);
|
||||
Watcher(std::string const & address, std::string const & key,
|
||||
std::function<void(Response)> callback, bool recursive=false);
|
||||
Watcher(std::string const & address, std::string const & key,
|
||||
std::string const &range_end,
|
||||
std::function<void(Response)> callback);
|
||||
Watcher(std::string const & address, std::string const & key, int fromIndex,
|
||||
std::function<void(Response)> callback, bool recursive=false);
|
||||
Watcher(std::string const & address, std::string const & key,
|
||||
std::string const &range_end, int fromIndex,
|
||||
std::function<void(Response)> callback);
|
||||
Watcher(std::string const & address,
|
||||
std::string const & username, std::string const & password,
|
||||
std::string const & key,
|
||||
std::function<void(Response)> callback, bool recursive=false);
|
||||
Watcher(std::string const & address,
|
||||
std::string const & username, std::string const & password,
|
||||
std::string const & key, std::string const &range_end,
|
||||
std::function<void(Response)> callback);
|
||||
Watcher(std::string const & address,
|
||||
std::string const & username, std::string const & password,
|
||||
std::string const & key, int fromIndex,
|
||||
std::function<void(Response)> callback, bool recursive=false);
|
||||
Watcher(std::string const & address,
|
||||
std::string const & username, std::string const & password,
|
||||
std::string const & key, std::string const &range_end, int fromIndex,
|
||||
std::function<void(Response)> callback);
|
||||
|
||||
Watcher(Watcher const &) = delete;
|
||||
Watcher(Watcher &&) = delete;
|
||||
|
|
@ -55,6 +75,7 @@ namespace etcd
|
|||
|
||||
protected:
|
||||
void doWatch(std::string const & key,
|
||||
std::string const & range_end,
|
||||
std::string const & auth_token,
|
||||
std::function<void(Response)> callback);
|
||||
|
||||
|
|
|
|||
|
|
@ -17,12 +17,27 @@ etcd::Watcher::Watcher(Client const &client, std::string const & key,
|
|||
Watcher(client, key, -1, callback, recursive) {
|
||||
}
|
||||
|
||||
etcd::Watcher::Watcher(Client const &client, std::string const & key,
|
||||
std::string const &range_end,
|
||||
std::function<void(Response)> callback):
|
||||
Watcher(client, key, range_end, -1, callback) {
|
||||
}
|
||||
|
||||
etcd::Watcher::Watcher(Client const &client, std::string const & key, int fromIndex,
|
||||
std::function<void(Response)> callback, bool recursive):
|
||||
fromIndex(fromIndex), recursive(recursive) {
|
||||
stubs.reset(new EtcdServerStubs{});
|
||||
stubs->watchServiceStub = Watch::NewStub(client.channel);
|
||||
doWatch(key, client.auth_token, callback);
|
||||
doWatch(key, "", client.auth_token, callback);
|
||||
}
|
||||
|
||||
etcd::Watcher::Watcher(Client const &client, std::string const & key,
|
||||
std::string const &range_end, int fromIndex,
|
||||
std::function<void(Response)> callback):
|
||||
fromIndex(fromIndex), recursive(false) {
|
||||
stubs.reset(new EtcdServerStubs{});
|
||||
stubs->watchServiceStub = Watch::NewStub(client.channel);
|
||||
doWatch(key, range_end, client.auth_token, callback);
|
||||
}
|
||||
|
||||
etcd::Watcher::Watcher(std::string const & address, std::string const & key,
|
||||
|
|
@ -30,11 +45,23 @@ etcd::Watcher::Watcher(std::string const & address, std::string const & key,
|
|||
Watcher(address, key, -1, callback, recursive) {
|
||||
}
|
||||
|
||||
etcd::Watcher::Watcher(std::string const & address, std::string const & key,
|
||||
std::string const & range_end,
|
||||
std::function<void(Response)> callback):
|
||||
Watcher(address, key, range_end, -1, callback) {
|
||||
}
|
||||
|
||||
etcd::Watcher::Watcher(std::string const & address, std::string const & key, int fromIndex,
|
||||
std::function<void(Response)> callback, bool recursive):
|
||||
Watcher(Client(address), key, fromIndex, callback, recursive) {
|
||||
}
|
||||
|
||||
etcd::Watcher::Watcher(std::string const & address, std::string const & key,
|
||||
std::string const & range_end, int fromIndex,
|
||||
std::function<void(Response)> callback):
|
||||
Watcher(Client(address), key, range_end, fromIndex, callback) {
|
||||
}
|
||||
|
||||
etcd::Watcher::Watcher(std::string const & address,
|
||||
std::string const & username, std::string const & password,
|
||||
std::string const & key,
|
||||
|
|
@ -42,6 +69,13 @@ etcd::Watcher::Watcher(std::string const & address,
|
|||
Watcher(address, username, password, key, -1, callback, recursive) {
|
||||
}
|
||||
|
||||
etcd::Watcher::Watcher(std::string const & address,
|
||||
std::string const & username, std::string const & password,
|
||||
std::string const & key, std::string const & range_end,
|
||||
std::function<void(Response)> callback):
|
||||
Watcher(address, username, password, key, range_end, -1, callback) {
|
||||
}
|
||||
|
||||
etcd::Watcher::Watcher(std::string const & address,
|
||||
std::string const & username, std::string const & password,
|
||||
std::string const & key, int fromIndex,
|
||||
|
|
@ -49,6 +83,13 @@ etcd::Watcher::Watcher(std::string const & address,
|
|||
Watcher(Client(address, username, password), key, fromIndex, callback, recursive) {
|
||||
}
|
||||
|
||||
etcd::Watcher::Watcher(std::string const & address,
|
||||
std::string const & username, std::string const & password,
|
||||
std::string const & key, std::string const & range_end, int fromIndex,
|
||||
std::function<void(Response)> callback):
|
||||
Watcher(Client(address, username, password), key, range_end, fromIndex, callback) {
|
||||
}
|
||||
|
||||
etcd::Watcher::~Watcher()
|
||||
{
|
||||
stubs->call->CancelWatch();
|
||||
|
|
@ -76,12 +117,14 @@ void etcd::Watcher::Cancel()
|
|||
}
|
||||
|
||||
void etcd::Watcher::doWatch(std::string const & key,
|
||||
std::string const & range_end,
|
||||
std::string const & auth_token,
|
||||
std::function<void(Response)> callback)
|
||||
{
|
||||
etcdv3::ActionParameters params;
|
||||
params.auth_token.assign(auth_token);
|
||||
params.key.assign(key);
|
||||
params.range_end.assign(range_end);
|
||||
if (fromIndex >= 0) {
|
||||
params.revision = fromIndex;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -46,7 +46,29 @@ TEST_CASE("create watcher with cancel")
|
|||
CHECK(4 == watcher_called);
|
||||
|
||||
etcd.rmdir("/test", true);
|
||||
}
|
||||
|
||||
TEST_CASE("create watcher on ranges with cancel")
|
||||
{
|
||||
etcd::SyncClient etcd(etcd_uri);
|
||||
etcd.rmdir("/test", true);
|
||||
|
||||
watcher_called = 0;
|
||||
etcd::Watcher watcher(etcd_uri, "/test/key1", "/test/key5", printResponse);
|
||||
std::this_thread::sleep_for(std::chrono::seconds(3));
|
||||
etcd.set("/test/key1", "42");
|
||||
etcd.set("/test/key2", "43");
|
||||
etcd.rm("/test/key1");
|
||||
etcd.set("/test/key5", "44");
|
||||
std::this_thread::sleep_for(std::chrono::seconds(3));
|
||||
CHECK(3 == watcher_called);
|
||||
watcher.Cancel();
|
||||
etcd.set("/test/key3", "50");
|
||||
etcd.set("/test/key4", "51");
|
||||
std::this_thread::sleep_for(std::chrono::seconds(3));
|
||||
CHECK(3 == watcher_called);
|
||||
|
||||
etcd.rmdir("/test", true);
|
||||
}
|
||||
|
||||
TEST_CASE("create watcher")
|
||||
|
|
|
|||
Loading…
Reference in New Issue