1 module tests; 2 3 import vibe.core.core; 4 import vibe.d; 5 6 import core.stdc.stdlib; 7 8 import std.stdio; 9 10 struct TestStruct {int i;} 11 12 interface IService 13 { 14 @safe TestStruct getTest(int sleepsecs); 15 @safe TestStruct getTest2(int sleepsecs); 16 } 17 18 class Service : IService 19 { 20 @safe TestStruct getTestImpl(int sleep_msecs) 21 { 22 sleep(sleep_msecs.msecs); 23 TestStruct test_struct = {sleep_msecs}; 24 return test_struct; 25 } 26 27 @safe TestStruct getTest(int sleep_msecs) 28 { 29 return getTestImpl(sleep_msecs); 30 } 31 32 @safe TestStruct getTest2(int sleep_msecs) 33 { 34 return getTestImpl(sleep_msecs); 35 } 36 } 37 38 void main() 39 { 40 setLogLevel(LogLevel.trace); 41 auto settings_server = new HTTPServerSettings; 42 settings_server.port = 5000; 43 settings_server.bindAddresses = ["127.0.0.1"]; 44 auto router = new URLRouter; 45 router.registerRestInterface(new Service); 46 auto server_addr = listenHTTP(settings_server, router); 47 48 runTask({ 49 scope (exit) exitEventLoop(true); 50 auto settings = new RestInterfaceSettings(); 51 settings.httpClientSettings = new HTTPClientSettings(); 52 settings.httpClientSettings.readTimeout = 1.seconds; 53 settings.baseURL = URL("http://127.0.0.1:5000/"); 54 auto service_client = new RestInterfaceClient!IService(settings); 55 try {service_client.getTest(2000);} catch (Exception e) {} 56 sleep(1200.msecs); 57 auto result = service_client.getTest2(500); 58 writeln("result:", result); 59 assert(result.i == 500); 60 }); 61 62 runApplication(); 63 return; 64 }