Example #1
0
void TCPServerTest::testThreadCapacity(){
	ServerSocket svs(0);
	TCPServerParams* pParams = new TCPServerParams;
	pParams->setMaxThreads(64);
	TCPServer srv(new TCPServerConnectionFactoryImpl<EchoConnection>(), svs, pParams);
	srv.start();
	assert (srv.maxThreads() >= 64);
}
Example #2
0
void TCPServerTest::testMultiConnections()
{
	SecureServerSocket svs(0);
	TCPServerParams* pParams = new TCPServerParams;
	pParams->setMaxThreads(4);
	pParams->setMaxQueued(4);
	pParams->setThreadIdleTime(100);
	TCPServer srv(new TCPServerConnectionFactoryImpl<EchoConnection>(), svs, pParams);
	srv.start();
	assert (srv.currentConnections() == 0);
	assert (srv.currentThreads() == 0);
	assert (srv.queuedConnections() == 0);
	assert (srv.totalConnections() == 0);
	
	SocketAddress sa("localhost", svs.address().port());
	SecureStreamSocket ss1(sa);
	SecureStreamSocket ss2(sa);
	SecureStreamSocket ss3(sa);
	SecureStreamSocket ss4(sa);
	std::string data("hello, world");
	ss1.sendBytes(data.data(), (int) data.size());
	ss2.sendBytes(data.data(), (int) data.size());
	ss3.sendBytes(data.data(), (int) data.size());
	ss4.sendBytes(data.data(), (int) data.size());

	char buffer[256];
	int n = ss1.receiveBytes(buffer, sizeof(buffer));
	assert (n > 0);
	assert (std::string(buffer, n) == data);

	n = ss2.receiveBytes(buffer, sizeof(buffer));
	assert (n > 0);
	assert (std::string(buffer, n) == data);

	n = ss3.receiveBytes(buffer, sizeof(buffer));
	assert (n > 0);
	assert (std::string(buffer, n) == data);

	n = ss4.receiveBytes(buffer, sizeof(buffer));
	assert (n > 0);
	assert (std::string(buffer, n) == data);
	
	assert (srv.currentConnections() == 4);
	assert (srv.currentThreads() == 4);
	assert (srv.queuedConnections() == 0);
	assert (srv.totalConnections() == 4);
	
	SecureStreamSocket ss5;
	ss5.setLazyHandshake();
	ss5.connect(sa);
	Thread::sleep(200);
	assert (srv.queuedConnections() == 1);
	SecureStreamSocket ss6;
	ss6.setLazyHandshake();
	ss6.connect(sa);
	Thread::sleep(200);
	assert (srv.queuedConnections() == 2);
	
	ss1.close();
	Thread::sleep(300);
	assert (srv.currentConnections() == 4);
	assert (srv.currentThreads() == 4);
	assert (srv.queuedConnections() == 1);
	assert (srv.totalConnections() == 5);

	ss2.close();
	Thread::sleep(300);
	assert (srv.currentConnections() == 4);
	assert (srv.currentThreads() == 4);
	assert (srv.queuedConnections() == 0);
	assert (srv.totalConnections() == 6);
	
	ss3.close();
	Thread::sleep(300);
	assert (srv.currentConnections() == 3);
	assert (srv.currentThreads() == 3);
	assert (srv.queuedConnections() == 0);
	assert (srv.totalConnections() == 6);

	ss4.close();
	Thread::sleep(300);
	assert (srv.currentConnections() == 2);
	assert (srv.currentThreads() == 2);
	assert (srv.queuedConnections() == 0);
	assert (srv.totalConnections() == 6);

	ss5.close();
	ss6.close();
	Thread::sleep(300);
	assert (srv.currentConnections() == 0);
}