drogonframework / drogon

Drogon: A C++14/17/20 based HTTP web application framework running on Linux/macOS/Unix/Windows
MIT License
11.52k stars 1.1k forks source link

Is it possible to test Drogon via GoogleTest? #1417

Closed Rofl113 closed 1 year ago

Rofl113 commented 2 years ago

For ORM Drogon to work, you need to launch the app.

auto& appDrogon = drogon::HttpAppFramework::instance();
return appDrogon.run();

To run Google Test, you also need to run your own loop.

testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();

Is it possible to do this?

Rofl113 commented 1 year ago

I wrote the following code but it didn't help:

std::promise<void> p1;
std::future<void> f1 = p1.get_future();

std::thread thr([&p1]()
{
    app().getLoop()->queueInLoop([&p1] ()
    {
        p1.set_value();
    });
    app().run();
});

f1.get();
testing::InitGoogleTest(&argc, argv);
int testStatus = RUN_ALL_TESTS();
app().getLoop()->queueInLoop([] ()
{
    app().quit();
});
thr.join();
return testStatus;

Crashes on creating Sqlite3Client:

auto clientDb = drogon::orm::DbClient::newSqlite3Client
Rofl113 commented 1 year ago

The problem turned out to be that the testing::Test object is created every time and it was necessary to make the database client static with the creation on the first test. Sorry to distract you for nothing.