Windows: use Boost 1.59, msvc 14.0
[metaproxy-moved-to-github.git] / src / test_pipe.cpp
1 /* This file is part of Metaproxy.
2    Copyright (C) Index Data
3
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
7 version.
8
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
12 for more details.
13
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
17 */
18
19 #include "config.hpp"
20 #include <errno.h>
21 #include <yazpp/socket-manager.h>
22
23 #if HAVE_UNISTD_H
24 #include <unistd.h>
25 #endif
26
27 #ifdef WIN32
28 #include <winsock.h>
29 #endif
30
31 #if HAVE_SYS_SOCKET_H
32 #include <sys/socket.h>
33 #endif
34
35 #include <iostream>
36 #include <stdexcept>
37
38 #include <metaproxy/util.hpp>
39 #include "pipe.hpp"
40
41 #define BOOST_AUTO_TEST_MAIN
42 #define BOOST_TEST_DYN_LINK
43 #include <boost/test/auto_unit_test.hpp>
44
45 using namespace boost::unit_test;
46 namespace mp = metaproxy_1;
47
48 class Timer : public yazpp_1::ISocketObserver {
49 private:
50     yazpp_1::ISocketObservable *m_obs;
51     mp::Pipe m_pipe;
52     bool m_data;
53     bool m_timeout;
54 public:
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; };
59 };
60
61
62 Timer::Timer(yazpp_1::ISocketObservable *obs,
63                                  int duration) :
64     m_obs(obs), m_pipe(9122), m_data(false), m_timeout(false)
65 {
66     obs->addObserver(m_pipe.read_fd(), this);
67     obs->maskObserver(this, yazpp_1::SOCKET_OBSERVE_READ);
68     obs->timeoutObserver(this, duration);
69 #ifdef WIN32
70     int r = send(m_pipe.write_fd(), "", 1, 0);
71 #else
72     int r = write(m_pipe.write_fd(), "", 1);
73 #endif
74     if (r == -1)
75     {
76         std::cout << "Error write: "<< strerror(errno) << std::endl;
77     }
78     BOOST_CHECK_EQUAL(r, 1);
79 }
80
81 void Timer::socketNotify(int event)
82 {
83     if (event & yazpp_1::SOCKET_OBSERVE_READ)
84     {
85         m_data = true;
86         char buf[3];
87 #ifdef WIN32
88         int r = recv(m_pipe.read_fd(), buf, 1, 0);
89 #else
90         int r = read(m_pipe.read_fd(), buf, 1);
91 #endif
92         if (r == -1)
93         {
94             std::cout << "Error read: "<< strerror(errno) << std::endl;
95         }
96     }
97     else if (event && yazpp_1::SOCKET_OBSERVE_TIMEOUT)
98     {
99         m_timeout = true;
100         m_obs->deleteObserver(this);
101     }
102 }
103
104 BOOST_AUTO_TEST_CASE( test_pipe_1 )
105 {
106     yazpp_1::SocketManager mySocketManager;
107
108     Timer t(&mySocketManager, 1);
109
110     while (mySocketManager.processEvent() > 0)
111         if (t.timeout())
112             break;
113     BOOST_CHECK(t.timeout());
114     BOOST_CHECK(t.data());
115 }
116
117 /*
118  * Local variables:
119  * c-basic-offset: 4
120  * c-file-style: "Stroustrup"
121  * indent-tabs-mode: nil
122  * End:
123  * vim: shiftwidth=4 tabstop=8 expandtab
124  */
125