GPL v2.
[metaproxy-moved-to-github.git] / src / test_pipe.cpp
1 /* $Id: test_pipe.cpp,v 1.11 2007-05-09 21:23:09 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 #include <boost/test/auto_unit_test.hpp>
46
47 using namespace boost::unit_test;
48 namespace mp = metaproxy_1;
49
50 class Timer : public yazpp_1::ISocketObserver {
51 private:
52     yazpp_1::ISocketObservable *m_obs;
53     mp::Pipe m_pipe;
54     bool m_data;
55     bool m_timeout;
56 public:
57     Timer(yazpp_1::ISocketObservable *obs, int duration);
58     void socketNotify(int event);
59     bool timeout() { return m_timeout; };
60     bool data() { return m_data; };
61 };
62
63
64 Timer::Timer(yazpp_1::ISocketObservable *obs,
65                                  int duration) : 
66     m_obs(obs), m_pipe(9122), m_data(false), m_timeout(false)
67 {
68     obs->addObserver(m_pipe.read_fd(), this);
69     obs->maskObserver(this, yazpp_1::SOCKET_OBSERVE_READ);
70     obs->timeoutObserver(this, duration);
71 #ifdef WIN32
72     int r = send(m_pipe.write_fd(), "", 1, 0);
73 #else
74     int r = write(m_pipe.write_fd(), "", 1);
75 #endif
76     if (r == -1)
77     {
78         std::cout << "Error write: "<< strerror(errno) << std::endl;
79     }
80     BOOST_CHECK_EQUAL(r, 1);
81 }
82
83 void Timer::socketNotify(int event)
84 {
85     if (event & yazpp_1::SOCKET_OBSERVE_READ)
86     {
87         m_data = true;
88         char buf[3];
89 #ifdef WIN32
90         int r = recv(m_pipe.read_fd(), buf, 1, 0);
91 #else
92         int r = read(m_pipe.read_fd(), buf, 1);
93 #endif
94         if (r == -1)
95         {
96             std::cout << "Error read: "<< strerror(errno) << std::endl;
97         }
98     }
99     else if (event && yazpp_1::SOCKET_OBSERVE_TIMEOUT)
100     {
101         m_timeout = true;
102         m_obs->deleteObserver(this);
103     }
104 }
105
106 BOOST_AUTO_UNIT_TEST( test_pipe_1 )
107 {
108     yazpp_1::SocketManager mySocketManager;
109     
110     Timer t(&mySocketManager, 1);
111
112     while (mySocketManager.processEvent() > 0)
113         if (t.timeout())
114             break;
115     BOOST_CHECK(t.timeout());
116     BOOST_CHECK(t.data());
117 }
118
119 /*
120  * Local variables:
121  * c-basic-offset: 4
122  * indent-tabs-mode: nil
123  * c-file-style: "stroustrup"
124  * End:
125  * vim: shiftwidth=4 tabstop=8 expandtab
126  */