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