Fixed bug #1589: tests does not compile for libboost 1.34.1.
[metaproxy-moved-to-github.git] / src / test_session2.cpp
1 /* $Id: test_session2.cpp,v 1.11 2007-11-02 17:47:41 adam Exp $
2    Copyright (c) 2005-2007, Index Data.
3
4 This file is part of Metaproxy.
5
6 Metaproxy is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
10
11 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Metaproxy; see the file LICENSE.  If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.
20  */
21
22 #include "config.hpp"
23 #include "session.hpp"
24
25 #include <iostream>
26 #include <list>
27
28 #include <boost/thread/mutex.hpp>
29 #include <boost/thread/thread.hpp>
30
31 #define BOOST_AUTO_TEST_MAIN
32 #define BOOST_TEST_DYN_LINK
33 #include <boost/test/auto_unit_test.hpp>
34
35 using namespace boost::unit_test;
36 namespace mp = metaproxy_1;
37
38 boost::mutex io_mutex;
39
40 class Worker 
41 {
42     public:
43         Worker(int nr = 0) 
44             :  m_nr(nr){};
45         
46         void operator() (void) {
47             for (int i=0; i < 100; ++i)
48             {
49                 mp::Session session;
50                 m_id = session.id();   
51                 //print();
52             }
53         }
54
55         void print()
56         {
57             boost::mutex::scoped_lock scoped_lock(io_mutex);
58             std::cout << "Worker " << m_nr 
59                       << " session.id() " << m_id << std::endl;
60         }
61         
62     private: 
63         int m_nr;
64         int m_id;
65 };
66
67
68
69 BOOST_AUTO_TEST_CASE( testsession2 ) 
70 {
71
72     // test session 
73     try {
74
75         const int num_threads = 100;
76         boost::thread_group thrds;
77         
78
79         for (int i=0; i < num_threads; ++i)
80         {
81             // Notice that each Worker has it's own session object!
82             Worker w(i);
83             thrds.add_thread(new boost::thread(w));
84         }
85         thrds.join_all();
86
87         mp::Session session;
88         BOOST_CHECK (session.id() == 10001);
89         
90     }
91     catch (std::exception &e) {
92         std::cout << e.what() << "\n";
93         BOOST_CHECK (false);
94     }
95     catch (...) {
96         BOOST_CHECK (false);
97     }
98 }
99
100 /*
101  * Local variables:
102  * c-basic-offset: 4
103  * indent-tabs-mode: nil
104  * c-file-style: "stroustrup"
105  * End:
106  * vim: shiftwidth=4 tabstop=8 expandtab
107  */