Improving channel lifetime in HTTP-based tests.

Previously, closed channels were not cleaned up, which looks to be
the caused of the test failures on Jenkins.
This commit is contained in:
James Turner 2016-06-06 17:26:50 +01:00
parent 1492de4391
commit cb80af0ebe
2 changed files with 50 additions and 13 deletions

View File

@ -577,7 +577,7 @@ cout << "testing proxy close" << endl;
cout << "testing HTTP 1.1 pipelining" << endl; cout << "testing HTTP 1.1 pipelining" << endl;
{ {
testServer.resetConnectCount(); testServer.disconnectAll();
cl.clearAllConnections(); cl.clearAllConnections();
cl.setProxy("", 80); cl.setProxy("", 80);
@ -689,7 +689,7 @@ cout << "testing proxy close" << endl;
// test cancel // test cancel
{ {
cout << "cancel request" << endl; cout << "cancel request" << endl;
testServer.resetConnectCount(); testServer.disconnectAll();
cl.clearAllConnections(); cl.clearAllConnections();
cl.setProxy("", 80); cl.setProxy("", 80);
@ -722,7 +722,7 @@ cout << "testing proxy close" << endl;
// test cancel // test cancel
{ {
cout << "cancel middle request" << endl; cout << "cancel middle request" << endl;
testServer.resetConnectCount(); testServer.disconnectAll();
cl.clearAllConnections(); cl.clearAllConnections();
cl.setProxy("", 80); cl.setProxy("", 80);

View File

@ -2,6 +2,7 @@
#define SIMGEAR_IO_TEST_HTTP_HXX #define SIMGEAR_IO_TEST_HTTP_HXX
#include <sstream> #include <sstream>
#include <vector>
#include <simgear/io/sg_netChat.hxx> #include <simgear/io/sg_netChat.hxx>
#include <simgear/misc/strutils.hxx> #include <simgear/misc/strutils.hxx>
@ -27,6 +28,11 @@ public:
} }
virtual ~TestServerChannel()
{
std::cerr << "dtor test server channel" << std::endl;
}
virtual void collectIncomingData(const char* s, int n) virtual void collectIncomingData(const char* s, int n)
{ {
buffer += std::string(s, n); buffer += std::string(s, n);
@ -160,6 +166,13 @@ public:
} }
} }
virtual void handleClose (void)
{
std::cerr << "channel close" << std::endl;
NetBufferChannel::handleClose();
}
State state; State state;
std::string buffer; std::string buffer;
std::string method; std::string method;
@ -170,17 +183,25 @@ public:
int requestContentLength; int requestContentLength;
}; };
class EraseIfClosed
{
public:
bool operator()(simgear::NetChannel* chan) const
{
return chan->isClosed();
}
};
template <class T> template <class T>
class TestServer : public NetChannel class TestServer : public NetChannel
{ {
simgear::NetChannelPoller _poller; simgear::NetChannelPoller _poller;
int _connectCount; std::vector<T*> _channels;
public: public:
TestServer() TestServer()
{ {
Socket::initSockets(); Socket::initSockets();
_connectCount = 0;
open(); open();
bind(NULL, 2000); // localhost, any port bind(NULL, 2000); // localhost, any port
@ -199,27 +220,43 @@ public:
{ {
simgear::IPAddress addr ; simgear::IPAddress addr ;
int handle = accept ( &addr ) ; int handle = accept ( &addr ) ;
TestServerChannel* chan = new T(); T* chan = new T();
chan->setHandle(handle); chan->setHandle(handle);
_channels.push_back(chan);
_poller.addChannel(chan); _poller.addChannel(chan);
_connectCount++;
} }
void poll() void poll()
{ {
_poller.poll(); _poller.poll();
typename std::vector<T*>::iterator it;
it = std::remove_if(_channels.begin(), _channels.end(), EraseIfClosed());
for (typename std::vector<T*>::iterator it2 = it; it2 != _channels.end(); ++it2) {
_poller.removeChannel(*it2);
delete *it2;
} }
void resetConnectCount() _channels.erase(it, _channels.end());
{
_connectCount = 0;
} }
int connectCount() int connectCount()
{ {
return _connectCount; return _channels.size();
}
void disconnectAll()
{
typename std::vector<T*>::iterator it;
for (it = _channels.begin(); it != _channels.end(); ++it) {
_poller.removeChannel(*it);
delete *it;
}
_channels.clear();
} }
}; };