5 #include <gtest/gtest.h>
8 #include "../resource_pool.h"
10 using namespace mabain;
14 #define MB_DIR "/var/tmp/mabain_test/"
16 class WriterLockTest : public ::testing::Test
21 virtual ~WriterLockTest() {
23 virtual void SetUp() {
24 std::string cmd = std::string("rm ") + MB_DIR + "_*";
25 if(system(cmd.c_str()) != 0) {
28 virtual void TearDown() {
29 ResourcePool::getInstance().RemoveAll();
35 TEST_F(WriterLockTest, test_lock)
37 int options = CONSTS::WriterOptions();
38 DB db(MB_DIR, options);
39 EXPECT_TRUE(db.is_open());
41 DB db1(MB_DIR, options);
42 EXPECT_TRUE(db1.Status() == MBError::WRITER_EXIST);
45 DB db2(MB_DIR, options);
46 EXPECT_TRUE(db2.is_open());
48 options = CONSTS::ReaderOptions();
49 DB db3(MB_DIR, options);
50 EXPECT_TRUE(db3.is_open());
52 DB db4(MB_DIR, options);
53 EXPECT_TRUE(db4.is_open());
56 EXPECT_TRUE(db4.is_open());
59 EXPECT_TRUE(db5.is_open());
62 // Multiple process lock test
64 TEST_F(WriterLockTest, test_lock_process)
66 #define LOCK_WRITER_SOURCE " \
67 #include <unistd.h>\n \
68 #include \"../db.h\"\n \
69 using namespace mabain;\n \
70 int main(int argc, char *argv[]) { \
71 int time_to_run = 30; \
72 const char *db_dir = \"/var/tmp/mabain_test/\"; \
77 time_to_run = atoi(argv[2]); \
79 int options = CONSTS::WriterOptions(); \
80 DB db(db_dir, options); \
82 int time_left = time_to_run; \
83 while(time_left-- > 0) { \
87 std::cerr << \"failed to open db \" << db_dir << std::endl; \
93 std::ofstream cpp_source("./lock_writer.cpp");
94 cpp_source << LOCK_WRITER_SOURCE;
96 std::string cmd = "g++ -o lock_writer lock_writer.cpp -L.. -lmabain";
98 assert(access("./lock_writer", F_OK) == 0);
100 cmd = std::string("./lock_writer ") + MB_DIR + " 60 &";
104 int options = CONSTS::WriterOptions();
105 DB db(MB_DIR, options);
106 EXPECT_TRUE(db.Status() == MBError::WRITER_EXIST);
109 system("pkill -9 lock_writer");
111 db = DB(MB_DIR, options);
112 EXPECT_TRUE(db.Status() == MBError::SUCCESS);
114 unlink("./lock_writer.cpp");
115 unlink("./lock_writer");