unittest_poller.cpp 6.79 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
Copyright (c) 2018 Contributors as noted in the AUTHORS file

This file is part of 0MQ.

0MQ is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

0MQ is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "../tests/testutil.hpp"

#include <poller.hpp>
23 24
#include <i_poll_events.hpp>
#include <ip.hpp>
25 26 27

#include <unity.h>

28
#ifndef _WIN32
29
#include <unistd.h>
30 31 32
#define closesocket close
#endif

33 34 35 36 37 38 39 40 41 42 43 44 45
void setUp ()
{
}
void tearDown ()
{
}

void test_create ()
{
    zmq::thread_ctx_t thread_ctx;
    zmq::poller_t poller (thread_ctx);
}

46 47 48 49 50 51 52 53 54 55 56 57 58 59
#if 0
// TODO this triggers an assertion. should it be a valid use case?
void test_start_empty ()
{
    zmq::thread_ctx_t thread_ctx;
    zmq::poller_t poller (thread_ctx);
    poller.start ();
    msleep (SETTLE_TIME);
}
#endif

struct test_events_t : zmq::i_poll_events
{
    test_events_t (zmq::fd_t fd_, zmq::poller_t &poller_) :
60 61
        _fd (fd_),
        _poller (poller_)
62
    {
63
        (void) _fd;
64 65 66 67
    }

    virtual void in_event ()
    {
68 69
        _poller.rm_fd (_handle);
        _handle = (zmq::poller_t::handle_t) NULL;
70 71 72

        // this must only be incremented after rm_fd
        in_events.add (1);
73 74 75 76 77 78 79 80 81 82 83 84
    }


    virtual void out_event ()
    {
        // TODO
    }


    virtual void timer_event (int id_)
    {
        LIBZMQ_UNUSED (id_);
85 86
        _poller.rm_fd (_handle);
        _handle = (zmq::poller_t::handle_t) NULL;
87 88 89

        // this must only be incremented after rm_fd
        timer_events.add (1);
90 91
    }

92
    void set_handle (zmq::poller_t::handle_t handle_) { _handle = handle_; }
93 94 95 96

    zmq::atomic_counter_t in_events, timer_events;

  private:
97 98 99
    zmq::fd_t _fd;
    zmq::poller_t &_poller;
    zmq::poller_t::handle_t _handle;
100 101
};

102
void wait_in_events (test_events_t &events_)
103 104
{
    void *watch = zmq_stopwatch_start ();
105
    while (events_.in_events.get () < 1) {
106 107 108 109 110 111 112 113 114
#ifdef ZMQ_BUILD_DRAFT
        TEST_ASSERT_LESS_OR_EQUAL_MESSAGE (SETTLE_TIME,
                                           zmq_stopwatch_intermediate (watch),
                                           "Timeout waiting for in event");
#endif
    }
    zmq_stopwatch_stop (watch);
}

115
void wait_timer_events (test_events_t &events_)
116 117
{
    void *watch = zmq_stopwatch_start ();
118
    while (events_.timer_events.get () < 1) {
119 120 121 122 123 124 125 126 127
#ifdef ZMQ_BUILD_DRAFT
        TEST_ASSERT_LESS_OR_EQUAL_MESSAGE (SETTLE_TIME,
                                           zmq_stopwatch_intermediate (watch),
                                           "Timeout waiting for timer event");
#endif
    }
    zmq_stopwatch_stop (watch);
}

128
void create_nonblocking_fdpair (zmq::fd_t *r_, zmq::fd_t *w_)
129
{
130
    int rc = zmq::make_fdpair (r_, w_);
131
    TEST_ASSERT_EQUAL_INT (0, rc);
132 133 134 135
    TEST_ASSERT_NOT_EQUAL (zmq::retired_fd, *r_);
    TEST_ASSERT_NOT_EQUAL (zmq::retired_fd, *w_);
    zmq::unblock_socket (*r_);
    zmq::unblock_socket (*w_);
136 137
}

138
void send_signal (zmq::fd_t w_)
139 140 141
{
#if defined ZMQ_HAVE_EVENTFD
    const uint64_t inc = 1;
142
    ssize_t sz = write (w_, &inc, sizeof (inc));
143 144 145 146
    assert (sz == sizeof (inc));
#else
    {
        char msg[] = "test";
147
        int rc = send (w_, msg, sizeof (msg), 0);
148 149 150 151 152
        assert (rc == sizeof (msg));
    }
#endif
}

