From ca9119af62b4ab0dc741dc6c4a867fad49bb46c5 Mon Sep 17 00:00:00 2001 From: Geod24 Date: Thu, 20 Aug 2020 11:57:07 +0900 Subject: [PATCH] Fix deprecations in tests and examples --- examples/echo-server/source/app.d | 7 +++- tests/0-tcp.d | 12 +++--- tests/0-tcpproxy.d | 62 +++++++++++++++++-------------- tests/dirwatcher.d | 6 +-- tests/vibe.core.net.1376.d | 24 +++++++----- tests/vibe.core.net.1726.d | 9 +++-- 6 files changed, 70 insertions(+), 50 deletions(-) diff --git a/examples/echo-server/source/app.d b/examples/echo-server/source/app.d index 3b47b3f..76a9c74 100644 --- a/examples/echo-server/source/app.d +++ b/examples/echo-server/source/app.d @@ -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(); } diff --git a/tests/0-tcp.d b/tests/0-tcp.d index 453f6e6..f4801be 100644 --- a/tests/0-tcp.d +++ b/tests/0-tcp.d @@ -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; }(); } diff --git a/tests/0-tcpproxy.d b/tests/0-tcpproxy.d index 38be778..65668ec 100644 --- a/tests/0-tcpproxy.d +++ b/tests/0-tcpproxy.d @@ -49,38 +49,47 @@ void runTest() import std.socket : AddressFamily; // server for a simple line based protocol - auto l1 = listenTCP(0, (client) { - while (!client.empty) { - auto ln = client.readLine(); - if (ln == "quit") { - client.write("Bye bye!\n"); - client.close(); - break; - } + auto l1 = listenTCP(0, (client) @safe nothrow { + try + { + while (!client.empty) { + auto ln = client.readLine(); + if (ln == "quit") { + client.write("Bye bye!\n"); + client.close(); + 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 server = connectTCP(l1.bindAddress); + 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 - auto t = runTask!(TCPConnection, TCPConnection)((client, server) { - scope (exit) client.close(); - pipe(server, client); - logInfo("Proxy 2 out"); - }, client, server); + // pipe server to client as long as the server connection is alive + auto t = runTask!(TCPConnection, TCPConnection)((client, server) { + scope (exit) client.close(); + pipe(server, client); + logInfo("Proxy 2 out"); + }, client, server); - // pipe client to server as long as the client connection is alive - scope (exit) { - server.close(); - t.join(); - } - pipe(client, server); - logInfo("Proxy out"); + // pipe client to server as long as the client connection is alive + scope (exit) { + server.close(); + t.join(); + } + 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; } - diff --git a/tests/dirwatcher.d b/tests/dirwatcher.d index d1f739f..2cf17a5 100644 --- a/tests/dirwatcher.d +++ b/tests/dirwatcher.d @@ -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]); diff --git a/tests/vibe.core.net.1376.d b/tests/vibe.core.net.1376.d index d9afb6f..e3a9d2a 100644 --- a/tests/vibe.core.net.1376.d +++ b/tests/vibe.core.net.1376.d @@ -11,16 +11,20 @@ import core.time : msecs; void main() { - auto l = listenTCP(0, (conn) { - auto td = runTask!TCPConnection((conn) { - ubyte [3] buf; - try { - conn.read(buf); - assert(false, "Expected read() to throw an exception."); - } catch (Exception) {} // expected - }, conn); - sleep(10.msecs); - conn.close(); + auto l = listenTCP(0, (conn) @safe nothrow { + try { + auto td = runTask!TCPConnection((conn) { + ubyte [3] buf; + try { + conn.read(buf); + assert(false, "Expected read() to throw an exception."); + } catch (Exception) {} // expected + }, conn); + sleep(10.msecs); + conn.close(); + } + catch (Exception e) + assert(0, e.msg); }, "127.0.0.1"); runTask({ diff --git a/tests/vibe.core.net.1726.d b/tests/vibe.core.net.1726.d index 6f0838f..586544e 100644 --- a/tests/vibe.core.net.1726.d +++ b/tests/vibe.core.net.1726.d @@ -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); - rt.join(); - wt.join(); + 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.");