From 7678d5e6ed6fcc9361416ef41c43fa09a1d49f6f Mon Sep 17 00:00:00 2001 From: peterbell10 Date: Tue, 10 Sep 2019 12:31:09 +0100 Subject: Fix race condition in UrlClientTest --- tests/HTTP/UrlClientTest.cpp | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) (limited to 'tests/HTTP') diff --git a/tests/HTTP/UrlClientTest.cpp b/tests/HTTP/UrlClientTest.cpp index 206165dee..05844ca53 100644 --- a/tests/HTTP/UrlClientTest.cpp +++ b/tests/HTTP/UrlClientTest.cpp @@ -18,8 +18,8 @@ class cCallbacks: public cUrlClient::cCallbacks { public: - cCallbacks(cEvent & a_Event): - m_Event(a_Event) + cCallbacks(std::shared_ptr a_Event): + m_Event(std::move(a_Event)) { ++g_ActiveCallbacks; LOGD("Created a cCallbacks instance at %p", reinterpret_cast(this)); @@ -86,7 +86,7 @@ public: virtual void OnBodyFinished() override { LOG("Body finished."); - m_Event.Set(); + m_Event->Set(); } @@ -99,11 +99,11 @@ public: virtual void OnError(const AString & a_ErrorMsg) override { LOG("Error: %s", a_ErrorMsg.c_str()); - m_Event.Set(); + m_Event->Set(); } protected: - cEvent & m_Event; + std::shared_ptr m_Event; }; @@ -113,14 +113,14 @@ protected: int TestRequest1() { LOG("Running test 1"); - cEvent evtFinished; + auto evtFinished = std::make_shared(); auto callbacks = cpp14::make_unique(evtFinished); AStringMap options; options["MaxRedirects"] = "0"; auto res = cUrlClient::Get("http://github.com", std::move(callbacks), AStringMap(), AString(), options); if (res.first) { - evtFinished.Wait(); + evtFinished->Wait(); } else { @@ -137,12 +137,12 @@ int TestRequest1() int TestRequest2() { LOG("Running test 2"); - cEvent evtFinished; + auto evtFinished = std::make_shared(); auto callbacks = cpp14::make_unique(evtFinished); auto res = cUrlClient::Get("http://github.com", std::move(callbacks)); if (res.first) { - evtFinished.Wait(); + evtFinished->Wait(); } else { @@ -159,14 +159,14 @@ int TestRequest2() int TestRequest3() { LOG("Running test 3"); - cEvent evtFinished; + auto evtFinished = std::make_shared(); auto callbacks = cpp14::make_unique(evtFinished); AStringMap options; options["MaxRedirects"] = "0"; auto res = cUrlClient::Get("https://github.com", std::move(callbacks), AStringMap(), AString(), options); if (res.first) { - evtFinished.Wait(); + evtFinished->Wait(); } else { @@ -183,12 +183,12 @@ int TestRequest3() int TestRequest4() { LOG("Running test 4"); - cEvent evtFinished; + auto evtFinished = std::make_shared(); auto callbacks = cpp14::make_unique(evtFinished); auto res = cUrlClient::Get("https://github.com", std::move(callbacks)); if (res.first) { - evtFinished.Wait(); + evtFinished->Wait(); } else { @@ -204,14 +204,15 @@ int TestRequest4() int TestRequests() { - std::function tests[] = + using func_t = int(void); + func_t * tests[] = { &TestRequest1, &TestRequest2, &TestRequest3, &TestRequest4, }; - for (const auto & test: tests) + for (auto test: tests) { LOG("%s", AString(60, '-').c_str()); auto res = test(); -- cgit v1.2.3