added multithreaded session test using 10 threads and 100 id updates each
[metaproxy-moved-to-github.git] / src / test_session2.cpp
1 #include "session.hpp"
2
3 #include <iostream>
4 #include <list>
5
6 #include <boost/thread/mutex.hpp>
7 #include <boost/thread/thread.hpp>
8
9 #define BOOST_AUTO_TEST_MAIN
10 #include <boost/test/auto_unit_test.hpp>
11
12 using namespace boost::unit_test;
13
14 boost::mutex io_mutex;
15
16 class Worker 
17 {
18     public:
19         Worker(yp2::Session *session, int nr = 0) 
20             : m_session(session), m_nr(nr){};
21         
22         void operator() (void) {
23             for (int i=0; i < 100; ++i)
24             {
25                 m_id = m_session->id();   
26                 //print();
27             }
28         }
29
30         void print()
31         {
32             boost::mutex::scoped_lock scoped_lock(io_mutex);
33             std::cout << "Worker " << m_nr 
34                       << " session.id() " << m_id << std::endl;
35         }
36         
37     private: 
38         yp2::Session *m_session;
39         int m_nr;
40         int m_id;
41 };
42
43
44
45 BOOST_AUTO_TEST_CASE( testsession2 ) 
46 {
47
48     // test session 
49     try {
50         yp2::Session session;
51
52         const int num_threads = 10;
53         boost::thread_group thrds;
54         
55         for (int i=0; i < num_threads; ++i)
56         {
57             Worker w(&session, i);
58             thrds.add_thread(new boost::thread(w));
59         }
60         thrds.join_all();
61
62         BOOST_CHECK (session.id() == 1001);
63         
64     }
65     catch (std::exception &e) {
66         std::cout << e.what() << "\n";
67         BOOST_CHECK (false);
68     }
69     catch (...) {
70         BOOST_CHECK (false);
71     }
72 }
73
74 /*
75  * Local variables:
76  * c-basic-offset: 4
77  * indent-tabs-mode: nil
78  * End:
79  * vim: shiftwidth=4 tabstop=8 expandtab
80  */