Skip to content

Commit

Permalink
test(server): adding unit tests for reply builder (#764)
Browse files Browse the repository at this point in the history
Signed-off-by: Boaz Sade <[email protected]>
  • Loading branch information
boazsade authored Feb 12, 2023
1 parent 1dd4148 commit b62a590
Show file tree
Hide file tree
Showing 4 changed files with 686 additions and 25 deletions.
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

0 comments on commit b62a590

Please sign in to comment.