153
void close_fdpair (zmq::fd_t w_, zmq::fd_t r_)
154
{
155
    int rc = closesocket (w_);
156 157
    TEST_ASSERT_EQUAL_INT (0, rc);
#if !defined ZMQ_HAVE_EVENTFD
158
    rc = closesocket (r_);
159 160
    TEST_ASSERT_EQUAL_INT (0, rc);
#else
161
    LIBZMQ_UNUSED (r_);
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
#endif
}

void test_add_fd_and_start_and_receive_data ()
{
    zmq::thread_ctx_t thread_ctx;
    zmq::poller_t poller (thread_ctx);

    zmq::fd_t r, w;
    create_nonblocking_fdpair (&r, &w);

    test_events_t events (r, poller);

    zmq::poller_t::handle_t handle = poller.add_fd (r, &events);
    events.set_handle (handle);
    poller.set_pollin (handle);
    poller.start ();

    send_signal (w);

    wait_in_events (events);

    // required cleanup
    close_fdpair (w, r);
}

void test_add_fd_and_remove_by_timer ()
{
    zmq::fd_t r, w;
    create_nonblocking_fdpair (&r, &w);

    zmq::thread_ctx_t thread_ctx;
    zmq::poller_t poller (thread_ctx);

    test_events_t events (r, poller);

    zmq::poller_t::handle_t handle = poller.add_fd (r, &events);
    events.set_handle (handle);

    poller.add_timer (50, &events, 0);
    poller.start ();

    wait_timer_events (events);

    // required cleanup
    close_fdpair (w, r);
}

210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
#ifdef _WIN32
void test_add_fd_with_pending_failing_connect ()
{
    zmq::thread_ctx_t thread_ctx;
    zmq::poller_t poller (thread_ctx);

    zmq::fd_t bind_socket = socket (AF_INET, SOCK_STREAM, 0);
    sockaddr_in addr = {0};
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
    addr.sin_port = 0;
    TEST_ASSERT_EQUAL_INT (0, bind (bind_socket,
                                    reinterpret_cast<const sockaddr *> (&addr),
                                    sizeof (addr)));

    int addr_len = static_cast<int> (sizeof (addr));
    TEST_ASSERT_EQUAL_INT (0, getsockname (bind_socket,
                                           reinterpret_cast<sockaddr *> (&addr),
                                           &addr_len));

    zmq::fd_t connect_socket = socket (AF_INET, SOCK_STREAM, 0);
    zmq::unblock_socket (connect_socket);

    TEST_ASSERT_EQUAL_INT (
      -1, connect (connect_socket, reinterpret_cast<const sockaddr *> (&addr),
                   sizeof (addr)));
    TEST_ASSERT_EQUAL_INT (WSAEWOULDBLOCK, WSAGetLastError ());

    test_events_t events (connect_socket, poller);

    zmq::poller_t::handle_t handle = poller.add_fd (connect_socket, &events);
    events.set_handle (handle);
    poller.set_pollin (handle);
    poller.start ();

    wait_in_events (events);

    int value;
    int value_len = sizeof (value);
    TEST_ASSERT_EQUAL_INT (0, getsockopt (connect_socket, SOL_SOCKET, SO_ERROR,
                                          reinterpret_cast<char *> (&value),
                                          &value_len));
    TEST_ASSERT_EQUAL_INT (WSAECONNREFUSED, value);

    // required cleanup
    close (connect_socket);
    close (bind_socket);
}
#endif

260 261
int main (void)
{
262 263 264
    UNITY_BEGIN ();

    zmq::initialize_network ();
265 266 267
    setup_test_environment ();

    RUN_TEST (test_create);
268 269 270
    RUN_TEST (test_add_fd_and_start_and_receive_data);
    RUN_TEST (test_add_fd_and_remove_by_timer);

271 272 273 274
#if defined _WIN32
    RUN_TEST (test_add_fd_with_pending_failing_connect);
#endif

275
    zmq::shutdown_network ();
276 277 278

    return UNITY_END ();
}