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