1 /* $Id: test_pipe.cpp,v 1.10 2007-02-21 14:01:27 adam Exp $
2 Copyright (c) 2005-2007, Index Data.
4 See the LICENSE file for details
9 #include <yazpp/socket-manager.h>
20 #include <sys/socket.h>
29 #define BOOST_AUTO_TEST_MAIN
30 #include <boost/test/auto_unit_test.hpp>
32 using namespace boost::unit_test;
33 namespace mp = metaproxy_1;
35 class Timer : public yazpp_1::ISocketObserver {
37 yazpp_1::ISocketObservable *m_obs;
42 Timer(yazpp_1::ISocketObservable *obs, int duration);
43 void socketNotify(int event);
44 bool timeout() { return m_timeout; };
45 bool data() { return m_data; };
49 Timer::Timer(yazpp_1::ISocketObservable *obs,
51 m_obs(obs), m_pipe(9122), m_data(false), m_timeout(false)
53 obs->addObserver(m_pipe.read_fd(), this);
54 obs->maskObserver(this, yazpp_1::SOCKET_OBSERVE_READ);
55 obs->timeoutObserver(this, duration);
57 int r = send(m_pipe.write_fd(), "", 1, 0);
59 int r = write(m_pipe.write_fd(), "", 1);
63 std::cout << "Error write: "<< strerror(errno) << std::endl;
65 BOOST_CHECK_EQUAL(r, 1);
68 void Timer::socketNotify(int event)
70 if (event & yazpp_1::SOCKET_OBSERVE_READ)
75 int r = recv(m_pipe.read_fd(), buf, 1, 0);
77 int r = read(m_pipe.read_fd(), buf, 1);
81 std::cout << "Error read: "<< strerror(errno) << std::endl;
84 else if (event && yazpp_1::SOCKET_OBSERVE_TIMEOUT)
87 m_obs->deleteObserver(this);
91 BOOST_AUTO_UNIT_TEST( test_pipe_1 )
93 yazpp_1::SocketManager mySocketManager;
95 Timer t(&mySocketManager, 1);
97 while (mySocketManager.processEvent() > 0)
100 BOOST_CHECK(t.timeout());
101 BOOST_CHECK(t.data());
107 * indent-tabs-mode: nil
108 * c-file-style: "stroustrup"
110 * vim: shiftwidth=4 tabstop=8 expandtab