Merge pull request #123 from vibe-d/test_issue_115

Make the test for #115 actually fail if a socket gets leaked.
merged-on-behalf-of: Sönke Ludwig <s-ludwig@users.noreply.github.com>
This commit is contained in:
The Dlang Bot 2019-01-20 12:35:27 +01:00 committed by GitHub
commit 92fa6aea2b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -8,15 +8,46 @@ import vibe.core.core;
import vibe.core.log;
import vibe.core.net;
import core.time;
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) { }
// make sure that the internal driver is initialized, so that
// base resources are all allocated
sleep(1.msecs);
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.exception : enforce;
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(cast(int)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);
}
}