From e80b65b1327634ca281bd7518ec2d0d49e9d5057 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 15 Jan 2021 00:44:46 +0100 Subject: [PATCH] Add timeout to wxWebRequest test Ensure that the test terminates even if we don't get the expected event. --- tests/net/webrequest.cpp | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/tests/net/webrequest.cpp b/tests/net/webrequest.cpp index 94de431152..188a6ff0b0 100644 --- a/tests/net/webrequest.cpp +++ b/tests/net/webrequest.cpp @@ -33,7 +33,7 @@ // disable running the test entirely. static const char* WX_TEST_WEBREQUEST_URL_DEFAULT = "https://httpbin.org"; -class RequestFixture : public wxEvtHandler +class RequestFixture : public wxTimer { public: RequestFixture() @@ -93,18 +93,31 @@ public: } } + void Notify() wxOVERRIDE + { + WARN("Exiting loop on timeout"); + loop.Exit(); + } + void OnData(wxWebRequestEvent& evt) { // Count all bytes recieved via data event for Storage_None 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, int requiredStatus = 200) { REQUIRE( request.GetState() == wxWebRequest::State_Idle ); request.Start(); - loop.Run(); + RunLoopWithTimeout(); REQUIRE( request.GetState() == requiredState ); if (requiredStatus) REQUIRE( request.GetResponse().GetStatus() == requiredStatus ); @@ -240,7 +253,7 @@ TEST_CASE_METHOD(RequestFixture, SECTION("Good password") { UseCredentials("wxtest", "wxwidgets"); - loop.Run(); + RunLoopWithTimeout(); CHECK( request.GetResponse().GetStatus() == 200 ); CHECK( request.GetState() == wxWebRequest::State_Completed ); } @@ -248,7 +261,7 @@ TEST_CASE_METHOD(RequestFixture, SECTION("Bad password") { UseCredentials("wxtest", "foobar"); - loop.Run(); + RunLoopWithTimeout(); CHECK( request.GetResponse().GetStatus() == 401 ); CHECK( request.GetState() == wxWebRequest::State_Unauthorized ); } @@ -273,7 +286,7 @@ TEST_CASE_METHOD(RequestFixture, SECTION("Good password") { UseCredentials("wxtest", "wxwidgets"); - loop.Run(); + RunLoopWithTimeout(); CHECK( request.GetResponse().GetStatus() == 200 ); CHECK( request.GetState() == wxWebRequest::State_Completed ); } @@ -281,7 +294,7 @@ TEST_CASE_METHOD(RequestFixture, SECTION("Bad password") { UseCredentials("foo", "bar"); - loop.Run(); + RunLoopWithTimeout(); CHECK( request.GetResponse().GetStatus() == 401 ); CHECK( request.GetState() == wxWebRequest::State_Unauthorized ); }