void ObservableTest::testAsyncGarbagCollector() { BOOST_TEST_MESSAGE("Testing observer pattern with an asynchronous " "garbage collector (JVM/.NET use case)..."); // This test core dumps if used with the ordinary implementation // of the observer pattern (comparable situation // in JVM or .NET eco systems). const ext::shared_ptr<SimpleQuote> quote(new SimpleQuote(-1.0)); GarbageCollector gc; boost::thread workerThread(&GarbageCollector::run, &gc); for (Size i=0; i < 10000; ++i) { const ext::shared_ptr<MTUpdateCounter> observer(new MTUpdateCounter); observer->registerWith(quote); gc.addObj(observer); for (Size j=0; j < 10; ++j) quote->setValue(Real(j)); } gc.terminate(); workerThread.join(); if (MTUpdateCounter::instanceCounter() != 0) { BOOST_FAIL("garbage collection does not work."); } }
void ObservableTest::testMultiThreadingGlobalSettings() { BOOST_TEST_MESSAGE("Testing observer global settings in a " "multithreading environment..."); const ext::shared_ptr<SimpleQuote> quote(new SimpleQuote(-1.0)); ObservableSettings::instance().disableUpdates(true); GarbageCollector gc; boost::thread workerThread(&GarbageCollector::run, &gc); typedef std::list<ext::shared_ptr<MTUpdateCounter> > local_list_type; local_list_type localList; for (Size i=0; i < 4000; ++i) { const ext::shared_ptr<MTUpdateCounter> observer(new MTUpdateCounter); observer->registerWith(quote); if ((i%4) == 0) { localList.push_back(observer); for (Size j=0; j < 5; ++j) quote->setValue(Real(j)); } gc.addObj(observer); } gc.terminate(); workerThread.join(); if (localList.size() != Size(MTUpdateCounter::instanceCounter())) { BOOST_FAIL("garbage collection does not work."); } for (local_list_type::iterator iter = localList.begin(); iter != localList.end(); ++iter) { if ((*iter)->counter() != 0) { BOOST_FAIL("notification should have been blocked"); } } ObservableSettings::instance().enableUpdates(); for (local_list_type::iterator iter = localList.begin(); iter != localList.end(); ++iter) { if ((*iter)->counter() != 1) { BOOST_FAIL("only one notification should have been sent"); } } }