Adjust Session class and include close state in it.
[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                 yp2::Session session;
27                 m_id = session.id();   
28                 //print();
29             }
30         }
31
32         void print()
33         {
34             boost::mutex::scoped_lock scoped_lock(io_mutex);
35             std::cout << "Worker " << m_nr 
36                       << " session.id() " << m_id << std::endl;
37         }
38         
39     private: 
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
56         for (int i=0; i < num_threads; ++i)
57         {
58             // Notice that each Worker has it's own session object!
59             Worker w(i);
60             thrds.add_thread(new boost::thread(w));
61         }
62         thrds.join_all();
63
64         yp2::Session session;
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  */