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