This repository has been archived by the owner on Jun 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
rpc: minor refactoring on message_reader #252
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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
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,99 @@ | ||
// Copyright (c) 2019, Xiaomi, Inc. All rights reserved. | ||
// This source code is licensed under the Apache License Version 2.0, which | ||
// can be found in the LICENSE file in the root directory of this source tree. | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include <dsn/tool-api/message_parser.h> | ||
|
||
namespace dsn { | ||
|
||
class message_reader_test : public testing::Test | ||
{ | ||
public: | ||
void test_init() | ||
{ | ||
message_reader reader(4096); | ||
ASSERT_EQ(reader._buffer_block_size, 4096); | ||
ASSERT_EQ(reader._buffer_occupied, 0); | ||
ASSERT_EQ(reader._buffer.length(), 0); | ||
} | ||
|
||
void test_read_buffer() | ||
{ | ||
message_reader reader(4096); | ||
|
||
const char *p1 = reader.read_buffer_ptr(10); | ||
ASSERT_EQ(reader._buffer_occupied, 0); | ||
ASSERT_EQ(reader._buffer.length(), 4096); | ||
reader.mark_read(10); | ||
ASSERT_EQ(reader._buffer_occupied, 10); | ||
|
||
const char *p2 = reader.read_buffer_ptr(10); | ||
ASSERT_EQ(reader._buffer_occupied, 10); | ||
ASSERT_EQ(reader._buffer.length(), 4096); | ||
reader.mark_read(10); | ||
ASSERT_EQ(reader._buffer_occupied, 20); | ||
ASSERT_EQ(p2 - p1, 10); // p1, p2 reside on the same allocated memory buffer. | ||
|
||
reader.read_buffer_ptr(4076); | ||
ASSERT_EQ(reader._buffer_occupied, 20); | ||
ASSERT_EQ(reader._buffer.length(), 4096); | ||
reader.mark_read(4076); | ||
ASSERT_EQ(reader._buffer_occupied, 4096); | ||
|
||
// buffer capacity extends | ||
p1 = reader.read_buffer_ptr(1); | ||
ASSERT_EQ(reader._buffer_occupied, 4096); | ||
ASSERT_EQ(reader._buffer.length(), 4097); | ||
reader.mark_read(1); | ||
ASSERT_EQ(reader._buffer_occupied, 4097); | ||
|
||
// if buffer is not consumed in time, | ||
// each read will cause one data copy | ||
p2 = reader.read_buffer_ptr(3); | ||
reader.mark_read(3); | ||
ASSERT_EQ(reader._buffer.length(), 4100); | ||
ASSERT_EQ(reader._buffer_occupied, 4100); | ||
ASSERT_NE(p2 - p1, 3); | ||
} | ||
|
||
void test_read_data() | ||
{ | ||
message_reader reader(4096); | ||
|
||
std::string data = std::string("THFT") + std::string(44, '\0'); // 48 bytes | ||
data[7] = data[9] = '\1'; | ||
|
||
char *buf = reader.read_buffer_ptr(data.length()); | ||
memcpy(buf, data.data(), data.size()); | ||
reader.mark_read(data.length()); | ||
ASSERT_EQ(reader.buffer().size(), data.length()); | ||
ASSERT_EQ(reader.buffer().to_string(), data); | ||
} | ||
|
||
void test_consume_buffer() | ||
{ | ||
message_reader reader(5000); | ||
|
||
reader.read_buffer_ptr(1000); | ||
reader.mark_read(1000); | ||
ASSERT_EQ(reader._buffer_occupied, 1000); | ||
ASSERT_EQ(reader._buffer.length(), 5000); | ||
ASSERT_EQ(reader.buffer().size(), 1000); | ||
|
||
reader.consume_buffer(500); | ||
ASSERT_EQ(reader._buffer.length(), 4500); | ||
ASSERT_EQ(reader._buffer_occupied, 500); | ||
} | ||
}; | ||
|
||
TEST_F(message_reader_test, init) { test_init(); } | ||
|
||
TEST_F(message_reader_test, read_buffer) { test_read_buffer(); } | ||
|
||
TEST_F(message_reader_test, read_data) { test_read_data(); } | ||
|
||
TEST_F(message_reader_test, consume_buffer) { test_consume_buffer(); } | ||
|
||
} // namespace dsn |
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.
这个函数什么时候用啊?
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.
https://github.com/XiaoMi/rdsn/blob/master/src/core/tools/common/dsn_message_parser.cpp#L80
你看这行代码,reader 读完数据之后把读完的 buffer 释放掉,怎么释放的?
这段代码就是 repeat your code 的典型,consume_buffer 这个函数就是做的这个事情,后面重构可以把上面的代码用 consume_buffer 代替
我这边后面的 PR 会用到这个函数。
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.
嗯,好的,blob的range函数支持传negative的参数(https://github.com/XiaoMi/rdsn/blob/3989922cd3a99c7ce357af8de2d504bd6a5dd1af/include/dsn/utility/blob.h#L115)
不知道有没有更通用的写法,另外加下注释吧
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.
注释已加