Fixed bug #1589: tests does not compile for libboost 1.34.1.
[metaproxy-moved-to-github.git] / src / test_thread_pool_observer.cpp
1 /* $Id: test_thread_pool_observer.cpp,v 1.15 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 <stdlib.h>
24 #include <ctype.h>
25
26 #include <yazpp/pdu-assoc.h>
27 #include <yazpp/socket-manager.h>
28 #include <yaz/log.h>
29 #include "pipe.hpp"
30 #include "thread_pool_observer.hpp"
31
32 #define BOOST_AUTO_TEST_MAIN
33 #define BOOST_TEST_DYN_LINK
34 #include <boost/test/auto_unit_test.hpp>
35
36 using namespace boost::unit_test;
37 using namespace yazpp_1;
38 namespace mp = metaproxy_1;
39
40 class My_Timer_Thread;
41
42 class My_Msg : public mp::IThreadPoolMsg {
43 public:
44     mp::IThreadPoolMsg *handle();
45     void result();
46     int m_val;
47     My_Timer_Thread *m_timer;
48 };
49
50 class My_Timer_Thread : public ISocketObserver {
51 private:
52     ISocketObservable *m_obs;
53     mp::Pipe m_pipe;
54     mp::ThreadPoolSocketObserver *m_t;
55 public:
56     int m_sum;
57     int m_requests;
58     int m_responses;
59     My_Timer_Thread(ISocketObservable *obs, mp::ThreadPoolSocketObserver *t);
60     void socketNotify(int event);
61 };
62
63
64 mp::IThreadPoolMsg *My_Msg::handle()
65 {
66     My_Msg *res = new My_Msg;
67
68     if (m_val == 7)
69         sleep(1);
70
71     res->m_val = m_val;
72     res->m_timer = m_timer;
73     return res;
74 }
75
76 void My_Msg::result()
77 {
78     m_timer->m_sum += m_val;
79     m_timer->m_responses++;
80 }
81
82 My_Timer_Thread::My_Timer_Thread(ISocketObservable *obs,
83                                  mp::ThreadPoolSocketObserver *t) : 
84     m_obs(obs), m_pipe(9123) 
85 {
86     m_t = t;
87     m_sum = 0;
88     m_requests = 0;
89     m_responses = 0;
90     obs->addObserver(m_pipe.read_fd(), this);
91     obs->maskObserver(this, SOCKET_OBSERVE_READ);
92     obs->timeoutObserver(this, 0);
93 }
94
95 void My_Timer_Thread::socketNotify(int event)
96 {
97     My_Msg *m = new My_Msg;
98     m->m_val = m_requests++;
99     m->m_timer = this;
100     m_t->put(m);
101 #if 0
102     // prevent input queue from being filled up..
103     // bug #1064: Test test_thread_pool_observer hangs
104     // fortunately we don't need this hack. because put (ebove)
105     // will block itself if needed
106     if (m->m_val == 30)
107          m_obs->deleteObserver(this);
108 #endif
109 }
110
111 BOOST_AUTO_TEST_CASE( thread_pool_observer1 ) 
112 {
113     SocketManager mySocketManager;
114
115     mp::ThreadPoolSocketObserver m(&mySocketManager, 3);
116     My_Timer_Thread t(&mySocketManager, &m) ;
117     while (t.m_responses < 30 && mySocketManager.processEvent() > 0)
118         ;
119     BOOST_CHECK_EQUAL(t.m_responses, 30);
120     BOOST_CHECK(t.m_sum >= 435); // = 29*30/2
121 }
122
123 /*
124  * Local variables:
125  * c-basic-offset: 4
126  * indent-tabs-mode: nil
127  * c-file-style: "stroustrup"
128  * End:
129  * vim: shiftwidth=4 tabstop=8 expandtab
130  */
131