diff options
-rw-r--r-- | src/nostr_service.cpp | 5 | ||||
-rw-r--r-- | test/nostr_service_test.cpp | 37 |
2 files changed, 32 insertions, 10 deletions
diff --git a/src/nostr_service.cpp b/src/nostr_service.cpp index e8f14f6..5b32beb 100644 --- a/src/nostr_service.cpp +++ b/src/nostr_service.cpp @@ -150,8 +150,9 @@ tuple<RelayList, RelayList> NostrService::publishEvent(shared_ptr<Event> event) } lock_guard<mutex> lock(this->_propertyMutex); + RelayList targetRelays = this->_activeRelays; vector<future<tuple<string, bool>>> publishFutures; - for (const string& relay : this->_activeRelays) + for (const string& relay : targetRelays) { promise<tuple<string, bool>> publishPromise; publishFutures.push_back(move(publishPromise.get_future())); @@ -196,7 +197,7 @@ tuple<RelayList, RelayList> NostrService::publishEvent(shared_ptr<Event> event) } } - size_t targetCount = this->_activeRelays.size(); + size_t targetCount = targetRelays.size(); size_t successfulCount = successfulRelays.size(); PLOG_INFO << "Published event to " << successfulCount << "/" << targetCount << " target relays."; diff --git a/test/nostr_service_test.cpp b/test/nostr_service_test.cpp index 854de78..cd6307b 100644 --- a/test/nostr_service_test.cpp +++ b/test/nostr_service_test.cpp @@ -422,10 +422,16 @@ TEST_F(NostrServiceTest, PublishEvent_CorrectlyIndicates_AllSuccesses) defaultTestRelays); nostrService->openRelayConnections(); - EXPECT_CALL(*mockClient, send(_, _)) + EXPECT_CALL(*mockClient, send(_, _, _)) .Times(2) - .WillRepeatedly(Invoke([](string message, string uri) + .WillRepeatedly(Invoke([](string message, string uri, function<void(const string&)> messageHandler) { + json messageArr = json::parse(message); + auto event = nostr::Event::fromString(messageArr[1]); + + json jarr = json::array({ "OK", event.id, true, "Event accepted" }); + messageHandler(jarr.dump()); + return make_tuple(uri, true); })); @@ -467,9 +473,10 @@ TEST_F(NostrServiceTest, PublishEvent_CorrectlyIndicates_AllFailures) defaultTestRelays); nostrService->openRelayConnections(); - EXPECT_CALL(*mockClient, send(_, _)) + // Simulate a case where the message failed to send to all relays. + EXPECT_CALL(*mockClient, send(_, _, _)) .Times(2) - .WillRepeatedly(Invoke([](string message, string uri) + .WillRepeatedly(Invoke([](string message, string uri, function<void(const string&)> messageHandler) { return make_tuple(uri, false); })); @@ -512,15 +519,23 @@ TEST_F(NostrServiceTest, PublishEvent_CorrectlyIndicates_MixedSuccessesAndFailur defaultTestRelays); nostrService->openRelayConnections(); - EXPECT_CALL(*mockClient, send(_, defaultTestRelays[0])) + // Simulate a scenario where the message fails to send to one relay, but sends successfully to + // the other, and the relay accepts it. + EXPECT_CALL(*mockClient, send(_, defaultTestRelays[0], _)) .Times(1) - .WillRepeatedly(Invoke([](string message, string uri) + .WillRepeatedly(Invoke([](string message, string uri, function<void(const string&)> messageHandler) { + json messageArr = json::parse(message); + auto event = nostr::Event::fromString(messageArr[1]); + + json jarr = json::array({ "OK", event.id, true, "Event accepted" }); + messageHandler(jarr.dump()); + return make_tuple(uri, true); })); - EXPECT_CALL(*mockClient, send(_, defaultTestRelays[1])) + EXPECT_CALL(*mockClient, send(_, defaultTestRelays[1], _)) .Times(1) - .WillRepeatedly(Invoke([](string message, string uri) + .WillRepeatedly(Invoke([](string message, string uri, function<void(const string&)> messageHandler) { return make_tuple(uri, false); })); @@ -534,4 +549,10 @@ TEST_F(NostrServiceTest, PublishEvent_CorrectlyIndicates_MixedSuccessesAndFailur ASSERT_EQ(failures.size(), 1); ASSERT_EQ(failures[0], defaultTestRelays[1]); }; + +// TODO: Add unit tests for events rejected by relays. + +// TODO: Add unit tests for queries. + +// TODO: Add unit tests for closing subscriptions. } // namespace nostr_test |