chore(JSON): add stringify unicode tests #4707 (#4720)

* chore(JSON): add stringify unicode tests #4707

* enh(Mutex): Error code for pthread_mutex_lock failure #4712
This commit is contained in:
Aleksandar Fabijanic 2024-10-14 06:48:45 -05:00 committed by GitHub
parent 8e958f685f
commit 71a085c1dc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 88 additions and 47 deletions

View File

@ -43,6 +43,9 @@ public:
static std::string getMessage(int errorCode); static std::string getMessage(int errorCode);
/// Utility function translating numeric error code to string. /// Utility function translating numeric error code to string.
#endif #endif
static std::string getLastMessage();
/// Utility function returning the last error message.
}; };

View File

@ -20,8 +20,8 @@
#include "Poco/Foundation.h" #include "Poco/Foundation.h"
#include "Poco/Exception.h" #include "Poco/Exception.h"
#include "Poco/Error.h"
#include <pthread.h> #include <pthread.h>
#include <errno.h>
namespace Poco { namespace Poco {
@ -56,8 +56,9 @@ protected:
// //
inline void MutexImpl::lockImpl() inline void MutexImpl::lockImpl()
{ {
if (pthread_mutex_lock(&_mutex)) int rc;
throw SystemException("cannot lock mutex"); if ((rc = pthread_mutex_lock(&_mutex)))
throw SystemException("cannot lock mutex", Error::getMessage(rc));
} }
@ -69,14 +70,15 @@ inline bool MutexImpl::tryLockImpl()
else if (rc == EBUSY) else if (rc == EBUSY)
return false; return false;
else else
throw SystemException("cannot lock mutex"); throw SystemException("cannot lock mutex", Error::getMessage(rc));
} }
inline void MutexImpl::unlockImpl() inline void MutexImpl::unlockImpl()
{ {
if (pthread_mutex_unlock(&_mutex)) int rc;
throw SystemException("cannot unlock mutex"); if ((rc = pthread_mutex_unlock(&_mutex)))
throw SystemException("cannot unlock mutex", Error::getMessage(rc));
} }

View File

@ -20,6 +20,7 @@
#include "Poco/Foundation.h" #include "Poco/Foundation.h"
#include "Poco/Exception.h" #include "Poco/Exception.h"
#include "Poco/Error.h"
#include "Poco/UnWindows.h" #include "Poco/UnWindows.h"
@ -55,7 +56,7 @@ inline void MutexImpl::lockImpl()
} }
catch (...) catch (...)
{ {
throw SystemException("cannot lock mutex"); throw SystemException("cannot lock mutex", Error::getLastMessage());
} }
} }
@ -69,7 +70,7 @@ inline bool MutexImpl::tryLockImpl()
catch (...) catch (...)
{ {
} }
throw SystemException("cannot lock mutex"); throw SystemException("cannot lock mutex", Error::getLastMessage());
} }

View File

@ -20,8 +20,8 @@
#include "Poco/Foundation.h" #include "Poco/Foundation.h"
#include "Poco/Exception.h" #include "Poco/Exception.h"
#include "Poco/Error.h"
#include <pthread.h> #include <pthread.h>
#include <errno.h>
namespace Poco { namespace Poco {
@ -48,8 +48,9 @@ private:
// //
inline void RWLockImpl::readLockImpl() inline void RWLockImpl::readLockImpl()
{ {
if (pthread_mutex_lock(&_mutex)) int rc = 0;
throw SystemException("cannot lock mutex"); if ((rc = pthread_mutex_lock(&_mutex)))
throw SystemException("cannot lock mutex", Error::getMessage(rc));
} }
@ -61,7 +62,7 @@ inline bool RWLockImpl::tryReadLockImpl()
else if (rc == EBUSY) else if (rc == EBUSY)
return false; return false;
else else
throw SystemException("cannot lock mutex"); throw SystemException("cannot lock mutex", Error::getMessage(rc));
} }
@ -81,8 +82,9 @@ inline bool RWLockImpl::tryWriteLockImpl()
inline void RWLockImpl::unlockImpl() inline void RWLockImpl::unlockImpl()
{ {
if (pthread_mutex_unlock(&_mutex)) int rc = 0;
throw SystemException("cannot unlock mutex"); if ((rc = pthread_mutex_unlock(&_mutex)))
throw SystemException("cannot unlock mutex", Error::getMessage(rc));
} }

View File

@ -102,8 +102,13 @@ namespace Poco {
return helper.message(); return helper.message();
} }
#endif #endif
std::string Error::getLastMessage()
{
return getMessage(last());
}
} // namespace Poco } // namespace Poco

View File

@ -131,7 +131,7 @@ bool MutexImpl::tryLockImpl(long milliseconds)
else if (rc == ETIMEDOUT) else if (rc == ETIMEDOUT)
return false; return false;
else else
throw SystemException("cannot lock mutex"); throw SystemException("cannot lock mutex", Error::getMessage(rc));
#else #else
const int sleepMillis = 5; const int sleepMillis = 5;
Timestamp now; Timestamp now;
@ -142,7 +142,7 @@ bool MutexImpl::tryLockImpl(long milliseconds)
if (rc == 0) if (rc == 0)
return true; return true;
else if (rc != EBUSY) else if (rc != EBUSY)
throw SystemException("cannot lock mutex"); throw SystemException("cannot lock mutex", Error::getMessage(rc));
#if defined(POCO_VXWORKS) #if defined(POCO_VXWORKS)
struct timespec ts; struct timespec ts;
ts.tv_sec = 0; ts.tv_sec = 0;

View File

@ -47,7 +47,7 @@ bool MutexImpl::tryLockImpl(long milliseconds)
} }
catch (...) catch (...)
{ {
throw SystemException("cannot lock mutex"); throw SystemException("cannot lock mutex", Error::getLastMessage());
} }
Sleep(sleepMillis); Sleep(sleepMillis);
} }

View File

@ -1583,6 +1583,34 @@ void JSONTest::testStringify()
" }\n" " }\n"
"}"; "}";
assertTrue (ostr.str() == str); assertTrue (ostr.str() == str);
{
std::string jsonStr = R"json({"default":"\u0007\u0007"})json";
auto jsonStrUnescape = Poco::UTF8::unescape(jsonStr);
Poco::JSON::Parser parser;
Poco::Dynamic::Var result = parser.parse(jsonStr);
const auto & obj = result.extract<Poco::JSON::Object::Ptr>();
auto default_val = obj->get("default");
Poco::JSON::Object::Ptr json = new Poco::JSON::Object();
json->set("default", default_val);
std::stringstream ss;
json->stringify(ss);
assertEqual(ss.str(), jsonStr);
}
{
std::string jsonStr = R"json({"default":"\u0050\u0050"})json";
auto jsonStrUnescape = Poco::UTF8::unescape(jsonStr);
Poco::JSON::Parser parser;
Poco::Dynamic::Var result = parser.parse(jsonStr);
const auto & obj = result.extract<Poco::JSON::Object::Ptr>();
auto default_val = obj->get("default");
Poco::JSON::Object::Ptr json = new Poco::JSON::Object();
json->set("default", default_val);
std::stringstream ss;
json->stringify(ss);
assertEqual(ss.str(), jsonStrUnescape);
}
} }