|
| 1 | +#define DROGON_TEST_MAIN |
| 2 | +#include <drogon/drogon_test.h> |
| 3 | +#include <drogon/HttpAppFramework.h> |
| 4 | +#include <drogon/config.h> |
| 5 | + |
| 6 | +using namespace drogon; |
| 7 | +using namespace trantor; |
| 8 | + |
| 9 | +DROGON_TEST(DbApiTest) |
| 10 | +{ |
| 11 | +#if USE_POSTGRESQL |
| 12 | + { |
| 13 | + auto client = app().getDbClient("pg_non_fast"); |
| 14 | + CHECK(client != nullptr); |
| 15 | + client->closeAll(); |
| 16 | + drogon::app().getLoop()->runInLoop([TEST_CTX]() { |
| 17 | + auto client = app().getFastDbClient("pg_fast"); |
| 18 | + CHECK(client != nullptr); |
| 19 | + client->closeAll(); |
| 20 | + }); |
| 21 | + drogon::app().getIOLoop(0)->runInLoop([TEST_CTX]() { |
| 22 | + auto client = app().getFastDbClient("pg_fast"); |
| 23 | + CHECK(client != nullptr); |
| 24 | + client->closeAll(); |
| 25 | + }); |
| 26 | + } |
| 27 | +#endif |
| 28 | + |
| 29 | +#if USE_MYSQL |
| 30 | + { |
| 31 | + auto client = app().getDbClient("mysql_non_fast"); |
| 32 | + CHECK(client != nullptr); |
| 33 | + client->closeAll(); |
| 34 | + drogon::app().getLoop()->runInLoop([TEST_CTX]() { |
| 35 | + auto client = app().getFastDbClient("mysql_fast"); |
| 36 | + CHECK(client != nullptr); |
| 37 | + client->closeAll(); |
| 38 | + }); |
| 39 | + drogon::app().getIOLoop(0)->runInLoop([TEST_CTX]() { |
| 40 | + auto client = app().getFastDbClient("mysql_fast"); |
| 41 | + CHECK(client != nullptr); |
| 42 | + client->closeAll(); |
| 43 | + }); |
| 44 | + } |
| 45 | +#endif |
| 46 | + |
| 47 | +#if USE_SQLITE3 |
| 48 | + { |
| 49 | + auto client = app().getDbClient("sqlite3_non_fast"); |
| 50 | + CHECK(client != nullptr); |
| 51 | + client->closeAll(); |
| 52 | + } |
| 53 | +#endif |
| 54 | + |
| 55 | + app().getLoop()->runAfter(5, [TEST_CTX]() {}); // wait for some time |
| 56 | +} |
| 57 | + |
| 58 | +const std::string_view pg_non_fast_config = R"({ |
| 59 | + "name": "pg_non_fast", |
| 60 | + "rdbms": "postgresql", |
| 61 | + "host": "127.0.0.1", |
| 62 | + "port": 5432, |
| 63 | + "dbname": "test", |
| 64 | + "user": "postgres", |
| 65 | + "passwd": "123456", |
| 66 | + "is_fast": false |
| 67 | +})"; |
| 68 | + |
| 69 | +const std::string_view pg_fast_config = R"({ |
| 70 | + "name": "pg_fast", |
| 71 | + "rdbms": "postgresql", |
| 72 | + "host": "127.0.0.1", |
| 73 | + "port": 5432, |
| 74 | + "dbname": "test", |
| 75 | + "user": "postgres", |
| 76 | + "passwd": "123456", |
| 77 | + "is_fast": true |
| 78 | +})"; |
| 79 | + |
| 80 | +const std::string_view mysql_non_fast_config = R"({ |
| 81 | + "name": "mysql_non_fast", |
| 82 | + "rdbms": "mysql", |
| 83 | + "host": "127.0.0.1", |
| 84 | + "port": 3306, |
| 85 | + "dbname": "test", |
| 86 | + "user": "root", |
| 87 | + "passwd": "123456", |
| 88 | + "is_fast": false |
| 89 | +})"; |
| 90 | + |
| 91 | +const std::string_view mysql_fast_config = R"({ |
| 92 | + "name": "mysql_fast", |
| 93 | + "rdbms": "mysql", |
| 94 | + "host": "127.0.0.1", |
| 95 | + "port": 3306, |
| 96 | + "dbname": "test", |
| 97 | + "user": "root", |
| 98 | + "passwd": "123456", |
| 99 | + "is_fast": true |
| 100 | +})"; |
| 101 | + |
| 102 | +const std::string_view sqlite3_non_fast_config = R"({ |
| 103 | + "name": "sqlite3_non_fast", |
| 104 | + "rdbms": "sqlite3", |
| 105 | + "filename": "test.db", |
| 106 | + "is_fast": false |
| 107 | +})"; |
| 108 | + |
| 109 | +bool parseJson(std::string_view str, Json::Value *root, Json::String *errs) |
| 110 | +{ |
| 111 | + static Json::CharReaderBuilder &builder = |
| 112 | + []() -> Json::CharReaderBuilder & { |
| 113 | + static Json::CharReaderBuilder builder; |
| 114 | + builder["collectComments"] = false; |
| 115 | + return builder; |
| 116 | + }(); |
| 117 | + std::unique_ptr<Json::CharReader> jsonReader(builder.newCharReader()); |
| 118 | + return jsonReader->parse(str.data(), str.data() + str.size(), root, errs); |
| 119 | +} |
| 120 | + |
| 121 | +int main(int argc, char **argv) |
| 122 | +{ |
| 123 | + Json::Value config; |
| 124 | + config["app"]["number_of_threads"] = 1; |
| 125 | + config["db_clients"] = Json::arrayValue; |
| 126 | + |
| 127 | + std::string err; |
| 128 | + Json::Value cfg; |
| 129 | +#if USE_POSTGRESQL |
| 130 | + cfg = Json::objectValue; |
| 131 | + if (!parseJson(pg_non_fast_config, &cfg, &err)) |
| 132 | + { |
| 133 | + LOG_ERROR << "Failed to parse json: " << err; |
| 134 | + return 1; |
| 135 | + } |
| 136 | + config["db_clients"].append(cfg); |
| 137 | + cfg = Json::objectValue; |
| 138 | + if (!parseJson(pg_fast_config.data(), &cfg, &err)) |
| 139 | + { |
| 140 | + LOG_ERROR << "Failed to parse json: " << err; |
| 141 | + return 1; |
| 142 | + } |
| 143 | + config["db_clients"].append(cfg); |
| 144 | +#endif |
| 145 | + |
| 146 | +#if USE_MYSQL |
| 147 | + cfg = Json::objectValue; |
| 148 | + if (!parseJson(mysql_non_fast_config, &cfg, &err)) |
| 149 | + { |
| 150 | + LOG_ERROR << "Failed to parse json: " << err; |
| 151 | + return 1; |
| 152 | + } |
| 153 | + config["db_clients"].append(cfg); |
| 154 | + cfg = Json::objectValue; |
| 155 | + if (!parseJson(mysql_fast_config.data(), &cfg, &err)) |
| 156 | + { |
| 157 | + LOG_ERROR << "Failed to parse json: " << err; |
| 158 | + return 1; |
| 159 | + } |
| 160 | + config["db_clients"].append(cfg); |
| 161 | +#endif |
| 162 | + |
| 163 | +#if USE_SQLITE3 |
| 164 | + cfg = Json::objectValue; |
| 165 | + if (!parseJson(sqlite3_non_fast_config, &cfg, &err)) |
| 166 | + { |
| 167 | + LOG_ERROR << "Failed to parse json: " << err; |
| 168 | + return 1; |
| 169 | + } |
| 170 | + config["db_clients"].append(cfg); |
| 171 | +#endif |
| 172 | + |
| 173 | + std::promise<void> p1; |
| 174 | + std::future<void> f1 = p1.get_future(); |
| 175 | + app().setThreadNum(1); |
| 176 | + std::thread thr([&]() { |
| 177 | + app().getLoop()->queueInLoop([&]() { p1.set_value(); }); |
| 178 | + app().loadConfigJson(config).run(); |
| 179 | + }); |
| 180 | + |
| 181 | + f1.get(); |
| 182 | + int testStatus = test::run(argc, argv); |
| 183 | + app().getLoop()->queueInLoop([]() { app().quit(); }); |
| 184 | + thr.join(); |
| 185 | + return testStatus; |
| 186 | +} |
0 commit comments