Add a test to show that two watchers can co-work
Signed-off-by: Tao He <linzhu.ht@alibaba-inc.com>
This commit is contained in:
parent
ba6216385f
commit
c911c83c53
|
|
@ -91,7 +91,9 @@ class Value {
|
||||||
int64_t leaseId;
|
int64_t leaseId;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef std::vector<Value> Values;
|
using Values = std::vector<Value>;
|
||||||
|
|
||||||
|
std::ostream& operator<<(std::ostream& os, const Value& value);
|
||||||
|
|
||||||
class Event {
|
class Event {
|
||||||
public:
|
public:
|
||||||
|
|
@ -122,7 +124,12 @@ class Event {
|
||||||
bool _has_kv, _has_prev_kv;
|
bool _has_kv, _has_prev_kv;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef std::vector<Event> Events;
|
using Events = std::vector<Event>;
|
||||||
|
|
||||||
|
std::ostream& operator<<(std::ostream& os, const Event::EventType& value);
|
||||||
|
|
||||||
|
std::ostream& operator<<(std::ostream& os, const Event& event);
|
||||||
|
|
||||||
} // namespace etcd
|
} // namespace etcd
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -44,6 +44,19 @@ int etcd::Value::ttl() const { return _ttl; }
|
||||||
|
|
||||||
int64_t etcd::Value::lease() const { return leaseId; }
|
int64_t etcd::Value::lease() const { return leaseId; }
|
||||||
|
|
||||||
|
std::ostream& etcd::operator<<(std::ostream& os, const etcd::Value& value) {
|
||||||
|
os << "Event: {";
|
||||||
|
os << "Key: " << value.key() << ", ";
|
||||||
|
os << "Value: " << value.as_string() << ", ";
|
||||||
|
os << "Created: " << value.created_index() << ", ";
|
||||||
|
os << "Modified: " << value.modified_index() << ", ";
|
||||||
|
os << "Version: " << value.version() << ", ";
|
||||||
|
os << "TTL: " << value.ttl() << ", ";
|
||||||
|
os << "Lease: " << value.lease() << ", ";
|
||||||
|
os << "}";
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
etcd::Event::Event(mvccpb::Event const& event) {
|
etcd::Event::Event(mvccpb::Event const& event) {
|
||||||
_has_kv = event.has_kv();
|
_has_kv = event.has_kv();
|
||||||
_has_prev_kv = event.has_prev_kv();
|
_has_prev_kv = event.has_prev_kv();
|
||||||
|
|
@ -73,3 +86,30 @@ bool etcd::Event::has_prev_kv() const { return _has_prev_kv; }
|
||||||
const etcd::Value& etcd::Event::kv() const { return _kv; }
|
const etcd::Value& etcd::Event::kv() const { return _kv; }
|
||||||
|
|
||||||
const etcd::Value& etcd::Event::prev_kv() const { return _prev_kv; }
|
const etcd::Value& etcd::Event::prev_kv() const { return _prev_kv; }
|
||||||
|
|
||||||
|
std::ostream& etcd::operator<<(std::ostream& os,
|
||||||
|
const etcd::Event::EventType& value) {
|
||||||
|
switch (value) {
|
||||||
|
case etcd::Event::EventType::PUT:
|
||||||
|
os << "PUT";
|
||||||
|
break;
|
||||||
|
case etcd::Event::EventType::DELETE_:
|
||||||
|
os << "DELETE";
|
||||||
|
break;
|
||||||
|
case etcd::Event::EventType::INVALID:
|
||||||
|
os << "INVALID";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::ostream& etcd::operator<<(std::ostream& os, const etcd::Event& event) {
|
||||||
|
os << "Event type: " << event.event_type();
|
||||||
|
if (event.has_kv()) {
|
||||||
|
os << ", KV: " << event.kv();
|
||||||
|
}
|
||||||
|
if (event.has_prev_kv()) {
|
||||||
|
os << ", Prev KV: " << event.prev_kv();
|
||||||
|
}
|
||||||
|
return os;
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -191,6 +191,41 @@ TEST_CASE("create two watcher") {
|
||||||
std::this_thread::sleep_for(std::chrono::seconds(5));
|
std::this_thread::sleep_for(std::chrono::seconds(5));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("using two watcher") {
|
||||||
|
etcd::SyncClient etcd(etcd_url);
|
||||||
|
|
||||||
|
int watched1 = 0;
|
||||||
|
int watched2 = 0;
|
||||||
|
|
||||||
|
etcd::Watcher w1(
|
||||||
|
etcd, "/test/def",
|
||||||
|
[&](etcd::Response const& resp) {
|
||||||
|
std::cout << "w1 called: " << resp.events().at(0).event_type() << " on "
|
||||||
|
<< resp.events().at(0).kv().key() << std::endl;
|
||||||
|
++watched1;
|
||||||
|
},
|
||||||
|
true);
|
||||||
|
etcd::Watcher w2(
|
||||||
|
etcd, "/test",
|
||||||
|
[&](etcd::Response const& resp) {
|
||||||
|
std::cout << "w2 called: " << resp.events().at(0).event_type() << " on "
|
||||||
|
<< resp.events().at(0).kv().key() << std::endl;
|
||||||
|
++watched2;
|
||||||
|
},
|
||||||
|
true);
|
||||||
|
|
||||||
|
std::this_thread::sleep_for(std::chrono::seconds(5));
|
||||||
|
|
||||||
|
etcd.put("/test/def/xxx", "42");
|
||||||
|
etcd.put("/test/abc", "42");
|
||||||
|
etcd.rm("/test/def/xxx");
|
||||||
|
etcd.rm("/test/abc");
|
||||||
|
|
||||||
|
std::this_thread::sleep_for(std::chrono::seconds(5));
|
||||||
|
CHECK(2 == watched1);
|
||||||
|
CHECK(4 == watched2);
|
||||||
|
}
|
||||||
|
|
||||||
// TEST_CASE("request cancellation")
|
// TEST_CASE("request cancellation")
|
||||||
// {
|
// {
|
||||||
// etcd::Client etcd(etcd_url);
|
// etcd::Client etcd(etcd_url);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue