GPL v2.
[metaproxy-moved-to-github.git] / src / test_thread_pool_observer.cpp
1 /* $Id: test_thread_pool_observer.cpp,v 1.14 2007-05-09 21:23:09 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 #include <boost/test/auto_unit_test.hpp>
34
35 using namespace boost::unit_test;
36 using namespace yazpp_1;
37 namespace mp = metaproxy_1;
38
39 class My_Timer_Thread;
40
41 class My_Msg : public mp::IThreadPoolMsg {
42 public:
43     mp::IThreadPoolMsg *handle();
44     void result();
45     int m_val;
46     My_Timer_Thread *m_timer;
47 };
48
49 class My_Timer_Thread : public ISocketObserver {
50 private:
51     ISocketObservable *m_obs;
52     mp::Pipe m_pipe;
53     mp::ThreadPoolSocketObserver *m_t;
54 public:
55     int m_sum;
56     int m_requests;
57     int m_responses;
58     My_Timer_Thread(ISocketObservable *obs, mp::ThreadPoolSocketObserver *t);
59     void socketNotify(int event);
60 };
61
62
63 mp::IThreadPoolMsg *My_Msg::handle()
64 {
65     My_Msg *res = new My_Msg;
66
67     if (m_val == 7)
68         sleep(1);
69
70     res->m_val = m_val;
71     res->m_timer = m_timer;
72     return res;
73 }
74
75 void My_Msg::result()
76 {
77     m_timer->m_sum += m_val;
78     m_timer->m_responses++;
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     My_Msg *m = new My_Msg;
97     m->m_val = m_requests++;
98     m->m_timer = this;
99     m_t->put(m);
100 #if 0
101     // prevent input queue from being filled up..
102     // bug #1064: Test test_thread_pool_observer hangs
103     // fortunately we don't need this hack. because put (ebove)
104     // will block itself if needed
105     if (m->m_val == 30)
106          m_obs->deleteObserver(this);
107 #endif
108 }
109
110 BOOST_AUTO_UNIT_TEST( thread_pool_observer1 ) 
111 {
112     SocketManager mySocketManager;
113
114     mp::ThreadPoolSocketObserver m(&mySocketManager, 3);
115     My_Timer_Thread t(&mySocketManager, &m) ;
116     while (t.m_responses < 30 && mySocketManager.processEvent() > 0)
117         ;
118     BOOST_CHECK_EQUAL(t.m_responses, 30);
119     BOOST_CHECK(t.m_sum >= 435); // = 29*30/2
120 }
121
122 /*
123  * Local variables:
124  * c-basic-offset: 4
125  * indent-tabs-mode: nil
126  * c-file-style: "stroustrup"
127  * End:
128  * vim: shiftwidth=4 tabstop=8 expandtab
129  */
130