Added optional timeout parameter to core::provider::PoolTemplate::get

This commit is contained in:
MHaselmaier 2021-05-04 19:33:58 +02:00
parent 13eaca0dc7
commit c92567632e
5 changed files with 204 additions and 46 deletions

View File

@ -30,6 +30,7 @@
#include <thread>
#include <condition_variable>
#include <limits>
namespace oatpp { namespace provider {
@ -223,7 +224,7 @@ public:
{
/* "new" is called directly to keep constructor private */
auto ptr = std::shared_ptr<PoolTemplate>(new PoolTemplate(provider, maxResources, maxResourceTTL.count()));
ptr->startCleanupTask();
ptr->startCleanupTask(ptr);
return ptr;
}
@ -231,14 +232,16 @@ public:
stop();
}
std::shared_ptr<TResource> get(const std::shared_ptr<PoolTemplate>& _this) {
std::shared_ptr<TResource> get(const std::shared_ptr<PoolTemplate>& _this, const std::chrono::duration<v_int64, std::micro>& timeout = std::chrono::microseconds::max()) {
{
std::unique_lock<std::mutex> guard(m_lock);
while (m_running && m_bench.size() == 0 && m_counter >= m_maxResources ) {
m_condition.wait(guard);
auto finishedPredicate = [this]() { return !m_running || !m_bench.empty() || m_counter < m_maxResources; };
if (!m_condition.wait_for(guard, timeout, std::move(finishedPredicate)))
{
return nullptr;
}
if(!m_running) {

View File

@ -57,6 +57,8 @@ add_executable(oatppAllTests
oatpp/core/parser/CaretTest.hpp
oatpp/core/provider/PoolTest.cpp
oatpp/core/provider/PoolTest.hpp
oatpp/core/provider/PoolTemplateTest.cpp
oatpp/core/provider/PoolTemplateTest.hpp
oatpp/encoding/Base64Test.cpp
oatpp/encoding/Base64Test.hpp
oatpp/encoding/UnicodeTest.cpp

View File

@ -27,6 +27,7 @@
#include "oatpp/core/parser/CaretTest.hpp"
#include "oatpp/core/provider/PoolTest.hpp"
#include "oatpp/core/provider/PoolTemplateTest.hpp"
#include "oatpp/core/async/LockTest.hpp"
#include "oatpp/core/data/mapping/type/UnorderedMapTest.hpp"
@ -78,21 +79,21 @@ void runTests() {
OATPP_RUN_TEST(oatpp::test::base::CommandLineArgumentsTest);
OATPP_RUN_TEST(oatpp::test::base::LoggerTest);
OATPP_RUN_TEST(oatpp::test::memory::MemoryPoolTest);
OATPP_RUN_TEST(oatpp::test::memory::PerfTest);
OATPP_RUN_TEST(oatpp::test::collection::LinkedListTest);
OATPP_RUN_TEST(oatpp::test::core::data::share::MemoryLabelTest);
OATPP_RUN_TEST(oatpp::test::core::data::share::LazyStringMapTest);
OATPP_RUN_TEST(oatpp::test::core::data::share::StringTemplateTest);
OATPP_RUN_TEST(oatpp::test::core::data::buffer::ProcessorTest);
OATPP_RUN_TEST(oatpp::test::core::data::stream::ChunkedBufferTest);
OATPP_RUN_TEST(oatpp::test::core::data::stream::BufferStreamTest);
OATPP_RUN_TEST(oatpp::test::core::data::mapping::type::ObjectWrapperTest);
OATPP_RUN_TEST(oatpp::test::core::data::mapping::type::TypeTest);
OATPP_RUN_TEST(oatpp::test::core::data::mapping::type::StringTest);
@ -105,99 +106,100 @@ void runTests() {
OATPP_RUN_TEST(oatpp::test::core::data::mapping::type::AnyTest);
OATPP_RUN_TEST(oatpp::test::core::data::mapping::type::EnumTest);
OATPP_RUN_TEST(oatpp::test::core::data::mapping::type::ObjectTest);
OATPP_RUN_TEST(oatpp::test::core::data::mapping::type::InterpretationTest);
OATPP_RUN_TEST(oatpp::test::core::data::mapping::TypeResolverTest);
OATPP_RUN_TEST(oatpp::test::async::LockTest);
OATPP_RUN_TEST(oatpp::test::parser::CaretTest);
OATPP_RUN_TEST(oatpp::test::core::provider::PoolTest);
OATPP_RUN_TEST(oatpp::test::core::provider::PoolTemplateTest);
OATPP_RUN_TEST(oatpp::test::parser::json::mapping::EnumTest);
OATPP_RUN_TEST(oatpp::test::parser::json::mapping::UnorderedSetTest);
OATPP_RUN_TEST(oatpp::test::parser::json::mapping::DeserializerTest);
OATPP_RUN_TEST(oatpp::test::parser::json::mapping::DTOMapperPerfTest);
OATPP_RUN_TEST(oatpp::test::parser::json::mapping::DTOMapperTest);
OATPP_RUN_TEST(oatpp::test::encoding::Base64Test);
OATPP_RUN_TEST(oatpp::test::encoding::UnicodeTest);
OATPP_RUN_TEST(oatpp::test::network::UrlTest);
OATPP_RUN_TEST(oatpp::test::network::ConnectionPoolTest);
OATPP_RUN_TEST(oatpp::test::network::virtual_::PipeTest);
OATPP_RUN_TEST(oatpp::test::network::virtual_::InterfaceTest);
OATPP_RUN_TEST(oatpp::test::web::protocol::http::encoding::ChunkedTest);
OATPP_RUN_TEST(oatpp::test::web::mime::multipart::StatefulParserTest);
OATPP_RUN_TEST(oatpp::test::web::server::HttpRouterTest);
OATPP_RUN_TEST(oatpp::test::web::server::api::ApiControllerTest);
OATPP_RUN_TEST(oatpp::test::web::server::handler::AuthorizationHandlerTest);
{
oatpp::test::web::PipelineTest test_virtual(0, 3000);
test_virtual.run();
oatpp::test::web::PipelineTest test_port(8000, 3000);
test_port.run();
}
{
oatpp::test::web::PipelineAsyncTest test_virtual(0, 3000);
test_virtual.run();
oatpp::test::web::PipelineAsyncTest test_port(8000, 3000);
test_port.run();
}
{
oatpp::test::web::FullTest test_virtual(0, 1000);
test_virtual.run();
oatpp::test::web::FullTest test_port(8000, 5);
test_port.run();
}
{
oatpp::test::web::FullAsyncTest test_virtual(0, 1000);
test_virtual.run();
oatpp::test::web::FullAsyncTest test_port(8000, 5);
test_port.run();
}
{
oatpp::test::web::FullAsyncClientTest test_virtual(0, 1000);
test_virtual.run(20);
oatpp::test::web::FullAsyncClientTest test_port(8000, 5);
test_port.run(1);
}
{
oatpp::test::web::ClientRetryTest test_virtual(0);
test_virtual.run();
oatpp::test::web::ClientRetryTest test_port(8000);
test_port.run();
}
}

View File

@ -0,0 +1,108 @@
/***************************************************************************
*
* Project _____ __ ____ _ _
* ( _ ) /__\ (_ _)_| |_ _| |_
* )(_)( /(__)\ )( (_ _)(_ _)
* (_____)(__)(__)(__) |_| |_|
*
*
* Copyright 2018-present, Leonid Stryzhevskyi <lganzzzo@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
***************************************************************************/
#include "PoolTemplateTest.hpp"
#include <future>
#include "oatpp/core/provider/Pool.hpp"
namespace oatpp { namespace test { namespace core { namespace provider {
namespace {
struct Resource {
};
class Provider : public oatpp::provider::Provider<Resource> {
public:
std::shared_ptr<Resource> get() override {
return std::make_shared<Resource>();
}
async::CoroutineStarterForResult<const std::shared_ptr<Resource> &> getAsync() override {
class GetCoroutine : public oatpp::async::CoroutineWithResult<GetCoroutine, const std::shared_ptr<Resource>&> {
private:
Provider* m_provider;
public:
GetCoroutine(Provider* provider)
: m_provider(provider)
{}
Action act() override {
return _return(std::make_shared<Resource>());
}
};
return GetCoroutine::startForResult(this);
}
void invalidate(const std::shared_ptr<Resource>& resource) override {
(void) resource;
}
void stop() override {
OATPP_LOGD("Provider", "stop()");
}
};
struct AcquisitionProxy : public oatpp::provider::AcquisitionProxy<Resource, AcquisitionProxy> {
AcquisitionProxy(const std::shared_ptr<Resource>& resource, const std::shared_ptr<PoolInstance>& pool)
: oatpp::provider::AcquisitionProxy<Resource, AcquisitionProxy>(resource, pool)
{}
};
typedef oatpp::provider::PoolTemplate<Resource, AcquisitionProxy> PoolTemplate;
}
void PoolTemplateTest::onRun() {
const auto provider = std::make_shared<Provider>();
const v_int64 maxResources = 1;
auto poolTemplate = PoolTemplate::createShared(provider, maxResources, std::chrono::seconds(1));
std::shared_ptr<Resource> resource = poolTemplate->get(poolTemplate);
OATPP_ASSERT(resource != nullptr);
OATPP_ASSERT(poolTemplate->get(poolTemplate, std::chrono::milliseconds(500)) == nullptr);
std::future<std::shared_ptr<Resource>> futureResource = std::async(std::launch::async, [&poolTemplate]() {
return poolTemplate->get(poolTemplate);
});
OATPP_ASSERT(futureResource.wait_for(std::chrono::seconds(1)) == std::future_status::timeout);
poolTemplate->stop();
OATPP_ASSERT(poolTemplate->get(poolTemplate, std::chrono::milliseconds(500)) == nullptr);
OATPP_ASSERT(poolTemplate->get(poolTemplate) == nullptr);
}
}}}}

View File

@ -0,0 +1,43 @@
/***************************************************************************
*
* Project _____ __ ____ _ _
* ( _ ) /__\ (_ _)_| |_ _| |_
* )(_)( /(__)\ )( (_ _)(_ _)
* (_____)(__)(__)(__) |_| |_|
*
*
* Copyright 2018-present, Leonid Stryzhevskyi <lganzzzo@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
***************************************************************************/
#ifndef oatpp_test_provider_PoolTemplateTest_hpp
#define oatpp_test_provider_PoolTemplateTest_hpp
#include "oatpp-test/UnitTest.hpp"
namespace oatpp { namespace test { namespace core { namespace provider {
class PoolTemplateTest : public UnitTest {
public:
PoolTemplateTest() :UnitTest("TEST[provider::PoolTemplateTest]") {}
void onRun() override;
};
}}}}
#endif //oatpp_test_provider_PoolTemplateTest_hpp