Skip to content

Commit 5e1e8b5

Browse files
committed
Add tests
Signed-off-by: Björn Svensson <bjorn.a.svensson@est.tech>
1 parent eac3c60 commit 5e1e8b5

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

tests/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ target_include_directories(ut_slotmap_update PRIVATE "${PROJECT_SOURCE_DIR}/src"
9797
target_link_libraries(ut_slotmap_update valkey_unittest)
9898
add_test(NAME ut_slotmap_update COMMAND "$<TARGET_FILE:ut_slotmap_update>")
9999

100+
add_executable(ut_slot_by_key ut_slot_by_key.c)
101+
target_link_libraries(ut_slot_by_key valkey_unittest)
102+
add_test(NAME ut_slot_by_key COMMAND "$<TARGET_FILE:ut_slot_by_key>")
103+
100104
# Cluster tests
101105
add_executable(ct_commands ct_commands.c test_utils.c)
102106
target_link_libraries(ct_commands valkey ${TLS_LIBRARY})

tests/ct_specific_nodes.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,16 @@ void test_async_transaction(void) {
498498
event_base_free(base);
499499
}
500500

501+
void test_get_node_by_key(valkeyClusterContext *cc) {
502+
/* A key with embedded null should use the full length for slot
503+
* calculation, not stop at the null byte. */
504+
valkeyClusterNode *node_full = valkeyClusterGetNodeByKey(cc, (char *)"ke\0y", 4);
505+
valkeyClusterNode *node_truncated = valkeyClusterGetNodeByKey(cc, (char *)"ke", 2);
506+
assert(node_full != NULL);
507+
assert(node_truncated != NULL);
508+
assert(node_full != node_truncated);
509+
}
510+
501511
int main(void) {
502512
valkeyClusterOptions options = {0};
503513
options.initial_nodes = CLUSTER_NODE;
@@ -512,6 +522,7 @@ int main(void) {
512522
test_command_to_all_nodes(cc);
513523
test_transaction(cc);
514524
test_streams(cc);
525+
test_get_node_by_key(cc);
515526

516527
// Pipeline API
517528
test_pipeline_to_single_node(cc);

tests/ut_slot_by_key.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* Unit tests for valkeyClusterGetSlotByKey. */
2+
3+
#include "cluster.h"
4+
#include "test_utils.h"
5+
6+
#include <assert.h>
7+
8+
void test_slot_by_key_basic(void) {
9+
/* A simple key should produce a consistent slot. */
10+
unsigned int slot = valkeyClusterGetSlotByKey((char *)"foo", 3);
11+
assert(slot == valkeyClusterGetSlotByKey((char *)"foo", 3));
12+
}
13+
14+
void test_slot_by_key_with_hashtag(void) {
15+
/* Keys with the same hash tag should map to the same slot. */
16+
unsigned int slot1 = valkeyClusterGetSlotByKey((char *)"{tag}key1", 9);
17+
unsigned int slot2 = valkeyClusterGetSlotByKey((char *)"{tag}key2", 9);
18+
assert(slot1 == slot2);
19+
}
20+
21+
void test_slot_by_key_binary_safe(void) {
22+
/* Key with embedded null: "ke\0y" (length 4) should produce a
23+
* different slot than "ke" (length 2, what strlen would give). */
24+
unsigned int slot_full = valkeyClusterGetSlotByKey((char *)"ke\0y", 4);
25+
unsigned int slot_truncated = valkeyClusterGetSlotByKey((char *)"ke", 2);
26+
assert(slot_full != slot_truncated);
27+
}
28+
29+
int main(void) {
30+
test_slot_by_key_basic();
31+
test_slot_by_key_with_hashtag();
32+
test_slot_by_key_binary_safe();
33+
return 0;
34+
}

0 commit comments

Comments
 (0)