Commit cfff1dce authored by Flavien BRIDAULT-LOUCHEZ's avatar Flavien BRIDAULT-LOUCHEZ
Browse files

merge(fix/fwMemoryTest): into dev

parents e9fc6c61 ae7ea102
......@@ -86,6 +86,9 @@ std::shared_future<void> BufferManager::unregisterBuffer(BufferManager::BufferPt
void BufferManager::unregisterBufferImpl(BufferManager::BufferPtrType bufferPtr)
{
auto& bufferInfo = m_bufferInfos[bufferPtr];
OSLM_ASSERT("There is still " << bufferInfo.lockCount() << " locks on this BufferObject (" << this << ")",
bufferInfo.lockCounter.expired());
m_bufferInfos.erase(bufferPtr);
m_updatedSig->asyncEmit();
......
/* ***** BEGIN LICENSE BLOCK *****
* FW4SPL - Copyright (C) IRCAD, 2009-2017.
* FW4SPL - Copyright (C) IRCAD, 2009-2018.
* Distributed under the terms of the GNU Lesser General Public License (LGPL) as
* published by the Free Software Foundation.
* ****** END LICENSE BLOCK ****** */
......@@ -12,7 +12,7 @@ fwCampImplementMacro((fwMemory)(BufferObject))
{
builder.tag("buffer")
.function("classname", &::fwMemory::BufferObject::className)
.function("is_a", (bool(::fwMemory::BufferObject::*)(const std::string &) const) &::fwMemory::BufferObject::isA);
.function("is_a", (bool(::fwMemory::BufferObject::*)(const std::string&) const) & ::fwMemory::BufferObject::isA);
}
namespace fwMemory
......@@ -31,8 +31,8 @@ BufferObject::BufferObject() :
BufferObject::~BufferObject()
{
OSLM_ASSERT("There is still " << m_count.use_count() << " locks on this BufferObject (" << this << ")",
m_count.expired());
// In the past we asserted that m_count was expired, but it can not be ensured because the unlock is asynchronous
// So we simply unregister the buffer and we will check the counter value on the buffer manager thread instead
m_bufferManager->unregisterBuffer(&m_buffer).get();
}
......@@ -119,4 +119,3 @@ void BufferObject::setIStreamFactory(const SPTR(::fwMemory::stream::in::IFactory
}
} //namespace fwMemory
/* ***** BEGIN LICENSE BLOCK *****
* FW4SPL - Copyright (C) IRCAD, 2009-2015.
* FW4SPL - Copyright (C) IRCAD, 2009-2018.
* Distributed under the terms of the GNU Lesser General Public License (LGPL) as
* published by the Free Software Foundation.
* ****** END LICENSE BLOCK ****** */
#include <fwMemory/BufferManager.hpp>
#include <fwMemory/BufferObject.hpp>
#include "BufferManagerTest.hpp"
#include <fwMemory/BufferManager.hpp>
#include <fwMemory/BufferObject.hpp>
#include "BufferManagerTest.hpp"
#include <fwTest/helper/wait.hpp>
// Registers the fixture into the 'registry'
CPPUNIT_TEST_SUITE_REGISTRATION( ::fwMemory::ut::BufferManagerTest );
......@@ -19,16 +19,22 @@ namespace fwMemory
namespace ut
{
//------------------------------------------------------------------------------
void BufferManagerTest::setUp()
{
// Set up context before running a test.
}
//------------------------------------------------------------------------------
void BufferManagerTest::tearDown()
{
// Clean up after the test run.
}
//------------------------------------------------------------------------------
void BufferManagerTest::allocateTest()
{
::fwMemory::BufferManager::sptr manager = ::fwMemory::BufferManager::getDefault();
......@@ -45,14 +51,19 @@ void BufferManagerTest::allocateTest()
CPPUNIT_ASSERT_EQUAL( static_cast< ::fwMemory::BufferObject::SizeType>(SIZE), bo->getSize() );
CPPUNIT_ASSERT( bo->lock().getBuffer() != NULL );
// We need to wait before checking that the buffer was unlocked because all buffer operations are done on a worker.
// The actual buffer ref count might still be owned (as a std::promise) by the worker task when we reach this point.
fwTestWaitMacro(bo->lockCount() == 0);
CPPUNIT_ASSERT_EQUAL( static_cast<long>(0), bo->lockCount() );
{
::fwMemory::BufferObject::Lock lock(bo->lock());
fwTestWaitMacro(bo->lockCount() == 1);
CPPUNIT_ASSERT_EQUAL( static_cast<long>(1), bo->lockCount() );
char *buf = static_cast<char*>(lock.getBuffer());
char* buf = static_cast<char*>(lock.getBuffer());
for (int i = 0; i<SIZE; ++i)
for (int i = 0; i < SIZE; ++i)
{
buf[i] = (i%256);
}
......@@ -60,26 +71,32 @@ void BufferManagerTest::allocateTest()
{
::fwMemory::BufferObject::Lock lock(bo->lock());
char *buf = static_cast<char*>(lock.getBuffer());
char* buf = static_cast<char*>(lock.getBuffer());
for (int i = 0; i<SIZE; ++i)
for (int i = 0; i < SIZE; ++i)
{
CPPUNIT_ASSERT_EQUAL(static_cast<char>(i%256), buf[i]);
}
}
fwTestWaitMacro(bo->lockCount() == 0);
CPPUNIT_ASSERT_EQUAL( static_cast<long>(0), bo->lockCount() );
{
::fwMemory::BufferObject::Lock lock(bo->lock());
fwTestWaitMacro(bo->lockCount() == 1);
CPPUNIT_ASSERT_EQUAL( static_cast<long>(1), bo->lockCount() );
::fwMemory::BufferObject::Lock lock2(bo->lock());
fwTestWaitMacro(bo->lockCount() == 2);
CPPUNIT_ASSERT_EQUAL( static_cast<long>(2), bo->lockCount() );
::fwMemory::BufferObject::csptr cbo = bo;
::fwMemory::BufferObject::ConstLock clock(cbo->lock());
fwTestWaitMacro(bo->lockCount() == 3);
CPPUNIT_ASSERT_EQUAL( static_cast<long>(3), bo->lockCount() );
}
fwTestWaitMacro(bo->lockCount() == 0);
CPPUNIT_ASSERT_EQUAL( static_cast<long>(0), bo->lockCount() );
bo->destroy();
......@@ -87,8 +104,6 @@ void BufferManagerTest::allocateTest()
CPPUNIT_ASSERT( bo->isEmpty() );
CPPUNIT_ASSERT( bo->lock().getBuffer() == NULL );
CPPUNIT_ASSERT( bo->isEmpty() );
CPPUNIT_ASSERT( bo->lock().getBuffer() == NULL );
......@@ -98,12 +113,11 @@ void BufferManagerTest::allocateTest()
CPPUNIT_ASSERT_EQUAL( static_cast< ::fwMemory::BufferObject::SizeType>(SIZE), bo->getSize() );
CPPUNIT_ASSERT( bo->lock().getBuffer() != NULL );
{
::fwMemory::BufferObject::Lock lock(bo->lock());
char *buf = static_cast<char*>(lock.getBuffer());
char* buf = static_cast<char*>(lock.getBuffer());
for (int i = 0; i<SIZE; ++i)
for (int i = 0; i < SIZE; ++i)
{
buf[i] = (i%256);
}
......@@ -111,22 +125,22 @@ void BufferManagerTest::allocateTest()
{
::fwMemory::BufferObject::Lock lock(bo->lock());
char *buf = static_cast<char*>(lock.getBuffer());
char* buf = static_cast<char*>(lock.getBuffer());
for (int i = 0; i<SIZE; ++i)
for (int i = 0; i < SIZE; ++i)
{
CPPUNIT_ASSERT_EQUAL(static_cast<char>(i%256), buf[i]);
}
}
bo->destroy();
CPPUNIT_ASSERT( bo->isEmpty() );
CPPUNIT_ASSERT( bo->lock().getBuffer() == NULL );
}
//------------------------------------------------------------------------------
void BufferManagerTest::memoryInfoTest()
{
::fwMemory::BufferManager::sptr manager = ::fwMemory::BufferManager::getDefault();
......@@ -154,7 +168,7 @@ void BufferManagerTest::memoryInfoTest()
SLM_INFO(manager->toString().get());
bo1->allocate(SIZE);
bo2->allocate(SIZE);
char * buff = new char[SIZE];
char* buff = new char[SIZE];
bo->setBuffer( buff, SIZE, ::fwMemory::BufferNewPolicy::New() );
SLM_INFO(manager->toString().get());
......@@ -167,4 +181,3 @@ void BufferManagerTest::memoryInfoTest()
} // namespace ut
} // namespace fwMemory
/* ***** BEGIN LICENSE BLOCK *****
* FW4SPL - Copyright (C) IRCAD, 2009-2017.
* FW4SPL - Copyright (C) IRCAD, 2009-2018.
* Distributed under the terms of the GNU Lesser General Public License (LGPL) as
* published by the Free Software Foundation.
* ****** END LICENSE BLOCK ****** */
......@@ -10,10 +10,14 @@
#include <fwMemory/BufferObject.hpp>
#include <fwMemory/exception/Memory.hpp>
#include <fwTest/helper/wait.hpp>
#include <boost/thread/thread.hpp>
#include <chrono>
#include <functional>
#include <limits>
#include <thread>
#include <type_traits>
// Registers the fixture into the 'registry'
......@@ -61,10 +65,14 @@ void BufferObjectTest::allocateTest()
CPPUNIT_ASSERT_EQUAL( static_cast< ::fwMemory::BufferObject::SizeType>(SIZE), bo->getSize() );
CPPUNIT_ASSERT( bo->lock().getBuffer() != NULL );
// We need to wait before checking that the buffer was unlocked because all buffer operations are done on a worker.
// The actual buffer ref count might still be owned (as a std::promise) by the worker task when we reach this point.
fwTestWaitMacro(bo->lockCount() == 0);
CPPUNIT_ASSERT_EQUAL( static_cast<long>(0), bo->lockCount() );
{
::fwMemory::BufferObject::Lock lock(bo->lock());
fwTestWaitMacro(bo->lockCount() == 1);
CPPUNIT_ASSERT_EQUAL( static_cast<long>(1), bo->lockCount() );
char* buf = static_cast<char*>(lock.getBuffer());
......@@ -84,20 +92,26 @@ void BufferObjectTest::allocateTest()
}
}
fwTestWaitMacro(bo->lockCount() == 0);
CPPUNIT_ASSERT_EQUAL( static_cast<long>(0), bo->lockCount() );
{
::fwMemory::BufferObject::Lock lock(bo->lock());
fwTestWaitMacro(bo->lockCount() == 1);
CPPUNIT_ASSERT_EQUAL( static_cast<long>(1), bo->lockCount() );
::fwMemory::BufferObject::Lock lock2(bo->lock());
fwTestWaitMacro(bo->lockCount() == 2);
CPPUNIT_ASSERT_EQUAL( static_cast<long>(2), bo->lockCount() );
::fwMemory::BufferObject::csptr cbo = bo;
::fwMemory::BufferObject::ConstLock clock(cbo->lock());
fwTestWaitMacro(bo->lockCount() == 3);
CPPUNIT_ASSERT_EQUAL( static_cast<long>(3), bo->lockCount() );
CPPUNIT_ASSERT( isPointedValueConst( clock.getBuffer() ) );
CPPUNIT_ASSERT( isPointedValueConst( cbo->lock().getBuffer() ) );
}
fwTestWaitMacro(bo->lockCount() == 0);
CPPUNIT_ASSERT_EQUAL( static_cast<long>(0), bo->lockCount() );
bo->destroy();
......@@ -209,6 +223,7 @@ void stressLock(::fwMemory::BufferObject::sptr bo, int nbLocks, int nbTest)
m_locks.push_back(bo->lock());
}
fwTestWaitMacro(bo->lockCount() >= 3);
CPPUNIT_ASSERT( bo->lockCount() >= static_cast<long>(3) );
m_locks.clear();
......@@ -231,10 +246,10 @@ void BufferObjectTest::lockThreadedStressTest()
group.join_all();
fwTestWaitMacro(bo->lockCount() == 0);
CPPUNIT_ASSERT_EQUAL( static_cast<long>(0), bo->lockCount() );
}
} // namespace ut
} // namespace fwMemory
......@@ -24,6 +24,8 @@
#include <fwRuntime/EConfigurationElement.hpp>
#include <fwRuntime/helper.hpp>
#include <fwTest/helper/wait.hpp>
#include <fwThread/Worker.hpp>
#include <thread>
......@@ -282,16 +284,6 @@ TestServiceSignals::~TestServiceSignals()
{
}
// Wait at worst 1s for a given condition
#define WAIT(cond) \
::fwCore::TimeStamp BOOST_PP_CAT(timeStamp, __LINE__); \
BOOST_PP_CAT(timeStamp, __LINE__).setLifePeriod(1000); \
BOOST_PP_CAT(timeStamp, __LINE__).modified(); \
while(!(cond) && !BOOST_PP_CAT(timeStamp, __LINE__).periodExpired()) \
{ \
std::this_thread::sleep_for( std::chrono::milliseconds(10)); \
}
//------------------------------------------------------------------------------
void ServiceTest::testCommunication()
......@@ -348,7 +340,7 @@ void ServiceTest::testCommunication()
CPPUNIT_ASSERT(service1->isStarted());
CPPUNIT_ASSERT(service2->isStarted());
WAIT(receiver1->m_started && receiver2->m_started)
fwTestWaitMacro(receiver1->m_started && receiver2->m_started)
CPPUNIT_ASSERT_EQUAL(true, receiver1->m_started);
CPPUNIT_ASSERT_EQUAL(false, receiver1->m_updated);
CPPUNIT_ASSERT_EQUAL(false, receiver1->m_stopped);
......@@ -379,7 +371,7 @@ void ServiceTest::testCommunication()
service2->update().wait();
CPPUNIT_ASSERT(service2->getIsUpdated2());
WAIT(receiver1->m_updated && receiver2->m_updated)
fwTestWaitMacro(receiver1->m_updated && receiver2->m_updated)
CPPUNIT_ASSERT_EQUAL(true, receiver1->m_started);
CPPUNIT_ASSERT_EQUAL(true, receiver1->m_updated);
CPPUNIT_ASSERT_EQUAL(false, receiver1->m_stopped);
......@@ -391,7 +383,7 @@ void ServiceTest::testCommunication()
service1->stop().wait();
service2->stop().wait();
WAIT(receiver1->m_stopped && receiver2->m_stopped)
fwTestWaitMacro(receiver1->m_stopped && receiver2->m_stopped)
CPPUNIT_ASSERT_EQUAL(true, receiver1->m_started);
CPPUNIT_ASSERT_EQUAL(true, receiver1->m_updated);
CPPUNIT_ASSERT_EQUAL(true, receiver1->m_stopped);
......
/* ***** BEGIN LICENSE BLOCK *****
* FW4SPL - Copyright (C) IRCAD, 2018.
* Distributed under the terms of the GNU Lesser General Public License (LGPL) as
* published by the Free Software Foundation.
* ****** END LICENSE BLOCK ****** */
#pragma once
#include <fwCore/TimeStamp.hpp>
#include <chrono>
#include <thread>
// Wait at worst 1s for a given condition
#define fwTestWaitMacro(cond) \
::fwCore::TimeStamp BOOST_PP_CAT(timeStamp, __LINE__); \
BOOST_PP_CAT(timeStamp, __LINE__).setLifePeriod(1000); \
BOOST_PP_CAT(timeStamp, __LINE__).modified(); \
while(!(cond) && !BOOST_PP_CAT(timeStamp, __LINE__).periodExpired()) \
{ \
std::this_thread::sleep_for( std::chrono::milliseconds(10)); \
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment