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: 15 @safe TestStruct getTest(int sleepsecs); 16 @safe TestStruct getTest2(int sleepsecs); 17 } 18 19 class Service : IService 20 { 21 @safe TestStruct getTestImpl(int sleep_msecs) 22 { 23 sleep(sleep_msecs.msecs); 24 TestStruct test_struct = {sleep_msecs}; 25 return test_struct; 26 } 27 28 @safe TestStruct getTest(int sleep_msecs) 29 { 30 return getTestImpl(sleep_msecs); 31 } 32 33 @safe TestStruct getTest2(int sleep_msecs) 34 { 35 return getTestImpl(sleep_msecs); 36 } 37 } 38 39 void main() 40 { 41 setLogLevel(LogLevel.trace); 42 auto settings_server = new HTTPServerSettings; 43 settings_server.port = 5000; 44 settings_server.bindAddresses = ["127.0.0.1"]; 45 auto router = new URLRouter; 46 router.registerRestInterface(new Service); 47 auto server_addr = listenHTTP(settings_server, router); 48 49 runTask({ 50 scope (exit) exitEventLoop(true); 51 try { 52 auto settings = new RestInterfaceSettings(); 53 settings.httpClientSettings = new HTTPClientSettings(); 54 settings.httpClientSettings.readTimeout = 1.seconds; 55 settings.baseURL = URL("http://127.0.0.1:5000/"); 56 auto service_client = new RestInterfaceClient!IService(settings); 57 try {service_client.getTest(2000);} catch (Exception e) {} 58 sleep(1200.msecs); 59 auto result = service_client.getTest2(500); 60 writeln("result:", result); 61 assert(result.i == 500); 62 } catch (Exception e) assert(false, e.msg); 63 }); 64 65 runApplication(); 66 return; 67 }