1 // Written in D programming language 2 /** 3 * This module defines rpc client class for testing rpc server. 4 * 5 * Copyright: © 2014 DSoftOut 6 * License: Subject to the terms of the MIT license, as written in the included LICENSE file. 7 * Authors: NCrashed <ncrashed@gmail.com> 8 */ 9 module client.client; 10 11 import std.stdio; 12 import core.time; 13 import vibe.data.json; 14 import vibe.web.rest; 15 import client.rpcapi; 16 import client.test.testcase; 17 import pgator.db.pool; 18 import pgator.db.async.pool; 19 import pgator.db.pq.libpq; 20 import pgator.db.pq.connection; 21 import dlogg.strict; 22 23 class RpcClient(T...) 24 { 25 this(string host, string connString, string jsonRpcTable, uint serverPid) 26 { 27 this.jsonRpcTable = jsonRpcTable; 28 this.serverPid = serverPid; 29 30 api = new RestInterfaceClient!IRpcApi(host); 31 api.requestFilter = (HTTPClientRequest rq) 32 { 33 rq.headers.addField("Authorization", "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="); 34 }; 35 36 logger = new shared StrictLogger("rpc-client.log"); 37 38 auto postgresApi = new shared PostgreSQL(logger); 39 auto connProvider = new shared PQConnProvider(logger, postgresApi); 40 41 pool = new shared AsyncPool(logger, connProvider, dur!"seconds"(1), dur!"seconds"(5), dur!"seconds"(3)); 42 43 pool.addServer(connString, 2); 44 } 45 46 void finalize() 47 { 48 pool.finalize(); 49 logger.finalize(); 50 } 51 52 void runTests() 53 { 54 foreach(TestCase; T) 55 { 56 static assert(is(TestCase : ITestCase)); 57 auto test = new TestCase(); 58 test.run(api, pool, jsonRpcTable, serverPid); 59 } 60 } 61 62 private 63 { 64 RestInterfaceClient!IRpcApi api; 65 shared IConnectionPool pool; 66 shared ILogger logger; 67 string jsonRpcTable; 68 uint serverPid; 69 } 70 }