bitcoinleveldb_env/env_test.rs
1crate::ix!();
2
3//-------------------------------------------[.cpp/bitcoin/src/leveldb/util/env_test.cc]
4
5const DELAY_MICROS: i32 = 100000;
6
7struct EnvTest {
8 env: Rc<RefCell<dyn Env>>,
9}
10
11impl Default for EnvTest {
12
13 fn default() -> Self {
14 todo!();
15 /*
16
17
18 : env_(Env::Default())
19 */
20 }
21}
22
23#[test] fn env_test_read_write() {
24 todo!();
25 /*
26
27 Random rnd(test::RandomSeed());
28
29 // Get file to use for testing.
30 std::string test_dir;
31 ASSERT_OK(env_->GetTestDirectory(&test_dir));
32 std::string test_file_name = test_dir + "/open_on_read.txt";
33 WritableFile* writable_file;
34 ASSERT_OK(env_->NewWritableFile(test_file_name, &writable_file));
35
36 // Fill a file with data generated via a sequence of randomly sized writes.
37 static const size_t kDataSize = 10 * 1048576;
38 std::string data;
39 while (data.size() < kDataSize) {
40 int len = rnd.Skewed(18); // Up to 2^18 - 1, but typically much smaller
41 std::string r;
42 test::RandomString(&rnd, len, &r);
43 ASSERT_OK(writable_file->Append(r));
44 data += r;
45 if (rnd.OneIn(10)) {
46 ASSERT_OK(writable_file->Flush());
47 }
48 }
49 ASSERT_OK(writable_file->Sync());
50 ASSERT_OK(writable_file->Close());
51 delete writable_file;
52
53 // Read all data using a sequence of randomly sized reads.
54 SequentialFile* sequential_file;
55 ASSERT_OK(env_->NewSequentialFile(test_file_name, &sequential_file));
56 std::string read_result;
57 std::string scratch;
58 while (read_result.size() < data.size()) {
59 int len = std::min<int>(rnd.Skewed(18), data.size() - read_result.size());
60 scratch.resize(std::max(len, 1)); // at least 1 so &scratch[0] is legal
61 Slice read;
62 ASSERT_OK(sequential_file->Read(len, &read, &scratch[0]));
63 if (len > 0) {
64 ASSERT_GT(read.size(), 0);
65 }
66 ASSERT_LE(read.size(), len);
67 read_result.append(read.data(), read.size());
68 }
69 ASSERT_EQ(read_result, data);
70 delete sequential_file;
71
72 */
73}
74
75#[test] fn env_test_run_immediately() {
76 todo!();
77 /*
78
79 struct RunState {
80 Mutex mu;
81 CondVar cvar{&mu};
82 bool called = false;
83
84 static c_void Run(c_void* arg) {
85 RunState* state = reinterpret_cast<RunState*>(arg);
86 MutexLock l(&state->mu);
87 ASSERT_EQ(state->called, false);
88 state->called = true;
89 state->cvar.Signal();
90 }
91 };
92
93 RunState state;
94 env_->Schedule(&RunState::Run, &state);
95
96 MutexLock l(&state.mu);
97 while (!state.called) {
98 state.cvar.Wait();
99 }
100
101 */
102}
103
104#[test] fn env_test_run_many() {
105 todo!();
106 /*
107
108 struct RunState {
109 Mutex mu;
110 CondVar cvar{&mu};
111 int last_id = 0;
112 };
113
114 struct Callback {
115 RunState* state_; // Pointer to shared state.
116 const int id_; // Order# for the execution of this callback.
117
118 Callback(RunState* s, int id) : state_(s), id_(id) {}
119
120 static c_void Run(c_void* arg) {
121 Callback* callback = reinterpret_cast<Callback*>(arg);
122 RunState* state = callback->state_;
123
124 MutexLock l(&state->mu);
125 ASSERT_EQ(state->last_id, callback->id_ - 1);
126 state->last_id = callback->id_;
127 state->cvar.Signal();
128 }
129 };
130
131 RunState state;
132 Callback callback1(&state, 1);
133 Callback callback2(&state, 2);
134 Callback callback3(&state, 3);
135 Callback callback4(&state, 4);
136 env_->Schedule(&Callback::Run, &callback1);
137 env_->Schedule(&Callback::Run, &callback2);
138 env_->Schedule(&Callback::Run, &callback3);
139 env_->Schedule(&Callback::Run, &callback4);
140
141 MutexLock l(&state.mu);
142 while (state.last_id != 4) {
143 state.cvar.Wait();
144 }
145
146 */
147}
148
149///--------------------
150struct State {
151 mu: Mutex<state::Inner>,
152 cvar: Condvar, //{&mu};
153}
154
155mod state {
156 pub struct Inner {
157 val: i32,
158 num_running: i32,
159 }
160}
161
162impl State {
163
164 pub fn new(
165 val: i32,
166 num_running: i32) -> Self {
167
168 todo!();
169 /*
170 : val(val),
171 : num_running(num_running),
172
173
174 */
175 }
176}
177
178fn thread_body(arg: *mut c_void) {
179
180 todo!();
181 /*
182 State* s = reinterpret_cast<State*>(arg);
183 s->mu.Lock();
184 s->val += 1;
185 s->num_running -= 1;
186 s->cvar.Signal();
187 s->mu.Unlock();
188 */
189}
190
191#[test] fn env_test_start_thread() {
192 todo!();
193 /*
194
195 State state(0, 3);
196 for (int i = 0; i < 3; i++) {
197 env_->StartThread(&ThreadBody, &state);
198 }
199
200 MutexLock l(&state.mu);
201 while (state.num_running != 0) {
202 state.cvar.Wait();
203 }
204 ASSERT_EQ(state.val, 3);
205
206 */
207}
208
209#[test] fn env_test_open_non_existent_file() {
210 todo!();
211 /*
212
213 // Write some test data to a single file that will be opened |n| times.
214 std::string test_dir;
215 ASSERT_OK(env_->GetTestDirectory(&test_dir));
216
217 std::string non_existent_file = test_dir + "/non_existent_file";
218 ASSERT_TRUE(!env_->FileExists(non_existent_file));
219
220 RandomAccessFile* random_access_file;
221 crate::Status status =
222 env_->NewRandomAccessFile(non_existent_file, &random_access_file);
223 ASSERT_TRUE(status.IsNotFound());
224
225 SequentialFile* sequential_file;
226 status = env_->NewSequentialFile(non_existent_file, &sequential_file);
227 ASSERT_TRUE(status.IsNotFound());
228
229 */
230}
231
232#[test] fn env_test_reopen_writable_file() {
233 todo!();
234 /*
235
236 std::string test_dir;
237 ASSERT_OK(env_->GetTestDirectory(&test_dir));
238 std::string test_file_name = test_dir + "/reopen_writable_file.txt";
239 env_->DeleteFile(test_file_name);
240
241 WritableFile* writable_file;
242 ASSERT_OK(env_->NewWritableFile(test_file_name, &writable_file));
243 std::string data("hello world!");
244 ASSERT_OK(writable_file->Append(data));
245 ASSERT_OK(writable_file->Close());
246 delete writable_file;
247
248 ASSERT_OK(env_->NewWritableFile(test_file_name, &writable_file));
249 data = "42";
250 ASSERT_OK(writable_file->Append(data));
251 ASSERT_OK(writable_file->Close());
252 delete writable_file;
253
254 ASSERT_OK(ReadFileToString(env_, test_file_name, &data));
255 ASSERT_EQ(std::string("42"), data);
256 env_->DeleteFile(test_file_name);
257
258 */
259}
260
261#[test] fn env_test_reopen_appendable_file() {
262 todo!();
263 /*
264
265 std::string test_dir;
266 ASSERT_OK(env_->GetTestDirectory(&test_dir));
267 std::string test_file_name = test_dir + "/reopen_appendable_file.txt";
268 env_->DeleteFile(test_file_name);
269
270 WritableFile* appendable_file;
271 ASSERT_OK(env_->NewAppendableFile(test_file_name, &appendable_file));
272 std::string data("hello world!");
273 ASSERT_OK(appendable_file->Append(data));
274 ASSERT_OK(appendable_file->Close());
275 delete appendable_file;
276
277 ASSERT_OK(env_->NewAppendableFile(test_file_name, &appendable_file));
278 data = "42";
279 ASSERT_OK(appendable_file->Append(data));
280 ASSERT_OK(appendable_file->Close());
281 delete appendable_file;
282
283 ASSERT_OK(ReadFileToString(env_, test_file_name, &data));
284 ASSERT_EQ(std::string("hello world!42"), data);
285 env_->DeleteFile(test_file_name);
286
287 */
288}
289
290fn testenv_test_main (
291 argc: i32,
292 argv: *mut *mut u8) -> i32 {
293
294 todo!();
295 /*
296 return leveldb::test::RunAllTests();
297 */
298}