From 3fba3ccd96b066d10401f1f9b65293d59d14082c Mon Sep 17 00:00:00 2001 From: sundb Date: Tue, 7 Mar 2023 15:06:58 +0800 Subject: Skip test for sdsRemoveFreeSpace when mem_allocator is not jemalloc (#11878) Test `trim on SET with big value` (introduced from #11817) fails under mac m1 with libc mem_allocator. The reason is that malloc(33000) will allocate 65536 bytes(>42000). This test still passes under ubuntu with libc mem_allocator. ``` *** [err]: trim on SET with big value in tests/unit/type/string.tcl Expected [r memory usage key] < 42000 (context: type source line 471 file /Users/iospack/data/redis_fork/tests/unit/type/string.tcl cmd {assert {[r memory usage key] < 42000}} proc ::test) ``` simple test under mac m1 with libc mem_allocator: ```c void *p = zmalloc(33000); printf("malloc size: %zu\n", zmalloc_size(p)); # output malloc size: 65536 ``` --- tests/modules/basics.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'tests/modules') diff --git a/tests/modules/basics.c b/tests/modules/basics.c index 579d8f629..897cb5d87 100644 --- a/tests/modules/basics.c +++ b/tests/modules/basics.c @@ -417,11 +417,22 @@ int TestTrimString(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) { size_t string_len = RedisModule_MallocSizeString(s); RedisModule_TrimStringAllocation(s); size_t len_after_trim = RedisModule_MallocSizeString(s); - if (len_after_trim < string_len) { + + /* Determine if using jemalloc memory allocator. */ + RedisModuleServerInfoData *info = RedisModule_GetServerInfo(ctx, "memory"); + const char *field = RedisModule_ServerInfoGetFieldC(info, "mem_allocator"); + int use_jemalloc = !strncmp(field, "jemalloc", 8); + + /* Jemalloc will reallocate `s` from 2k to 1k after RedisModule_TrimStringAllocation(), + * but non-jemalloc memory allocators may keep the old size. */ + if ((use_jemalloc && len_after_trim < string_len) || + (!use_jemalloc && len_after_trim <= string_len)) + { RedisModule_ReplyWithSimpleString(ctx, "OK"); } else { RedisModule_ReplyWithError(ctx, "String was not trimmed as expected."); } + RedisModule_FreeServerInfo(ctx, info); RedisModule_Free(tmp); RedisModule_FreeString(ctx,s); return REDISMODULE_OK; -- cgit v1.2.1