-
Notifications
You must be signed in to change notification settings - Fork 1
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
YoshiakiNishimura
wants to merge
1
commit into
master
Choose a base branch
from
stream_socket_test
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 さんと相談してみてください。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
buffer_がprivate member variableなので
の2択ですが、これはテストコード以外を修正する必要があるのでいったんこのテストコードをmergeして別のcommitとしてよいですか?
@kuron99 @t-horikawa アクセス関数とフレンドクラスの場合どちらがtsurugiの流儀になりますか?指定がなければsetterを用意してbuffer_を編集して、同時にgetterを用意してbuffer_の中身の確認するテストも追加します。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tsurugiの流儀と言えるものはない気がするのですが、私がやるとすればテスト用のアクセス関数を追加するほうですね。カプセル化を壊すのですが、コメントでテスト専用と書いて、本番コードからは呼ばないように気をつけると。
あとテストのやり方によってはgetterだけあればOKかもしれません。