added AsyncIO

This commit is contained in:
Guenter Obiltschnig
2007-06-18 07:00:47 +00:00
parent 5feefc75cd
commit 049c3b914b
21 changed files with 1682 additions and 6 deletions

View File

@@ -0,0 +1,183 @@
//
// AsyncIOChannelTest.cpp
//
// $Id: //poco/Main/Foundation/testsuite/src/AsyncIOChannelTest.cpp#1 $
//
// Copyright (c) 2007, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#include "AsyncIOChannelTest.h"
#include "CppUnit/TestCaller.h"
#include "CppUnit/TestSuite.h"
#include "Poco/AsyncStreamChannel.h"
#include "Poco/AsyncIOCommand.h"
#include "Poco/AsyncIOEvent.h"
#include "Poco/Delegate.h"
#include <sstream>
using Poco::AsyncStreamChannel;
using Poco::AsyncIOCommand;
using Poco::AsyncReadCommand;
using Poco::AsyncWriteCommand;
using Poco::AsyncSeekCommand;
using Poco::AsyncIOEvent;
using Poco::ActiveResult;
using Poco::delegate;
AsyncIOChannelTest::AsyncIOChannelTest(const std::string& name):
CppUnit::TestCase(name),
_completed(0),
_failed(0)
{
}
AsyncIOChannelTest::~AsyncIOChannelTest()
{
}
void AsyncIOChannelTest::testWrite()
{
std::stringstream str;
AsyncStreamChannel channel(str);
channel.enqueue(new AsyncWriteCommand("Hello", 5));
channel.enqueue(new AsyncWriteCommand(", ", 2));
ActiveResult<int> result = channel.enqueue(new AsyncWriteCommand("world!", 6));
result.wait();
std::string s(str.str());
assert (s == "Hello, world!");
}
void AsyncIOChannelTest::testRead()
{
std::istringstream istr("Hello, world!");
char buffer[16];
AsyncStreamChannel channel(istr);
ActiveResult<int> result = channel.enqueue(new AsyncReadCommand(buffer, sizeof(buffer)));
result.wait();
std::string s(buffer, result.data());
assert (s == "Hello, world!");
}
void AsyncIOChannelTest::testSeek()
{
std::istringstream istr("Hello, world!");
char buffer[16];
AsyncStreamChannel channel(istr);
channel.enqueue(new AsyncSeekCommand(7, std::ios::beg));
ActiveResult<int> result = channel.enqueue(new AsyncReadCommand(buffer, 5));
result.wait();
std::string s(buffer, result.data());
assert (s == "world");
}
void AsyncIOChannelTest::testEvents()
{
std::stringstream str;
AsyncStreamChannel channel(str);
AsyncIOCommand::Ptr pWrite(new AsyncWriteCommand("Hello, world!", 13));
pWrite->commandCompleted += delegate(this, &AsyncIOChannelTest::onCompleted);
channel.commandCompleted += delegate(this, &AsyncIOChannelTest::onCompleted);
assert (pWrite->state() == AsyncIOCommand::CMD_PENDING);
ActiveResult<int> result = channel.enqueue(pWrite);
pWrite->wait();
result.wait();
assert (pWrite->succeeded());
assert (!pWrite->failed());
assert (pWrite->state() == AsyncIOCommand::CMD_COMPLETED);
assert (pWrite->result() == 13);
assert (pWrite->exception() == 0);
assert (_completed == 2);
reset();
std::istringstream istr;
AsyncStreamChannel ichannel(istr);
pWrite = new AsyncWriteCommand("Hello, world!", 13);
pWrite->commandFailed += delegate(this, &AsyncIOChannelTest::onFailed);
ichannel.commandFailed += delegate(this, &AsyncIOChannelTest::onFailed);
pWrite->commandCompleted += delegate(this, &AsyncIOChannelTest::onCompleted);
ichannel.commandCompleted += delegate(this, &AsyncIOChannelTest::onCompleted);
assert (pWrite->state() == AsyncIOCommand::CMD_PENDING);
result = ichannel.enqueue(pWrite);
pWrite->wait();
result.wait();
assert (!pWrite->succeeded());
assert (pWrite->failed());
assert (pWrite->exception() != 0);
assert (_completed == 0);
assert (_failed == 2);
}
void AsyncIOChannelTest::setUp()
{
reset();
}
void AsyncIOChannelTest::tearDown()
{
}
void AsyncIOChannelTest::reset()
{
_completed = 0;
_failed = 0;
}
void AsyncIOChannelTest::onCompleted(const void* sender, Poco::AsyncIOEvent& event)
{
++_completed;
}
void AsyncIOChannelTest::onFailed(const void* sender, Poco::AsyncIOEvent& event)
{
++_failed;
}
CppUnit::Test* AsyncIOChannelTest::suite()
{
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("AsyncIOChannelTest");
CppUnit_addTest(pSuite, AsyncIOChannelTest, testWrite);
CppUnit_addTest(pSuite, AsyncIOChannelTest, testRead);
CppUnit_addTest(pSuite, AsyncIOChannelTest, testSeek);
CppUnit_addTest(pSuite, AsyncIOChannelTest, testEvents);
return pSuite;
}

View File

@@ -0,0 +1,71 @@
//
// AsyncIOChannelTest.h
//
// $Id: //poco/Main/Foundation/testsuite/src/AsyncIOChannelTest.h#1 $
//
// Definition of the AsyncIOChannelTest class.
//
// Copyright (c) 2007, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#ifndef AsyncIOChannelTest_INCLUDED
#define AsyncIOChannelTest_INCLUDED
#include "Poco/Foundation.h"
#include "CppUnit/TestCase.h"
#include "Poco/AsyncIOEvent.h"
class AsyncIOChannelTest: public CppUnit::TestCase
{
public:
AsyncIOChannelTest(const std::string& name);
~AsyncIOChannelTest();
void testWrite();
void testRead();
void testSeek();
void testEvents();
void setUp();
void tearDown();
static CppUnit::Test* suite();
protected:
void reset();
void onCompleted(const void* sender, Poco::AsyncIOEvent& event);
void onFailed(const void* sender, Poco::AsyncIOEvent& event);
private:
int _completed;
int _failed;
};
#endif // AsyncIOChannelTest_INCLUDED

View File

@@ -0,0 +1,44 @@
//
// AsyncIOTestSuite.cpp
//
// $Id: //poco/Main/Foundation/testsuite/src/AsyncIOTestSuite.cpp#1 $
//
// Copyright (c) 2007, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#include "AsyncIOTestSuite.h"
#include "AsyncIOChannelTest.h"
CppUnit::Test* AsyncIOTestSuite::suite()
{
CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("AsyncIOTestSuite");
pSuite->addTest(AsyncIOChannelTest::suite());
return pSuite;
}

View File

@@ -0,0 +1,49 @@
//
// AsyncIOTestSuite.h
//
// $Id: //poco/Main/Foundation/testsuite/src/AsyncIOTestSuite.h#1 $
//
// Definition of the AsyncIOTestSuite class.
//
// Copyright (c) 2007, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#ifndef AsyncIOTestSuite_INCLUDED
#define AsyncIOTestSuite_INCLUDED
#include "CppUnit/TestSuite.h"
class AsyncIOTestSuite
{
public:
static CppUnit::Test* suite();
};
#endif // AsyncIOTestSuite_INCLUDED

View File

@@ -1,7 +1,7 @@
//
// FoundationTestSuite.cpp
//
// $Id: //poco/Main/Foundation/testsuite/src/FoundationTestSuite.cpp#12 $
// $Id: //poco/Main/Foundation/testsuite/src/FoundationTestSuite.cpp#13 $
//
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
@@ -48,6 +48,7 @@
#include "EventTestSuite.h"
#include "CacheTestSuite.h"
#include "HashingTestSuite.h"
#include "AsyncIOTestSuite.h"
CppUnit::Test* FoundationTestSuite::suite()
@@ -71,6 +72,7 @@ CppUnit::Test* FoundationTestSuite::suite()
pSuite->addTest(EventTestSuite::suite());
pSuite->addTest(CacheTestSuite::suite());
pSuite->addTest(HashingTestSuite::suite());
pSuite->addTest(AsyncIOTestSuite::suite());
return pSuite;
}