Skip to content

Commit 3f90218

Browse files
committed
Update event serialization tests to use FeatureEvent constructor
1 parent eb799f3 commit 3f90218

File tree

5 files changed

+63
-46
lines changed

5 files changed

+63
-46
lines changed

libs/common/include/events/client_events.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ struct FeatureEventBase {
3030
Value value;
3131
std::optional<Reason> reason;
3232
Value default_;
33+
34+
explicit FeatureEventBase(FeatureEventParams const& params);
3335
};
3436

3537
struct FeatureEvent : public FeatureEventBase {

libs/common/src/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,11 @@ add_library(${LIBNAME}
4242
events/outbox.cpp
4343
events/conn_pool.cpp
4444
events/summarizer.cpp
45+
events/client_events.cpp
4546
config/http_properties.cpp
4647
config/data_source_builder.cpp
4748
config/http_properties_builder.cpp)
48-
49+
4950

5051
add_library(launchdarkly::common ALIAS ${LIBNAME})
5152

libs/common/src/events/asio_event_processor.cpp

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -173,21 +173,7 @@ std::vector<OutputEvent> AsioEventProcessor::Process(InputEvent input_event) {
173173
return;
174174
}
175175

176-
// TODO(cwaldren): should also add the reason if the
177-
// variation method was VariationDetail().
178-
std::optional<Reason> reason;
179-
if (event.eval_result.track_reason()) {
180-
reason = event.eval_result.detail().reason();
181-
}
182-
183-
client::FeatureEventBase base = {
184-
event.creation_date,
185-
std::move(event.key),
186-
event.eval_result.version(),
187-
event.eval_result.detail().variation_index(),
188-
event.eval_result.detail().value(),
189-
reason,
190-
std::move(event.default_)};
176+
client::FeatureEventBase base{event};
191177

192178
auto debug_until_date =
193179
event.eval_result.debug_events_until_date();
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include "events/client_events.hpp"
2+
3+
namespace launchdarkly::events::client {
4+
FeatureEventBase::FeatureEventBase(FeatureEventParams const& params)
5+
: creation_date(params.creation_date),
6+
key(params.key),
7+
version(params.eval_result.version()),
8+
variation(params.eval_result.detail().variation_index()),
9+
value(params.eval_result.detail().value()),
10+
default_(params.default_) {
11+
12+
// TODO(cwaldren): should also add the reason if the
13+
// variation method was VariationDetail().
14+
if (params.eval_result.track_reason()) {
15+
reason = params.eval_result.detail().reason();
16+
}
17+
}
18+
19+
} // namespace launchdarkly::events::client

libs/common/tests/event_serialization_test.cpp

Lines changed: 39 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,61 +13,70 @@ namespace launchdarkly::events {
1313

1414
TEST(EventSerialization, FeatureEvent) {
1515
auto creation_date = std::chrono::system_clock::from_time_t({});
16-
auto e = events::client::FeatureEvent{
17-
{creation_date, "key", 17, 2, "value",
18-
EvaluationReason("foo", std::nullopt, std::nullopt, std::nullopt,
19-
std::nullopt, false, std::nullopt),
20-
21-
17},
16+
auto event = events::client::FeatureEvent{
17+
client::FeatureEventBase(client::FeatureEventParams{
18+
creation_date,
19+
"key",
20+
ContextBuilder().kind("foo", "bar").build(),
21+
EvaluationResult(
22+
1, std::nullopt, false, false, std::nullopt,
23+
EvaluationDetailInternal(
24+
Value(42), 2,
25+
Reason("error", std::nullopt, std::nullopt, std::nullopt,
26+
std::nullopt, false, std::nullopt))),
27+
3,
28+
}),
2229
std::map<std::string, std::string>{{"foo", "bar"}}};
2330

24-
auto event = boost::json::value_from(e);
31+
auto event_json = boost::json::value_from(event);
2532

2633
auto result = boost::json::parse(
27-
"{\"creationDate\":0,\"key\":\"key\",\"version\":17,\"variation\":2,"
28-
"\"value\":\"value\",\"reason\":{\"kind\":\"foo\"},\"default\":1.7E1,"
29-
"\"kind\":\"feature\",\"contextKeys\":{\"foo\":\"bar\"}}");
34+
R"({"creationDate":0,"key":"key","version":1,"variation":2,"value":4.2E1,"default":3E0,"kind":"feature","contextKeys":{"foo":"bar"}})");
3035

31-
ASSERT_EQ(result, event);
36+
ASSERT_EQ(result, event_json);
3237
}
3338

3439
TEST(EventSerialization, DebugEvent) {
3540
auto creation_date = std::chrono::system_clock::from_time_t({});
3641
AttributeReference::SetType attrs;
3742
ContextFilter filter(false, attrs);
38-
auto e = events::client::DebugEvent{
39-
{creation_date, "key", 17, 2, "value",
40-
EvaluationReason("foo", std::nullopt, std::nullopt, std::nullopt,
41-
std::nullopt, false, std::nullopt),
42-
43-
17},
44-
filter.filter(ContextBuilder().kind("foo", "bar").build())};
45-
46-
auto event = boost::json::value_from(e);
43+
auto context = ContextBuilder().kind("foo", "bar").build();
44+
auto event = events::client::DebugEvent{
45+
client::FeatureEventBase(client::FeatureEventParams{
46+
creation_date,
47+
"key",
48+
context,
49+
EvaluationResult(
50+
1, std::nullopt, false, false, std::nullopt,
51+
EvaluationDetailInternal(
52+
Value(42), 2,
53+
Reason("error", std::nullopt, std::nullopt, std::nullopt,
54+
std::nullopt, false, std::nullopt))),
55+
3,
56+
}),
57+
filter.filter(context)};
58+
59+
auto event_json = boost::json::value_from(event);
4760

4861
auto result = boost::json::parse(
49-
"{\"creationDate\":0,\"key\":\"key\",\"version\":17,\"variation\":2,"
50-
"\"value\":\"value\",\"reason\":{\"kind\":\"foo\"},\"default\":1.7E1,"
51-
"\"kind\":\"debug\",\"context\":{\"key\":\"bar\",\"kind\":\"foo\"}}");
62+
R"({"creationDate":0,"key":"key","version":1,"variation":2,"value":4.2E1,"default":3E0,"kind":"debug","context":{"key":"bar","kind":"foo"}})");
5263

53-
ASSERT_EQ(result, event);
64+
ASSERT_EQ(result, event_json);
5465
}
5566

5667
TEST(EventSerialization, IdentifyEvent) {
5768
auto creation_date = std::chrono::system_clock::from_time_t({});
5869
AttributeReference::SetType attrs;
5970
ContextFilter filter(false, attrs);
60-
auto e = events::client::IdentifyEvent{
71+
auto event = events::client::IdentifyEvent{
6172
creation_date,
6273
filter.filter(ContextBuilder().kind("foo", "bar").build())};
6374

64-
auto event = boost::json::value_from(e);
75+
auto event_json = boost::json::value_from(event);
6576

6677
auto result = boost::json::parse(
67-
"{\"kind\":\"identify\",\"creationDate\":0,\"context\":{\"key\":"
68-
"\"bar\",\"kind\":\"foo\"}}");
69-
70-
ASSERT_EQ(result, event);
78+
R"({"kind":"identify","creationDate":0,"context":{"key":"bar","kind":"foo"}})");
79+
ASSERT_EQ(result, event_json);
7180
}
7281

7382
} // namespace launchdarkly::events

0 commit comments

Comments
 (0)