From e29acba814738dedb3a9a0479371f896f4560c6a Mon Sep 17 00:00:00 2001 From: toonn Date: Tue, 9 Nov 2021 14:44:57 +0100 Subject: test: Make CApi test cross-platform The test made an assumption of being able to open 1024 - 50 files. On some platforms, like older Darwin, the default limit is only 256. To avoid the issue entirely we retrieve the current limit for the process. We decrease the OPEN_MAX limit if it's too high. On some platforms fopen returned nullptrs before reaching the limit (-50) and this doesn't happen if we decrease the limit to 1024. --- test/unit/test_c_api.cpp | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'test/unit/test_c_api.cpp') diff --git a/test/unit/test_c_api.cpp b/test/unit/test_c_api.cpp index d3e81089..3548293d 100644 --- a/test/unit/test_c_api.cpp +++ b/test/unit/test_c_api.cpp @@ -47,6 +47,10 @@ #include +#if !defined(_WIN32) +#include +#endif + #ifndef __MINGW32__ #include #endif @@ -6104,9 +6108,18 @@ TEST_F(CApi, open_plenty_of_contexts) { // database std::vector dummyFilePointers; std::vector ctxts; - // 1024 is the number of file descriptors that can be opened simultaneously - // by a Linux process (by default) - for (int i = 0; i < 1024 - 50; i++) { + // The number of file descriptors that can be opened simultaneously by a + // process varies across platforms so we make use of getrlimit(2) to + // retrieve it. + struct rlimit open_max; + getrlimit(RLIMIT_NOFILE, &open_max); + // On some platforms fopen returned nullptrs before reaching limit - 50, we + // can avoid this by capping the limit to 1024. + if (open_max.rlim_cur > 1024) { + open_max.rlim_cur = 1024; + setrlimit(RLIMIT_NOFILE, &open_max); + } + for (rlim_t i = 0; i < open_max.rlim_cur - 50; i++) { FILE *f = fopen("/dev/null", "rb"); ASSERT_TRUE(f != nullptr); dummyFilePointers.push_back(f); -- cgit v1.2.3