eventcore/tests/0-tcp-cancelconn.d
Sönke Ludwig b32b329d15 Fix the approach to determine connect failures.
On macOS it could happen that both, onConnect and onConnectError, were triggered, resulting in seemingly overlapping connection attempts when they really were sequential. This in turn triggered a connection error leak test in vibe-core.

Now using only the write-ready flag plus the reported socket error status to determine failed connections, guaranteeing a single call back.
2020-03-15 18:49:54 +01:00

42 lines
1.2 KiB
D

/++ dub.sdl:
name "test"
dependency "eventcore" path=".."
+/
module test;
import eventcore.core;
import eventcore.socket;
import std.socket : parseAddress;
import core.time : Duration, msecs;
ubyte[256] s_rbuf;
bool s_done;
void main()
{
// (hopefully) non-existant host, so that the connection times out
auto baddr = parseAddress("192.0.2.1", 1);
auto sock = eventDriver.sockets.connectStream(baddr, null, (sock, status) {
assert(false, "Connection callback should not have been called.");
});
assert(sock != StreamSocketFD.invalid, "Expected connection to be in progress.");
assert(eventDriver.sockets.getConnectionState(sock) == ConnectionState.connecting);
auto tm = eventDriver.timers.create();
eventDriver.timers.set(tm, 100.msecs, 0.msecs);
eventDriver.timers.wait(tm, (tm) {
assert(eventDriver.sockets.getConnectionState(sock) == ConnectionState.connecting);
eventDriver.sockets.cancelConnectStream(sock);
eventDriver.sockets.releaseRef(sock);
s_done = true;
});
ExitReason er;
do er = eventDriver.core.processEvents(Duration.max);
while (er == ExitReason.idle);
assert(er == ExitReason.outOfWaiters);
assert(s_done);
s_done = false;
}