diff options
author | Roman Smrž <roman.smrz@seznam.cz> | 2022-11-14 21:36:14 +0100 |
---|---|---|
committer | Roman Smrž <roman.smrz@seznam.cz> | 2022-11-14 21:36:14 +0100 |
commit | 14d6b6f7e4876b897592812475f201a01084c13d (patch) | |
tree | beb8f734040239e0171232be474a29349c0e3944 | |
parent | 7297ec818dae9c86b1f614743129553242cf23b6 (diff) |
Test: get and wait for shared state
-rw-r--r-- | src/main.cpp | 40 | ||||
-rw-r--r-- | test/sync.test | 5 |
2 files changed, 45 insertions, 0 deletions
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<string> &) server.reset(); } +void sharedStateGet(const vector<string> &) +{ + ostringstream ss; + ss << "shared-state-get"; + for (const auto & r : h->behavior().lens<vector<Ref>>().get()) + ss << " " << string(r.digest()); + printLine(ss.str()); +} + +void sharedStateWait(const vector<string> & args) +{ + static optional<Watched<vector<Ref>>> watched; + watched = h->behavior().lens<vector<Ref>>().watch([args, watched = watched] (const vector<Ref> & refs) mutable { + vector<Stored<Object>> objs; + objs.reserve(refs.size()); + for (const auto & r : refs) + objs.push_back(Stored<Object>::load(r)); + + auto objs2 = objs; + for (const auto & a : args) + if (auto ref = st.ref(Digest(a))) + objs2.push_back(Stored<Object>::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<string> &) { auto bhv = h->behavior().lens<optional<Identity>>(); @@ -395,6 +433,8 @@ vector<Command> 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 |