Skip to content

Commit 8ec7a37

Browse files
committed
test set_with_get_option
1 parent b55c1dd commit 8ec7a37

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

test/src/sw/redis++/string_cmds_test.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class StringCmdTest {
4141

4242
void _test_getset();
4343

44+
void _test_set_with_get_option();
45+
4446
void _test_mgetset();
4547

4648
RedisInstance &_redis;

test/src/sw/redis++/string_cmds_test.hpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ void StringCmdTest<RedisInstance>::run() {
3636

3737
_test_getset();
3838

39+
_test_set_with_get_option();
40+
3941
_test_mgetset();
4042
}
4143

@@ -204,6 +206,34 @@ void StringCmdTest<RedisInstance>::_test_getset() {
204206
REDIS_ASSERT(_redis.pttl(key) <= pttl.count(), "failed to test psetex");
205207
}
206208

209+
template <typename RedisInstance>
210+
void StringCmdTest<RedisInstance>::_test_set_with_get_option() {
211+
auto key = test_key("set_with_get_option");
212+
auto non_exist_key = test_key("non-existent");
213+
214+
KeyDeleter<RedisInstance> deleter(_redis, {key, non_exist_key});
215+
216+
std::string val("value");
217+
REDIS_ASSERT(!_redis.set_with_get_option(key, val), "failed to test set_with_get_option");
218+
219+
auto v = _redis.set_with_get_option(key, val + val, std::chrono::milliseconds(0), UpdateType::NOT_EXIST);
220+
REDIS_ASSERT(v && *v == val, "failed to test set_with_get_option (GET)");
221+
v = _redis.get(key);
222+
REDIS_ASSERT(v && *v == val, "failed to test set_with_get_option (NOT_EXIST)");
223+
224+
auto ttl = std::chrono::seconds(10);
225+
_redis.set_with_get_option(non_exist_key, val, ttl, UpdateType::EXIST);
226+
REDIS_ASSERT(!_redis.get(non_exist_key), "failed to test set_with_get_option (EXIST)");
227+
228+
_redis.set_with_get_option(key, val, ttl);
229+
auto ttl_v = _redis.ttl(key);
230+
REDIS_ASSERT(ttl_v >= 0 && ttl_v <= ttl.count(), "failed to test set_with_get_option (TTL)");
231+
232+
_redis.set_with_get_option(key, val, true);
233+
ttl_v = _redis.ttl(key);
234+
REDIS_ASSERT(ttl_v >= 0 && ttl_v <= ttl.count(), "failed to test set_with_get_option (KEEPTTL)");
235+
}
236+
207237
template <typename RedisInstance>
208238
void StringCmdTest<RedisInstance>::_test_mgetset() {
209239
auto kvs = {std::make_pair(test_key("k1"), "v1"),

0 commit comments

Comments
 (0)