|
20 | 20 | * Author: wuhanqing
|
21 | 21 | */
|
22 | 22 |
|
23 |
| -#include <gtest/gtest.h> |
24 |
| -#include <gflags/gflags.h> |
25 | 23 | #include "src/client/file_instance.h"
|
26 | 24 |
|
| 25 | +#include <brpc/server.h> |
| 26 | +#include <gmock/gmock.h> |
| 27 | +#include <gtest/gtest.h> |
| 28 | + |
| 29 | +#include "test/client/mock/mock_namespace_service.h" |
| 30 | + |
27 | 31 | namespace curve {
|
28 | 32 | namespace client {
|
29 | 33 |
|
| 34 | +using ::testing::_; |
| 35 | +using ::testing::DoAll; |
| 36 | +using ::testing::Invoke; |
| 37 | +using ::testing::SaveArgPointee; |
| 38 | + |
30 | 39 | TEST(FileInstanceTest, CommonTest) {
|
31 | 40 | UserInfo userInfo{"test", "passwd"};
|
32 | 41 | std::shared_ptr<MDSClient> mdsclient = std::make_shared<MDSClient>();
|
@@ -72,5 +81,145 @@ TEST(FileInstanceTest, IoAlignmentTest) {
|
72 | 81 | ASSERT_FALSE(CheckAlign(511, 511, 512));
|
73 | 82 | }
|
74 | 83 |
|
| 84 | +constexpr size_t kMiB = 1 << 20; |
| 85 | +constexpr size_t kGiB = 1 << 30; |
| 86 | + |
| 87 | +class FileInstanceGetFileInfoTest : public ::testing::Test { |
| 88 | + protected: |
| 89 | + void SetUp() override { |
| 90 | + FileServiceOption opts; |
| 91 | + opts.metaServerOpt.mdsAddrs = {kSvrAddr}; |
| 92 | + |
| 93 | + ASSERT_EQ(0, mdsclient_->Initialize(opts.metaServerOpt)); |
| 94 | + ASSERT_EQ(0, server_.AddService(&nameService_, |
| 95 | + brpc::SERVER_DOESNT_OWN_SERVICE)); |
| 96 | + ASSERT_EQ(0, server_.Start(kSvrAddr, nullptr)); |
| 97 | + |
| 98 | + UserInfo user("test", ""); |
| 99 | + instance_.reset(FileInstance::NewInitedFileInstance( |
| 100 | + opts, mdsclient_, "/test", user, OpenFlags{}, |
| 101 | + /*readonly=*/false)); |
| 102 | + |
| 103 | + EXPECT_CALL(nameService_, OpenFile(_, _, _, _)) |
| 104 | + .WillOnce(Invoke([](::google::protobuf::RpcController* cntl, |
| 105 | + const curve::mds::OpenFileRequest*, |
| 106 | + curve::mds::OpenFileResponse* response, |
| 107 | + google::protobuf::Closure* done) { |
| 108 | + response->set_statuscode(curve::mds::StatusCode::kOK); |
| 109 | + auto* info = response->mutable_fileinfo(); |
| 110 | + info->set_id(2); |
| 111 | + info->set_parentid(1); |
| 112 | + info->set_chunksize(16ULL * kMiB); |
| 113 | + info->set_segmentsize(1ULL * kGiB); |
| 114 | + info->set_length(1ULL * kGiB); |
| 115 | + auto* session = response->mutable_protosession(); |
| 116 | + session->set_sessionid("1"); |
| 117 | + session->set_leasetime(60); |
| 118 | + session->set_createtime(0); |
| 119 | + session->set_sessionstatus( |
| 120 | + curve::mds::SessionStatus::kSessionOK); |
| 121 | + done->Run(); |
| 122 | + })); |
| 123 | + |
| 124 | + EXPECT_CALL(nameService_, CloseFile(_, _, _, _)) |
| 125 | + .WillOnce(Invoke([](::google::protobuf::RpcController* cntl, |
| 126 | + const curve::mds::CloseFileRequest*, |
| 127 | + curve::mds::CloseFileResponse* response, |
| 128 | + google::protobuf::Closure* done) { |
| 129 | + response->set_statuscode(curve::mds::StatusCode::kOK); |
| 130 | + done->Run(); |
| 131 | + })); |
| 132 | + |
| 133 | + EXPECT_CALL(nameService_, RefreshSession(_, _, _, _)) |
| 134 | + .WillRepeatedly( |
| 135 | + Invoke([](::google::protobuf::RpcController* cntl, |
| 136 | + const curve::mds::ReFreshSessionRequest*, |
| 137 | + curve::mds::ReFreshSessionResponse* response, |
| 138 | + google::protobuf::Closure* done) { |
| 139 | + response->set_statuscode(curve::mds::StatusCode::kOK); |
| 140 | + response->set_sessionid(""); |
| 141 | + done->Run(); |
| 142 | + })); |
| 143 | + |
| 144 | + ASSERT_EQ(0, instance_->Open()); |
| 145 | + } |
| 146 | + |
| 147 | + void TearDown() override { |
| 148 | + ASSERT_EQ(0, instance_->Close()); |
| 149 | + |
| 150 | + server_.Stop(0); |
| 151 | + server_.Join(); |
| 152 | + } |
| 153 | + |
| 154 | + protected: |
| 155 | + const char* kSvrAddr = "127.0.0.1:9610"; |
| 156 | + |
| 157 | + std::shared_ptr<MDSClient> mdsclient_ = std::make_shared<MDSClient>(); |
| 158 | + std::unique_ptr<FileInstance> instance_; |
| 159 | + |
| 160 | + brpc::Server server_; |
| 161 | + curve::mds::MockNameService nameService_; |
| 162 | +}; |
| 163 | + |
| 164 | +TEST_F(FileInstanceGetFileInfoTest, TestGetInfoFromCache) { |
| 165 | + auto info = instance_->GetCurrentFileInfo(); |
| 166 | + ASSERT_EQ(1ULL * kGiB, info.length); |
| 167 | +} |
| 168 | + |
| 169 | +TEST_F(FileInstanceGetFileInfoTest, TestForceGetInfo) { |
| 170 | + // get info from mds success, return cached fileinfo |
| 171 | + { |
| 172 | + EXPECT_CALL(nameService_, GetFileInfo(_, _, _, _)) |
| 173 | + .WillOnce(Invoke([](::google::protobuf::RpcController* cntl, |
| 174 | + const curve::mds::GetFileInfoRequest*, |
| 175 | + curve::mds::GetFileInfoResponse* response, |
| 176 | + google::protobuf::Closure* done) { |
| 177 | + response->set_statuscode( |
| 178 | + curve::mds::StatusCode::kFileNotExists); |
| 179 | + done->Run(); |
| 180 | + })); |
| 181 | + |
| 182 | + auto info = instance_->GetCurrentFileInfo(/*force=*/true); |
| 183 | + ASSERT_EQ(1ULL * kGiB, info.length); |
| 184 | + } |
| 185 | + |
| 186 | + // get info from mds success, return new fileinfo |
| 187 | + { |
| 188 | + EXPECT_CALL(nameService_, GetFileInfo(_, _, _, _)) |
| 189 | + .WillOnce(Invoke([](::google::protobuf::RpcController* cntl, |
| 190 | + const curve::mds::GetFileInfoRequest*, |
| 191 | + curve::mds::GetFileInfoResponse* response, |
| 192 | + google::protobuf::Closure* done) { |
| 193 | + response->set_statuscode(curve::mds::StatusCode::kOK); |
| 194 | + auto* info = response->mutable_fileinfo(); |
| 195 | + info->set_id(2); |
| 196 | + info->set_parentid(1); |
| 197 | + info->set_chunksize(16ULL * kMiB); |
| 198 | + info->set_segmentsize(1ULL * kGiB); |
| 199 | + info->set_length(2ULL * kGiB); // increase file size to 2GiB |
| 200 | + done->Run(); |
| 201 | + })); |
| 202 | + |
| 203 | + auto info = instance_->GetCurrentFileInfo(/*force=*/true); |
| 204 | + ASSERT_EQ(2ULL * kGiB, info.length); |
| 205 | + } |
| 206 | + |
| 207 | + // get info from mds success, return cached fileinfo |
| 208 | + { |
| 209 | + EXPECT_CALL(nameService_, GetFileInfo(_, _, _, _)) |
| 210 | + .WillOnce(Invoke([](::google::protobuf::RpcController* cntl, |
| 211 | + const curve::mds::GetFileInfoRequest*, |
| 212 | + curve::mds::GetFileInfoResponse* response, |
| 213 | + google::protobuf::Closure* done) { |
| 214 | + response->set_statuscode( |
| 215 | + curve::mds::StatusCode::kFileNotExists); |
| 216 | + done->Run(); |
| 217 | + })); |
| 218 | + |
| 219 | + auto info = instance_->GetCurrentFileInfo(/*force=*/true); |
| 220 | + ASSERT_EQ(2ULL * kGiB, info.length); |
| 221 | + } |
| 222 | +} |
| 223 | + |
75 | 224 | } // namespace client
|
76 | 225 | } // namespace curve
|
0 commit comments