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