From 14d6b6f7e4876b897592812475f201a01084c13d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Mon, 14 Nov 2022 21:36:14 +0100 Subject: Test: get and wait for shared state --- src/main.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ test/sync.test | 5 +++++ 2 files changed, 45 insertions(+) diff --git a/src/main.cpp b/src/main.cpp index dad307c..387fe4a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -306,6 +306,44 @@ void stopServer(const vector &) server.reset(); } +void sharedStateGet(const vector &) +{ + ostringstream ss; + ss << "shared-state-get"; + for (const auto & r : h->behavior().lens>().get()) + ss << " " << string(r.digest()); + printLine(ss.str()); +} + +void sharedStateWait(const vector & args) +{ + static optional>> watched; + watched = h->behavior().lens>().watch([args, watched = watched] (const vector & refs) mutable { + vector> objs; + objs.reserve(refs.size()); + for (const auto & r : refs) + objs.push_back(Stored::load(r)); + + auto objs2 = objs; + for (const auto & a : args) + if (auto ref = st.ref(Digest(a))) + objs2.push_back(Stored::load(*ref)); + else + return; + + filterAncestors(objs2); + if (objs2 == objs) { + ostringstream ss; + ss << "shared-state-wait"; + for (const auto & a : args) + ss << " " << a; + printLine(ss.str()); + + watched = std::nullopt; + } + }); +} + void watchLocalIdentity(const vector &) { auto bhv = h->behavior().lens>(); @@ -395,6 +433,8 @@ vector commands = { { "create-identity", createIdentity }, { "start-server", startServer }, { "stop-server", stopServer }, + { "shared-state-get", sharedStateGet }, + { "shared-state-wait", sharedStateWait }, { "watch-local-identity", watchLocalIdentity }, { "watch-shared-identity", watchSharedIdentity }, { "update-local-identity", updateLocalIdentity }, diff --git a/test/sync.test b/test/sync.test index 06d1a92..b9f3354 100644 --- a/test/sync.test +++ b/test/sync.test @@ -43,3 +43,8 @@ test: send "update-shared-identity NewOwner3" to p1 expect /shared-identity NewOwner3/ from p1 expect /shared-identity NewOwner3/ from p2 + + send "shared-state-get" to p1 + expect /shared-state-get (.*)/ from p1 capture p1state + send "shared-state-wait $p1state" to p2 + expect /shared-state-wait $p1state/ from p2 -- cgit v1.2.3