Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
data_model::ItemDescriptor<TData>(data->value())};
}

tl::expected<std::optional<DataSourceEventHandler::Put>, JsonError> tag_invoke(

Check warning on line 53 in libs/server-sdk/src/data_systems/background_sync/sources/streaming/event_handler.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

libs/server-sdk/src/data_systems/background_sync/sources/streaming/event_handler.cpp:53:69 [readability-function-cognitive-complexity]

function 'tag_invoke' has cognitive complexity of 37 (threshold 25)
boost::json::value_to_tag<
tl::expected<std::optional<DataSourceEventHandler::Put>,
JsonError>> const& unused,
Expand Down Expand Up @@ -101,10 +101,11 @@
return std::nullopt;
}

static tl::expected<DataSourceEventHandler::Delete, JsonError> tag_invoke(
boost::json::value_to_tag<
tl::expected<DataSourceEventHandler::Delete, JsonError>> const& unused,
boost::json::value const& json_value) {
static tl::expected<std::optional<DataSourceEventHandler::Delete>, JsonError>
tag_invoke(boost::json::value_to_tag<
tl::expected<std::optional<DataSourceEventHandler::Delete>,
JsonError>> const& unused,
boost::json::value const& json_value) {
boost::ignore_unused(unused);

if (!json_value.is_object()) {
Expand All @@ -119,9 +120,13 @@
PARSE_REQUIRED_FIELD(path, obj, "path");

auto kind = StreamingDataKinds::Kind(path);
if (!kind.has_value()) {
// The SDK ignores a delete for an unknown kind of item.
return std::nullopt;
}
auto key = StreamingDataKinds::Key(path);

if (kind.has_value() && key.has_value()) {
if (key.has_value()) {
del.kind = *kind;
del.key = *key;
return del;
Expand All @@ -136,7 +141,7 @@
: handler_(handler), logger_(logger), status_manager_(status_manager) {}

DataSourceEventHandler::MessageStatus DataSourceEventHandler::HandleMessage(
std::string const& type,

Check warning on line 144 in libs/server-sdk/src/data_systems/background_sync/sources/streaming/event_handler.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

libs/server-sdk/src/data_systems/background_sync/sources/streaming/event_handler.cpp:144:5 [bugprone-easily-swappable-parameters]

2 adjacent parameters of 'HandleMessage' of similar type ('const std::string &') are easily swapped by mistake
std::string const& data) {
if (type == "put") {
boost::system::error_code error_code;
Expand Down Expand Up @@ -211,21 +216,26 @@
return MessageStatus::kInvalidMessage;
}

auto res =
boost::json::value_to<tl::expected<Delete, JsonError>>(parsed);
auto res = boost::json::value_to<
tl::expected<std::optional<Delete>, JsonError>>(parsed);

if (res.has_value()) {
switch (res->kind) {
// The kind of the item is unknown, so ignore the delete.
if (!res->has_value()) {
return MessageStatus::kMessageHandled;
}
auto const& del = **res;
switch (del.kind) {
case data_components::DataKind::kFlag: {
handler_.Upsert(res->key,
handler_.Upsert(del.key,
data_model::FlagDescriptor(
data_model::Tombstone(res->version)));
data_model::Tombstone(del.version)));
return MessageStatus::kMessageHandled;
}
case data_components::DataKind::kSegment: {
handler_.Upsert(res->key,
handler_.Upsert(del.key,
data_model::SegmentDescriptor(
data_model::Tombstone(res->version)));
data_model::Tombstone(del.version)));
return MessageStatus::kMessageHandled;
}
default: {
Expand Down
17 changes: 17 additions & 0 deletions libs/server-sdk/tests/data_source_event_handler_test.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#include <gtest/gtest.h>

#include "data_components/memory_store/memory_store.hpp"
Expand Down Expand Up @@ -88,6 +88,23 @@
manager.Status().State());
}

TEST(DataSourceEventHandlerTests, HandlesDeleteForUnknownPath) {
auto logger = launchdarkly::logging::NullLogger();
auto store = std::make_shared<MemoryStore>();
DataSourceStatusManager manager;
DataSourceEventHandler event_handler(*store, logger, manager);

// Delete an item whose kind the SDK does not know.
auto res = event_handler.HandleMessage(
"delete", R"({"path": "/cats/Jack", "version": 1})");

// The SDK ignores the message and does not record an error.
ASSERT_EQ(DataSourceEventHandler::MessageStatus::kMessageHandled, res);
EXPECT_EQ(DataSourceStatus::DataSourceState::kInitializing,
manager.Status().State());
EXPECT_FALSE(manager.Status().LastError());
}

TEST(DataSourceEventHandlerTests, HandlesInvalidDelete) {
auto logger = launchdarkly::logging::NullLogger();
auto store = std::make_shared<MemoryStore>();
Expand All @@ -109,7 +126,7 @@
auto store = std::make_shared<MemoryStore>();
DataSourceStatusManager manager;
DataSourceEventHandler event_handler(*store, logger, manager);
auto payload =

Check warning on line 129 in libs/server-sdk/tests/data_source_event_handler_test.cpp

View workflow job for this annotation

GitHub Actions / cpp-linter

libs/server-sdk/tests/data_source_event_handler_test.cpp:129:5 [readability-qualified-auto]

'auto payload' can be declared as 'const auto *payload'
R"({"path":"/","data":{"segments":{"special":{"key":"special","included":["bob"],
"version":2}},"flags":{"HasBob":{"key":"HasBob","on":true,"fallthrough":
{"variation":1},"variations":[true,false],"version":4}}}})";
Expand Down
Loading