From 9528dcbf1a567c4fe0a2ecab7dbc7fc6f4a2d6ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Sat, 11 Dec 2021 21:51:36 +0100 Subject: Test: use numeric index for peer identification --- src/main.cpp | 57 +++++++++++++++++++++++++++++++++++++++++++------------- test/attach.test | 14 +++++++------- test/sync.test | 14 +++++++------- 3 files changed, 58 insertions(+), 27 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index b061747..690f7fb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -64,19 +64,40 @@ void printLine(const string & line) Storage st(getErebosDir()); optional> h; optional server; -map> attachAnswer; -Peer getPeer(const string & name) +struct TestPeer { - auto & plist = server->peerList(); - for (size_t i = 0; i < plist.size(); i++) - if (plist.at(i).name() == name) - return plist.at(i); + Peer peer; + size_t id; + bool deleted = false; + promise attachAnswer {}; +}; +vector testPeers; + +TestPeer & getPeer(const string & name) +{ + try { + return testPeers.at(std::stoi(name) - 1); + } + catch (const std::invalid_argument &) {} + + for (auto & p : testPeers) + if (p.peer.name() == name) + return p; + ostringstream ss; ss << "peer '" << name << "' not found"; throw invalid_argument(ss.str().c_str()); } +TestPeer & getPeer(const Peer & peer) +{ + for (auto & p : testPeers) + if (p.peer == peer) + return p; + throw invalid_argument("peer not found"); +} + struct Command { string name; @@ -111,7 +132,7 @@ void printAttachResult(Peer peer, future && success) { bool s = success.get(); ostringstream ss; - ss << "attach-result " << peer.name() << " " << s; + ss << "attach-result " << getPeer(peer).id << " " << s; printLine(ss.str()); } @@ -121,10 +142,10 @@ future confirmAttach(const Peer & peer, string confirm, future && su promise promise; auto input = promise.get_future(); - attachAnswer[peer] = move(promise); + getPeer(peer).attachAnswer = move(promise); ostringstream ss; - ss << "attach-confirm " << peer.name() << " " << confirm; + ss << "attach-confirm " << getPeer(peer).id << " " << confirm; printLine(ss.str()); return input; } @@ -143,9 +164,19 @@ void startServer(const vector &) server.emplace(*h, move(services)); server->peerList().onUpdate([](size_t idx, const Peer * peer) { + size_t i = 0; + while (idx > 0 && i < testPeers.size() && testPeers[i].deleted) { + if (!testPeers[i].deleted) + idx--; + i++; + } + ostringstream ss; - ss << "peer " << idx + 1; + ss << "peer " << i + 1; if (peer) { + if (i >= testPeers.size()) + testPeers.push_back(TestPeer { .peer = *peer, .id = i + 1 }); + if (peer->identity()) { ss << " id"; for (auto idt = peer->identity(); idt; idt = idt->owner()) @@ -155,6 +186,7 @@ void startServer(const vector &) ss << " addr " << inet_ntoa(paddr.sin_addr) << " " << ntohs(paddr.sin_port); } } else { + testPeers[i].deleted = true; ss << " deleted"; } printLine(ss.str()); @@ -216,13 +248,12 @@ void updateSharedIdentity(const vector & params) void attach(const vector & params) { - server->svc().attachTo(getPeer(params.at(0))); + server->svc().attachTo(getPeer(params.at(0)).peer); } void attachAccept(const vector & params) { - attachAnswer.extract(getPeer(params[0])) - .mapped().set_value(params[1] == "1"); + getPeer(params.at(0)).attachAnswer.set_value(params[1] == "1"); } vector commands = { diff --git a/test/attach.test b/test/attach.test index 7115b55..35fbde4 100644 --- a/test/attach.test +++ b/test/attach.test @@ -16,14 +16,14 @@ test: /peer 1 addr 192.168.0.11 29665/ /peer 1 id Device1 Owner/ - send "attach Owner" to p2 - expect /attach-confirm .*/ from p1 - expect /attach-confirm .*/ from p2 + send "attach 1" to p2 + expect /attach-confirm 1 .*/ from p1 + expect /attach-confirm 1 .*/ from p2 # TODO: check code match - send "attach-accept Device2 1" to p1 - send "attach-accept Owner 1" to p2 - expect /attach-result Device2 1/ from p1 - expect /attach-result Owner 1/ from p2 + send "attach-accept 1 1" to p1 + send "attach-accept 1 1" to p2 + expect /attach-result 1 1/ from p1 + expect /attach-result 1 1/ from p2 expect /local-identity Device2 Owner/ from p2 expect /peer 1 id Device2 Owner/ from p1 diff --git a/test/sync.test b/test/sync.test index 8cf5b4d..9ae22dc 100644 --- a/test/sync.test +++ b/test/sync.test @@ -19,15 +19,15 @@ test: /peer 1 addr 192.168.0.11 29665/ /peer 1 id Device1 Owner/ - send "attach Owner" to p2 - expect /attach-confirm .*/ from p1 - expect /attach-confirm .*/ from p2 + send "attach 1" to p2 + expect /attach-confirm 1 .*/ from p1 + expect /attach-confirm 1 .*/ from p2 # TODO: check code match - send "attach-accept Device2 1" to p1 - send "attach-accept Owner 1" to p2 - expect /attach-result Device2 1/ from p1 - expect /attach-result Owner 1/ from p2 + send "attach-accept 1 1" to p1 + send "attach-accept 1 1" to p2 + expect /attach-result 1 1/ from p1 + expect /attach-result 1 1/ from p2 expect /local-identity Device2 Owner/ from p2 expect /shared-identity Owner/ from p2 expect /peer 1 id Device2 Owner/ from p1 -- cgit v1.2.3