aboutsummaryrefslogtreecommitdiff
path: root/examples/redis-unstable/tests/unit/moduleapi/hash.tcl
diff options
context:
space:
mode:
Diffstat (limited to 'examples/redis-unstable/tests/unit/moduleapi/hash.tcl')
-rw-r--r--examples/redis-unstable/tests/unit/moduleapi/hash.tcl176
1 files changed, 0 insertions, 176 deletions
diff --git a/examples/redis-unstable/tests/unit/moduleapi/hash.tcl b/examples/redis-unstable/tests/unit/moduleapi/hash.tcl
deleted file mode 100644
index 8373c2d..0000000
--- a/examples/redis-unstable/tests/unit/moduleapi/hash.tcl
+++ /dev/null
@@ -1,176 +0,0 @@
1set testmodule [file normalize tests/modules/hash.so]
2
3start_server {tags {"modules external:skip"}} {
4 r module load $testmodule
5
6 test {Module hash set} {
7 r set k mystring
8 assert_error "WRONGTYPE*" {r hash.set k "" hello world}
9 r del k
10 # "" = count updates and deletes of existing fields only
11 assert_equal 0 [r hash.set k "" squirrel yes]
12 # "a" = COUNT_ALL = count inserted, modified and deleted fields
13 assert_equal 2 [r hash.set k "a" banana no sushi whynot]
14 # "n" = NX = only add fields not already existing in the hash
15 # "x" = XX = only replace the value for existing fields
16 assert_equal 0 [r hash.set k "n" squirrel hoho what nothing]
17 assert_equal 1 [r hash.set k "na" squirrel hoho something nice]
18 assert_equal 0 [r hash.set k "xa" new stuff not inserted]
19 assert_equal 1 [r hash.set k "x" squirrel ofcourse]
20 assert_equal 1 [r hash.set k "" sushi :delete: none :delete:]
21 r hgetall k
22 } {squirrel ofcourse banana no what nothing something nice}
23
24 test {Module hash - set (override) NX expired field successfully} {
25 r debug set-active-expire 0
26 r del H1 H2
27 r hash.set H1 "n" f1 v1
28 r hpexpire H1 1 FIELDS 1 f1
29 r hash.set H2 "n" f1 v1 f2 v2
30 r hpexpire H2 1 FIELDS 1 f1
31 after 5
32 assert_equal 0 [r hash.set H1 "n" f1 xx]
33 assert_equal "f1 xx" [r hgetall H1]
34 assert_equal 0 [r hash.set H2 "n" f1 yy]
35 assert_equal "f1 f2 v2 yy" [lsort [r hgetall H2]]
36 r debug set-active-expire 1
37 } {OK} {needs:debug}
38
39 test {Module hash - set XX of expired field gets failed as expected} {
40 r debug set-active-expire 0
41 r del H1 H2
42 r hash.set H1 "n" f1 v1
43 r hpexpire H1 1 FIELDS 1 f1
44 r hash.set H2 "n" f1 v1 f2 v2
45 r hpexpire H2 1 FIELDS 1 f1
46 after 5
47
48 # expected to fail on condition XX. hgetall should return empty list
49 r hash.set H1 "x" f1 xx
50 assert_equal "" [lsort [r hgetall H1]]
51 # But expired field was not lazy deleted
52 assert_equal 1 [r hlen H1]
53
54 # expected to fail on condition XX. hgetall should return list without expired f1
55 r hash.set H2 "x" f1 yy
56 assert_equal "f2 v2" [lsort [r hgetall H2]]
57 # But expired field was not lazy deleted
58 assert_equal 2 [r hlen H2]
59
60 r debug set-active-expire 1
61 } {OK} {needs:debug}
62
63 test {Module hash - test open key with REDISMODULE_OPEN_KEY_ACCESS_EXPIRED to scan expired fields} {
64 r debug set-active-expire 0
65 r del H1
66 r hash.set H1 "n" f1 v1 f2 v2 f3 v3
67 r hpexpire H1 1 FIELDS 2 f1 f2
68 after 10
69 # Scan expired fields with flag REDISMODULE_OPEN_KEY_ACCESS_EXPIRED
70 assert_equal "f1 f2 f3 v1 v2 v3" [lsort [r hash.hscan_expired H1]]
71 # Get expired field with flag REDISMODULE_OPEN_KEY_ACCESS_EXPIRED
72 assert_equal {v1} [r hash.hget_expired H1 f1]
73 # Verify we can get the TTL of the expired field as well
74 set now [expr [clock seconds]*1000]
75 assert_range [r hash.hget_expire H1 f2] [expr {$now-1000}] [expr {$now+1000}]
76 # Verify key doesn't exist on normal access without the flag
77 assert_equal 0 [r hexists H1 f1]
78 assert_equal 0 [r hexists H1 f2]
79 # Scan again expired fields with flag REDISMODULE_OPEN_KEY_ACCESS_EXPIRED
80 assert_equal "f3 v3" [lsort [r hash.hscan_expired H1]]
81 r debug set-active-expire 1
82 }
83
84 test {Module hash - test open key with REDISMODULE_OPEN_KEY_ACCESS_EXPIRED to scan expired key} {
85 r debug set-active-expire 0
86 r del H1
87 r hash.set H1 "n" f1 v1 f2 v2 f3 v3
88 r pexpire H1 5
89 after 10
90 # Scan expired fields with flag REDISMODULE_OPEN_KEY_ACCESS_EXPIRED
91 assert_equal "f1 f2 f3 v1 v2 v3" [lsort [r hash.hscan_expired H1]]
92 # Get expired field with flag REDISMODULE_OPEN_KEY_ACCESS_EXPIRED
93 assert_equal {v1} [r hash.hget_expired H1 f1]
94 # Verify key doesn't exist on normal access without the flag
95 assert_equal 0 [r exists H1]
96 r debug set-active-expire 1
97 }
98
99 test {Module hash - Read field expiration time} {
100 r del H1
101 r hash.set H1 "n" f1 v1 f2 v2 f3 v3 f4 v4
102 r hexpire H1 10 FIELDS 1 f1
103 r hexpire H1 100 FIELDS 1 f2
104 r hexpire H1 1000 FIELDS 1 f3
105
106 # Validate that the expiration times for fields f1, f2, and f3 are correct
107 set nowMsec [expr [clock seconds]*1000]
108 assert_range [r hash.hget_expire H1 f1] [expr {$nowMsec+9000}] [expr {$nowMsec+11000}]
109 assert_range [r hash.hget_expire H1 f2] [expr {$nowMsec+90000}] [expr {$nowMsec+110000}]
110 assert_range [r hash.hget_expire H1 f3] [expr {$nowMsec+900000}] [expr {$nowMsec+1100000}]
111
112 # Assert that field f4 and f5_not_exist have no expiration (should return -1)
113 assert_equal [r hash.hget_expire H1 f4] -1
114 assert_equal [r hash.hget_expire H1 f5_not_exist] -1
115
116 # Assert that variadic version of hget_expire works as well
117 assert_equal [r hash.hget_two_expire H1 f1 f2] [list [r hash.hget_expire H1 f1] [r hash.hget_expire H1 f2]]
118 }
119
120 test {Module hash - Read minimum expiration time} {
121 r del H1
122 r hash.set H1 "n" f1 v1 f2 v2 f3 v3 f4 v4
123 r hexpire H1 100 FIELDS 1 f1
124 r hexpire H1 10 FIELDS 1 f2
125 r hexpire H1 1000 FIELDS 1 f3
126
127 # Validate that the minimum expiration time is correct
128 set nowMsec [expr [clock seconds]*1000]
129 assert_range [r hash.hget_min_expire H1] [expr {$nowMsec+9000}] [expr {$nowMsec+11000}]
130 assert_equal [r hash.hget_min_expire H1] [r hash.hget_expire H1 f2]
131
132 # Assert error if key not found
133 assert_error {*key not found*} {r hash.hget_min_expire non_exist_hash}
134
135 # Assert return -1 if no expiration (=REDISMODULE_NO_EXPIRE)
136 r del H2
137 r hash.set H2 "n" f1 v1
138 assert_equal [r hash.hget_min_expire H2] -1
139 }
140
141 test {Module hash - KEYSIZES is updated as expected} {
142 proc run_cmd_verify_hist {cmd expOutput {retries 1}} {
143 proc K {} {return [string map { "db0_distrib_hashes_items" "db0_HASH" "# Keysizes" "" " " "" "\n" "" "\r" "" } [r info keysizes] ]}
144 uplevel 1 $cmd
145 wait_for_condition 50 $retries {
146 $expOutput eq [K]
147 } else { fail "Expected: \n`$expOutput`\n Actual:\n`[K]`.\nFailed after command: $cmd" }
148 }
149
150 r select 0
151 r flushall
152 # Check RM_HashSet
153 run_cmd_verify_hist {r hash.set H1 "n" f1 v1} {db0_HASH:1=1}
154 run_cmd_verify_hist {r hash.set H2 "n" f1 v1} {db0_HASH:1=2}
155 run_cmd_verify_hist {r hash.set H2 "n" f1 v1} {db0_HASH:1=2}
156 run_cmd_verify_hist {r hash.set H2 "x" f1 v1} {db0_HASH:1=2}
157 run_cmd_verify_hist {r hash.set H3 "x" f1 v1} {db0_HASH:1=2}
158 run_cmd_verify_hist {r hash.set H1 "n" f2 v2} {db0_HASH:1=1,2=1}
159 run_cmd_verify_hist {r hash.set H1 "a" f3 v3 f4 v4} {db0_HASH:1=1,4=1}
160 run_cmd_verify_hist {r del H1} {db0_HASH:1=1}
161 run_cmd_verify_hist {r del H2} {}
162
163 # Check lazy expire
164 r debug set-active-expire 0
165 run_cmd_verify_hist {r hash.set H1 "n" f1 v1} {db0_HASH:1=1}
166 run_cmd_verify_hist {r hpexpire H1 1 FIELDS 1 f1} {db0_HASH:1=1}
167 run_cmd_verify_hist {after 5} {db0_HASH:1=1}
168 run_cmd_verify_hist {r hash.hget_expired H1 f1} {db0_HASH:1=1}
169 r debug set-active-expire 1
170 run_cmd_verify_hist {after 5} {} 50
171 }
172
173 test "Unload the module - hash" {
174 assert_equal {OK} [r module unload hash]
175 }
176}