YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/rocksdb/util/memenv_test.cc
Line
Count
Source
1
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style license that can be
3
// found in the LICENSE file. See the AUTHORS file for names of contributors.
4
//
5
// The following only applies to changes made to this file as part of YugaByte development.
6
//
7
// Portions Copyright (c) YugaByte, Inc.
8
//
9
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
10
// in compliance with the License.  You may obtain a copy of the License at
11
//
12
// http://www.apache.org/licenses/LICENSE-2.0
13
//
14
// Unless required by applicable law or agreed to in writing, software distributed under the License
15
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
16
// or implied.  See the License for the specific language governing permissions and limitations
17
// under the License.
18
//
19
20
#ifndef ROCKSDB_LITE
21
22
#include <memory>
23
#include <string>
24
#include <vector>
25
26
#include "yb/rocksdb/db.h"
27
#include "yb/rocksdb/env.h"
28
#include "yb/rocksdb/util/testharness.h"
29
#include "yb/rocksdb/util/testutil.h"
30
31
#include "yb/util/test_util.h"
32
33
namespace rocksdb {
34
35
class MemEnvTest : public RocksDBTest {
36
 public:
37
  Env* env_;
38
  const EnvOptions soptions_;
39
40
  MemEnvTest()
41
5
      : env_(NewMemEnv(Env::Default())) {
42
5
  }
43
5
  ~MemEnvTest() {
44
5
    delete env_;
45
5
  }
46
};
47
48
1
TEST_F(MemEnvTest, Basics) {
49
1
  uint64_t file_size;
50
1
  unique_ptr<WritableFile> writable_file;
51
1
  std::vector<std::string> children;
52
53
1
  ASSERT_OK(env_->CreateDir("/dir"));
54
55
  // Check that the directory is empty.
56
1
  ASSERT_TRUE(env_->FileExists("/dir/non_existent").IsNotFound());
57
1
  ASSERT_TRUE(!env_->GetFileSize("/dir/non_existent", &file_size).ok());
58
1
  ASSERT_OK(env_->GetChildren("/dir", &children));
59
1
  ASSERT_EQ(0U, children.size());
60
61
  // Create a file.
62
1
  ASSERT_OK(env_->NewWritableFile("/dir/f", &writable_file, soptions_));
63
1
  writable_file.reset();
64
65
  // Check that the file exists.
66
1
  ASSERT_OK(env_->FileExists("/dir/f"));
67
1
  ASSERT_OK(env_->GetFileSize("/dir/f", &file_size));
68
1
  ASSERT_EQ(0U, file_size);
69
1
  ASSERT_OK(env_->GetChildren("/dir", &children));
70
1
  ASSERT_EQ(1U, children.size());
71
1
  ASSERT_EQ("f", children[0]);
72
73
  // Write to the file.
74
1
  ASSERT_OK(env_->NewWritableFile("/dir/f", &writable_file, soptions_));
75
1
  ASSERT_OK(writable_file->Append("abc"));
76
1
  writable_file.reset();
77
78
  // Check for expected size.
79
1
  ASSERT_OK(env_->GetFileSize("/dir/f", &file_size));
80
1
  ASSERT_EQ(3U, file_size);
81
82
  // Check that renaming works.
83
1
  ASSERT_TRUE(!env_->RenameFile("/dir/non_existent", "/dir/g").ok());
84
1
  ASSERT_OK(env_->RenameFile("/dir/f", "/dir/g"));
85
1
  ASSERT_TRUE(env_->FileExists("/dir/f").IsNotFound());
86
1
  ASSERT_OK(env_->FileExists("/dir/g"));
87
1
  ASSERT_OK(env_->GetFileSize("/dir/g", &file_size));
88
1
  ASSERT_EQ(3U, file_size);
89
90
  // Check that opening non-existent file fails.
91
1
  unique_ptr<SequentialFile> seq_file;
92
1
  unique_ptr<RandomAccessFile> rand_file;
93
1
  ASSERT_TRUE(!env_->NewSequentialFile("/dir/non_existent", &seq_file,
94
1
                                       soptions_).ok());
95
1
  ASSERT_TRUE(!seq_file);
96
1
  ASSERT_TRUE(!env_->NewRandomAccessFile("/dir/non_existent", &rand_file,
97
1
                                         soptions_).ok());
98
1
  ASSERT_TRUE(!rand_file);
99
100
  // Check that deleting works.
101
1
  ASSERT_TRUE(!env_->DeleteFile("/dir/non_existent").ok());
102
1
  ASSERT_OK(env_->DeleteFile("/dir/g"));
103
1
  ASSERT_TRUE(env_->FileExists("/dir/g").IsNotFound());
104
1
  ASSERT_OK(env_->GetChildren("/dir", &children));
105
1
  ASSERT_EQ(0U, children.size());
106
1
  ASSERT_OK(env_->DeleteDir("/dir"));
107
1
}
108
109
1
TEST_F(MemEnvTest, ReadWrite) {
110
1
  unique_ptr<WritableFile> writable_file;
111
1
  unique_ptr<SequentialFile> seq_file;
112
1
  unique_ptr<RandomAccessFile> rand_file;
113
1
  Slice result;
114
1
  uint8_t scratch[100];
115
116
1
  ASSERT_OK(env_->CreateDir("/dir"));
117
118
1
  ASSERT_OK(env_->NewWritableFile("/dir/f", &writable_file, soptions_));
119
1
  ASSERT_OK(writable_file->Append("hello "));
120
1
  ASSERT_OK(writable_file->Append("world"));
121
1
  writable_file.reset();
122
123
  // Read sequentially.
124
1
  ASSERT_OK(env_->NewSequentialFile("/dir/f", &seq_file, soptions_));
125
1
  ASSERT_OK(seq_file->Read(5, &result, scratch)); // Read "hello".
126
1
  ASSERT_EQ(0, result.compare("hello"));
127
1
  ASSERT_OK(seq_file->Skip(1));
128
1
  ASSERT_OK(seq_file->Read(1000, &result, scratch)); // Read "world".
129
1
  ASSERT_EQ(0, result.compare("world"));
130
1
  ASSERT_OK(seq_file->Read(1000, &result, scratch)); // Try reading past EOF.
131
1
  ASSERT_EQ(0U, result.size());
132
1
  ASSERT_OK(seq_file->Skip(100)); // Try to skip past end of file.
133
1
  ASSERT_OK(seq_file->Read(1000, &result, scratch));
134
1
  ASSERT_EQ(0U, result.size());
135
136
  // Random reads.
137
1
  ASSERT_OK(env_->NewRandomAccessFile("/dir/f", &rand_file, soptions_));
138
1
  ASSERT_OK(rand_file->Read(6, 5, &result, scratch)); // Read "world".
139
1
  ASSERT_EQ(0, result.compare("world"));
140
1
  ASSERT_OK(rand_file->Read(0, 5, &result, scratch)); // Read "hello".
141
1
  ASSERT_EQ(0, result.compare("hello"));
142
1
  ASSERT_OK(rand_file->Read(10, 100, &result, scratch)); // Read "d".
143
1
  ASSERT_EQ(0, result.compare("d"));
144
145
  // Too high offset.
146
1
  ASSERT_TRUE(!rand_file->Read(1000, 5, &result, scratch).ok());
147
1
}
148
149
1
TEST_F(MemEnvTest, Locks) {
150
1
  FileLock* lock;
151
152
  // These are no-ops, but we test they return success.
153
1
  ASSERT_OK(env_->LockFile("some file", &lock));
154
1
  ASSERT_OK(env_->UnlockFile(lock));
155
1
}
156
157
1
TEST_F(MemEnvTest, Misc) {
158
1
  std::string test_dir;
159
1
  ASSERT_OK(env_->GetTestDirectory(&test_dir));
160
1
  ASSERT_TRUE(!test_dir.empty());
161
162
1
  unique_ptr<WritableFile> writable_file;
163
1
  ASSERT_OK(env_->NewWritableFile("/a/b", &writable_file, soptions_));
164
165
  // These are no-ops, but we test they return success.
166
1
  ASSERT_OK(writable_file->Sync());
167
1
  ASSERT_OK(writable_file->Flush());
168
1
  ASSERT_OK(writable_file->Close());
169
1
  writable_file.reset();
170
1
}
171
172
1
TEST_F(MemEnvTest, LargeWrite) {
173
1
  const size_t kWriteSize = 300 * 1024;
174
1
  uint8_t* scratch = new uint8_t[kWriteSize * 2];
175
176
1
  std::string write_data;
177
307k
  for (size_t i = 0; i < kWriteSize; ++i) {
178
307k
    write_data.append(1, static_cast<char>(i));
179
307k
  }
180
181
1
  unique_ptr<WritableFile> writable_file;
182
1
  ASSERT_OK(env_->NewWritableFile("/dir/f", &writable_file, soptions_));
183
1
  ASSERT_OK(writable_file->Append("foo"));
184
1
  ASSERT_OK(writable_file->Append(write_data));
185
1
  writable_file.reset();
186
187
1
  unique_ptr<SequentialFile> seq_file;
188
1
  Slice result;
189
1
  ASSERT_OK(env_->NewSequentialFile("/dir/f", &seq_file, soptions_));
190
1
  ASSERT_OK(seq_file->Read(3, &result, scratch)); // Read "foo".
191
1
  ASSERT_EQ(0, result.compare("foo"));
192
193
1
  size_t read = 0;
194
1
  std::string read_data;
195
2
  while (read < kWriteSize) {
196
1
    ASSERT_OK(seq_file->Read(kWriteSize - read, &result, scratch));
197
1
    read_data.append(result.cdata(), result.size());
198
1
    read += result.size();
199
1
  }
200
1
  ASSERT_TRUE(write_data == read_data);
201
1
  delete [] scratch;
202
1
}
203
204
}  // namespace rocksdb
205
206
13.2k
int main(int argc, char** argv) {
207
13.2k
  ::testing::InitGoogleTest(&argc, argv);
208
13.2k
  return RUN_ALL_TESTS();
209
13.2k
}
210
211
#else
212
#include <stdio.h>
213
214
int main(int argc, char** argv) {
215
  fprintf(stderr, "SKIPPED as MemEnv is not supported in ROCKSDB_LITE\n");
216
  return 0;
217
}
218
219
#endif  // !ROCKSDB_LITE