From 00bf61b3b7fcf9250e0053486a0d2611a9890c87 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Sat, 19 Jan 2019 15:26:18 +0100 Subject: [PATCH] Make the test for #115 actually fail if a socket gets leaked. --- tests/issue-115-connect-fail-leak.d | 41 ++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/tests/issue-115-connect-fail-leak.d b/tests/issue-115-connect-fail-leak.d index 5a6577c..196f9db 100644 --- a/tests/issue-115-connect-fail-leak.d +++ b/tests/issue-115-connect-fail-leak.d @@ -10,13 +10,36 @@ import vibe.core.net; void main() { - foreach (_; 0 .. 20) { - TCPConnection conn; - try { - conn = connectTCP("127.0.0.1", 16565); - logError("Connection: %s", conn); - conn.close(); - assert(false, "Didn't expect TCP connection on port 16565 to succeed"); - } catch (Exception) { } + auto initial = determineSocketCount(); + + TCPConnection conn; + try { + conn = connectTCP("127.0.0.1", 16565); + logError("Connection: %s", conn); + conn.close(); + assert(false, "Didn't expect TCP connection on port 16565 to succeed"); + } catch (Exception) { } + + assert(determineSocketCount() == initial, "Sockets leaked!"); +} + +size_t determineSocketCount() +{ + import std.algorithm.searching : count; + import std.range : iota; + + version (Posix) { + import core.sys.posix.sys.resource : getrlimit, rlimit, RLIMIT_NOFILE; + import core.sys.posix.fcntl : fcntl, F_GETFD; + + rlimit rl; + enforce(getrlimit(RLIMIT_NOFILE, &rl) == 0); + return iota(rl.rlim_cur).count(fd => fcntl(fd, F_GETFD) != -1); + } else { + import core.sys.windows.winsock2 : getsockopt, SOL_SOCKET, SO_TYPE; + + int st; + int stlen = st.sizeof; + return iota(65536).count!(s => getsockopt(s, SOL_SOCKET, SO_TYPE, cast(void*)&st, &stlen) == 0); } -} \ No newline at end of file +}