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

add stream_socket_test stream_write_test #231

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
96 changes: 96 additions & 0 deletions test/tateyama/endpoint/stream/stream_socket_test.cpp
Original file line number Diff line number Diff line change
@@ -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 <cstring>
#include <glog/logging.h>
#include <gtest/gtest.h>
#include <iostream>
#include <memory>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <string>
#include <string_view>
#include <sys/socket.h>
#include <sys/types.h>
#include <tateyama/logging.h>
#include <unistd.h>
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<connection_socket> envelope_;
std::shared_ptr<stream_socket> ss;
};

TEST_F(stream_socket_test, SendTest) {
envelope_ = std::make_unique<MockConnectionSocket>();
std::string_view info = "abcdefgh";
ss = std::make_unique<MockStreamSocket>(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<MockConnectionSocket>();
std::string_view info = "abcdefgh";
ss = std::make_unique<MockStreamSocket>(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<MockConnectionSocket>();
std::string_view info = "abcdefgh";
ss = std::make_unique<MockStreamSocket>(socket(AF_INET, SOCK_STREAM, 0), info,
envelope_.get());
EXPECT_EQ(ss->connection_info(),info);
envelope_->close();
}

} // namespace tateyama::endpoint::stream
97 changes: 97 additions & 0 deletions test/tateyama/endpoint/stream/stream_write_test.cpp
Original file line number Diff line number Diff line change
@@ -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 <cstring>
#include <glog/logging.h>
#include <gtest/gtest.h>
#include <iostream>
#include <memory>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <string>
#include <string_view>
#include <sys/socket.h>
#include <sys/types.h>
#include <tateyama/logging.h>
#include <unistd.h>
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<connection_socket> envelope_;
std::shared_ptr<stream_socket> ss;
std::shared_ptr<stream_writer> sw;
};

TEST_F(stream_write_test, CommitTest) {
envelope_ = std::make_unique<MockConnectionSocket>();
std::string_view info = "abcdefgh";
ss = std::make_unique<MockStreamSocket>(socket(AF_INET, SOCK_STREAM, 0), info,
envelope_.get());
sw = std::make_shared<stream_writer>(ss, 1, 2);
EXPECT_EQ(sw->commit(), tateyama::status::ok);
envelope_->close();
}
TEST_F(stream_write_test, WriteTest) {
envelope_ = std::make_unique<MockConnectionSocket>();
std::string_view info = "abcdefgh";
ss = std::make_unique<MockStreamSocket>(socket(AF_INET, SOCK_STREAM, 0), info,
envelope_.get());
sw = std::make_shared<stream_writer>(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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

このあたりのテストでstatus::okが戻るのは自明なので、できればcommitをするまでバッファに書かれない、commitでバッファされていたものが書かれる、というところが確認できるといいですね。ただ、既存のコードに変更が必要になるかもしれないので最終的にどこまでテストするかは @t-horikawa さんと相談してみてください。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

buffer_がprivate member variableなので

  • buffer_ へのアクセス関数を用意
  • フレンドクラスを使用する

の2択ですが、これはテストコード以外を修正する必要があるのでいったんこのテストコードをmergeして別のcommitとしてよいですか?

@kuron99 @t-horikawa アクセス関数とフレンドクラスの場合どちらがtsurugiの流儀になりますか?指定がなければsetterを用意してbuffer_を編集して、同時にgetterを用意してbuffer_の中身の確認するテストも追加します。

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tsurugiの流儀と言えるものはない気がするのですが、私がやるとすればテスト用のアクセス関数を追加するほうですね。カプセル化を壊すのですが、コメントでテスト専用と書いて、本番コードからは呼ばないように気をつけると。
あとテストのやり方によってはgetterだけあれば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