mirror of
https://github.com/pocoproject/poco.git
synced 2025-03-02 20:30:11 +01:00
SplitterChannel::addChannel() should only add a channel once
SplitterChannel::addChannel() should only add a channel once to the internal vector. This prevents issues where the channel is accidentally added twice but only removed once because removeChannel stops at the first result. (#4270)
This commit is contained in:
parent
2e608624c8
commit
39e35c316d
@ -43,21 +43,24 @@ void SplitterChannel::addChannel(Channel::Ptr pChannel)
|
|||||||
poco_check_ptr(pChannel);
|
poco_check_ptr(pChannel);
|
||||||
|
|
||||||
FastMutex::ScopedLock lock(_mutex);
|
FastMutex::ScopedLock lock(_mutex);
|
||||||
|
|
||||||
|
// ensure that the channel is only added once
|
||||||
|
if (std::find(_channels.begin(), _channels.end(), pChannel) == _channels.end())
|
||||||
|
{
|
||||||
_channels.push_back(pChannel);
|
_channels.push_back(pChannel);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void SplitterChannel::removeChannel(Channel::Ptr pChannel)
|
void SplitterChannel::removeChannel(Channel::Ptr pChannel)
|
||||||
{
|
{
|
||||||
FastMutex::ScopedLock lock(_mutex);
|
FastMutex::ScopedLock lock(_mutex);
|
||||||
|
|
||||||
for (ChannelVec::iterator it = _channels.begin(); it != _channels.end(); ++it)
|
const auto it = std::find(_channels.begin(), _channels.end(), pChannel);
|
||||||
{
|
|
||||||
if (*it == pChannel)
|
if (it != _channels.end())
|
||||||
{
|
{
|
||||||
_channels.erase(it);
|
_channels.erase(it);
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,16 +84,34 @@ ChannelTest::~ChannelTest()
|
|||||||
|
|
||||||
|
|
||||||
void ChannelTest::testSplitter()
|
void ChannelTest::testSplitter()
|
||||||
|
{
|
||||||
|
AutoPtr<TestChannel> pChannel1 = new TestChannel;
|
||||||
|
AutoPtr<TestChannel> pChannel2 = new TestChannel;
|
||||||
|
AutoPtr<SplitterChannel> pSplitter = new SplitterChannel;
|
||||||
|
pSplitter->addChannel(pChannel1);
|
||||||
|
pSplitter->addChannel(pChannel2);
|
||||||
|
Message msg;
|
||||||
|
pSplitter->log(msg);
|
||||||
|
assertTrue(pChannel1->list().size() == 1);
|
||||||
|
assertTrue(pChannel2->list().size() == 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ChannelTest::testSplitterAddSameChannelTwice()
|
||||||
{
|
{
|
||||||
AutoPtr<TestChannel> pChannel = new TestChannel;
|
AutoPtr<TestChannel> pChannel = new TestChannel;
|
||||||
AutoPtr<SplitterChannel> pSplitter = new SplitterChannel;
|
AutoPtr<SplitterChannel> pSplitter = new SplitterChannel;
|
||||||
pSplitter->addChannel(pChannel);
|
pSplitter->addChannel(pChannel);
|
||||||
pSplitter->addChannel(pChannel);
|
pSplitter->addChannel(pChannel);
|
||||||
|
|
||||||
|
assertTrue(pSplitter->count() == 1);
|
||||||
|
|
||||||
Message msg;
|
Message msg;
|
||||||
pSplitter->log(msg);
|
pSplitter->log(msg);
|
||||||
assertTrue (pChannel->list().size() == 2);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
pSplitter->removeChannel(pChannel);
|
||||||
|
|
||||||
|
assertTrue(pSplitter->count() == 0);
|
||||||
|
}
|
||||||
|
|
||||||
void ChannelTest::testAsync()
|
void ChannelTest::testAsync()
|
||||||
{
|
{
|
||||||
@ -162,6 +180,7 @@ CppUnit::Test* ChannelTest::suite()
|
|||||||
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("ChannelTest");
|
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("ChannelTest");
|
||||||
|
|
||||||
CppUnit_addTest(pSuite, ChannelTest, testSplitter);
|
CppUnit_addTest(pSuite, ChannelTest, testSplitter);
|
||||||
|
CppUnit_addTest(pSuite, ChannelTest, testSplitterAddSameChannelTwice);
|
||||||
CppUnit_addTest(pSuite, ChannelTest, testAsync);
|
CppUnit_addTest(pSuite, ChannelTest, testAsync);
|
||||||
CppUnit_addTest(pSuite, ChannelTest, testFormatting);
|
CppUnit_addTest(pSuite, ChannelTest, testFormatting);
|
||||||
CppUnit_addTest(pSuite, ChannelTest, testConsole);
|
CppUnit_addTest(pSuite, ChannelTest, testConsole);
|
||||||
|
@ -25,6 +25,7 @@ public:
|
|||||||
~ChannelTest();
|
~ChannelTest();
|
||||||
|
|
||||||
void testSplitter();
|
void testSplitter();
|
||||||
|
void testSplitterAddSameChannelTwice();
|
||||||
void testAsync();
|
void testAsync();
|
||||||
void testFormatting();
|
void testFormatting();
|
||||||
void testConsole();
|
void testConsole();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user