added multithreaded session test using 10 threads and 100 id updates each
authorMarc Cromme <marc@indexdata.dk>
Sat, 8 Oct 2005 16:32:01 +0000 (16:32 +0000)
committerMarc Cromme <marc@indexdata.dk>
Sat, 8 Oct 2005 16:32:01 +0000 (16:32 +0000)
src/Makefile.am
src/test_session2.cpp [new file with mode: 0644]

index 7ebc614..6e72261 100644 (file)
@@ -1,4 +1,4 @@
-## $Id: Makefile.am,v 1.11 2005-10-07 22:46:16 marc Exp $
+## $Id: Makefile.am,v 1.12 2005-10-08 16:32:01 marc Exp $
 
 MAINTAINERCLEANFILES    = Makefile.in config.in config.hpp
 
@@ -8,8 +8,9 @@ AM_CXXFLAGS = $(YAZPPINC) $(XSLT_CFLAGS)
 YP2_INCHPP = session.hpp package.hpp filter.hpp router.hpp
 
 bin_PROGRAMS =
-check_PROGRAMS = test_filter1 test_filter2 test_session1 \
-                test_thread_pool_observer test_boost_threads
+check_PROGRAMS = test_filter1 test_filter2 \
+                 test_session1 test_session2 \
+                 test_thread_pool_observer test_boost_threads
 noinst_PROGRAMS =  p2 
 
 TESTS=$(check_PROGRAMS)
@@ -17,6 +18,7 @@ TESTS=$(check_PROGRAMS)
 test_filter1_SOURCES=test_filter1.cpp $(YP2_INCHPP)
 test_filter2_SOURCES=test_filter2.cpp $(YP2_INCHPP)
 test_session1_SOURCES=test_session1.cpp $(YP2_INCHPP)
+test_session2_SOURCES=test_session2.cpp $(YP2_INCHPP)
 test_boost_threads_SOURCES=test_boost_threads.cpp
 test_thread_pool_observer_SOURCES = test_thread_pool_observer.cpp \
           thread_pool_observer.cpp thread_pool_observer.h
@@ -34,6 +36,7 @@ LDADD= $(YAZPPLALIB) $(XSLT_LIBS)
 test_filter1_LDADD = $(LDADD) -lboost_unit_test_framework
 test_filter2_LDADD = $(LDADD) -lboost_unit_test_framework
 test_session1_LDADD = $(LDADD) -lboost_unit_test_framework
+test_session2_LDADD = $(LDADD) -lboost_unit_test_framework
 test_boost_threads_LDADD = $(LDADD) -lboost_unit_test_framework
 
 
diff --git a/src/test_session2.cpp b/src/test_session2.cpp
new file mode 100644 (file)
index 0000000..4f4623c
--- /dev/null
@@ -0,0 +1,80 @@
+#include "session.hpp"
+
+#include <iostream>
+#include <list>
+
+#include <boost/thread/mutex.hpp>
+#include <boost/thread/thread.hpp>
+
+#define BOOST_AUTO_TEST_MAIN
+#include <boost/test/auto_unit_test.hpp>
+
+using namespace boost::unit_test;
+
+boost::mutex io_mutex;
+
+class Worker 
+{
+    public:
+        Worker(yp2::Session *session, int nr = 0) 
+            : m_session(session), m_nr(nr){};
+        
+        void operator() (void) {
+            for (int i=0; i < 100; ++i)
+            {
+                m_id = m_session->id();   
+                //print();
+            }
+        }
+
+        void print()
+        {
+            boost::mutex::scoped_lock scoped_lock(io_mutex);
+            std::cout << "Worker " << m_nr 
+                      << " session.id() " << m_id << std::endl;
+        }
+        
+    private: 
+        yp2::Session *m_session;
+        int m_nr;
+        int m_id;
+};
+
+
+
+BOOST_AUTO_TEST_CASE( testsession2 ) 
+{
+
+    // test session 
+    try {
+        yp2::Session session;
+
+        const int num_threads = 10;
+        boost::thread_group thrds;
+        
+        for (int i=0; i < num_threads; ++i)
+        {
+            Worker w(&session, i);
+            thrds.add_thread(new boost::thread(w));
+        }
+        thrds.join_all();
+
+        BOOST_CHECK (session.id() == 1001);
+        
+    }
+    catch (std::exception &e) {
+        std::cout << e.what() << "\n";
+        BOOST_CHECK (false);
+    }
+    catch (...) {
+        BOOST_CHECK (false);
+    }
+}
+
+/*
+ * Local variables:
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ * vim: shiftwidth=4 tabstop=8 expandtab
+ */