Fix deprecations in tests and examples

This commit is contained in:
Geod24 2020-08-20 11:57:07 +09:00
parent 467c6a0996
commit ca9119af62
6 changed files with 70 additions and 50 deletions

View file

@ -1,9 +1,14 @@
import vibe.core.core : runApplication;
import vibe.core.log;
import vibe.core.net : listenTCP;
import vibe.core.stream : pipe;
void main()
{
listenTCP(7000, (conn) { pipe(conn, conn); });
listenTCP(7000, (conn) @safe nothrow {
try pipe(conn, conn);
catch (Exception e)
logError("Error: %s", e.msg);
});
runApplication();
}

View file

@ -25,7 +25,7 @@ void test1()
Test test;
Task lt;
auto l = listenTCP(0, (conn) {
auto l = listenTCP(0, (conn) @safe nothrow {
lt = Task.getThis();
try {
while (!conn.empty) {
@ -111,7 +111,7 @@ void test2()
{
Task lt;
logInfo("Perform test \"disconnect with pending data\"");
auto l = listenTCP(0, (conn) {
auto l = listenTCP(0, (conn) @safe nothrow {
try {
lt = Task.getThis();
sleep(1.seconds);
@ -156,13 +156,13 @@ void main()
runEventLoop();
}
string readLine(TCPConnection c)
string readLine(TCPConnection c) @safe
{
import std.string : indexOf;
string ret;
while (!c.empty) {
auto buf = cast(char[])c.peek();
auto buf = () @trusted { return cast(char[])c.peek(); }();
auto idx = buf.indexOf('\n');
if (idx < 0) {
ret ~= buf;
@ -176,7 +176,7 @@ string readLine(TCPConnection c)
return ret;
}
string readAll(TCPConnection c)
string readAll(TCPConnection c) @safe
{
import std.algorithm.comparison : min;
@ -186,5 +186,5 @@ string readAll(TCPConnection c)
ret.length += len;
c.read(ret[$-len .. $]);
}
return cast(string)ret;
return () @trusted { return cast(string) ret; }();
}

View file

@ -49,7 +49,9 @@ void runTest()
import std.socket : AddressFamily;
// server for a simple line based protocol
auto l1 = listenTCP(0, (client) {
auto l1 = listenTCP(0, (client) @safe nothrow {
try
{
while (!client.empty) {
auto ln = client.readLine();
if (ln == "quit") {
@ -58,13 +60,18 @@ void runTest()
break;
}
client.write(format("Hash: %08X\n", typeid(string).getHash(&ln)));
client.write(format("Hash: %08X\n",
() @trusted { return typeid(string).getHash(&ln); }()));
}
}
catch (Exception e)
assert(0, e.msg);
}, "127.0.0.1");
scope (exit) l1.stopListening;
// proxy server
auto l2 = listenTCP(0, (client) {
auto l2 = listenTCP(0, (client) @safe nothrow {
try {
auto server = connectTCP(l1.bindAddress);
// pipe server to client as long as the server connection is alive
@ -81,6 +88,8 @@ void runTest()
}
pipe(client, server);
logInfo("Proxy out");
} catch (Exception e)
assert(0, e.msg);
}, "127.0.0.1");
scope (exit) l2.stopListening;
@ -112,13 +121,13 @@ int main()
return ret;
}
string readLine(TCPConnection c)
string readLine(TCPConnection c) @safe
{
import std.string : indexOf;
string ret;
while (!c.empty) {
auto buf = cast(char[])c.peek();
auto buf = () @trusted { return cast(char[])c.peek(); }();
auto idx = buf.indexOf('\n');
if (idx < 0) {
ret ~= buf;
@ -131,4 +140,3 @@ string readLine(TCPConnection c)
}
return ret;
}

View file

@ -32,7 +32,7 @@ void runTest()
scope(exit) rmdirRecurse(dir);
DirectoryWatcher watcher;
try watcher = Path(dir).watchDirectory(No.recursive);
try watcher = NativePath(dir).watchDirectory(No.recursive);
catch (AssertError e) {
logInfo("DirectoryWatcher not yet implemented. Skipping test.");
return;
@ -43,7 +43,7 @@ void runTest()
auto foo = dir.buildPath("foo");
alias Type = DirectoryChangeType;
static DirectoryChange dc(Type t, string p) { return DirectoryChange(t, Path(p)); }
static DirectoryChange dc(Type t, string p) { return DirectoryChange(t, NativePath(p)); }
void check(DirectoryChange[] expected)
{
sleep(sleepTime);
@ -73,7 +73,7 @@ void runTest()
write(bar, null);
assert(!watcher.readChanges(changes, 100.msecs));
remove(bar);
watcher = Path(dir).watchDirectory(Yes.recursive);
watcher = NativePath(dir).watchDirectory(Yes.recursive);
write(foo, null);
sleep(sleepTime);
write(foo, [0, 1]);

View file

@ -11,7 +11,8 @@ import core.time : msecs;
void main()
{
auto l = listenTCP(0, (conn) {
auto l = listenTCP(0, (conn) @safe nothrow {
try {
auto td = runTask!TCPConnection((conn) {
ubyte [3] buf;
try {
@ -21,6 +22,9 @@ void main()
}, conn);
sleep(10.msecs);
conn.close();
}
catch (Exception e)
assert(0, e.msg);
}, "127.0.0.1");
runTask({

View file

@ -14,7 +14,7 @@ ubyte[] buf;
void performTest(bool reverse)
{
auto l = listenTCP(11375, (conn) {
auto l = listenTCP(11375, (conn) @safe nothrow {
bool read_ex = false;
bool write_ex = false;
auto rt = runTask!TCPConnection((conn) {
@ -43,8 +43,11 @@ void performTest(bool reverse)
} // expected
}, conn);
try {
rt.join();
wt.join();
} catch (Exception e)
assert(0, e.msg);
assert(read_ex, "No read exception thrown");
assert(write_ex, "No write exception thrown");
logInfo("Test has finished successfully.");