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"
12 static const int kDelayMicros
= 100000;
13 static const int kReadOnlyFileLimit
= 4;
14 static const int kMMapLimit
= 4;
23 EnvTest() : env_(Env::Default()) { }
26 static void SetBool(void* ptr
) {
27 reinterpret_cast<port::AtomicPointer
*>(ptr
)->NoBarrier_Store(ptr
);
30 TEST(EnvTest
, RunImmediately
) {
31 port::AtomicPointer
called (NULL
);
32 env_
->Schedule(&SetBool
, &called
);
33 env_
->SleepForMicroseconds(kDelayMicros
);
34 ASSERT_TRUE(called
.NoBarrier_Load() != NULL
);
37 TEST(EnvTest
, RunMany
) {
38 port::AtomicPointer
last_id (NULL
);
41 port::AtomicPointer
* last_id_ptr
; // Pointer to shared slot
42 uintptr_t id
; // Order# for the execution of this callback
44 CB(port::AtomicPointer
* p
, int i
) : last_id_ptr(p
), id(i
) { }
46 static void Run(void* v
) {
47 CB
* cb
= reinterpret_cast<CB
*>(v
);
48 void* cur
= cb
->last_id_ptr
->NoBarrier_Load();
49 ASSERT_EQ(cb
->id
-1, reinterpret_cast<uintptr_t>(cur
));
50 cb
->last_id_ptr
->Release_Store(reinterpret_cast<void*>(cb
->id
));
54 // Schedule in different order than start time
59 env_
->Schedule(&CB::Run
, &cb1
);
60 env_
->Schedule(&CB::Run
, &cb2
);
61 env_
->Schedule(&CB::Run
, &cb3
);
62 env_
->Schedule(&CB::Run
, &cb4
);
64 env_
->SleepForMicroseconds(kDelayMicros
);
65 void* cur
= last_id
.Acquire_Load();
66 ASSERT_EQ(4, reinterpret_cast<uintptr_t>(cur
));
75 static void ThreadBody(void* arg
) {
76 State
* s
= reinterpret_cast<State
*>(arg
);
83 TEST(EnvTest
, StartThread
) {
86 state
.num_running
= 3;
87 for (int i
= 0; i
< 3; i
++) {
88 env_
->StartThread(&ThreadBody
, &state
);
92 int num
= state
.num_running
;
97 env_
->SleepForMicroseconds(kDelayMicros
);
99 ASSERT_EQ(state
.val
, 3);
102 } // namespace leveldb
104 int main(int argc
, char** argv
) {
105 return leveldb::test::RunAllTests();