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