Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test Nullable* with operator bool() const, and std::move() them. #13

Merged
merged 2 commits into from
May 4, 2024
Merged
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
8 changes: 8 additions & 0 deletions src/lib_c5t_actor_model.h
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ class ActorSubscriberScope final {
ActorSubscriberScope(ActorSubscriberScope const&) = delete;
ActorSubscriberScope& operator=(ActorSubscriberScope const&) = delete;

friend class NullableActorSubscriberScope;
std::unique_ptr<ActorSubscriberScopeImpl> type_erased_impl_;

public:
Expand Down Expand Up @@ -352,10 +353,17 @@ class NullableActorSubscriberScope final {
return *this;
}

NullableActorSubscriberScope& operator=(ActorSubscriberScope&& rhs) {
type_erased_impl_ = std::move(rhs.type_erased_impl_);
return *this;
}

NullableActorSubscriberScope& operator=(std::nullptr_t) {
type_erased_impl_ = nullptr;
return *this;
}

operator bool() const { return type_erased_impl_ != nullptr; }
};

template <class T, class... ARGS>
Expand Down
9 changes: 9 additions & 0 deletions src/lib_c5t_storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,15 @@ class C5T_STORAGE_FIELD_ACCESSOR final {

bool Has(std::string key) const { return InnerGet(std::move(key)) != nullptr; }

T GetOrDefault(std::string key, T def = T()) const {
auto const& p = InnerGet(std::move(key));
if (p != nullptr) {
return *p;
} else {
return def;
}
}

template <class E = StorageKeyNotFoundException, typename... ARGS>
T const& GetOrThrow(std::string key, ARGS&&... args) const {
auto const& p = InnerGet(std::move(key));
Expand Down
4 changes: 0 additions & 4 deletions src/lib_test_actor_model.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
#pragma once

// #include "lib_c5t_actor_model.h"

#include "typesystem/types.h"

struct Event_DL2TEST final : crnt::CurrentSuper {
int v;
Event_DL2TEST(int v) : v(v) {}
};

void DefineTestStorageFields();
33 changes: 33 additions & 0 deletions src/test_c5t_actor_model.cc
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,39 @@ TEST(ActorModelTest, Smoke) {
}
}

TEST(ActorModelTest, Nullable) {
auto const b = Topic<TestEvent<'b'>>("b");

std::ostringstream oss;
ActorSubscriberScope s1 = C5T_SUBSCRIBE<TestWorker>(b, oss);

NullableActorSubscriberScope s2;
NullableActorSubscriberScope s3(nullptr);
s2 = nullptr;
EXPECT_FALSE(s2);
EXPECT_FALSE(s3);

s2 = std::move(s1);
EXPECT_TRUE(s2);
EXPECT_FALSE(s3);

s3 = std::move(s2);
EXPECT_FALSE(s2);
EXPECT_TRUE(s3);

oss.clear();
C5T_EMIT<TestEvent<'b'>>(b, 501);
C5T_ACTORS_DEBUG_WAIT_FOR_ALL_EVENTS_TO_PROPAGATE();
EXPECT_EQ("b501", oss.str());

s3 = nullptr;
EXPECT_FALSE(s2);
EXPECT_FALSE(s3);
C5T_EMIT<TestEvent<'b'>>(b, 502);
C5T_ACTORS_DEBUG_WAIT_FOR_ALL_EVENTS_TO_PROPAGATE();
EXPECT_EQ("b501", oss.str());
}

TEST(ActorModelTest, InjectedFromDLib) {
EXPECT_EQ(42,
C5T_DLIB_CALL("test_actor_model", [&](C5T_DLib& dlib) { return dlib.CallOrDefault<int()>("Smoke42"); }));
Expand Down
3 changes: 3 additions & 0 deletions src/test_c5t_storage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ TEST(StorageTest, MapStringString) {

{
ASSERT_FALSE(C5T_STORAGE(kv1).Has("k"));
ASSERT_EQ("nope", C5T_STORAGE(kv1).GetOrDefault("k", "nope"));
ASSERT_THROW(C5T_STORAGE(kv1).GetOrThrow("k"), StorageKeyNotFoundException);
ASSERT_FALSE(Exists(C5T_STORAGE(kv1).Get("k")));
}
Expand All @@ -88,6 +89,8 @@ TEST(StorageTest, MapStringString) {
ASSERT_TRUE(C5T_STORAGE(kv1).Has("k"));
EXPECT_EQ("v", C5T_STORAGE(kv1).GetOrThrow("k"));

ASSERT_EQ("v", C5T_STORAGE(kv1).GetOrDefault("k", "nope"));

auto const o = C5T_STORAGE(kv1).Get("k");
ASSERT_TRUE(Exists(o));
EXPECT_EQ("v", Value(o));
Expand Down