#include "gtest/gtest.h"
|
|
// #include "leveldb/env.h"
|
|
// #include "leveldb/db.h"
|
|
#include "fielddb/field_db.h"
|
|
#include "test/helper.cc"
|
|
#include <thread>
|
|
#include <csignal>
|
|
#include <exception>
|
|
using namespace fielddb;
|
|
|
|
TEST(TestNormalRecover, Recover) {
|
|
fielddb::DestroyDB("testdb3.1",Options());
|
|
FieldDB *db = new FieldDB();
|
|
|
|
if(OpenDB("testdb3.1", &db).ok() == false) {
|
|
std::cerr << "open db failed" << std::endl;
|
|
abort();
|
|
}
|
|
db->CreateIndexOnField("address");
|
|
db->CreateIndexOnField("age");
|
|
InsertFieldData(db);
|
|
bool allowNotFound = false;
|
|
GetFieldData(db, allowNotFound);
|
|
findKeysByCityIndex(db, true);
|
|
findKeysByAgeIndex(db, true);
|
|
|
|
delete db;
|
|
db = new FieldDB();
|
|
if(OpenDB("testdb3.1", &db).ok() == false) {
|
|
std::cerr << "open db failed" << std::endl;
|
|
abort();
|
|
}
|
|
//仍然能读到之前写入的数据和索引
|
|
GetFieldData(db, allowNotFound);
|
|
findKeysByCityIndex(db, true);
|
|
findKeysByAgeIndex(db, true);
|
|
}
|
|
|
|
TEST(TestParalPutRecover, Recover) {
|
|
//第一次运行
|
|
// fielddb::DestroyDB("testdb3.2",Options());
|
|
// FieldDB *db = new FieldDB();
|
|
|
|
// if(OpenDB("testdb3.2", &db).ok() == false) {
|
|
// std::cerr << "open db failed" << std::endl;
|
|
// abort();
|
|
// }
|
|
// db->CreateIndexOnField("address");
|
|
// db->CreateIndexOnField("age");
|
|
// shanghaiKeys.clear();
|
|
// age20Keys.clear();
|
|
// int thread_num_ = 2;
|
|
// std::vector<std::thread> threads(thread_num_);
|
|
// threads[0] = std::thread([db](){
|
|
// InsertFieldData(db);
|
|
// });
|
|
// threads[1] = std::thread([db](){
|
|
// InsertOneField(db);
|
|
// delete db;
|
|
// });
|
|
// for (auto& t : threads) {
|
|
// if (t.joinable()) {
|
|
// t.join();
|
|
// }
|
|
// }
|
|
//线程1导致了线程0错误,测试会终止(模拟数据库崩溃)
|
|
//这会导致线程0在写入的各种奇怪的时间点崩溃
|
|
//第二次运行注释掉上面的代码,运行下面的代码测试恢复
|
|
|
|
|
|
//第二次运行
|
|
FieldDB *db = new FieldDB();
|
|
if(OpenDB("testdb3.2", &db).ok() == false) {
|
|
std::cerr << "open db failed" << std::endl;
|
|
abort();
|
|
}
|
|
GetOneField(db);
|
|
checkDataInKVAndIndex(db);
|
|
}
|
|
|
|
int main(int argc, char** argv) {
|
|
// All tests currently run with the same read-only file limits.
|
|
testing::InitGoogleTest(&argc, argv);
|
|
return RUN_ALL_TESTS();
|
|
}
|