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