Add timeout to wxWebRequest test

Ensure that the test terminates even if we don't get the expected event.
This commit is contained in:
Vadim Zeitlin
2021-01-15 00:44:46 +01:00
parent d2840d2516
commit e80b65b132

View File

@@ -33,7 +33,7 @@
// disable running the test entirely. // disable running the test entirely.
static const char* WX_TEST_WEBREQUEST_URL_DEFAULT = "https://httpbin.org"; static const char* WX_TEST_WEBREQUEST_URL_DEFAULT = "https://httpbin.org";
class RequestFixture : public wxEvtHandler class RequestFixture : public wxTimer
{ {
public: public:
RequestFixture() RequestFixture()
@@ -93,18 +93,31 @@ public:
} }
} }
void Notify() wxOVERRIDE
{
WARN("Exiting loop on timeout");
loop.Exit();
}
void OnData(wxWebRequestEvent& evt) void OnData(wxWebRequestEvent& evt)
{ {
// Count all bytes recieved via data event for Storage_None // Count all bytes recieved via data event for Storage_None
dataSize += evt.GetDataSize(); dataSize += evt.GetDataSize();
} }
void RunLoopWithTimeout()
{
StartOnce(10000); // Ensure that we exit the loop after 10s.
loop.Run();
Stop();
}
void Run(wxWebRequest::State requiredState = wxWebRequest::State_Completed, void Run(wxWebRequest::State requiredState = wxWebRequest::State_Completed,
int requiredStatus = 200) int requiredStatus = 200)
{ {
REQUIRE( request.GetState() == wxWebRequest::State_Idle ); REQUIRE( request.GetState() == wxWebRequest::State_Idle );
request.Start(); request.Start();
loop.Run(); RunLoopWithTimeout();
REQUIRE( request.GetState() == requiredState ); REQUIRE( request.GetState() == requiredState );
if (requiredStatus) if (requiredStatus)
REQUIRE( request.GetResponse().GetStatus() == requiredStatus ); REQUIRE( request.GetResponse().GetStatus() == requiredStatus );
@@ -240,7 +253,7 @@ TEST_CASE_METHOD(RequestFixture,
SECTION("Good password") SECTION("Good password")
{ {
UseCredentials("wxtest", "wxwidgets"); UseCredentials("wxtest", "wxwidgets");
loop.Run(); RunLoopWithTimeout();
CHECK( request.GetResponse().GetStatus() == 200 ); CHECK( request.GetResponse().GetStatus() == 200 );
CHECK( request.GetState() == wxWebRequest::State_Completed ); CHECK( request.GetState() == wxWebRequest::State_Completed );
} }
@@ -248,7 +261,7 @@ TEST_CASE_METHOD(RequestFixture,
SECTION("Bad password") SECTION("Bad password")
{ {
UseCredentials("wxtest", "foobar"); UseCredentials("wxtest", "foobar");
loop.Run(); RunLoopWithTimeout();
CHECK( request.GetResponse().GetStatus() == 401 ); CHECK( request.GetResponse().GetStatus() == 401 );
CHECK( request.GetState() == wxWebRequest::State_Unauthorized ); CHECK( request.GetState() == wxWebRequest::State_Unauthorized );
} }
@@ -273,7 +286,7 @@ TEST_CASE_METHOD(RequestFixture,
SECTION("Good password") SECTION("Good password")
{ {
UseCredentials("wxtest", "wxwidgets"); UseCredentials("wxtest", "wxwidgets");
loop.Run(); RunLoopWithTimeout();
CHECK( request.GetResponse().GetStatus() == 200 ); CHECK( request.GetResponse().GetStatus() == 200 );
CHECK( request.GetState() == wxWebRequest::State_Completed ); CHECK( request.GetState() == wxWebRequest::State_Completed );
} }
@@ -281,7 +294,7 @@ TEST_CASE_METHOD(RequestFixture,
SECTION("Bad password") SECTION("Bad password")
{ {
UseCredentials("foo", "bar"); UseCredentials("foo", "bar");
loop.Run(); RunLoopWithTimeout();
CHECK( request.GetResponse().GetStatus() == 401 ); CHECK( request.GetResponse().GetStatus() == 401 );
CHECK( request.GetState() == wxWebRequest::State_Unauthorized ); CHECK( request.GetState() == wxWebRequest::State_Unauthorized );
} }