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.
4 This file is part of Metaproxy.
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
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
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
26 #include <yazpp/pdu-assoc.h>
27 #include <yazpp/socket-manager.h>
30 #include "thread_pool_observer.hpp"
32 #define BOOST_AUTO_TEST_MAIN
33 #include <boost/test/auto_unit_test.hpp>
35 using namespace boost::unit_test;
36 using namespace yazpp_1;
37 namespace mp = metaproxy_1;
39 class My_Timer_Thread;
41 class My_Msg : public mp::IThreadPoolMsg {
43 mp::IThreadPoolMsg *handle();
46 My_Timer_Thread *m_timer;
49 class My_Timer_Thread : public ISocketObserver {
51 ISocketObservable *m_obs;
53 mp::ThreadPoolSocketObserver *m_t;
58 My_Timer_Thread(ISocketObservable *obs, mp::ThreadPoolSocketObserver *t);
59 void socketNotify(int event);
63 mp::IThreadPoolMsg *My_Msg::handle()
65 My_Msg *res = new My_Msg;
71 res->m_timer = m_timer;
77 m_timer->m_sum += m_val;
78 m_timer->m_responses++;
81 My_Timer_Thread::My_Timer_Thread(ISocketObservable *obs,
82 mp::ThreadPoolSocketObserver *t) :
83 m_obs(obs), m_pipe(9123)
89 obs->addObserver(m_pipe.read_fd(), this);
90 obs->maskObserver(this, SOCKET_OBSERVE_READ);
91 obs->timeoutObserver(this, 0);
94 void My_Timer_Thread::socketNotify(int event)
96 My_Msg *m = new My_Msg;
97 m->m_val = m_requests++;
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
106 m_obs->deleteObserver(this);
110 BOOST_AUTO_UNIT_TEST( thread_pool_observer1 )
112 SocketManager mySocketManager;
114 mp::ThreadPoolSocketObserver m(&mySocketManager, 3);
115 My_Timer_Thread t(&mySocketManager, &m) ;
116 while (t.m_responses < 30 && mySocketManager.processEvent() > 0)
118 BOOST_CHECK_EQUAL(t.m_responses, 30);
119 BOOST_CHECK(t.m_sum >= 435); // = 29*30/2
125 * indent-tabs-mode: nil
126 * c-file-style: "stroustrup"
128 * vim: shiftwidth=4 tabstop=8 expandtab