2017-02-16 23:49:40 +00:00
|
|
|
/+ dub.sdl:
|
2016-10-24 06:37:32 +00:00
|
|
|
name "test"
|
|
|
|
description "Tests vibe.d's std.concurrency integration"
|
|
|
|
dependency "vibe-core" path="../"
|
2016-10-24 06:32:16 +00:00
|
|
|
+/
|
|
|
|
module test;
|
|
|
|
|
|
|
|
import vibe.core.core;
|
|
|
|
import vibe.core.log;
|
2020-05-22 08:33:17 +00:00
|
|
|
import std.algorithm;
|
2016-10-24 06:32:16 +00:00
|
|
|
import std.concurrency;
|
|
|
|
import core.atomic;
|
|
|
|
import core.time;
|
|
|
|
import core.stdc.stdlib : exit;
|
|
|
|
|
|
|
|
__gshared Tid t1, t2;
|
|
|
|
shared watchdog_count = 0;
|
|
|
|
|
2016-10-24 06:37:32 +00:00
|
|
|
void main()
|
2016-10-24 06:32:16 +00:00
|
|
|
{
|
|
|
|
t1 = spawn({
|
2020-05-22 08:33:17 +00:00
|
|
|
// ensure that asynchronous operations can run in parallel to receive()
|
2016-10-24 06:32:16 +00:00
|
|
|
int wc = 0;
|
2020-05-22 08:33:17 +00:00
|
|
|
MonoTime stime = MonoTime.currTime;
|
|
|
|
runTask({
|
|
|
|
while (true) {
|
|
|
|
sleepUntil((wc + 1) * 250.msecs, stime, 200.msecs);
|
|
|
|
wc++;
|
|
|
|
logInfo("Watchdog receiver %s", wc);
|
|
|
|
}
|
|
|
|
});
|
2016-10-24 06:32:16 +00:00
|
|
|
|
|
|
|
bool finished = false;
|
|
|
|
try while (!finished) {
|
|
|
|
logDebug("receive1");
|
|
|
|
receive(
|
|
|
|
(string msg) {
|
|
|
|
logInfo("Received string message: %s", msg);
|
|
|
|
},
|
|
|
|
(int msg) {
|
|
|
|
logInfo("Received int message: %s", msg);
|
|
|
|
});
|
|
|
|
logDebug("receive2");
|
|
|
|
receive(
|
|
|
|
(double msg) {
|
|
|
|
logInfo("Received double: %s", msg);
|
|
|
|
},
|
|
|
|
(int a, int b, int c) {
|
|
|
|
logInfo("Received iii: %s %s %s", a, b, c);
|
|
|
|
|
|
|
|
if (a == 1 && b == 2 && c == 3)
|
|
|
|
finished = true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
catch (Exception e) assert(false, "Receiver thread failed: "~e.msg);
|
|
|
|
|
|
|
|
logInfo("Receive loop finished.");
|
2019-11-02 14:41:31 +00:00
|
|
|
version (OSX) enum tolerance = 4; // macOS CI VMs have particularly bad timing behavior
|
|
|
|
else enum tolerance = 1;
|
|
|
|
if (wc < 6 * 4 - tolerance) {
|
2016-10-24 06:32:16 +00:00
|
|
|
logError("Receiver watchdog failure.");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
logInfo("Exiting normally");
|
|
|
|
});
|
|
|
|
|
|
|
|
t2 = spawn({
|
2020-05-22 08:33:17 +00:00
|
|
|
MonoTime stime = MonoTime.currTime;
|
|
|
|
|
2016-10-24 06:32:16 +00:00
|
|
|
scope (failure) assert(false);
|
2020-05-22 08:33:17 +00:00
|
|
|
sleepUntil(1.seconds, stime, 900.msecs);
|
2016-10-24 06:32:16 +00:00
|
|
|
logInfo("send Hello World");
|
|
|
|
t1.send("Hello, World!");
|
|
|
|
|
2020-05-22 08:33:17 +00:00
|
|
|
sleepUntil(2.seconds, stime, 900.msecs);
|
2016-10-24 06:32:16 +00:00
|
|
|
logInfo("send int 1");
|
|
|
|
t1.send(1);
|
|
|
|
|
2020-05-22 08:33:17 +00:00
|
|
|
sleepUntil(3.seconds, stime, 900.msecs);
|
2016-10-24 06:32:16 +00:00
|
|
|
logInfo("send double 1.2");
|
|
|
|
t1.send(1.2);
|
|
|
|
|
2020-05-22 08:33:17 +00:00
|
|
|
sleepUntil(4.seconds, stime, 900.msecs);
|
2016-10-24 06:32:16 +00:00
|
|
|
logInfo("send int 2");
|
|
|
|
t1.send(2);
|
|
|
|
|
2020-05-22 08:33:17 +00:00
|
|
|
sleepUntil(5.seconds, stime, 900.msecs);
|
2016-10-24 06:32:16 +00:00
|
|
|
logInfo("send 3xint 1 2 3");
|
|
|
|
t1.send(1, 2, 3);
|
|
|
|
|
2020-05-22 08:33:17 +00:00
|
|
|
sleepUntil(6.seconds, stime, 900.msecs);
|
2016-10-24 06:32:16 +00:00
|
|
|
logInfo("send string Bye bye");
|
|
|
|
t1.send("Bye bye");
|
|
|
|
|
|
|
|
sleep(100.msecs);
|
|
|
|
logInfo("Exiting.");
|
|
|
|
exitEventLoop(true);
|
|
|
|
});
|
2016-10-24 06:37:32 +00:00
|
|
|
|
|
|
|
runApplication();
|
2016-10-24 06:32:16 +00:00
|
|
|
}
|
2020-05-22 08:33:17 +00:00
|
|
|
|
|
|
|
// corrects for small timing inaccuracies to avoid the counter
|
|
|
|
// getting systematically out of sync when sleep timing is inaccurate
|
|
|
|
void sleepUntil(Duration until, MonoTime start_time, Duration min_sleep)
|
|
|
|
{
|
|
|
|
auto tm = MonoTime.currTime;
|
|
|
|
auto timeout = max(start_time - tm + until, min_sleep);
|
|
|
|
sleep(timeout);
|
|
|
|
}
|