1 /* $Id: test_pipe.cpp,v 1.12 2007-11-02 17:47:41 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 #define BOOST_TEST_DYN_LINK
46 #include <boost/test/auto_unit_test.hpp>
48 using namespace boost::unit_test;
49 namespace mp = metaproxy_1;
51 class Timer : public yazpp_1::ISocketObserver {
53 yazpp_1::ISocketObservable *m_obs;
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; };
65 Timer::Timer(yazpp_1::ISocketObservable *obs,
67 m_obs(obs), m_pipe(9122), m_data(false), m_timeout(false)
69 obs->addObserver(m_pipe.read_fd(), this);
70 obs->maskObserver(this, yazpp_1::SOCKET_OBSERVE_READ);
71 obs->timeoutObserver(this, duration);
73 int r = send(m_pipe.write_fd(), "", 1, 0);
75 int r = write(m_pipe.write_fd(), "", 1);
79 std::cout << "Error write: "<< strerror(errno) << std::endl;
81 BOOST_CHECK_EQUAL(r, 1);
84 void Timer::socketNotify(int event)
86 if (event & yazpp_1::SOCKET_OBSERVE_READ)
91 int r = recv(m_pipe.read_fd(), buf, 1, 0);
93 int r = read(m_pipe.read_fd(), buf, 1);
97 std::cout << "Error read: "<< strerror(errno) << std::endl;
100 else if (event && yazpp_1::SOCKET_OBSERVE_TIMEOUT)
103 m_obs->deleteObserver(this);
107 BOOST_AUTO_TEST_CASE( test_pipe_1 )
109 yazpp_1::SocketManager mySocketManager;
111 Timer t(&mySocketManager, 1);
113 while (mySocketManager.processEvent() > 0)
116 BOOST_CHECK(t.timeout());
117 BOOST_CHECK(t.data());
123 * indent-tabs-mode: nil
124 * c-file-style: "stroustrup"
126 * vim: shiftwidth=4 tabstop=8 expandtab