Implements the "etcdctl lease list" functionality (#174)

* Implements the "etcdctl lease list" functionalities.
* LeaseLeases requires etcd >= 3.3

Signed-off-by: Tao He <sighingnow@gmail.com>
This commit is contained in:
Tao He 2022-10-13 00:04:18 +08:00 committed by GitHub
parent 71b5b3a1a3
commit 1fb5abac28
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 166 additions and 75 deletions

View File

@ -504,6 +504,11 @@ namespace etcd
*/
pplx::task<Response> leasetimetolive(int64_t lease_id);
/**
* List all alive leases, equivalent to `etcdctl lease list`.
*/
pplx::task<Response> leases();
/**
* Gains a lock at a key, using a default created lease, using the default lease (10 seconds), with
* keeping alive has already been taken care of by the library.

View File

@ -198,6 +198,11 @@ namespace etcd
*/
uint64_t raft_term() const;
/**
* Returns ther current raft term.
*/
std::vector<int64_t> const & leases() const;
protected:
Response(const etcdv3::V3Response& response, std::chrono::microseconds const& duration);
Response(int error_code, std::string const& error_message);
@ -218,10 +223,14 @@ namespace etcd
// execute duration (in microseconds), during the action created and response parsed
std::chrono::microseconds _duration;
// cluster metadata
uint64_t _cluster_id;
uint64_t _member_id;
uint64_t _raft_term;
// for lease list
std::vector<int64_t> _leases;
friend class Client;
friend class SyncClient;
friend class etcdv3::AsyncWatchAction;

View File

@ -558,6 +558,11 @@ namespace etcd
*/
Response leasetimetolive(int64_t lease_id);
/**
* List all alive leases, equivalent to `etcdctl lease list`.
*/
Response leases();
/**
* Gains a lock at a key, using a default created lease, using the default lease (10 seconds), with
* keeping alive has already been taken care of by the library.
@ -685,6 +690,7 @@ namespace etcd
std::shared_ptr<etcdv3::AsyncWatchAction> watch_internal(std::string const & key, std::string const &range_end, int64_t fromIndex);
std::shared_ptr<etcdv3::AsyncLeaseRevokeAction> leaserevoke_internal(int64_t lease_id);
std::shared_ptr<etcdv3::AsyncLeaseTimeToLiveAction> leasetimetolive_internal(int64_t lease_id);
std::shared_ptr<etcdv3::AsyncLeaseLeasesAction> leases_internal();
Response lock_internal(std::string const &key, std::shared_ptr<etcd::KeepAlive> const &keepalive);
std::shared_ptr<etcdv3::AsyncLockAction> lock_with_lease_internal(std::string const &key, int64_t lease_id);
std::shared_ptr<etcdv3::AsyncUnlockAction> unlock_internal(std::string const &lock_key);

View File

@ -34,6 +34,7 @@ namespace etcdv3
uint64_t get_cluster_id() const;
uint64_t get_member_id() const;
uint64_t get_raft_term() const;
std::vector<int64_t> const &get_leases() const;
protected:
int error_code;
int64_t index;
@ -47,9 +48,14 @@ namespace etcdv3
std::string lock_key; // for lock
std::string name; // for campaign (in v3election)
std::vector<mvccpb::Event> events; // for watch
// cluster metadata
uint64_t cluster_id;
uint64_t member_id;
uint64_t raft_term;
// for lease list
std::vector<int64_t> leases;
};
}
#endif

View File

@ -549,6 +549,13 @@ pplx::task<etcd::Response> etcd::Client::leasetimetolive(int64_t lease_id)
this->client->leasetimetolive_internal(lease_id));
}
pplx::task<etcd::Response> etcd::Client::leases()
{
return etcd::detail::asyncify(
static_cast<responser_t<etcdv3::AsyncLeaseLeasesAction>>(Response::create),
this->client->leases_internal());
}
pplx::task<etcd::Response> etcd::Client::lock(std::string const &key) {
static const int DEFAULT_LEASE_TTL_FOR_LOCK = 10; // see also etcd::SyncClient::lock
return this->lock(key, DEFAULT_LEASE_TTL_FOR_LOCK);

View File

@ -27,6 +27,8 @@ etcd::Response::Response(const etcd::Response & response) {
this->_cluster_id = response._cluster_id;
this->_member_id = response._member_id;
this->_raft_term = response._raft_term;
this->_leases = response._leases;
}
etcd::Response::Response(const etcdv3::V3Response& reply, std::chrono::microseconds const& duration)
@ -65,6 +67,9 @@ etcd::Response::Response(const etcdv3::V3Response& reply, std::chrono::microseco
_cluster_id = reply.get_cluster_id();
_member_id = reply.get_member_id();
_raft_term = reply.get_raft_term();
// lease list
this->_leases = reply.get_leases();
}
etcd::Response::Response(int error_code, std::string const& error_message)
@ -178,3 +183,7 @@ uint64_t etcd::Response::member_id() const {
uint64_t etcd::Response::raft_term() const {
return this->_raft_term;
}
std::vector<int64_t> const & etcd::Response::leases() const {
return this->_leases;
}

View File

@ -933,6 +933,19 @@ std::shared_ptr<etcdv3::AsyncLeaseTimeToLiveAction> etcd::SyncClient::leasetimet
return std::make_shared<etcdv3::AsyncLeaseTimeToLiveAction>(std::move(params));
}
etcd::Response etcd::SyncClient::leases()
{
return Response::create(this->leases_internal());
}
std::shared_ptr<etcdv3::AsyncLeaseLeasesAction> etcd::SyncClient::leases_internal() {
etcdv3::ActionParameters params;
params.auth_token.assign(this->token_authenticator->renew_if_expired());
params.grpc_timeout = this->grpc_timeout;
params.lease_stub = stubs->leaseServiceStub.get();
return std::make_shared<etcdv3::AsyncLeaseLeasesAction>(std::move(params));
}
etcd::Response etcd::SyncClient::lock(std::string const &key) {
// routines in lock usually will be fast, less than 10 seconds.
//

View File

@ -28,8 +28,7 @@ void etcdv3::AsyncLeaseTimeToLiveResponse::ParseResponse(LeaseTimeToLiveResponse
void etcdv3::AsyncLeaseLeasesResponse::ParseResponse(LeaseLeasesResponse& resp) {
index = resp.header().revision();
// FIXME: only the first leases is recorded.
if (resp.leases_size() > 0) {
value.kvs.set_lease(resp.leases(0).id());
for (auto lease : resp.leases()) {
leases.emplace_back(lease.id());
}
}

View File

@ -97,3 +97,7 @@ uint64_t etcdv3::V3Response::get_member_id() const {
uint64_t etcdv3::V3Response::get_raft_term() const {
return this->raft_term;
}
std::vector<int64_t> const & etcdv3::V3Response::get_leases() const {
return this->leases;
}

View File

@ -5,16 +5,17 @@
#include "etcd/Client.hpp"
static const std::string etcd_url("http://127.0.0.1:2379");
TEST_CASE("setup with auth")
{
etcd::Client *etcd = etcd::Client::WithUser("http://127.0.0.1:2379", "root", "root");
etcd::Client *etcd = etcd::Client::WithUser(etcd_url, "root", "root");
etcd->rmdir("/test", true).wait();
}
TEST_CASE("add a new key after authenticate")
{
etcd::Client *etcd = etcd::Client::WithUser("http://127.0.0.1:2379", "root", "root");
etcd::Client *etcd = etcd::Client::WithUser(etcd_url, "root", "root");
etcd->rmdir("/test", true).wait();
etcd::Response resp = etcd->add("/test/key1", "42").get();
REQUIRE(0 == resp.error_code());
@ -34,7 +35,7 @@ TEST_CASE("add a new key after authenticate")
TEST_CASE("read a value from etcd")
{
etcd::Client *etcd = etcd::Client::WithUser("http://127.0.0.1:2379", "root", "root");
etcd::Client *etcd = etcd::Client::WithUser(etcd_url, "root", "root");
etcd::Response resp = etcd->get("/test/key1").get();
CHECK("get" == resp.action());
REQUIRE(resp.is_ok());
@ -45,6 +46,6 @@ TEST_CASE("read a value from etcd")
TEST_CASE("cleanup")
{
etcd::Client *etcd = etcd::Client::WithUser("http://127.0.0.1:2379", "root", "root");
etcd::Client *etcd = etcd::Client::WithUser(etcd_url, "root", "root");
REQUIRE(0 == etcd->rmdir("/test", true).get().error_code());
}

View File

@ -8,16 +8,17 @@
#include "etcd/Client.hpp"
#include "etcd/KeepAlive.hpp"
static const std::string etcd_url("http://127.0.0.1:2379");
TEST_CASE("setup")
{
etcd::Client etcd("http://127.0.0.1:2379");
etcd::Client etcd(etcd_url);
etcd.rmdir("/test", true).wait();
}
TEST_CASE("campaign and resign")
{
etcd::Client etcd("http://127.0.0.1:2379");
etcd::Client etcd(etcd_url);
auto keepalive = etcd.leasekeepalive(60).get();
auto lease_id = keepalive->Lease();
@ -56,6 +57,6 @@ TEST_CASE("campaign and resign")
TEST_CASE("cleanup")
{
etcd::Client etcd("http://127.0.0.1:2379");
etcd::Client etcd(etcd_url);
etcd.rmdir("/test", true).get();
}

View File

@ -5,11 +5,11 @@
#include "etcd/SyncClient.hpp"
static std::string etcd_uri("http://127.0.0.1:2379");
static const std::string etcd_url("http://127.0.0.1:2379");
TEST_CASE("sync operations")
{
etcd::SyncClient etcd(etcd_uri);
etcd::SyncClient etcd(etcd_url);
etcd.rmdir("/test", true);
// add
@ -111,7 +111,7 @@ TEST_CASE("sync operations")
TEST_CASE("wait for a value change")
{
etcd::SyncClient etcd(etcd_uri);
etcd::SyncClient etcd(etcd_url);
etcd.set("/test/key1", "42");
std::thread watch_thrd([&]() {
@ -129,7 +129,7 @@ TEST_CASE("wait for a value change")
TEST_CASE("wait for a directory change")
{
etcd::SyncClient etcd(etcd_uri);
etcd::SyncClient etcd(etcd_url);
std::thread watch_thrd1([&]() {
etcd::Response res = etcd.watch("/test", true);
@ -156,7 +156,7 @@ TEST_CASE("wait for a directory change")
TEST_CASE("watch changes in the past")
{
etcd::SyncClient etcd(etcd_uri);
etcd::SyncClient etcd(etcd_url);
int64_t index = etcd.set("/test/key1", "42").index();
@ -181,7 +181,7 @@ TEST_CASE("watch changes in the past")
// TEST_CASE("request cancellation")
// {
// etcd::Client etcd(etcd_uri);
// etcd::Client etcd(etcd_url);
// etcd.set("/test/key1", "42").wait();
// pplx::task<etcd::Response> res = etcd.watch("/test/key1");

View File

@ -7,16 +7,17 @@
#include "etcd/Client.hpp"
static const std::string etcd_url("http://127.0.0.1:2379");
TEST_CASE("setup")
{
etcd::Client etcd("http://127.0.0.1:2379");
etcd::Client etcd(etcd_url);
etcd.rmdir("/test", true).wait();
}
TEST_CASE("add a new key")
{
etcd::Client etcd("http://127.0.0.1:2379");
etcd::Client etcd(etcd_url);
etcd.rmdir("/test", true).wait();
etcd::Response resp = etcd.add("/test/key1", "42").get();
REQUIRE(0 == resp.error_code());
@ -36,7 +37,7 @@ TEST_CASE("add a new key")
TEST_CASE("read a value from etcd")
{
etcd::Client etcd("http://127.0.0.1:2379");
etcd::Client etcd(etcd_url);
etcd::Response resp = etcd.get("/test/key1").get();
CHECK("get" == resp.action());
REQUIRE(resp.is_ok());
@ -47,7 +48,7 @@ TEST_CASE("read a value from etcd")
TEST_CASE("simplified read")
{
etcd::Client etcd("http://127.0.0.1:2379");
etcd::Client etcd(etcd_url);
CHECK("42" == etcd.get("/test/key1").get().value().as_string());
CHECK(etcd::ERROR_KEY_NOT_FOUND == etcd.get("/test/key2").get().error_code()); // Key not found
CHECK("" == etcd.get("/test/key2").get().value().as_string()); // Key not found
@ -55,7 +56,7 @@ TEST_CASE("simplified read")
TEST_CASE("modify a key")
{
etcd::Client etcd("http://127.0.0.1:2379");
etcd::Client etcd(etcd_url);
etcd::Response resp = etcd.modify("/test/key1", "43").get();
REQUIRE(0 == resp.error_code()); // overwrite
CHECK("update" == resp.action());
@ -65,7 +66,7 @@ TEST_CASE("modify a key")
TEST_CASE("set a key")
{
etcd::Client etcd("http://127.0.0.1:2379");
etcd::Client etcd(etcd_url);
etcd::Response resp = etcd.set("/test/key1", "43").get();
REQUIRE(0 == resp.error_code()); // overwrite
CHECK("set" == resp.action());
@ -85,7 +86,7 @@ TEST_CASE("set a key")
TEST_CASE("atomic compare-and-swap")
{
etcd::Client etcd("http://127.0.0.1:2379");
etcd::Client etcd(etcd_url);
etcd.set("/test/key1", "42").wait();
// modify success
@ -109,7 +110,7 @@ TEST_CASE("atomic compare-and-swap")
TEST_CASE("delete a value")
{
etcd::Client etcd("http://127.0.0.1:2379");
etcd::Client etcd(etcd_url);
etcd::Response resp = etcd.rm("/test/key11111").get();
CHECK(!resp.is_ok());
CHECK(etcd::ERROR_KEY_NOT_FOUND == resp.error_code());
@ -139,7 +140,7 @@ TEST_CASE("delete a value")
TEST_CASE("atomic compare-and-delete based on prevValue")
{
etcd::Client etcd("http://127.0.0.1:2379");
etcd::Client etcd(etcd_url);
etcd.set("/test/key1", "42").wait();
etcd::Response res = etcd.rm_if("/test/key1", "43").get();
@ -155,7 +156,7 @@ TEST_CASE("atomic compare-and-delete based on prevValue")
TEST_CASE("atomic compare-and-delete based on prevIndex")
{
etcd::Client etcd("http://127.0.0.1:2379");
etcd::Client etcd(etcd_url);
int64_t index = etcd.set("/test/key1", "42").get().index();
etcd::Response res = etcd.rm_if("/test/key1", index - 1).get();
@ -171,7 +172,7 @@ TEST_CASE("atomic compare-and-delete based on prevIndex")
TEST_CASE("deep atomic compare-and-swap")
{
etcd::Client etcd("http://127.0.0.1:2379");
etcd::Client etcd(etcd_url);
etcd.set("/test/key1", "42").wait();
// modify success
@ -202,7 +203,7 @@ TEST_CASE("deep atomic compare-and-swap")
TEST_CASE("list a directory")
{
etcd::Client etcd("http://127.0.0.1:2379");
etcd::Client etcd(etcd_url);
CHECK(0 == etcd.ls("/test/new_dir").get().keys().size());
etcd.set("/test/new_dir/key1", "value1").wait();
@ -234,7 +235,7 @@ TEST_CASE("list a directory")
TEST_CASE("list by range")
{
etcd::Client etcd("http://127.0.0.1:2379");
etcd::Client etcd(etcd_url);
CHECK(0 == etcd.ls("/test/new_dir").get().keys().size());
etcd.set("/test/new_dir/key0", "value0").wait();
@ -274,7 +275,7 @@ TEST_CASE("list by range")
TEST_CASE("delete a directory")
{
etcd::Client etcd("http://127.0.0.1:2379");
etcd::Client etcd(etcd_url);
etcd.set("/test/new_dir/key1", "value1").wait();
etcd.set("/test/new_dir/key2", "value2").wait();
@ -305,7 +306,7 @@ TEST_CASE("delete a directory")
TEST_CASE("delete by range")
{
etcd::Client etcd("http://127.0.0.1:2379");
etcd::Client etcd(etcd_url);
CHECK(etcd::ERROR_KEY_NOT_FOUND == etcd.rmdir("/test/new_dir").get().error_code()); // key not found
etcd::Response resp = etcd.ls("/test/new_dir").get();
@ -326,7 +327,7 @@ TEST_CASE("delete by range")
TEST_CASE("wait for a value change")
{
etcd::Client etcd("http://127.0.0.1:2379");
etcd::Client etcd(etcd_url);
etcd.set("/test/key1", "42").wait();
pplx::task<etcd::Response> res = etcd.watch("/test/key1");
@ -344,7 +345,7 @@ TEST_CASE("wait for a value change")
TEST_CASE("wait for a directory change")
{
etcd::Client etcd("http://127.0.0.1:2379");
etcd::Client etcd(etcd_url);
pplx::task<etcd::Response> res = etcd.watch("/test", true);
CHECK(!res.is_done());
@ -371,7 +372,7 @@ TEST_CASE("wait for a directory change")
TEST_CASE("watch changes in the past")
{
etcd::Client etcd("http://127.0.0.1:2379");
etcd::Client etcd(etcd_url);
REQUIRE(0 == etcd.rmdir("/test", true).get().error_code());
int64_t index = etcd.set("/test/key1", "42").get().index();
@ -398,7 +399,7 @@ TEST_CASE("watch changes in the past")
TEST_CASE("watch range changes in the past")
{
etcd::Client etcd("http://127.0.0.1:2379");
etcd::Client etcd(etcd_url);
REQUIRE(0 == etcd.rmdir("/test", true).get().error_code());
int64_t index = etcd.set("/test/key1", "42").get().index();
@ -423,7 +424,7 @@ TEST_CASE("watch range changes in the past")
}
TEST_CASE("watch multiple keys and use promise") {
etcd::Client etcd("http://127.0.0.1:2379");
etcd::Client etcd(etcd_url);
int64_t start_index = etcd.set("/test/key1", "value1").get().index();
etcd.set("/test/key2", "value2").get();
@ -439,7 +440,7 @@ TEST_CASE("watch multiple keys and use promise") {
TEST_CASE("lease grant")
{
etcd::Client etcd("http://127.0.0.1:2379");
etcd::Client etcd(etcd_url);
etcd::Response res = etcd.leasegrant(60).get();
REQUIRE(res.is_ok());
CHECK(60 == res.value().ttl());
@ -451,7 +452,7 @@ TEST_CASE("lease grant")
CHECK("set" == res.action());
CHECK(leaseid == res.value().lease());
//change leaseid
// change with lease id
res = etcd.leasegrant(10).get();
leaseid = res.value().lease();
res = etcd.set("/test/key1", "43", leaseid).get();
@ -471,7 +472,7 @@ TEST_CASE("lease grant")
CHECK(leaseid == res.value().lease());
CHECK("44" == res.value().as_string());
//failure to attach lease id
// failure to attach invalid lease id
res = etcd.modify("/test/key1", "45", leaseid+1).get();
REQUIRE(!res.is_ok());
REQUIRE(5 == res.error_code());
@ -483,13 +484,13 @@ TEST_CASE("lease grant")
CHECK("compareAndSwap" == res.action());
CHECK("45" == res.value().as_string());
//failure to attach lease id
// failure to attach invalid lease id
res = etcd.modify_if("/test/key1", "46", "45", leaseid+1).get();
REQUIRE(!res.is_ok());
REQUIRE(5 == res.error_code());
CHECK("etcdserver: requested lease not found" == res.error_message());
// succes with the correct index
// succes with the correct index & lease id
res = etcd.modify_if("/test/key1", "44", index, leaseid).get();
index = res.index();
REQUIRE(res.is_ok());
@ -506,15 +507,35 @@ TEST_CASE("lease grant")
CHECK("create" == res.action());
CHECK(leaseid == res.value().lease());
//failure to attach lease id
//failure to attach invalid lease id
res = etcd.set("/test/key11111", "43", leaseid+1).get();
REQUIRE(!res.is_ok());
REQUIRE(5 == res.error_code());
CHECK("etcdserver: requested lease not found" == res.error_message());
}
TEST_CASE("lease list")
{
etcd::Client etcd(etcd_url);
etcd::Response res = etcd.leasegrant(60).get();
REQUIRE(res.is_ok());
CHECK(60 == res.value().ttl());
CHECK(0 < res.value().lease());
int64_t leaseid = res.value().lease();
etcd::Response leasesresp = etcd.leases().get();
if (leasesresp.is_ok()) {
REQUIRE(leasesresp.is_ok());
auto const &leases = leasesresp.leases();
REQUIRE(leases.size() > 0);
CHECK(std::find(leases.begin(), leases.end(), leaseid) != leases.end());
} else {
REQUIRE(leasesresp.error_code() == etcdv3::ERROR_GRPC_UNIMPLEMENTED);
}
}
TEST_CASE("cleanup")
{
etcd::Client etcd("http://127.0.0.1:2379");
etcd::Client etcd(etcd_url);
REQUIRE(0 == etcd.rmdir("/test", true).get().error_code());
}

View File

@ -10,10 +10,11 @@
#include "etcd/Client.hpp"
#include "etcd/KeepAlive.hpp"
static const std::string etcd_url("http://127.0.0.1:2379");
TEST_CASE("lock and unlock")
{
etcd::Client etcd("http://127.0.0.1:2379");
etcd::Client etcd(etcd_url);
// lock
etcd::Response resp1 = etcd.lock("/test/abcd").get();
@ -30,7 +31,7 @@ TEST_CASE("lock and unlock")
TEST_CASE("double lock will fail")
{
etcd::Client etcd("http://127.0.0.1:2379");
etcd::Client etcd(etcd_url);
// lock
etcd::Response resp1 = etcd.lock("/test/abcd").get();
@ -85,7 +86,7 @@ TEST_CASE("double lock will fail")
TEST_CASE("lock could be timeout")
{
etcd::Client etcd("http://127.0.0.1:2379");
etcd::Client etcd(etcd_url);
// setup the timeout
etcd.set_grpc_timeout(std::chrono::seconds(5));
@ -114,7 +115,7 @@ TEST_CASE("lock could be timeout")
TEST_CASE("lock using lease")
{
etcd::Client etcd("http://127.0.0.1:2379");
etcd::Client etcd(etcd_url);
bool failed = false;
@ -198,7 +199,7 @@ TEST_CASE("lock using lease")
TEST_CASE("concurrent lock & unlock")
{
etcd::Client etcd("http://127.0.0.1:2379");
etcd::Client etcd(etcd_url);
std::string const lock_key = "/test/test_key";
constexpr size_t trials = 192;
@ -226,7 +227,7 @@ TEST_CASE("concurrent lock & unlock")
}
TEST_CASE("concurrent lock & unlock with a put in between") {
etcd::Client etcd("http://127.0.0.1:2379");
etcd::Client etcd(etcd_url);
std::string const lock_key = "/test/test_key";
constexpr size_t trials = 128;

View File

@ -6,6 +6,8 @@
#include "etcd/Client.hpp"
#include "etcd/KeepAlive.hpp"
static const std::string etcd_url("http://127.0.0.1:2379");
class DistributedLock {
public:
DistributedLock(const std::string &lock_name,
@ -23,7 +25,7 @@ private:
DistributedLock::DistributedLock(const std::string &lock_name,
uint timeout) {
_etcd_client = std::unique_ptr<etcd::Client>(new etcd::Client("localhost:2379"));
_etcd_client = std::unique_ptr<etcd::Client>(new etcd::Client(etcd_url));
try {
if (timeout == 0) {

View File

@ -8,7 +8,8 @@
#include "etcd/SyncClient.hpp"
#include "etcd/Watcher.hpp"
static std::string etcd_uri("http://127.0.0.1:2379");
static const std::string etcd_url("http://127.0.0.1:2379");
static int watcher_called = 0;
void print_response(etcd::Response const & resp)
@ -70,13 +71,14 @@ TEST_CASE("watch should can be re-established")
// the watcher initialized in this way will auto re-connect to etcd
std::shared_ptr<etcd::Watcher> watcher;
initialize_watcher(etcd_uri, my_prefix, print_response, watcher);
initialize_watcher(etcd_url, my_prefix, print_response, watcher);
// issue some changes to see if the watcher works
for (int round = 0; round < 10; ++round) {
for (int round = 0; round < 100000; ++round) {
try {
etcd::Client client(etcd_uri);
auto response = client.set(my_prefix + "/foo", "bar-" + std::to_string(round)).get();
etcd::Client client(etcd_url);
auto response = client.set(
my_prefix + "/foo", "bar-" + std::to_string(round)).get();
} catch (...) {
// pass
}
@ -90,8 +92,9 @@ TEST_CASE("watch should can be re-established")
// the watcher has been cancelled and shouldn't work anymore
for (int round = 10; round < 20; ++round) {
try {
etcd::Client client(etcd_uri);
auto response = client.set(my_prefix + "/foo", "bar-" + std::to_string(round)).get();
etcd::Client client(etcd_url);
auto response = client.set(
my_prefix + "/foo", "bar-" + std::to_string(round)).get();
} catch (...) {
// pass
}

View File

@ -9,15 +9,17 @@ static std::string ca = "security-config/certs/ca.crt";
static std::string cert = "security-config/certs/etcd0.example.com.crt";
static std::string key = "security-config/private/etcd0.example.com.key";
static const std::string etcd_url("https://127.0.0.1:2379");
TEST_CASE("setup with auth")
{
etcd::Client *etcd = etcd::Client::WithSSL("https://127.0.0.1:2379", ca, cert, key);
etcd::Client *etcd = etcd::Client::WithSSL(etcd_url, ca, cert, key);
etcd->rmdir("/test", true).wait();
}
TEST_CASE("add a new key after authenticate")
{
etcd::Client *etcd = etcd::Client::WithSSL("https://127.0.0.1:2379", ca, cert, key);
etcd::Client *etcd = etcd::Client::WithSSL(etcd_url, ca, cert, key);
etcd->rmdir("/test", true).wait();
etcd::Response resp = etcd->add("/test/key1", "42").get();
REQUIRE(0 == resp.error_code());
@ -37,7 +39,7 @@ TEST_CASE("add a new key after authenticate")
TEST_CASE("read a value from etcd")
{
etcd::Client *etcd = etcd::Client::WithSSL("https://127.0.0.1:2379", ca, cert, key);
etcd::Client *etcd = etcd::Client::WithSSL(etcd_url, ca, cert, key);
etcd::Response resp = etcd->get("/test/key1").get();
CHECK("get" == resp.action());
REQUIRE(resp.is_ok());
@ -48,6 +50,6 @@ TEST_CASE("read a value from etcd")
TEST_CASE("cleanup")
{
etcd::Client *etcd = etcd::Client::WithSSL("https://127.0.0.1:2379", ca, cert, key);
etcd::Client *etcd = etcd::Client::WithSSL(etcd_url, ca, cert, key);
REQUIRE(0 == etcd->rmdir("/test", true).get().error_code());
}

View File

@ -8,16 +8,17 @@
#include "etcd/Client.hpp"
#include "etcd/v3/Transaction.hpp"
static const std::string etcd_url("http://127.0.0.1:2379");
TEST_CASE("setup")
{
etcd::Client etcd("http://127.0.0.1:2379");
etcd::Client etcd(etcd_url);
etcd.rmdir("/test", true).wait();
}
TEST_CASE("add a new key")
{
etcd::Client etcd("http://127.0.0.1:2379");
etcd::Client etcd(etcd_url);
etcd.rmdir("/test", true).wait();
// do some put using txn
@ -84,6 +85,6 @@ TEST_CASE("add a new key")
TEST_CASE("cleanup")
{
etcd::Client etcd("http://127.0.0.1:2379");
etcd::Client etcd(etcd_url);
REQUIRE(0 == etcd.rmdir("/test", true).get().error_code());
}

View File

@ -7,7 +7,8 @@
#include "etcd/Watcher.hpp"
#include "etcd/SyncClient.hpp"
static std::string etcd_uri("http://127.0.0.1:2379");
static const std::string etcd_url("http://127.0.0.1:2379");
static int watcher_called = 0;
void printResponse(etcd::Response const & resp)
@ -34,11 +35,11 @@ void printResponse(etcd::Response const & resp)
TEST_CASE("create watcher with cancel")
{
etcd::SyncClient etcd(etcd_uri);
etcd::SyncClient etcd(etcd_url);
etcd.rmdir("/test", true);
watcher_called = 0;
etcd::Watcher watcher(etcd_uri, "/test", printResponse, true);
etcd::Watcher watcher(etcd_url, "/test", printResponse, true);
std::this_thread::sleep_for(std::chrono::seconds(3));
etcd.set("/test/key", "42");
etcd.set("/test/key", "43");
@ -57,11 +58,11 @@ TEST_CASE("create watcher with cancel")
TEST_CASE("create watcher on ranges with cancel")
{
etcd::SyncClient etcd(etcd_uri);
etcd::SyncClient etcd(etcd_url);
etcd.rmdir("/test", true);
watcher_called = 0;
etcd::Watcher watcher(etcd_uri, "/test/key1", "/test/key5", printResponse);
etcd::Watcher watcher(etcd_url, "/test/key1", "/test/key5", printResponse);
std::this_thread::sleep_for(std::chrono::seconds(3));
etcd.set("/test/key1", "42");
etcd.set("/test/key2", "43");
@ -80,12 +81,12 @@ TEST_CASE("create watcher on ranges with cancel")
TEST_CASE("create watcher")
{
etcd::SyncClient etcd(etcd_uri);
etcd::SyncClient etcd(etcd_url);
etcd.rmdir("/test", true);
watcher_called = 0;
{
etcd::Watcher watcher(etcd_uri, "/test", printResponse, true);
etcd::Watcher watcher(etcd_url, "/test", printResponse, true);
std::this_thread::sleep_for(std::chrono::seconds(3));
etcd.set("/test/key", "42");
std::this_thread::sleep_for(std::chrono::seconds(3));
@ -99,14 +100,14 @@ TEST_CASE("create watcher")
TEST_CASE("watch should exit normally")
{
// cancal immediately after start watch.
etcd::Watcher watcher(etcd_uri, "/test", printResponse, true);
etcd::Watcher watcher(etcd_url, "/test", printResponse, true);
watcher.Cancel();
}
TEST_CASE("watch should can be cancelled repeatedly")
{
// cancal immediately after start watch.
etcd::Watcher watcher(etcd_uri, "/test", printResponse, true);
etcd::Watcher watcher(etcd_url, "/test", printResponse, true);
std::vector<std::thread> threads(10);
for (size_t i = 0; i < 10; ++i) {
threads[i] = std::thread([&]() {
@ -120,7 +121,7 @@ TEST_CASE("watch should can be cancelled repeatedly")
// TEST_CASE("request cancellation")
// {
// etcd::Client etcd(etcd_uri);
// etcd::Client etcd(etcd_url);
// etcd.set("/test/key1", "42").wait();
// pplx::task<etcd::Response> res = etcd.watch("/test/key1");