Fixed bug #1589: tests does not compile for libboost 1.34.1.
[metaproxy-moved-to-github.git] / src / test_pipe.cpp
1 /* $Id: test_pipe.cpp,v 1.12 2007-11-02 17:47:41 adam Exp $
2    Copyright (c) 2005-2007, Index Data.
3
4 This file is part of Metaproxy.
5
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
9 version.
10
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
14 for more details.
15
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
19 02111-1307, USA.
20  */
21
22 #include "config.hpp"
23 #include <errno.h>
24 #include <yazpp/socket-manager.h>
25
26 #if HAVE_UNISTD_H
27 #include <unistd.h>
28 #endif
29
30 #ifdef WIN32
31 #include <winsock.h>
32 #endif
33
34 #if HAVE_SYS_SOCKET_H
35 #include <sys/socket.h>
36 #endif
37
38 #include <iostream>
39 #include <stdexcept>
40
41 #include "util.hpp"
42 #include "pipe.hpp"
43
44 #define BOOST_AUTO_TEST_MAIN
45 #define BOOST_TEST_DYN_LINK
46 #include <boost/test/auto_unit_test.hpp>
47
48 using namespace boost::unit_test;
49 namespace mp = metaproxy_1;
50
51 class Timer : public yazpp_1::ISocketObserver {
52 private:
53     yazpp_1::ISocketObservable *m_obs;
54     mp::Pipe m_pipe;
55     bool m_data;
56     bool m_timeout;
57 public:
58     Timer(yazpp_1::ISocketObservable *obs, int duration);
59     void socketNotify(int event);
60     bool timeout() { return m_timeout; };
61     bool data() { return m_data; };
62 };
63
64
65 Timer::Timer(yazpp_1::ISocketObservable *obs,
66                                  int duration) : 
67     m_obs(obs), m_pipe(9122), m_data(false), m_timeout(false)
68 {
69     obs->addObserver(m_pipe.read_fd(), this);
70     obs->maskObserver(this, yazpp_1::SOCKET_OBSERVE_READ);
71     obs->timeoutObserver(this, duration);
72 #ifdef WIN32
73     int r = send(m_pipe.write_fd(), "", 1, 0);
74 #else
75     int r = write(m_pipe.write_fd(), "", 1);
76 #endif
77     if (r == -1)
78     {
79         std::cout << "Error write: "<< strerror(errno) << std::endl;
80     }
81     BOOST_CHECK_EQUAL(r, 1);
82 }
83
84 void Timer::socketNotify(int event)
85 {
86     if (event & yazpp_1::SOCKET_OBSERVE_READ)
87     {
88         m_data = true;
89         char buf[3];
90 #ifdef WIN32
91         int r = recv(m_pipe.read_fd(), buf, 1, 0);
92 #else
93         int r = read(m_pipe.read_fd(), buf, 1);
94 #endif
95         if (r == -1)
96         {
97             std::cout << "Error read: "<< strerror(errno) << std::endl;
98         }
99     }
100     else if (event && yazpp_1::SOCKET_OBSERVE_TIMEOUT)
101     {
102         m_timeout = true;
103         m_obs->deleteObserver(this);
104     }
105 }
106
107 BOOST_AUTO_TEST_CASE( test_pipe_1 )
108 {
109     yazpp_1::SocketManager mySocketManager;
110     
111     Timer t(&mySocketManager, 1);
112
113     while (mySocketManager.processEvent() > 0)
114         if (t.timeout())
115             break;
116     BOOST_CHECK(t.timeout());
117     BOOST_CHECK(t.data());
118 }
119
120 /*
121  * Local variables:
122  * c-basic-offset: 4
123  * indent-tabs-mode: nil
124  * c-file-style: "stroustrup"
125  * End:
126  * vim: shiftwidth=4 tabstop=8 expandtab
127  */