Skip to content

Commit

Permalink
[rd-cpp] Fix serialization of uninitialized properties.
Browse files Browse the repository at this point in the history
  • Loading branch information
mirasrael committed Jan 16, 2024
1 parent 2b05bd0 commit 5c282ba
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
21 changes: 15 additions & 6 deletions rd-cpp/src/rd_framework_cpp/src/main/impl/RdProperty.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,29 @@ class RdProperty final : public RdPropertyBase<T, S>, public ISerializable
static RdProperty<T, S> read(SerializationCtx& ctx, Buffer& buffer)
{
RdId id = RdId::read(buffer);
bool not_null = buffer.read_bool(); // not null/
(void) not_null;
auto value = S::read(ctx, buffer);
RdProperty<T, S> property;
property.value = std::move(value);
withId(property, id);
const bool has_value = buffer.read_bool();
if (has_value)
{
auto value = S::read(ctx, buffer);
property.value = std::move(value);
}
return property;
}

void write(SerializationCtx& ctx, Buffer& buffer) const override
{
this->rdid.write(buffer);
buffer.write_bool(true);
S::write(ctx, buffer, this->get());
if (this->has_value())
{
buffer.write_bool(true);
S::write(ctx, buffer, this->get());
}
else
{
buffer.write_bool(false);
}
}

void advise(Lifetime lifetime, std::function<void(T const&)> handler) const override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class WiredRdTaskImpl : public RdReactiveBase
}

template <class NonBindable = T, std::enable_if_t<!util::is_bindable_v<NonBindable>, bool> = true>
TaskResult bind_result(TaskResult result) const
TaskResult bind_result(TaskResult task_result) const
{
return result;
}
Expand Down
30 changes: 30 additions & 0 deletions rd-cpp/src/rd_framework_cpp/src/test/cases/RdTaskTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,5 +204,35 @@ TEST_F(RdFrameworkTestBase, testAsyncBindableCall)
EXPECT_TRUE(server_result_lifetime_terminated) << "Expected server lifetime for result to be terminated.";
EXPECT_TRUE(server_result.unique()) << "Expected server_result to be released. Test should hold only reference to server result.";

AfterTest();
}

TEST_F(RdFrameworkTestBase, testUninitializedPropertyInResult)
{
RdEndpoint<std::wstring, test::util::DynamicEntity> server_entity;
RdCall<std::wstring, test::util::DynamicEntity> client_entity;

statics(server_entity, static_entity_id);
statics(client_entity, static_entity_id);

Wrapper<test::util::DynamicEntity> server_result;

server_entity.set([&](std::wstring const&)
{
server_result = wrapper::make_wrapper<test::util::DynamicEntity>();
return server_result;
});

bindStatic(serverProtocol.get(), server_entity, static_name);
bindStatic(clientProtocol.get(), client_entity, static_name);

auto task_result = client_entity.start(L"xy").value_or_throw();
auto client_result = task_result.get_value();
auto& foo_property = client_result->get_foo();
EXPECT_THROW(foo_property.get(), std::exception) << "Expected to throw when access unitialized property";

server_result->get_foo().set(2);
EXPECT_EQ(2, foo_property.get()) << "Expected to sync property value when it set on server.";

AfterTest();
}

0 comments on commit 5c282ba

Please sign in to comment.