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.
5 #include "leveldb/env.h"
8 #include "util/testharness.h"
9 #include "util/env_posix_test_helper.h"
13 static const int kDelayMicros
= 100000;
14 static const int kReadOnlyFileLimit
= 4;
15 static const int kMMapLimit
= 4;
20 EnvPosixTest() : env_(Env::Default()) { }
22 static void SetFileLimits(int read_only_file_limit
, int mmap_limit
) {
23 EnvPosixTestHelper::SetReadOnlyFDLimit(read_only_file_limit
);
24 EnvPosixTestHelper::SetReadOnlyMMapLimit(mmap_limit
);
28 TEST(EnvPosixTest
, TestOpenOnRead
) {
29 // Write some test data to a single file that will be opened |n| times.
31 ASSERT_OK(env_
->GetTestDirectory(&test_dir
));
32 std::string test_file
= test_dir
+ "/open_on_read.txt";
34 FILE* f
= fopen(test_file
.c_str(), "w");
35 ASSERT_TRUE(f
!= NULL
);
36 const char kFileData
[] = "abcdefghijklmnopqrstuvwxyz";
40 // Open test file some number above the sum of the two limits to force
41 // open-on-read behavior of POSIX Env leveldb::RandomAccessFile.
42 const int kNumFiles
= kReadOnlyFileLimit
+ kMMapLimit
+ 5;
43 leveldb::RandomAccessFile
* files
[kNumFiles
] = {0};
44 for (int i
= 0; i
< kNumFiles
; i
++) {
45 ASSERT_OK(env_
->NewRandomAccessFile(test_file
, &files
[i
]));
49 for (int i
= 0; i
< kNumFiles
; i
++) {
50 ASSERT_OK(files
[i
]->Read(i
, 1, &read_result
, &scratch
));
51 ASSERT_EQ(kFileData
[i
], read_result
[0]);
53 for (int i
= 0; i
< kNumFiles
; i
++) {
56 ASSERT_OK(env_
->DeleteFile(test_file
));
59 } // namespace leveldb
61 int main(int argc
, char** argv
) {
62 // All tests currently run with the same read-only file limits.
63 leveldb::EnvPosixTest::SetFileLimits(leveldb::kReadOnlyFileLimit
,
65 return leveldb::test::RunAllTests();