Skip to content

Commit

Permalink
Add a test to show that two watchers can co-work
Browse files Browse the repository at this point in the history
Signed-off-by: Tao He <linzhu.ht@alibaba-inc.com>
  • Loading branch information
sighingnow committed Mar 13, 2024
1 parent ba62163 commit c911c83
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 2 deletions.
11 changes: 9 additions & 2 deletions etcd/Value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ class Value {
int64_t leaseId;
};

typedef std::vector<Value> Values;
using Values = std::vector<Value>;

std::ostream& operator<<(std::ostream& os, const Value& value);

class Event {
public:
Expand Down Expand Up @@ -122,7 +124,12 @@ class Event {
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

#endif
40 changes: 40 additions & 0 deletions src/Value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ int etcd::Value::ttl() const { return _ttl; }

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) {
_has_kv = event.has_kv();
_has_prev_kv = event.has_prev_kv();
Expand Down Expand Up @@ -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::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;
}
35 changes: 35 additions & 0 deletions tst/WatcherTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,41 @@ TEST_CASE("create two watcher") {
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")
// {
// etcd::Client etcd(etcd_url);
Expand Down

0 comments on commit c911c83

Please sign in to comment.