1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
|
#include <raft.h>
#include <uv.h>
#include <unistd.h>
#include "../../../src/lib/transport.h"
#include "../../lib/endpoint.h"
#include "../../lib/runner.h"
#include "../../lib/uv.h"
TEST_MODULE(ext_uv);
/******************************************************************************
*
* Helpers
*
******************************************************************************/
struct fixture
{
struct uv_loop_s loop;
struct uv_stream_s *listener;
struct test_endpoint endpoint;
int client;
union {
uv_tcp_t tcp;
uv_pipe_t pipe;
uv_stream_t stream;
};
};
/* Return a buffer of size TEST_SOCKET_MIN_BUF_SIZE */
static uv_buf_t *buf_malloc(void)
{
uv_buf_t *buf = munit_malloc(sizeof *buf);
buf->base = munit_malloc(TEST_SOCKET_MIN_BUF_SIZE);
buf->len = TEST_SOCKET_MIN_BUF_SIZE;
return buf;
}
/* Free the buffer returned by buf_malloc() */
static void buf_free(uv_buf_t *buf)
{
free(buf->base);
free(buf);
}
/******************************************************************************
*
* Parameters
*
******************************************************************************/
/* Run the tests using both TCP and Unix sockets. */
static MunitParameterEnum endpointParams[] = {
{TEST_ENDPOINT_FAMILY, test_endpoint_family_values},
{NULL, NULL},
};
/******************************************************************************
*
* Setup and tear down
*
******************************************************************************/
static void listenCb(uv_stream_t *listener, int status)
{
struct fixture *f = listener->data;
int rv;
munit_assert_int(status, ==, 0);
switch (listener->type) {
case UV_TCP:
rv = uv_tcp_init(&f->loop, &f->tcp);
munit_assert_int(rv, ==, 0);
break;
case UV_NAMED_PIPE:
rv = uv_pipe_init(&f->loop, &f->pipe, 0);
munit_assert_int(rv, ==, 0);
break;
default:
munit_assert(0);
}
rv = uv_accept(listener, &f->stream);
munit_assert_int(rv, ==, 0);
}
static void *setup(const MunitParameter params[], void *user_data)
{
struct fixture *f = munit_malloc(sizeof *f);
int rv;
(void)user_data;
test_uv_setup(params, &f->loop);
test_endpoint_setup(&f->endpoint, params);
rv = transport__stream(&f->loop, f->endpoint.fd, &f->listener);
munit_assert_int(rv, ==, 0);
f->listener->data = f;
rv = uv_listen(f->listener, 128, listenCb);
munit_assert_int(rv, ==, 0);
f->client = test_endpoint_connect(&f->endpoint);
test_uv_run(&f->loop, 1);
return f;
}
static void tear_down(void *data)
{
struct fixture *f = data;
int rv;
rv = close(f->client);
munit_assert_int(rv, ==, 0);
uv_close((struct uv_handle_s *)f->listener, (uv_close_cb)raft_free);
test_endpoint_tear_down(&f->endpoint);
uv_close((uv_handle_t *)(&f->stream), NULL);
test_uv_stop(&f->loop);
test_uv_tear_down(&f->loop);
free(f);
}
/******************************************************************************
*
* uv_write
*
******************************************************************************/
TEST_SUITE(write);
TEST_SETUP(write, setup);
TEST_TEAR_DOWN(write, tear_down);
/* Writing an amount of data below the buffer size makes that data immediately
* available for reading. */
TEST_CASE(write, sync, endpointParams)
{
struct fixture *f = data;
uv_write_t req;
uv_buf_t *buf1 = buf_malloc();
uv_buf_t *buf2 = buf_malloc();
int rv;
(void)params;
rv = uv_write(&req, &f->stream, buf1, 1, NULL);
munit_assert_int(rv, ==, 0);
rv = read(f->client, buf2->base, buf2->len);
munit_assert_int(rv, ==, buf2->len);
test_uv_run(&f->loop, 1);
buf_free(buf1);
buf_free(buf2);
return MUNIT_OK;
}
/******************************************************************************
*
* uv_read
*
******************************************************************************/
TEST_SUITE(read);
TEST_SETUP(read, setup);
TEST_TEAR_DOWN(read, tear_down);
static void test_read_sync__alloc_cb(uv_handle_t *stream,
size_t _,
uv_buf_t *buf)
{
(void)stream;
(void)_;
buf->len = TEST_SOCKET_MIN_BUF_SIZE;
buf->base = munit_malloc(TEST_SOCKET_MIN_BUF_SIZE);
}
static void test_read_sync__read_cb(uv_stream_t *stream,
ssize_t nread,
const uv_buf_t *buf)
{
bool *read_cb_called;
/* Apprently there's an empty read before the actual one. */
if (nread == 0) {
free(buf->base);
return;
}
munit_assert_int(nread, ==, TEST_SOCKET_MIN_BUF_SIZE);
munit_assert_int(buf->len, ==, TEST_SOCKET_MIN_BUF_SIZE);
read_cb_called = stream->data;
*read_cb_called = true;
free(buf->base);
}
/* Reading an amount of data below the buffer happens synchronously. */
TEST_CASE(read, sync, endpointParams)
{
struct fixture *f = data;
uv_buf_t *buf = buf_malloc();
int rv;
bool read_cb_called;
(void)params;
f->stream.data = &read_cb_called;
rv = uv_read_start(&f->stream, test_read_sync__alloc_cb,
test_read_sync__read_cb);
rv = write(f->client, buf->base, buf->len);
munit_assert_int(rv, ==, buf->len);
test_uv_run(&f->loop, 1);
munit_assert_true(read_cb_called);
buf_free(buf);
return MUNIT_OK;
}
|