1 /* $Id: test_pipe.cpp,v 1.11 2007-05-09 21:23:09 adam Exp $
2 Copyright (c) 2005-2007, Index Data.
4 This file is part of Metaproxy.
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
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
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
24 #include <yazpp/socket-manager.h>
35 #include <sys/socket.h>
44 #define BOOST_AUTO_TEST_MAIN
45 #include <boost/test/auto_unit_test.hpp>
47 using namespace boost::unit_test;
48 namespace mp = metaproxy_1;
50 class Timer : public yazpp_1::ISocketObserver {
52 yazpp_1::ISocketObservable *m_obs;
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; };
64 Timer::Timer(yazpp_1::ISocketObservable *obs,
66 m_obs(obs), m_pipe(9122), m_data(false), m_timeout(false)
68 obs->addObserver(m_pipe.read_fd(), this);
69 obs->maskObserver(this, yazpp_1::SOCKET_OBSERVE_READ);
70 obs->timeoutObserver(this, duration);
72 int r = send(m_pipe.write_fd(), "", 1, 0);
74 int r = write(m_pipe.write_fd(), "", 1);
78 std::cout << "Error write: "<< strerror(errno) << std::endl;
80 BOOST_CHECK_EQUAL(r, 1);
83 void Timer::socketNotify(int event)
85 if (event & yazpp_1::SOCKET_OBSERVE_READ)
90 int r = recv(m_pipe.read_fd(), buf, 1, 0);
92 int r = read(m_pipe.read_fd(), buf, 1);
96 std::cout << "Error read: "<< strerror(errno) << std::endl;
99 else if (event && yazpp_1::SOCKET_OBSERVE_TIMEOUT)
102 m_obs->deleteObserver(this);
106 BOOST_AUTO_UNIT_TEST( test_pipe_1 )
108 yazpp_1::SocketManager mySocketManager;
110 Timer t(&mySocketManager, 1);
112 while (mySocketManager.processEvent() > 0)
115 BOOST_CHECK(t.timeout());
116 BOOST_CHECK(t.data());
122 * indent-tabs-mode: nil
123 * c-file-style: "stroustrup"
125 * vim: shiftwidth=4 tabstop=8 expandtab