Allow stack-size to be set for ThreadPoolSocketObserver
[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     if (m_val == 7)
65         sleep(1);
66     return this;
67 }
68
69 bool My_Msg::cleanup(void *info)
70 {
71     return false;
72 }
73
74 void My_Msg::result(const char *t_info)
75 {
76     m_timer->m_sum += m_val;
77     m_timer->m_responses++;
78     delete this;
79 }
80
81 My_Timer_Thread::My_Timer_Thread(ISocketObservable *obs,
82                                  mp::ThreadPoolSocketObserver *t) :
83     m_obs(obs), m_pipe(9123)
84 {
85     m_t = t;
86     m_sum = 0;
87     m_requests = 0;
88     m_responses = 0;
89     obs->addObserver(m_pipe.read_fd(), this);
90     obs->maskObserver(this, SOCKET_OBSERVE_READ);
91     obs->timeoutObserver(this, 0);
92 }
93
94 void My_Timer_Thread::socketNotify(int event)
95 {
96     if (m_requests == 30)
97          m_obs->deleteObserver(this);
98     else
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     }
105 }
106
107 BOOST_AUTO_TEST_CASE( thread_pool_observer1 )
108 {
109     SocketManager mySocketManager;
110
111     mp::ThreadPoolSocketObserver m(&mySocketManager, 3, 3, 16*1024);
112     My_Timer_Thread t(&mySocketManager, &m);
113
114     while (t.m_responses < 30 && mySocketManager.processEvent() > 0)
115         ;
116     BOOST_CHECK_EQUAL(t.m_responses, 30);
117     BOOST_CHECK(t.m_sum >= 435); // = 29*30/2
118 }
119
120 /*
121  * Local variables:
122  * c-basic-offset: 4
123  * c-file-style: "Stroustrup"
124  * indent-tabs-mode: nil
125  * End:
126  * vim: shiftwidth=4 tabstop=8 expandtab
127  */
128