1 /+ dub.sdl: 2 name "t5" 3 dflags "-I../source" 4 #dflags "-fsanitize=address" 5 #dflags "-debug" 6 #debugVersions "nbuff" 7 #debugVersions "timingwheels" 8 #debugVersions "cachetools" 9 lflags "-lcares" 10 buildRequirements "allowWarnings" 11 dependency "hio" version="*" 12 dependency "nbuff" version="*" 13 +/ 14 15 module tests.t5; 16 import core.thread; 17 import std.datetime; 18 import std.algorithm; 19 import std.string: fromStringz; 20 import std.experimental.logger; 21 import std.stdio; 22 import std.array; 23 import std.socket; 24 import core.sys.posix.arpa.inet; 25 import hio.resolver; 26 import hio.scheduler; 27 import hio.events; 28 import hio.loop; 29 30 void main() 31 { 32 globalLogLevel = LogLevel.info; 33 info("blocked resolving in main thread"); 34 enum iterations = 10; 35 // sync 36 for(int i=0; i< iterations; i++) 37 { 38 auto r0 = hio_gethostbyname("localhost"); 39 auto r4 = hio_gethostbyname("google.com"); 40 auto r6 = hio_gethostbyname6("google.com"); 41 writeln("localhost0 ", r0); 42 Thread.sleep(500.msecs); 43 } 44 45 info("concurrent resolving in tasks"); 46 App({ 47 void job1() 48 { 49 for(int i=0;i<iterations;i++) 50 { 51 auto r0 = hio_gethostbyname("localhost"); 52 auto r4 = hio_gethostbyname("dlang.org"); 53 auto r6 = hio_gethostbyname6("dlang.org"); 54 writeln("localhost1 ", r0); 55 writeln("dlang.org", r4,r6); 56 hlSleep(200.msecs); 57 } 58 } 59 void job2() 60 { 61 for(int i=0;i<iterations;i++) 62 { 63 auto r4 = hio_gethostbyname("ietf.org"); 64 auto r6 = hio_gethostbyname6("ietf.org"); 65 hlSleep(200.msecs); 66 } 67 } 68 auto t1 = task(&job1), t2 = task(&job2); 69 t1.start(); 70 t2.start(); 71 t1.wait(); 72 t2.wait(); 73 }); 74 // async/callbacks 75 info("async resolving in callbacks"); 76 auto loop = getDefaultLoop(); 77 int counter = 100; 78 Timer t; 79 void resolve4(ResolverResult4 r) @trusted 80 { 81 char[32] buf; 82 writefln("[%s]", r.addresses); 83 counter--; 84 if ( counter == 0 ) 85 { 86 loop.stop(); 87 } 88 } 89 void resolve6(ResolverResult6 r) @trusted 90 { 91 char[64] buf; 92 writefln("[%s]", r.addresses); 93 counter--; 94 if ( counter == 0 ) 95 { 96 loop.stop(); 97 } 98 } 99 void timer(AppEvent e) @safe 100 { 101 hio_gethostbyname("cloudflare.com", &resolve4); 102 hio_gethostbyname6("cloudflare.com", &resolve6); 103 hio_gethostbyname("github.com", &resolve4); 104 t.rearm(200.msecs); 105 loop.startTimer(t); 106 } 107 t = new Timer(200.msecs, &timer); 108 loop.startTimer(t); 109 loop.run(); 110 }