Skip to content

Commit

Permalink
Fix race condition in UrlClientTest
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbell10 authored and madmaxoft committed Sep 10, 2019
1 parent 3bc0f07 commit 7678d5e
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions tests/HTTP/UrlClientTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class cCallbacks:
public cUrlClient::cCallbacks
{
public:
cCallbacks(cEvent & a_Event):
m_Event(a_Event)
cCallbacks(std::shared_ptr<cEvent> a_Event):
m_Event(std::move(a_Event))
{
++g_ActiveCallbacks;
LOGD("Created a cCallbacks instance at %p", reinterpret_cast<void *>(this));
Expand Down Expand Up @@ -86,7 +86,7 @@ class cCallbacks:
virtual void OnBodyFinished() override
{
LOG("Body finished.");
m_Event.Set();
m_Event->Set();
}


Expand All @@ -99,11 +99,11 @@ class cCallbacks:
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<cEvent> m_Event;
};


Expand All @@ -113,14 +113,14 @@ class cCallbacks:
int TestRequest1()
{
LOG("Running test 1");
cEvent evtFinished;
auto evtFinished = std::make_shared<cEvent>();
auto callbacks = cpp14::make_unique<cCallbacks>(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
{
Expand All @@ -137,12 +137,12 @@ int TestRequest1()
int TestRequest2()
{
LOG("Running test 2");
cEvent evtFinished;
auto evtFinished = std::make_shared<cEvent>();
auto callbacks = cpp14::make_unique<cCallbacks>(evtFinished);
auto res = cUrlClient::Get("http://github.com", std::move(callbacks));
if (res.first)
{
evtFinished.Wait();
evtFinished->Wait();
}
else
{
Expand All @@ -159,14 +159,14 @@ int TestRequest2()
int TestRequest3()
{
LOG("Running test 3");
cEvent evtFinished;
auto evtFinished = std::make_shared<cEvent>();
auto callbacks = cpp14::make_unique<cCallbacks>(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
{
Expand All @@ -183,12 +183,12 @@ int TestRequest3()
int TestRequest4()
{
LOG("Running test 4");
cEvent evtFinished;
auto evtFinished = std::make_shared<cEvent>();
auto callbacks = cpp14::make_unique<cCallbacks>(evtFinished);
auto res = cUrlClient::Get("https://github.com", std::move(callbacks));
if (res.first)
{
evtFinished.Wait();
evtFinished->Wait();
}
else
{
Expand All @@ -204,14 +204,15 @@ int TestRequest4()

int TestRequests()
{
std::function<int(void)> 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();
Expand Down

0 comments on commit 7678d5e

Please sign in to comment.