diff --git a/etcd/Watcher.hpp b/etcd/Watcher.hpp index 3c9bdc0..0ed32a7 100644 --- a/etcd/Watcher.hpp +++ b/etcd/Watcher.hpp @@ -13,20 +13,40 @@ namespace etcd public: Watcher(Client const &client, std::string const & key, std::function callback, bool recursive=false); + Watcher(Client const &client, std::string const & key, + std::string const &range_end, + std::function callback); Watcher(Client const &client, std::string const & key, int fromIndex, std::function callback, bool recursive=false); + Watcher(Client const &client, std::string const & key, + std::string const &range_end, int fromIndex, + std::function callback); Watcher(std::string const & address, std::string const & key, std::function callback, bool recursive=false); + Watcher(std::string const & address, std::string const & key, + std::string const &range_end, + std::function callback); Watcher(std::string const & address, std::string const & key, int fromIndex, std::function callback, bool recursive=false); + Watcher(std::string const & address, std::string const & key, + std::string const &range_end, int fromIndex, + std::function callback); Watcher(std::string const & address, std::string const & username, std::string const & password, std::string const & key, std::function 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 callback); Watcher(std::string const & address, std::string const & username, std::string const & password, std::string const & key, int fromIndex, std::function 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 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 callback); diff --git a/src/Watcher.cpp b/src/Watcher.cpp index 303572b..89b979c 100644 --- a/src/Watcher.cpp +++ b/src/Watcher.cpp @@ -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 callback): + Watcher(client, key, range_end, -1, callback) { +} + etcd::Watcher::Watcher(Client const &client, std::string const & key, int fromIndex, std::function 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 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 callback): + Watcher(address, key, range_end, -1, callback) { +} + etcd::Watcher::Watcher(std::string const & address, std::string const & key, int fromIndex, std::function 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 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 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 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 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; } diff --git a/tst/WatcherTest.cpp b/tst/WatcherTest.cpp index 625babd..616116b 100644 --- a/tst/WatcherTest.cpp +++ b/tst/WatcherTest.cpp @@ -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")