1
0

Fix race condition in UrlClientTest

This commit is contained in:
peterbell10 2019-09-10 12:31:09 +01:00 committed by Mattes D
parent 3bc0f07fea
commit 7678d5e6ed

View File

@ -18,8 +18,8 @@ class cCallbacks:
public cUrlClient::cCallbacks public cUrlClient::cCallbacks
{ {
public: public:
cCallbacks(cEvent & a_Event): cCallbacks(std::shared_ptr<cEvent> a_Event):
m_Event(a_Event) m_Event(std::move(a_Event))
{ {
++g_ActiveCallbacks; ++g_ActiveCallbacks;
LOGD("Created a cCallbacks instance at %p", reinterpret_cast<void *>(this)); LOGD("Created a cCallbacks instance at %p", reinterpret_cast<void *>(this));
@ -86,7 +86,7 @@ public:
virtual void OnBodyFinished() override virtual void OnBodyFinished() override
{ {
LOG("Body finished."); LOG("Body finished.");
m_Event.Set(); m_Event->Set();
} }
@ -99,11 +99,11 @@ public:
virtual void OnError(const AString & a_ErrorMsg) override virtual void OnError(const AString & a_ErrorMsg) override
{ {
LOG("Error: %s", a_ErrorMsg.c_str()); LOG("Error: %s", a_ErrorMsg.c_str());
m_Event.Set(); m_Event->Set();
} }
protected: protected:
cEvent & m_Event; std::shared_ptr<cEvent> m_Event;
}; };
@ -113,14 +113,14 @@ protected:
int TestRequest1() int TestRequest1()
{ {
LOG("Running test 1"); LOG("Running test 1");
cEvent evtFinished; auto evtFinished = std::make_shared<cEvent>();
auto callbacks = cpp14::make_unique<cCallbacks>(evtFinished); auto callbacks = cpp14::make_unique<cCallbacks>(evtFinished);
AStringMap options; AStringMap options;
options["MaxRedirects"] = "0"; options["MaxRedirects"] = "0";
auto res = cUrlClient::Get("http://github.com", std::move(callbacks), AStringMap(), AString(), options); auto res = cUrlClient::Get("http://github.com", std::move(callbacks), AStringMap(), AString(), options);
if (res.first) if (res.first)
{ {
evtFinished.Wait(); evtFinished->Wait();
} }
else else
{ {
@ -137,12 +137,12 @@ int TestRequest1()
int TestRequest2() int TestRequest2()
{ {
LOG("Running test 2"); LOG("Running test 2");
cEvent evtFinished; auto evtFinished = std::make_shared<cEvent>();
auto callbacks = cpp14::make_unique<cCallbacks>(evtFinished); auto callbacks = cpp14::make_unique<cCallbacks>(evtFinished);
auto res = cUrlClient::Get("http://github.com", std::move(callbacks)); auto res = cUrlClient::Get("http://github.com", std::move(callbacks));
if (res.first) if (res.first)
{ {
evtFinished.Wait(); evtFinished->Wait();
} }
else else
{ {
@ -159,14 +159,14 @@ int TestRequest2()
int TestRequest3() int TestRequest3()
{ {
LOG("Running test 3"); LOG("Running test 3");
cEvent evtFinished; auto evtFinished = std::make_shared<cEvent>();
auto callbacks = cpp14::make_unique<cCallbacks>(evtFinished); auto callbacks = cpp14::make_unique<cCallbacks>(evtFinished);
AStringMap options; AStringMap options;
options["MaxRedirects"] = "0"; options["MaxRedirects"] = "0";
auto res = cUrlClient::Get("https://github.com", std::move(callbacks), AStringMap(), AString(), options); auto res = cUrlClient::Get("https://github.com", std::move(callbacks), AStringMap(), AString(), options);
if (res.first) if (res.first)
{ {
evtFinished.Wait(); evtFinished->Wait();
} }
else else
{ {
@ -183,12 +183,12 @@ int TestRequest3()
int TestRequest4() int TestRequest4()
{ {
LOG("Running test 4"); LOG("Running test 4");
cEvent evtFinished; auto evtFinished = std::make_shared<cEvent>();
auto callbacks = cpp14::make_unique<cCallbacks>(evtFinished); auto callbacks = cpp14::make_unique<cCallbacks>(evtFinished);
auto res = cUrlClient::Get("https://github.com", std::move(callbacks)); auto res = cUrlClient::Get("https://github.com", std::move(callbacks));
if (res.first) if (res.first)
{ {
evtFinished.Wait(); evtFinished->Wait();
} }
else else
{ {
@ -204,14 +204,15 @@ int TestRequest4()
int TestRequests() int TestRequests()
{ {
std::function<int(void)> tests[] = using func_t = int(void);
func_t * tests[] =
{ {
&TestRequest1, &TestRequest1,
&TestRequest2, &TestRequest2,
&TestRequest3, &TestRequest3,
&TestRequest4, &TestRequest4,
}; };
for (const auto & test: tests) for (auto test: tests)
{ {
LOG("%s", AString(60, '-').c_str()); LOG("%s", AString(60, '-').c_str());
auto res = test(); auto res = test();