Using boost::scoped_ptr for pimpl/rep for some filters
[metaproxy-moved-to-github.git] / src / test_thread_pool_observer.cpp
1 /* $Id: test_thread_pool_observer.cpp,v 1.6 2005-10-15 14:09:09 adam Exp $
2    Copyright (c) 2005, Index Data.
3
4 %LICENSE%
5  */
6 /* $Id: test_thread_pool_observer.cpp,v 1.6 2005-10-15 14:09:09 adam Exp $
7    Copyright (c) 1998-2005, 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 "thread_pool_observer.hpp"
35
36 #define BOOST_AUTO_TEST_MAIN
37 #include <boost/test/auto_unit_test.hpp>
38
39 using namespace boost::unit_test;
40 using namespace yazpp_1;
41
42 class My_Timer_Thread;
43
44 class My_Msg : public yp2::IThreadPoolMsg {
45 public:
46     yp2::IThreadPoolMsg *handle();
47     void result();
48     int m_val;
49     My_Timer_Thread *m_timer;
50 };
51
52 class My_Timer_Thread : public ISocketObserver {
53 private:
54     ISocketObservable *m_obs;
55     int m_fd[2];
56     yp2::ThreadPoolSocketObserver *m_t;
57 public:
58     int m_sum;
59     int m_requests;
60     int m_responses;
61     My_Timer_Thread(ISocketObservable *obs, yp2::ThreadPoolSocketObserver *t);
62     void socketNotify(int event);
63 };
64
65
66 yp2::IThreadPoolMsg *My_Msg::handle()
67 {
68     My_Msg *res = new My_Msg;
69
70     if (m_val == 7)
71         sleep(1);
72
73     res->m_val = m_val;
74     res->m_timer = m_timer;
75     return res;
76 }
77
78 void My_Msg::result()
79 {
80     m_timer->m_sum += m_val;
81     m_timer->m_responses++;
82 }
83
84 My_Timer_Thread::My_Timer_Thread(ISocketObservable *obs,
85                                  yp2::ThreadPoolSocketObserver *t) : m_obs(obs) 
86 {
87     pipe(m_fd);
88     m_t = t;
89     m_sum = 0;
90     m_requests = 0;
91     m_responses = 0;
92     obs->addObserver(m_fd[0], this);
93     obs->maskObserver(this, SOCKET_OBSERVE_READ);
94     obs->timeoutObserver(this, 0);
95 }
96
97 void My_Timer_Thread::socketNotify(int event)
98 {
99     My_Msg *m = new My_Msg;
100     m->m_val = m_requests++;
101     m->m_timer = this;
102     m_t->put(m);
103 }
104
105 BOOST_AUTO_TEST_CASE( thread_pool_observer1 ) 
106 {
107     SocketManager mySocketManager;
108
109     yp2::ThreadPoolSocketObserver m(&mySocketManager, 3);
110     My_Timer_Thread t(&mySocketManager, &m) ;
111     while (t.m_responses < 30 && mySocketManager.processEvent() > 0)
112         ;
113     BOOST_CHECK_EQUAL(t.m_responses, 30);
114     BOOST_CHECK(t.m_sum >= 435);
115 }
116
117 /*
118  * Local variables:
119  * c-basic-offset: 4
120  * indent-tabs-mode: nil
121  * c-file-style: "stroustrup"
122  * End:
123  * vim: shiftwidth=4 tabstop=8 expandtab
124  */
125