From c38d2b733e8428e0ffd5ec9263fdc5de17675813 Mon Sep 17 00:00:00 2001 From: augurier <14434658+augurier@user.noreply.gitee.com> Date: Thu, 31 Oct 2024 16:00:21 +0800 Subject: [PATCH] =?UTF-8?q?=E9=92=88=E5=AF=B9=E5=90=88=E5=B9=B6=E6=B5=8B?= =?UTF-8?q?=E5=88=B0=E6=96=B0=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/ttl_test.cc | 53 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 48 insertions(+), 5 deletions(-) diff --git a/test/ttl_test.cc b/test/ttl_test.cc index 40d38d4..eb9dc34 100644 --- a/test/ttl_test.cc +++ b/test/ttl_test.cc @@ -17,15 +17,15 @@ Status OpenDB(std::string dbName, DB **db) { return DB::Open(options, dbName, db); } -void InsertData(DB *db, uint64_t ttl/* second */) { +void InsertData(DB *db, uint64_t ttl/* second */, int vsize = 0) { WriteOptions writeOptions; int key_num = data_size / value_size; - srand(static_cast(time(0))); + srand(0); for (int i = 0; i < key_num; i++) { int key_ = rand() % key_num+1; std::string key = std::to_string(key_); - std::string value(value_size, 'a'); + std::string value(value_size + vsize, 'a'); db->Put(writeOptions, std::to_string(i+1), value, ttl); // db->Put(writeOptions, key, value, ttl); } @@ -36,7 +36,7 @@ void GetData(DB *db, int size = (1 << 30)) { int key_num = data_size / value_size; // 点查 - srand(static_cast(time(0))); + srand(0); for (int i = 0; i < 100; i++) { int key_ = rand() % key_num+1; std::string key = std::to_string(key_); @@ -60,7 +60,7 @@ TEST(TestTTL, ReadTTL) { ReadOptions readOptions; Status status; int key_num = data_size / value_size; - srand(static_cast(time(0))); + srand(0); for (int i = 0; i < 100; i++) { int key_ = rand() % key_num+1; std::string key = std::to_string(key_); @@ -83,6 +83,49 @@ TEST(TestTTL, ReadTTL) { delete db; } +TEST(TestTTL, GetEarlierData) { + DestroyDB("testdb",Options()); + DB *db; + if(OpenDB("testdb", &db).ok() == false) { + std::cerr << "open db failed" << std::endl; + abort(); + } + + uint64_t ttl1 = 3; + uint64_t ttl2 = 5; + uint64_t extra_size = 1; + + InsertData(db, ttl2); + InsertData(db, ttl1, extra_size); //后一个数据长度变化一下 + + //都没过期先找到后插的 + Env::Default()->SleepForMicroseconds(1 * 1000000); + int key_num = data_size / value_size; + ReadOptions readOptions; + Status status; + srand(0); + for (int i = 0; i < 100; i++) { + int key_ = rand() % key_num+1; + std::string key = std::to_string(key_); + std::string value; + status = db->Get(readOptions, key, &value); + ASSERT_TRUE(status.ok()); + ASSERT_EQ(value.size(), value_size + extra_size); + } + + //再找到前一次 + Env::Default()->SleepForMicroseconds(3 * 1000000); + for (int i = 0; i < 100; i++) { + int key_ = rand() % key_num+1; + std::string key = std::to_string(key_); + std::string value; + status = db->Get(readOptions, key, &value); + ASSERT_TRUE(status.ok()); + ASSERT_EQ(value.size(), value_size); + } + delete db; +} + TEST(TestTTL, CompactionTTL) { DestroyDB("testdb",Options()); DB *db;