forked from danielealbano/cachegrand
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-modules-redis-command-psetex.cpp
115 lines (93 loc) · 3.65 KB
/
test-modules-redis-command-psetex.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/**
* Copyright (C) 2018-2022 Vito Castellano
* All rights reserved.
*
* This software may be modified and distributed under the terms
* of the BSD license. See the LICENSE file for details.
**/
#include <catch2/catch.hpp>
#include <cstdbool>
#include <cstring>
#include <memory>
#include <string>
#include <unistd.h>
#include <netinet/in.h>
#include "clock.h"
#include "exttypes.h"
#include "spinlock.h"
#include "data_structures/small_circular_queue/small_circular_queue.h"
#include "data_structures/double_linked_list/double_linked_list.h"
#include "data_structures/hashtable/mcmp/hashtable.h"
#include "config.h"
#include "fiber.h"
#include "worker/worker_stats.h"
#include "worker/worker_context.h"
#include "signal_handler_thread.h"
#include "storage/io/storage_io_common.h"
#include "storage/channel/storage_channel.h"
#include "storage/db/storage_db.h"
#include "program.h"
#include "test-modules-redis-command-fixture.hpp"
#pragma GCC diagnostic ignored "-Wwrite-strings"
TEST_CASE_METHOD(TestModulesRedisCommandFixture, "Redis - command - PSETEX", "[redis][command][PSETEX]") {
SECTION("Missing parameters - key, milliseconds and value") {
REQUIRE(send_recv_resp_command_text(
client_fd,
std::vector<std::string>{"PSETEX"},
"-ERR wrong number of arguments for 'PSETEX' command\r\n"));
}
SECTION("Missing parameters - value") {
REQUIRE(send_recv_resp_command_text(
client_fd,
std::vector<std::string>{"PSETEX", "a_key", "100"},
"-ERR wrong number of arguments for 'PSETEX' command\r\n"));
}
SECTION("Too many parameters - one extra parameter") {
REQUIRE(send_recv_resp_command_text(
client_fd,
std::vector<std::string>{"PSETEX", "a_key", "100", "b_value", "extra parameter"},
"-ERR syntax error\r\n"));
}
SECTION("New key - expire in 500ms") {
char *key = "a_key";
char *value = "b_value";
config_module_network_timeout.read_ms = 1000;
REQUIRE(send_recv_resp_command_text(
client_fd,
std::vector<std::string>{"PSETEX", key, "500", value},
"+OK\r\n"));
REQUIRE(send_recv_resp_command_text(
client_fd,
std::vector<std::string>{"GET", key},
"$7\r\nb_value\r\n"));
// Wait for 600 ms and try to get the value after the expiration
usleep((500 + 100) * 1000);
REQUIRE(send_recv_resp_command_text(
client_fd,
std::vector<std::string>{"GET", key},
"$-1\r\n"));
storage_db_entry_index_t *entry_index = storage_db_get_entry_index(db, key, strlen(key));
REQUIRE(entry_index == NULL);
}
SECTION("New key - expire in 1s") {
char *key = "a_key";
char *value = "b_value";
config_module_network_timeout.read_ms = 2000;
REQUIRE(send_recv_resp_command_text(
client_fd,
std::vector<std::string>{"PSETEX", key, "1000", value},
"+OK\r\n"));
REQUIRE(send_recv_resp_command_text(
client_fd,
std::vector<std::string>{"GET", key},
"$7\r\nb_value\r\n"));
// Wait for 1100 ms and try to get the value after the expiration
usleep((1000 + 100) * 1000);
REQUIRE(send_recv_resp_command_text(
client_fd,
std::vector<std::string>{"GET", key},
"$-1\r\n"));
storage_db_entry_index_t *entry_index = storage_db_get_entry_index(db, key, strlen(key));
REQUIRE(entry_index == NULL);
}
}