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(server): adding unit tests for reply builder #764

Merged
merged 1 commit into from
Feb 12, 2023
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
3 changes: 2 additions & 1 deletion src/facade/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ if (DF_USE_SSL)
target_compile_definitions(dfly_facade PRIVATE DFLY_USE_SSL)
endif()

cxx_link(dfly_facade base uring_fiber_lib fibers_ext strings_lib http_server_lib
cxx_link(dfly_facade base uring_fiber_lib fibers_ext strings_lib http_server_lib
${TLS_LIB} TRDP::mimalloc TRDP::dconv)

add_library(facade_test facade_test.cc)
cxx_link(facade_test dfly_facade gtest_main_ext)

cxx_test(memcache_parser_test dfly_facade LABELS DFLY)
cxx_test(redis_parser_test facade_test LABELS DFLY)
cxx_test(reply_builder_test facade_test LABELS DFLY)

add_executable(ok_backend ok_main.cc)
cxx_link(ok_backend dfly_facade)
43 changes: 20 additions & 23 deletions src/facade/reply_builder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -218,42 +218,39 @@ void RedisReplyBuilder::SendBulkString(std::string_view str) {
return Send(v, ABSL_ARRAYSIZE(v));
}

void RedisReplyBuilder::SendError(OpStatus status) {
std::string_view RedisReplyBuilder::StatusToMsg(OpStatus status) {
switch (status) {
case OpStatus::OK:
SendOk();
break;
return "OK";
case OpStatus::KEY_NOTFOUND:
SendError(kKeyNotFoundErr);
break;
return kKeyNotFoundErr;
case OpStatus::WRONG_TYPE:
SendError(kWrongTypeErr);
break;
return kWrongTypeErr;
case OpStatus::OUT_OF_RANGE:
SendError(kIndexOutOfRange);
break;
return kIndexOutOfRange;
case OpStatus::INVALID_FLOAT:
SendError(kInvalidFloatErr);
break;
return kInvalidFloatErr;
case OpStatus::INVALID_INT:
SendError(kInvalidIntErr);
break;
return kInvalidIntErr;
case OpStatus::SYNTAX_ERR:
SendError(kSyntaxErr);
break;
return kSyntaxErr;
case OpStatus::OUT_OF_MEMORY:
SendError(kOutOfMemory);
break;
return kOutOfMemory;
case OpStatus::BUSY_GROUP:
SendError("-BUSYGROUP Consumer Group name already exists");
break;
return "-BUSYGROUP Consumer Group name already exists";
case OpStatus::INVALID_NUMERIC_RESULT:
SendError(kInvalidNumericResult);
break;
return kInvalidNumericResult;
default:
LOG(ERROR) << "Unsupported status " << status;
SendError("Internal error");
break;
return "Internal error";
}
}

void RedisReplyBuilder::SendError(OpStatus status) {
if (status == OpStatus::OK) {
SendOk();
} else {
SendError(StatusToMsg(status));
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/facade/reply_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,11 @@ class RedisReplyBuilder : public SinkReplyBuilder {

static char* FormatDouble(double val, char* dest, unsigned dest_len);

private:
// You normally should not call this - maps the status
// into the string that would be sent
static std::string_view StatusToMsg(OpStatus status);

private:
using StrPtr = std::variant<const std::string_view*, const std::string*>;
void SendStringArr(StrPtr str_ptr, uint32_t len);
};
Expand Down
Loading