#include "gtest/gtest.h"
|
|
|
|
#include "leveldb/env.h"
|
|
#include "leveldb/db.h"
|
|
|
|
|
|
using namespace leveldb;
|
|
|
|
constexpr int value_size = 2048;
|
|
constexpr int data_size = 128 << 20;
|
|
constexpr int key_num = 50000;
|
|
|
|
Status OpenDB(std::string dbName, DB **db) {
|
|
Options options;
|
|
options.create_if_missing = true;
|
|
return DB::Open(options, dbName, db);
|
|
}
|
|
|
|
void InsertData(DB *db, uint64_t ttl/* second */) {
|
|
WriteOptions writeOptions;
|
|
|
|
for (int i = 0; i < key_num; i++) {
|
|
int key_ = i;
|
|
std::string key = std::to_string(key_);
|
|
std::string value(value_size, 'a');
|
|
db->Put(writeOptions, key, value, ttl);
|
|
}
|
|
}
|
|
|
|
TEST(TestTTL, ReadTTL) {
|
|
DB *db;
|
|
if(OpenDB("test_garbage_db", &db).ok() == false) {
|
|
std::cerr << "open db failed" << std::endl;
|
|
abort();
|
|
}
|
|
|
|
uint64_t ttl = 10;
|
|
|
|
InsertData(db, ttl);
|
|
|
|
ReadOptions readOptions;
|
|
Status status;
|
|
srand(static_cast<unsigned int>(time(0)));
|
|
for (int i = 0; i < key_num; i++) {
|
|
int key_ = i;
|
|
std::string key = std::to_string(key_);
|
|
std::string value;
|
|
status = db->Get(readOptions, key, &value);
|
|
ASSERT_TRUE(status.ok());
|
|
}
|
|
|
|
Env::Default()->SleepForMicroseconds((ttl+5) * 1000000);
|
|
|
|
InsertData(db, ttl);
|
|
|
|
for (int i = 0; i < key_num; i++) {
|
|
int key_ = i;
|
|
std::string key = std::to_string(key_);
|
|
std::string value;
|
|
status = db->Get(readOptions, key, &value);
|
|
if(status.ok()){
|
|
std::cout<<"!!";
|
|
}
|
|
ASSERT_TRUE(status.ok());
|
|
}
|
|
}
|
|
|
|
|
|
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();
|
|
}
|