Skip to content

Commit

Permalink
Merge pull request #151 from project-tsurugi/wip/i_973
Browse files Browse the repository at this point in the history
add unset session variable functionality
  • Loading branch information
t-horikawa authored Sep 10, 2024
2 parents 9717b9a + 8f9dbeb commit 2b3d155
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 6 deletions.
11 changes: 9 additions & 2 deletions src/tateyama/proto/session/request.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ syntax = "proto3";

package tateyama.proto.session.request;

option java_multiple_files = false;
option java_package = "com.tsurugidb.session.proto";
option java_outer_classname = "SessionRequest";

// the request message to session pseudo service.
message Request {
// service message version (major)
Expand Down Expand Up @@ -82,8 +86,11 @@ message SessionSetVariable {
// the target variable name, case insensitive.
string name = 2;

// the text represented value to set.
string value = 3;
// the value to set, or NOT_SET to unset the variable.
oneof value_opt {
// the text represented value to set.
string value = 3;
}
}


Expand Down
4 changes: 3 additions & 1 deletion src/tateyama/session/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,9 @@ tgctl::return_code session_swtch(std::string_view session_ref, std::string_view
auto* command = request.mutable_session_set_variable();
command->set_session_specifier(std::string(session_ref));
command->set_name(std::string(set_key));
command->set_value(std::string(set_value));
if (!set_value.empty()) {
command->set_value(std::string(set_value));
}
auto response_opt = transport->send<::tateyama::proto::session::response::SessionSetVariable>(request);
request.clear_session_set_variable();

Expand Down
2 changes: 1 addition & 1 deletion src/tateyama/session/session.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ namespace tateyama::session {
tgctl::return_code session_list();
tgctl::return_code session_show(std::string_view session_ref);
tgctl::return_code session_shutdown(std::string_view session_ref);
tgctl::return_code session_swtch(std::string_view session_ref, std::string_view set_key, std::string_view set_value);
tgctl::return_code session_swtch(std::string_view session_ref, std::string_view set_key, std::string_view set_value = "");

} // tateyama::session
7 changes: 5 additions & 2 deletions src/tateyama/tgctl/tgctl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,13 @@ int tgctl_main(const std::vector<std::string>& args) { //NOLINT(readability-func
return tateyama::session::session_shutdown(args.at(3));
}
if (args.at(2) == "set") {
if (args.size() < 6) {
std::cerr << "need to specify session-ref, set-key, and set-value" << std::endl;
if (args.size() < 5) {
std::cerr << "need to specify session-ref and set-key" << std::endl;
return tateyama::tgctl::return_code::err;
}
if (args.size() < 6) {
return tateyama::session::session_swtch(args.at(3), args.at(4));
}
return tateyama::session::session_swtch(args.at(3), args.at(4), args.at(5));
}
std::cerr << "unknown session sub command '" << args.at(2) << "'" << std::endl;
Expand Down
27 changes: 27 additions & 0 deletions test/tateyama/session/session_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,4 +304,31 @@ TEST_F(session_test, session_set) {
EXPECT_EQ("test_value", rq.value());
}

TEST_F(session_test, session_unset) {
std::string command;
FILE *fp;

{
tateyama::proto::session::response::SessionSetVariable session_set{};
auto* success = session_set.mutable_success();
server_mock_->push_response(session_set.SerializeAsString());
}

command = "tgctl session set :123456 test_variable --conf ";
command += helper_->conf_file_path();
std::cout << command << std::endl;
if((fp = popen(command.c_str(), "r")) == nullptr){
std::cerr << "cannot tgctl session set" << std::endl;
}
auto result = read_pipe(fp);
std::cout << result << std::flush;

EXPECT_EQ(tateyama::framework::service_id_session, server_mock_->component_id());
tateyama::proto::session::request::SessionSetVariable rq{};
server_mock_->request_message(rq);
EXPECT_EQ(":123456", rq.session_specifier());
EXPECT_EQ("test_variable", rq.name());
EXPECT_EQ(tateyama::proto::session::request::SessionSetVariable::ValueOptCase::VALUE_OPT_NOT_SET, rq.value_opt_case());
}

} // namespace tateyama::session

0 comments on commit 2b3d155

Please sign in to comment.