From 8cf392b3ad32704a2bc538be57cf046d5cb08aea Mon Sep 17 00:00:00 2001 From: Yoshiaki Nishimura Date: Thu, 22 Aug 2024 19:10:23 +0900 Subject: [PATCH] add stream_socket_test stream_write_test --- .../endpoint/stream/stream_socket_test.cpp | 96 ++++++++++++++++++ .../endpoint/stream/stream_write_test.cpp | 97 +++++++++++++++++++ 2 files changed, 193 insertions(+) create mode 100644 test/tateyama/endpoint/stream/stream_socket_test.cpp create mode 100644 test/tateyama/endpoint/stream/stream_write_test.cpp diff --git a/test/tateyama/endpoint/stream/stream_socket_test.cpp b/test/tateyama/endpoint/stream/stream_socket_test.cpp new file mode 100644 index 00000000..5e5f3a02 --- /dev/null +++ b/test/tateyama/endpoint/stream/stream_socket_test.cpp @@ -0,0 +1,96 @@ +/* + * Copyright 2018-2024 Project Tsurugi. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "stream_client.h" +#include "tateyama/endpoint/stream/stream.h" +#include "tateyama/endpoint/stream/stream_response.h" +#include "tateyama/logging_helper.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +namespace tateyama::endpoint::stream { + +class MockConnectionSocket : public connection_socket { +public: + MockConnectionSocket() : connection_socket(0, 0, 0) {} +}; + +class MockStreamSocket : public stream_socket { +public: + MockStreamSocket(int socket, std::string_view info, + connection_socket *envelope) + : stream_socket(socket, info, envelope) { + LOG(INFO) << "MockStreamSocket constructor: socket=" << socket; + } +}; +class stream_socket_test : public ::testing::Test { +protected: + void SetUp() override {} + + void TearDown() override { + ss.reset(); + envelope_.reset(); + } + std::unique_ptr envelope_; + std::shared_ptr ss; +}; + +TEST_F(stream_socket_test, SendTest) { + envelope_ = std::make_unique(); + std::string_view info = "abcdefgh"; + ss = std::make_unique(socket(AF_INET, SOCK_STREAM, 0), info, + envelope_.get()); + std::string data = "abcdefghijklmnopqrstuvwxyz"; + std::uint16_t sport = 1; + EXPECT_NO_THROW(ss->send(sport, data.c_str(), false)); + EXPECT_NO_THROW(ss->send(sport, data.c_str(), true)); + EXPECT_NO_THROW(ss->send(sport, 'A', "test")); + EXPECT_NO_THROW(ss->send_result_set_hello(sport, "test")); + EXPECT_NO_THROW(ss->send_result_set_bye(sport)); + EXPECT_NO_THROW(ss->send_session_bye_ok()); + EXPECT_NO_THROW(ss->change_slot_size(32)); + envelope_->close(); +} + +TEST_F(stream_socket_test, CloseTest) { + envelope_ = std::make_unique(); + std::string_view info = "abcdefgh"; + ss = std::make_unique(socket(AF_INET, SOCK_STREAM, 0), info, + envelope_.get()); + EXPECT_NO_THROW(ss->close()); + envelope_->close(); +} + +TEST_F(stream_socket_test, ConnectionInfoTest) { + envelope_ = std::make_unique(); + std::string_view info = "abcdefgh"; + ss = std::make_unique(socket(AF_INET, SOCK_STREAM, 0), info, + envelope_.get()); + EXPECT_EQ(ss->connection_info(),info); + envelope_->close(); +} + +} // namespace tateyama::endpoint::stream diff --git a/test/tateyama/endpoint/stream/stream_write_test.cpp b/test/tateyama/endpoint/stream/stream_write_test.cpp new file mode 100644 index 00000000..e4842a13 --- /dev/null +++ b/test/tateyama/endpoint/stream/stream_write_test.cpp @@ -0,0 +1,97 @@ +/* + * Copyright 2018-2024 Project Tsurugi. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "stream_client.h" +#include "tateyama/endpoint/stream/stream.h" +#include "tateyama/endpoint/stream/stream_response.h" +#include "tateyama/logging_helper.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +namespace tateyama::endpoint::stream { + +class MockConnectionSocket : public connection_socket { +public: + /* + MockConnectionSocket(std::uint32_t port, std::size_t timeout, + std::size_t socket_limit) + : connection_socket(port, timeout, socket_limit) {} + */ +public: + MockConnectionSocket() : connection_socket(0, 0, 0) {} +}; + +class MockStreamSocket : public stream_socket { +public: + MockStreamSocket(int socket, std::string_view info, + connection_socket *envelope) + : stream_socket(socket, info, envelope) { + LOG(INFO) << "MockStreamSocket constructor: socket=" << socket; + } +}; +class stream_write_test : public ::testing::Test { +protected: + void SetUp() override {} + + void TearDown() override { + sw.reset(); + ss.reset(); + envelope_.reset(); + } + std::unique_ptr envelope_; + std::shared_ptr ss; + std::shared_ptr sw; +}; + +TEST_F(stream_write_test, CommitTest) { + envelope_ = std::make_unique(); + std::string_view info = "abcdefgh"; + ss = std::make_unique(socket(AF_INET, SOCK_STREAM, 0), info, + envelope_.get()); + sw = std::make_shared(ss, 1, 2); + EXPECT_EQ(sw->commit(), tateyama::status::ok); + envelope_->close(); +} +TEST_F(stream_write_test, WriteTest) { + envelope_ = std::make_unique(); + std::string_view info = "abcdefgh"; + ss = std::make_unique(socket(AF_INET, SOCK_STREAM, 0), info, + envelope_.get()); + sw = std::make_shared(ss, 1, 2); + std::string data = "abcdefghijklmnopqrstuvwxyz"; + std::size_t length = data.length(); + EXPECT_EQ(sw->write(data.c_str(), length), tateyama::status::ok); + EXPECT_EQ(sw->write(data.c_str(), 0), tateyama::status::ok); + EXPECT_EQ(sw->write(nullptr, 0), tateyama::status::ok); + EXPECT_EQ(sw->write(nullptr, INT_MIN), tateyama::status::ok); + EXPECT_EQ(sw->write(nullptr, INT_MAX), tateyama::status::ok); + std::uint16_t sport = 1; + ss->send(sport, data.c_str(), false); + EXPECT_NO_THROW(ss->send(sport, data.c_str(), false)); + EXPECT_NO_THROW(ss->send(sport, data.c_str(), true)); + envelope_->close(); +} + +} // namespace tateyama::endpoint::stream