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.core : runApplication;
import vibe.core.log;
import vibe.core.net : listenTCP; import vibe.core.net : listenTCP;
import vibe.core.stream : pipe; import vibe.core.stream : pipe;
void main() 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(); runApplication();
} }

View file

@ -25,7 +25,7 @@ void test1()
Test test; Test test;
Task lt; Task lt;
auto l = listenTCP(0, (conn) { auto l = listenTCP(0, (conn) @safe nothrow {
lt = Task.getThis(); lt = Task.getThis();
try { try {
while (!conn.empty) { while (!conn.empty) {
@ -111,7 +111,7 @@ void test2()
{ {
Task lt; Task lt;
logInfo("Perform test \"disconnect with pending data\""); logInfo("Perform test \"disconnect with pending data\"");
auto l = listenTCP(0, (conn) { auto l = listenTCP(0, (conn) @safe nothrow {
try { try {
lt = Task.getThis(); lt = Task.getThis();
sleep(1.seconds); sleep(1.seconds);
@ -156,13 +156,13 @@ void main()
runEventLoop(); runEventLoop();
} }
string readLine(TCPConnection c) string readLine(TCPConnection c) @safe
{ {
import std.string : indexOf; import std.string : indexOf;
string ret; string ret;
while (!c.empty) { while (!c.empty) {
auto buf = cast(char[])c.peek(); auto buf = () @trusted { return cast(char[])c.peek(); }();
auto idx = buf.indexOf('\n'); auto idx = buf.indexOf('\n');
if (idx < 0) { if (idx < 0) {
ret ~= buf; ret ~= buf;
@ -176,7 +176,7 @@ string readLine(TCPConnection c)
return ret; return ret;
} }
string readAll(TCPConnection c) string readAll(TCPConnection c) @safe
{ {
import std.algorithm.comparison : min; import std.algorithm.comparison : min;
@ -186,5 +186,5 @@ string readAll(TCPConnection c)
ret.length += len; ret.length += len;
c.read(ret[$-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; import std.socket : AddressFamily;
// server for a simple line based protocol // server for a simple line based protocol
auto l1 = listenTCP(0, (client) { auto l1 = listenTCP(0, (client) @safe nothrow {
try
{
while (!client.empty) { while (!client.empty) {
auto ln = client.readLine(); auto ln = client.readLine();
if (ln == "quit") { if (ln == "quit") {
@ -58,13 +60,18 @@ void runTest()
break; 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"); }, "127.0.0.1");
scope (exit) l1.stopListening; scope (exit) l1.stopListening;
// proxy server // proxy server
auto l2 = listenTCP(0, (client) { auto l2 = listenTCP(0, (client) @safe nothrow {
try {
auto server = connectTCP(l1.bindAddress); auto server = connectTCP(l1.bindAddress);
// pipe server to client as long as the server connection is alive // pipe server to client as long as the server connection is alive
@ -81,6 +88,8 @@ void runTest()
} }
pipe(client, server); pipe(client, server);
logInfo("Proxy out"); logInfo("Proxy out");
} catch (Exception e)
assert(0, e.msg);
}, "127.0.0.1"); }, "127.0.0.1");
scope (exit) l2.stopListening; scope (exit) l2.stopListening;
@ -112,13 +121,13 @@ int main()
return ret; return ret;
} }
string readLine(TCPConnection c) string readLine(TCPConnection c) @safe
{ {
import std.string : indexOf; import std.string : indexOf;
string ret; string ret;
while (!c.empty) { while (!c.empty) {
auto buf = cast(char[])c.peek(); auto buf = () @trusted { return cast(char[])c.peek(); }();
auto idx = buf.indexOf('\n'); auto idx = buf.indexOf('\n');
if (idx < 0) { if (idx < 0) {
ret ~= buf; ret ~= buf;
@ -131,4 +140,3 @@ string readLine(TCPConnection c)
} }
return ret; return ret;
} }

View file

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

View file

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

View file

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