Make the test for #115 actually fail if a socket gets leaked.
This commit is contained in:
parent
568cdb112b
commit
00bf61b3b7
|
@ -10,7 +10,8 @@ import vibe.core.net;
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
foreach (_; 0 .. 20) {
|
auto initial = determineSocketCount();
|
||||||
|
|
||||||
TCPConnection conn;
|
TCPConnection conn;
|
||||||
try {
|
try {
|
||||||
conn = connectTCP("127.0.0.1", 16565);
|
conn = connectTCP("127.0.0.1", 16565);
|
||||||
|
@ -18,5 +19,27 @@ void main()
|
||||||
conn.close();
|
conn.close();
|
||||||
assert(false, "Didn't expect TCP connection on port 16565 to succeed");
|
assert(false, "Didn't expect TCP connection on port 16565 to succeed");
|
||||||
} catch (Exception) { }
|
} 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue