eventcore/tests/0-tcp.d

85 lines
2.2 KiB
D
Raw Normal View History

2016-10-07 18:23:04 +00:00
/++ dub.sdl:
name "test"
dependency "eventcore" path=".."
+/
module test;
import eventcore.core;
import eventcore.socket;
import eventcore.internal.utils : print;
2016-10-07 18:23:04 +00:00
import std.socket : InternetAddress;
import core.time : Duration, msecs;
2016-10-07 18:23:04 +00:00
ubyte[256] s_rbuf;
bool s_done;
void main()
{
// watchdog timer in case of starvation/deadlocks
auto tm = eventDriver.timers.create();
eventDriver.timers.set(tm, 10000.msecs, 0.msecs);
eventDriver.timers.wait(tm, (tm) { assert(false, "Test hung."); });
2016-10-07 18:23:04 +00:00
static ubyte[] pack1 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
static ubyte[] pack2 = [4, 3, 2, 1, 0];
auto baddr = new InternetAddress(0x7F000001, 40001);
auto server = listenStream(baddr);
StreamSocket client;
StreamSocket incoming;
2017-01-15 20:59:15 +00:00
server.waitForConnections!((incoming_, addr) {
2016-10-07 18:23:04 +00:00
incoming = incoming_; // work around ref counting issue
print("Got incoming, reading data");
2016-10-07 18:23:04 +00:00
incoming.read!((status, bts) {
print("Got data");
2016-10-07 18:23:04 +00:00
assert(status == IOStatus.ok);
assert(bts == pack1.length);
assert(s_rbuf[0 .. pack1.length] == pack1);
print("Second write");
2016-10-07 18:23:04 +00:00
client.write!((status, bytes) {
print("Second write done");
2016-10-07 18:23:04 +00:00
assert(status == IOStatus.ok);
assert(bytes == pack2.length);
})(pack2, IOMode.once);
print("Second read");
2016-10-07 18:23:04 +00:00
incoming.read!((status, bts) {
print("Second read done");
2016-10-07 18:23:04 +00:00
assert(status == IOStatus.ok);
assert(bts == pack2.length);
assert(s_rbuf[0 .. pack2.length] == pack2);
destroy(client);
2016-10-07 18:23:04 +00:00
destroy(incoming);
destroy(server);
s_done = true;
eventDriver.timers.stop(tm);
// NOTE: one reference to incoming is still held by read()
//assert(eventDriver.core.waiterCount == 1);
2016-10-07 18:23:04 +00:00
})(s_rbuf, IOMode.once);
})(s_rbuf, IOMode.once);
});
print("Connect...");
2016-10-07 18:23:04 +00:00
connectStream!((sock, status) {
client = sock;
assert(status == ConnectStatus.connected);
print("Initial write");
2016-10-07 18:23:04 +00:00
client.write!((wstatus, bytes) {
print("Initial write done");
2016-10-07 18:23:04 +00:00
assert(wstatus == IOStatus.ok);
assert(bytes == 10);
})(pack1, IOMode.all);
})(baddr);
ExitReason er;
2016-10-19 20:01:11 +00:00
do er = eventDriver.core.processEvents(Duration.max);
2016-10-07 18:23:04 +00:00
while (er == ExitReason.idle);
assert(er == ExitReason.outOfWaiters);
2016-10-07 18:23:04 +00:00
assert(s_done);
s_done = false;
}