1 /* This file is part of Metaproxy.
2 Copyright (C) 2005-2013 Index Data
4 Metaproxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <yazpp/socket-manager.h>
32 #include <sys/socket.h>
38 #include <metaproxy/util.hpp>
41 #define BOOST_AUTO_TEST_MAIN
42 #define BOOST_TEST_DYN_LINK
43 #include <boost/test/auto_unit_test.hpp>
45 using namespace boost::unit_test;
46 namespace mp = metaproxy_1;
48 class Timer : public yazpp_1::ISocketObserver {
50 yazpp_1::ISocketObservable *m_obs;
55 Timer(yazpp_1::ISocketObservable *obs, int duration);
56 void socketNotify(int event);
57 bool timeout() { return m_timeout; };
58 bool data() { return m_data; };
62 Timer::Timer(yazpp_1::ISocketObservable *obs,
64 m_obs(obs), m_pipe(9122), m_data(false), m_timeout(false)
66 obs->addObserver(m_pipe.read_fd(), this);
67 obs->maskObserver(this, yazpp_1::SOCKET_OBSERVE_READ);
68 obs->timeoutObserver(this, duration);
70 int r = send(m_pipe.write_fd(), "", 1, 0);
72 int r = write(m_pipe.write_fd(), "", 1);
76 std::cout << "Error write: "<< strerror(errno) << std::endl;
78 BOOST_CHECK_EQUAL(r, 1);
81 void Timer::socketNotify(int event)
83 if (event & yazpp_1::SOCKET_OBSERVE_READ)
88 int r = recv(m_pipe.read_fd(), buf, 1, 0);
90 int r = read(m_pipe.read_fd(), buf, 1);
94 std::cout << "Error read: "<< strerror(errno) << std::endl;
97 else if (event && yazpp_1::SOCKET_OBSERVE_TIMEOUT)
100 m_obs->deleteObserver(this);
104 BOOST_AUTO_TEST_CASE( test_pipe_1 )
106 yazpp_1::SocketManager mySocketManager;
108 Timer t(&mySocketManager, 1);
110 while (mySocketManager.processEvent() > 0)
113 BOOST_CHECK(t.timeout());
114 BOOST_CHECK(t.data());
120 * c-file-style: "Stroustrup"
121 * indent-tabs-mode: nil
123 * vim: shiftwidth=4 tabstop=8 expandtab