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;
21 EnvPosixTest() : env_(Env::Default()) { }
24 static void SetBool(void* ptr) {
25 reinterpret_cast<port::AtomicPointer*>(ptr)->NoBarrier_Store(ptr);
28 TEST(EnvPosixTest, RunImmediately) {
29 port::AtomicPointer called (NULL);
30 env_->Schedule(&SetBool, &called);
31 Env::Default()->SleepForMicroseconds(kDelayMicros);
32 ASSERT_TRUE(called.NoBarrier_Load() != NULL);
35 TEST(EnvPosixTest, RunMany) {
36 port::AtomicPointer last_id (NULL);
39 port::AtomicPointer* last_id_ptr; // Pointer to shared slot
40 uintptr_t id; // Order# for the execution of this callback
42 CB(port::AtomicPointer* p, int i) : last_id_ptr(p), id(i) { }
44 static void Run(void* v) {
45 CB* cb = reinterpret_cast<CB*>(v);
46 void* cur = cb->last_id_ptr->NoBarrier_Load();
47 ASSERT_EQ(cb->id-1, reinterpret_cast<uintptr_t>(cur));
48 cb->last_id_ptr->Release_Store(reinterpret_cast<void*>(cb->id));
52 // Schedule in different order than start time
57 env_->Schedule(&CB::Run, &cb1);
58 env_->Schedule(&CB::Run, &cb2);
59 env_->Schedule(&CB::Run, &cb3);
60 env_->Schedule(&CB::Run, &cb4);
62 Env::Default()->SleepForMicroseconds(kDelayMicros);
63 void* cur = last_id.Acquire_Load();
64 ASSERT_EQ(4, reinterpret_cast<uintptr_t>(cur));
73 static void ThreadBody(void* arg) {
74 State* s = reinterpret_cast<State*>(arg);
81 TEST(EnvPosixTest, StartThread) {
84 state.num_running = 3;
85 for (int i = 0; i < 3; i++) {
86 env_->StartThread(&ThreadBody, &state);
90 int num = state.num_running;
95 Env::Default()->SleepForMicroseconds(kDelayMicros);
97 ASSERT_EQ(state.val, 3);
100 } // namespace leveldb
102 int main(int argc, char** argv) {
103 return leveldb::test::RunAllTests();