Rename from yp2 to metaproxy. The namespace for all definitions
[metaproxy-moved-to-github.git] / src / test_thread_pool_observer.cpp
1 /* $Id: test_thread_pool_observer.cpp,v 1.9 2006-03-16 10:40:59 adam Exp $
2    Copyright (c) 2005-2006, Index Data.
3
4 %LICENSE%
5  */
6 /* $Id: test_thread_pool_observer.cpp,v 1.9 2006-03-16 10:40:59 adam Exp $
7    Copyright (c) 1998-2005-2006, Index Data.
8
9 This file is part of the yaz-proxy.
10
11 YAZ proxy is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 2, or (at your option) any later
14 version.
15
16 YAZ proxy is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19 for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with YAZ proxy; see the file LICENSE.  If not, write to the
23 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
24 02111-1307, USA.
25  */
26
27 #include "config.hpp"
28 #include <stdlib.h>
29 #include <ctype.h>
30
31 #include <yaz++/pdu-assoc.h>
32 #include <yaz++/socket-manager.h>
33 #include <yaz/log.h>
34 #include "pipe.hpp"
35 #include "thread_pool_observer.hpp"
36
37 #define BOOST_AUTO_TEST_MAIN
38 #include <boost/test/auto_unit_test.hpp>
39
40 using namespace boost::unit_test;
41 using namespace yazpp_1;
42 namespace mp = metaproxy_1;
43
44 class My_Timer_Thread;
45
46 class My_Msg : public mp::IThreadPoolMsg {
47 public:
48     mp::IThreadPoolMsg *handle();
49     void result();
50     int m_val;
51     My_Timer_Thread *m_timer;
52 };
53
54 class My_Timer_Thread : public ISocketObserver {
55 private:
56     ISocketObservable *m_obs;
57     mp::Pipe m_pipe;
58     mp::ThreadPoolSocketObserver *m_t;
59 public:
60     int m_sum;
61     int m_requests;
62     int m_responses;
63     My_Timer_Thread(ISocketObservable *obs, mp::ThreadPoolSocketObserver *t);
64     void socketNotify(int event);
65 };
66
67
68 mp::IThreadPoolMsg *My_Msg::handle()
69 {
70     My_Msg *res = new My_Msg;
71
72     if (m_val == 7)
73         sleep(1);
74
75     res->m_val = m_val;
76     res->m_timer = m_timer;
77     return res;
78 }
79
80 void My_Msg::result()
81 {
82     m_timer->m_sum += m_val;
83     m_timer->m_responses++;
84 }
85
86 My_Timer_Thread::My_Timer_Thread(ISocketObservable *obs,
87                                  mp::ThreadPoolSocketObserver *t) : 
88     m_obs(obs), m_pipe(9123) 
89 {
90     m_t = t;
91     m_sum = 0;
92     m_requests = 0;
93     m_responses = 0;
94     obs->addObserver(m_pipe.read_fd(), this);
95     obs->maskObserver(this, SOCKET_OBSERVE_READ);
96     obs->timeoutObserver(this, 0);
97 }
98
99 void My_Timer_Thread::socketNotify(int event)
100 {
101     My_Msg *m = new My_Msg;
102     m->m_val = m_requests++;
103     m->m_timer = this;
104     m_t->put(m);
105 }
106
107 BOOST_AUTO_UNIT_TEST( thread_pool_observer1 ) 
108 {
109     SocketManager mySocketManager;
110
111     mp::ThreadPoolSocketObserver m(&mySocketManager, 3);
112     My_Timer_Thread t(&mySocketManager, &m) ;
113     while (t.m_responses < 30 && mySocketManager.processEvent() > 0)
114         ;
115     BOOST_CHECK_EQUAL(t.m_responses, 30);
116     BOOST_CHECK(t.m_sum >= 435);
117 }
118
119 /*
120  * Local variables:
121  * c-basic-offset: 4
122  * indent-tabs-mode: nil
123  * c-file-style: "stroustrup"
124  * End:
125  * vim: shiftwidth=4 tabstop=8 expandtab
126  */
127