c5810c2a63f77b774694196f9f866958251476b2
[metaproxy-moved-to-github.git] / src / test_thread_pool_observer.cpp
1 /* $Id: test_thread_pool_observer.cpp,v 1.13 2007-04-18 12:06:59 adam Exp $
2    Copyright (c) 2005-2007, Index Data.
3
4    See the LICENSE file for details
5  */
6
7 #include "config.hpp"
8 #include <stdlib.h>
9 #include <ctype.h>
10
11 #include <yazpp/pdu-assoc.h>
12 #include <yazpp/socket-manager.h>
13 #include <yaz/log.h>
14 #include "pipe.hpp"
15 #include "thread_pool_observer.hpp"
16
17 #define BOOST_AUTO_TEST_MAIN
18 #include <boost/test/auto_unit_test.hpp>
19
20 using namespace boost::unit_test;
21 using namespace yazpp_1;
22 namespace mp = metaproxy_1;
23
24 class My_Timer_Thread;
25
26 class My_Msg : public mp::IThreadPoolMsg {
27 public:
28     mp::IThreadPoolMsg *handle();
29     void result();
30     int m_val;
31     My_Timer_Thread *m_timer;
32 };
33
34 class My_Timer_Thread : public ISocketObserver {
35 private:
36     ISocketObservable *m_obs;
37     mp::Pipe m_pipe;
38     mp::ThreadPoolSocketObserver *m_t;
39 public:
40     int m_sum;
41     int m_requests;
42     int m_responses;
43     My_Timer_Thread(ISocketObservable *obs, mp::ThreadPoolSocketObserver *t);
44     void socketNotify(int event);
45 };
46
47
48 mp::IThreadPoolMsg *My_Msg::handle()
49 {
50     My_Msg *res = new My_Msg;
51
52     if (m_val == 7)
53         sleep(1);
54
55     res->m_val = m_val;
56     res->m_timer = m_timer;
57     return res;
58 }
59
60 void My_Msg::result()
61 {
62     m_timer->m_sum += m_val;
63     m_timer->m_responses++;
64 }
65
66 My_Timer_Thread::My_Timer_Thread(ISocketObservable *obs,
67                                  mp::ThreadPoolSocketObserver *t) : 
68     m_obs(obs), m_pipe(9123) 
69 {
70     m_t = t;
71     m_sum = 0;
72     m_requests = 0;
73     m_responses = 0;
74     obs->addObserver(m_pipe.read_fd(), this);
75     obs->maskObserver(this, SOCKET_OBSERVE_READ);
76     obs->timeoutObserver(this, 0);
77 }
78
79 void My_Timer_Thread::socketNotify(int event)
80 {
81     My_Msg *m = new My_Msg;
82     m->m_val = m_requests++;
83     m->m_timer = this;
84     m_t->put(m);
85 #if 0
86     // prevent input queue from being filled up..
87     // bug #1064: Test test_thread_pool_observer hangs
88     // fortunately we don't need this hack. because put (ebove)
89     // will block itself if needed
90     if (m->m_val == 30)
91          m_obs->deleteObserver(this);
92 #endif
93 }
94
95 BOOST_AUTO_UNIT_TEST( thread_pool_observer1 ) 
96 {
97     SocketManager mySocketManager;
98
99     mp::ThreadPoolSocketObserver m(&mySocketManager, 3);
100     My_Timer_Thread t(&mySocketManager, &m) ;
101     while (t.m_responses < 30 && mySocketManager.processEvent() > 0)
102         ;
103     BOOST_CHECK_EQUAL(t.m_responses, 30);
104     BOOST_CHECK(t.m_sum >= 435); // = 29*30/2
105 }
106
107 /*
108  * Local variables:
109  * c-basic-offset: 4
110  * indent-tabs-mode: nil
111  * c-file-style: "stroustrup"
112  * End:
113  * vim: shiftwidth=4 tabstop=8 expandtab
114  */
115