Implement TCPListener.stopListening and fix the vibe.core.net.1726 test.

The test fix follows the fix in the vibe.d repository: rejectedsoftware/vibe.d#f960427e5974c176c58b516647895a2af4ea181b
This commit is contained in:
Sönke Ludwig 2017-07-18 11:55:39 +02:00
parent a70f35e846
commit d7b2173cb3
2 changed files with 34 additions and 25 deletions

View file

@ -683,6 +683,9 @@ private void loopWithTimeout(alias LoopBody, ExceptionType = Exception)(Duration
Represents a listening TCP socket. Represents a listening TCP socket.
*/ */
struct TCPListener { struct TCPListener {
// FIXME: copying may lead to dangling FDs - this somehow needs to employ reference counting without breaking
// the previous behavior of keeping the socket alive when the listener isn't stored. At the same time,
// stopListening() needs to keep working.
private { private {
StreamListenSocketFD m_socket; StreamListenSocketFD m_socket;
NetworkAddress m_bindAddress; NetworkAddress m_bindAddress;
@ -704,7 +707,10 @@ struct TCPListener {
/// Stops listening and closes the socket. /// Stops listening and closes the socket.
void stopListening() void stopListening()
{ {
assert(false); if (m_socket != StreamListenSocketFD.invalid) {
eventDriver.sockets.releaseRef(m_socket);
m_socket = StreamListenSocketFD.invalid;
}
} }
} }

View file

@ -10,12 +10,11 @@ import vibe.core.net;
import core.time : msecs; import core.time : msecs;
import vibe.core.log; import vibe.core.log;
void main() ubyte[] buf;
{
bool done = false;
auto buf = new ubyte[512*1024*1024];
listenTCP(11375,(conn) { void performTest(bool reverse)
{
auto l = listenTCP(11375, (conn) {
bool read_ex = false; bool read_ex = false;
bool write_ex = false; bool write_ex = false;
auto rt = runTask!TCPConnection((conn) { auto rt = runTask!TCPConnection((conn) {
@ -29,10 +28,10 @@ void main()
} // expected } // expected
}, conn); }, conn);
auto wt = runTask!TCPConnection((conn) { auto wt = runTask!TCPConnection((conn) {
sleep(1.msecs); // give the connection time to establish sleep(reverse ? 100.msecs : 20.msecs); // give the connection time to establish
try { try {
conn.write(buf); conn.write(buf);
assert(false, "Expected read() to throw an exception."); assert(false, "Expected write() to throw an exception.");
} catch (Exception) { } catch (Exception) {
write_ex = true; write_ex = true;
conn.close(); conn.close();
@ -44,24 +43,28 @@ void main()
wt.join(); wt.join();
assert(read_ex, "No read exception thrown"); assert(read_ex, "No read exception thrown");
assert(write_ex, "No write exception thrown"); assert(write_ex, "No write exception thrown");
done = true; logInfo("Test has finished successfully.");
exitEventLoop();
}, "127.0.0.1"); }, "127.0.0.1");
runTask({ runTask({
try { try {
auto conn = connectTCP("127.0.0.1", 11375); auto conn = connectTCP("127.0.0.1", 11375);
sleep(10.msecs); sleep(reverse ? 20.msecs : 100.msecs);
conn.close(); conn.close();
} catch (Exception e) assert(false, e.msg); } catch (Exception e) assert(false, e.msg);
sleep(50.msecs);
assert(done, "Not done");
exitEventLoop();
}); });
setTimer(2000.msecs, { runEventLoop();
assert(false, "Test has hung.");
});
runApplication(); l.stopListening();
}
void main()
{
setTimer(10000.msecs, { assert(false, "Test has hung."); });
buf = new ubyte[512*1024*1024];
performTest(false);
performTest(true);
} }