[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[myserver-commit] [2815] Added basic tests for the ConnectionsScheduler
From: |
Giuseppe Scrivano |
Subject: |
[myserver-commit] [2815] Added basic tests for the ConnectionsScheduler class. |
Date: |
Sun, 14 Sep 2008 21:14:45 +0000 |
Revision: 2815
http://svn.sv.gnu.org/viewvc/?view=rev&root=myserver&revision=2815
Author: gscrivano
Date: 2008-09-14 21:14:44 +0000 (Sun, 14 Sep 2008)
Log Message:
-----------
Added basic tests for the ConnectionsScheduler class.
Modified Paths:
--------------
trunk/myserver/tests/Makefile.am
Added Paths:
-----------
trunk/myserver/tests/test_connections_scheduler.cpp
Modified: trunk/myserver/tests/Makefile.am
===================================================================
--- trunk/myserver/tests/Makefile.am 2008-09-14 21:09:18 UTC (rev 2814)
+++ trunk/myserver/tests/Makefile.am 2008-09-14 21:14:44 UTC (rev 2815)
@@ -2,5 +2,5 @@
#
bin_PROGRAMS = tests_suite
-tests_suite_SOURCES = main.cpp test_base64.cpp test_cached_file.cpp
test_cached_file_buffer.cpp test_cached_file_factory.cpp test_connection.cpp
test_files_utility.cpp test_filter_chain.cpp test_ftp.cpp test_gzip.cpp
test_hashmap.cpp test_homedir.cpp test_http_request.cpp test_http_response.cpp
test_md5.cpp test_mem_buff.cpp test_multicast.cpp test_mutex.cpp
test_recursive_mutex.cpp test_regex.cpp test_pipe.cpp test_safetime.cpp
test_semaphore.cpp test_thread.cpp test_utility.cpp
+tests_suite_SOURCES = main.cpp test_base64.cpp test_cached_file.cpp
test_cached_file_buffer.cpp test_cached_file_factory.cpp test_connection.cpp
test_connections_scheduler.cpp test_files_utility.cpp test_filter_chain.cpp
test_ftp.cpp test_gzip.cpp test_hashmap.cpp test_homedir.cpp
test_http_request.cpp test_http_response.cpp test_md5.cpp test_mem_buff.cpp
test_multicast.cpp test_mutex.cpp test_recursive_mutex.cpp test_regex.cpp
test_pipe.cpp test_safetime.cpp test_semaphore.cpp test_thread.cpp
test_utility.cpp
tests_suite_LDADD = ../src/libmyserver.a $(CPPUNIT_LDFLAGS) $(PTHREAD_LIB)
$(IDN_LIB) $(XNET_LIB) $(EVENT_LIB) $(DL_LIB) $(OPENSSL_LIB) $(ZLIB_LIB)
$(XML_LIBS) $(LDFLAGS)
Added: trunk/myserver/tests/test_connections_scheduler.cpp
===================================================================
--- trunk/myserver/tests/test_connections_scheduler.cpp
(rev 0)
+++ trunk/myserver/tests/test_connections_scheduler.cpp 2008-09-14 21:14:44 UTC
(rev 2815)
@@ -0,0 +1,233 @@
+/*
+ MyServer
+ Copyright (C) 2008 Free Software Foundation, Inc.
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <ctype.h>
+
+#include <cppunit/CompilerOutputter.h>
+#include <cppunit/extensions/TestFactoryRegistry.h>
+#include <cppunit/ui/text/TestRunner.h>
+#include <cppunit/extensions/HelperMacros.h>
+#include <include/connections_scheduler/connections_scheduler.h>
+
+
+class MockSocket : public Socket
+{
+ FileHandle handle;
+public:
+
+ /*
+ * BE SURE TO USE VALID HANDLES.
+ * 0, 1, 2 are valid ones.
+ */
+ MockSocket(FileHandle handle)
+ {
+ this->handle = handle;
+ }
+
+ virtual int connect(MYSERVER_SOCKADDR*, int)
+ {
+ return 0;
+ }
+ virtual int closesocket()
+ {
+ return 0;
+ }
+
+ virtual int shutdown(int how)
+ {
+ return 0;
+ }
+ virtual int recv(char*, int, int, u_long)
+ {
+ return 0;
+ }
+ virtual int recv(char*, int, int)
+ {
+ return 0;
+ }
+ virtual u_long bytesToRead()
+ {
+ return 0;
+ }
+
+ virtual FileHandle getHandle()
+ {
+ return (FileHandle) handle;
+ }
+
+};
+
+
+class TestSchedulerVisitor : public ConnectionsSchedulerVisitor
+{
+public:
+ TestSchedulerVisitor()
+ {
+ arg = NULL;
+ }
+
+ void *getArg()
+ {
+ return arg;
+ }
+
+ virtual int visitConnection(ConnectionPtr conn, void* param)
+ {
+ arg = param;
+ }
+private:
+ void *arg;
+};
+
+
+class TestConnectionsScheduler : public CppUnit::TestFixture
+{
+ CPPUNIT_TEST_SUITE( TestConnectionsScheduler );
+ CPPUNIT_TEST( testAddNewConnection );
+ CPPUNIT_TEST( testGetNumTotalConnections );
+ CPPUNIT_TEST( testAddNewReadyConnection );
+ CPPUNIT_TEST( testGetConnections );
+ CPPUNIT_TEST( testVisitor );
+
+ CPPUNIT_TEST_SUITE_END();
+
+ ConnectionsScheduler* scheduler;
+public:
+
+ void setUp()
+ {
+ scheduler = new ConnectionsScheduler(NULL);
+ scheduler->initialize();
+ }
+
+ void tearDown()
+ {
+ scheduler->release();
+ delete scheduler;
+ }
+
+ void testVisitor()
+ {
+ TestSchedulerVisitor visitor;
+
+ void* arg = this;
+ u_long max = 3;
+ u_long sum = 0;
+
+ for(u_long i = 0; i < max; i++)
+ {
+ ConnectionPtr conn = new Connection;
+ conn->socket = new MockSocket(i);
+ scheduler->addWaitingConnection(conn);
+ }
+
+ scheduler->accept(&visitor, arg);
+
+ CPPUNIT_ASSERT_EQUAL(arg, visitor.getArg());
+
+ }
+
+ void testGetConnections()
+ {
+ ConnectionPtr conn = new Connection;
+ conn->socket = new MockSocket(1);
+
+ list<ConnectionPtr> out;
+
+ scheduler->getConnections(out);
+
+ CPPUNIT_ASSERT_EQUAL(out.size(), (size_t)0);
+
+ conn = new Connection;
+ conn->socket = new MockSocket(2);
+
+ scheduler->addWaitingConnection(conn);
+
+ scheduler->getConnections(out);
+
+ CPPUNIT_ASSERT_EQUAL(out.size(), (size_t)1);
+ }
+
+ void testGetNumTotalConnections()
+ {
+ ConnectionPtr conn = new Connection;
+ conn->socket = new MockSocket(1);
+
+ CPPUNIT_ASSERT_EQUAL(scheduler->getNumTotalConnections(), 0ul);
+
+ scheduler->registerConnectionID(conn);
+
+ CPPUNIT_ASSERT_EQUAL(scheduler->getNumTotalConnections(), 1ul);
+
+ scheduler->registerConnectionID(conn);
+
+ CPPUNIT_ASSERT_EQUAL(scheduler->getNumTotalConnections(), 2ul);
+
+ scheduler->registerConnectionID(conn);
+
+ CPPUNIT_ASSERT_EQUAL(scheduler->getNumTotalConnections(), 3ul);
+
+ delete conn;
+ }
+
+ void testAddNewReadyConnection()
+ {
+ ConnectionPtr gotConn;
+
+ ConnectionPtr conn = new Connection;
+ conn->socket = new MockSocket(1);
+
+ CPPUNIT_ASSERT_EQUAL(scheduler->getNumTotalConnections(), 0ul);
+
+
+ scheduler->addReadyConnection(conn);
+
+ CPPUNIT_ASSERT_EQUAL(scheduler->getNumTotalConnections(), 0ul);
+
+ gotConn = scheduler->getConnection();
+
+ CPPUNIT_ASSERT_EQUAL(scheduler->getNumTotalConnections(), 0ul);
+ CPPUNIT_ASSERT_EQUAL(conn, gotConn);
+
+ delete conn;
+ }
+
+ void testAddNewConnection()
+ {
+ ConnectionPtr conn = new Connection;
+ conn->socket = new MockSocket(1);
+
+ CPPUNIT_ASSERT_EQUAL(scheduler->getNumTotalConnections(), 0ul);
+
+ scheduler->addWaitingConnection(conn);
+
+ CPPUNIT_ASSERT_EQUAL(scheduler->getConnectionsNumber(), 1ul);
+
+ scheduler->removeConnection(conn);
+
+ CPPUNIT_ASSERT_EQUAL(scheduler->getNumTotalConnections(), 0ul);
+
+ delete conn;
+ }
+
+
+
+
+};
+
+
+CPPUNIT_TEST_SUITE_REGISTRATION( TestConnectionsScheduler );
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [myserver-commit] [2815] Added basic tests for the ConnectionsScheduler class.,
Giuseppe Scrivano <=