Year 2007.
[metaproxy-moved-to-github.git] / src / test_thread_pool_observer.cpp
1 /* $Id: test_thread_pool_observer.cpp,v 1.12 2007-01-25 14:05:54 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 }
86
87 BOOST_AUTO_UNIT_TEST( thread_pool_observer1 ) 
88 {
89     SocketManager mySocketManager;
90
91     mp::ThreadPoolSocketObserver m(&mySocketManager, 3);
92     My_Timer_Thread t(&mySocketManager, &m) ;
93     while (t.m_responses < 30 && mySocketManager.processEvent() > 0)
94         ;
95     BOOST_CHECK_EQUAL(t.m_responses, 30);
96     BOOST_CHECK(t.m_sum >= 435);
97 }
98
99 /*
100  * Local variables:
101  * c-basic-offset: 4
102  * indent-tabs-mode: nil
103  * c-file-style: "stroustrup"
104  * End:
105  * vim: shiftwidth=4 tabstop=8 expandtab
106  */
107