File: test_uv_send.c

package info (click to toggle)
raft 0.22.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,504 kB
  • sloc: ansic: 37,539; makefile: 264; sh: 77; python: 22
file content (414 lines) | stat: -rw-r--r-- 12,470 bytes parent folder | download | duplicates (2)
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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#include <unistd.h>

#include "../lib/runner.h"
#include "../lib/tcp.h"
#include "../lib/uv.h"

/******************************************************************************
 *
 * Fixture with a libuv-based raft_io instance and some pre-set messages.
 *
 *****************************************************************************/

#define N_MESSAGES 5

struct fixture
{
    FIXTURE_UV_DEPS;
    FIXTURE_TCP_SERVER;
    FIXTURE_UV;
    struct raft_message messages[N_MESSAGES];
};

/******************************************************************************
 *
 * Helper macros
 *
 *****************************************************************************/

struct result
{
    int status;
    bool done;
};

static void sendCbAssertResult(struct raft_io_send *req, int status)
{
    struct result *result = req->data;
    munit_assert_int(status, ==, result->status);
    result->done = true;
}

/* Get I'th fixture's message. */
#define MESSAGE(I) (&f->messages[I])

/* Submit a send request for the I'th fixture's message. */
#define SEND_SUBMIT(I, RV, STATUS)                                         \
    struct raft_io_send _req##I;                                           \
    struct result _result##I = {STATUS, false};                            \
    int _rv##I;                                                            \
    _req##I.data = &_result##I;                                            \
    _rv##I =                                                               \
        f->io.send(&f->io, &_req##I, &f->messages[I], sendCbAssertResult); \
    munit_assert_int(_rv##I, ==, RV)

/* Wait for the submit request of the I'th message to finish. */
#define SEND_WAIT(I) LOOP_RUN_UNTIL(&_result##I.done)

/* Submit a send request for the I'th fixture's message and wait for the
 * operation to successfully complete. */
#define SEND(I)                                     \
    do {                                            \
        SEND_SUBMIT(I, 0 /* rv */, 0 /* status */); \
        SEND_WAIT(I);                               \
    } while (0)

/* Submit a send request and assert that it fails synchronously with the
 * given error code and message. */
#define SEND_ERROR(I, RV, ERRMSG)                                    \
    do {                                                             \
        SEND_SUBMIT(I, RV, 0 /* status */);                          \
        /* munit_assert_string_equal(f->transport.errmsg, ERRMSG);*/ \
    } while (0)

/* Submit a send request and wait for the operation to fail with the given code
 * and message. */
#define SEND_FAILURE(I, STATUS, ERRMSG)                             \
    do {                                                            \
        SEND_SUBMIT(I, 0 /* rv */, STATUS);                         \
        SEND_WAIT(I);                                               \
        /*munit_assert_string_equal(f->transport.errmsg, ERRMSG);*/ \
    } while (0)

/******************************************************************************
 *
 * Set up and tear down.
 *
 *****************************************************************************/

static void *setUpDeps(const MunitParameter params[], void *user_data)
{
    struct fixture *f = munit_malloc(sizeof *f);
    SETUP_UV_DEPS;
    SETUP_TCP_SERVER;
    f->io.version = 0; /* Magic value to avoid assuming that io.data is raft */
    f->io.data = f;
    return f;
}

static void tearDownDeps(void *data)
{
    struct fixture *f = data;
    TEAR_DOWN_TCP_SERVER;
    TEAR_DOWN_UV_DEPS;
    free(f);
}

static void *setUp(const MunitParameter params[], void *user_data)
{
    struct fixture *f = setUpDeps(params, user_data);
    unsigned i;
    SETUP_UV;
    raft_uv_set_connect_retry_delay(&f->io, 1);
    for (i = 0; i < N_MESSAGES; i++) {
        struct raft_message *message = &f->messages[i];
        message->type = RAFT_REQUEST_VOTE;
        message->server_id = 1;
        message->server_address = f->server.address;
    }
    return f;
}

static void tearDown(void *data)
{
    struct fixture *f = data;
    TEAR_DOWN_UV;
    tearDownDeps(f);
}

/******************************************************************************
 *
 * raft_io->send()
 *
 *****************************************************************************/

SUITE(send)

/* The first time a request is sent to a server a connection attempt is
 * triggered. If the connection succeeds the request gets written out. */
TEST(send, first, setUp, tearDown, 0, NULL)
{
    struct fixture *f = data;
    SEND(0);
    return MUNIT_OK;
}

/* The second time a request is sent it re-uses the connection that was already
 * established */
TEST(send, second, setUp, tearDown, 0, NULL)
{
    struct fixture *f = data;
    SEND(0);
    SEND(0);
    return MUNIT_OK;
}

/* Submit a few send requests in parallel. */
TEST(send, parallel, setUp, tearDown, 0, NULL)
{
    struct fixture *f = data;
    SEND_SUBMIT(0 /* message */, 0 /* rv */, 0 /* status */);
    SEND_SUBMIT(1 /* message */, 0 /* rv */, 0 /* status */);
    SEND_WAIT(0);
    SEND_WAIT(1);
    return MUNIT_OK;
}

/* Send a request vote result message. */
TEST(send, voteResult, setUp, tearDown, 0, NULL)
{
    struct fixture *f = data;
    MESSAGE(0)->type = RAFT_REQUEST_VOTE_RESULT;
    SEND(0);
    return MUNIT_OK;
}

/* Send an append entries message. */
TEST(send, appendEntries, setUp, tearDown, 0, NULL)
{
    struct fixture *f = data;
    struct raft_entry entries[2];
    entries[0].buf.base = raft_malloc(16);
    entries[0].buf.len = 16;
    entries[1].buf.base = raft_malloc(8);
    entries[1].buf.len = 8;

    MESSAGE(0)->type = RAFT_APPEND_ENTRIES;
    MESSAGE(0)->append_entries.entries = entries;
    MESSAGE(0)->append_entries.n_entries = 2;

    SEND(0);

    raft_free(entries[0].buf.base);
    raft_free(entries[1].buf.base);

    return MUNIT_OK;
}

/* Send an append entries message with zero entries (i.e. a heartbeat). */
TEST(send, heartbeat, setUp, tearDown, 0, NULL)
{
    struct fixture *f = data;
    MESSAGE(0)->type = RAFT_APPEND_ENTRIES;
    MESSAGE(0)->append_entries.entries = NULL;
    MESSAGE(0)->append_entries.n_entries = 0;
    SEND(0);
    return MUNIT_OK;
}

/* Send an append entries result message. */
TEST(send, appendEntriesResult, setUp, tearDown, 0, NULL)
{
    struct fixture *f = data;
    MESSAGE(0)->type = RAFT_APPEND_ENTRIES_RESULT;
    SEND(0);
    return MUNIT_OK;
}

/* Send an install snapshot message. */
TEST(send, installSnapshot, setUp, tearDown, 0, NULL)
{
    struct fixture *f = data;
    struct raft_install_snapshot *p = &MESSAGE(0)->install_snapshot;
    int rv;

    MESSAGE(0)->type = RAFT_INSTALL_SNAPSHOT;

    raft_configuration_init(&p->conf);
    rv = raft_configuration_add(&p->conf, 1, "1", RAFT_VOTER);
    munit_assert_int(rv, ==, 0);

    p->data.len = 8;
    p->data.base = raft_malloc(p->data.len);

    SEND(0);

    raft_configuration_close(&p->conf);
    raft_free(p->data.base);

    return MUNIT_OK;
}

/* A connection attempt fails asynchronously after the connect function
 * returns. */
TEST(send, noConnection, setUp, tearDownDeps, 0, NULL)
{
    struct fixture *f = data;
    MESSAGE(0)->server_address = "127.0.0.1:123456";
    SEND_SUBMIT(0 /* message */, 0 /* rv */, RAFT_CANCELED /* status */);
    TEAR_DOWN_UV;
    return MUNIT_OK;
}

/* The message has an invalid IPv4 address. */
TEST(send, badAddress, setUp, tearDownDeps, 0, NULL)
{
    struct fixture *f = data;
    MESSAGE(0)->server_address = "1";
    SEND_SUBMIT(0 /* message */, 0 /* rv */, RAFT_CANCELED /* status */);
    TEAR_DOWN_UV;
    return MUNIT_OK;
}

/* Make sure UvSend doesn't use a stale connection for a certain server id
 * by first sending a message to a valid address and then sending a message to
 * an invalid address, making sure the valid connection is not reused.
 * Afterwards assert that a send to the correct address still succeeds. */
TEST(send, changeToUnconnectedAddress, setUp, tearDownDeps, 0, NULL)
{
    struct fixture *f = data;

    /* Send a message to a server and a connected address */
    SEND(0);

    /* Send a message to the same server, but update the address to an
     * unconnected address and assert it fails. */
    munit_assert_ullong(MESSAGE(0)->server_id, ==, MESSAGE(1)->server_id);
    MESSAGE(1)->server_address = "127.0.0.2:1";
    SEND_SUBMIT(1 /* message */, 0 /* rv */, RAFT_CANCELED /* status */);

    /* Send another message to the same server and connected address */
    munit_assert_ullong(MESSAGE(0)->server_id, ==, MESSAGE(2)->server_id);
    SEND(2);

    /* Send another message to the same server and connected address */
    munit_assert_ullong(MESSAGE(0)->server_id, ==, MESSAGE(3)->server_id);
    SEND(3);

    TEAR_DOWN_UV;
    return MUNIT_OK;
}

/* The message has an invalid type. */
TEST(send, badMessage, setUp, tearDown, 0, NULL)
{
    struct fixture *f = data;
    MESSAGE(0)->type = 666;
    SEND_ERROR(0, RAFT_MALFORMED, "");
    return MUNIT_OK;
}

/* Old send requests that have accumulated and could not yet be sent are
 * progressively evicted. */
TEST(send, evictOldPending, setUp, tearDownDeps, 0, NULL)
{
    struct fixture *f = data;
    TCP_SERVER_STOP;
    SEND_SUBMIT(0 /* message */, 0 /* rv */, RAFT_NOCONNECTION /* status */);
    SEND_SUBMIT(1 /* message */, 0 /* rv */, RAFT_CANCELED /* status */);
    SEND_SUBMIT(2 /* message */, 0 /* rv */, RAFT_CANCELED /* status */);
    SEND_SUBMIT(3 /* message */, 0 /* rv */, RAFT_CANCELED /* status */);
    SEND_WAIT(0);
    TEAR_DOWN_UV;
    return MUNIT_OK;
}

/* After the connection is established the peer dies and then comes back a
 * little bit later. */
TEST(send, reconnectAfterWriteError, setUp, tearDown, 0, NULL)
{
    struct fixture *f = data;
    int socket;
    SEND(0);
    socket = TcpServerAccept(&f->server);
    close(socket);
    SEND_FAILURE(0, RAFT_IOERR, "");
    SEND(0);
    return MUNIT_OK;
}

/* After the connection is established the peer dies and then comes back a
 * little bit later. At the time the peer died there where several writes
 * pending. */
TEST(send, reconnectAfterMultipleWriteErrors, setUp, tearDown, 0, NULL)
{
    struct fixture *f = data;
    int socket;
    signal(SIGPIPE, SIG_IGN);
    SEND(0);
    socket = TcpServerAccept(&f->server);
    close(socket);
    SEND_SUBMIT(1 /* message */, 0 /* rv */, RAFT_IOERR /* status */);
    SEND_SUBMIT(2 /* message */, 0 /* rv */, RAFT_IOERR /* status */);
    SEND_WAIT(1);
    SEND_WAIT(2);
    SEND(3);
    return MUNIT_OK;
}

static char *oomHeapFaultDelay[] = {"0", "1", "2", "3", "4", NULL};
static char *oomHeapFaultRepeat[] = {"1", NULL};

static MunitParameterEnum oomParams[] = {
    {TEST_HEAP_FAULT_DELAY, oomHeapFaultDelay},
    {TEST_HEAP_FAULT_REPEAT, oomHeapFaultRepeat},
    {NULL, NULL},
};

/* Out of memory conditions. */
TEST(send, oom, setUp, tearDown, 0, oomParams)
{
    struct fixture *f = data;
    HEAP_FAULT_ENABLE;
    SEND_ERROR(0, RAFT_NOMEM, "");
    return MUNIT_OK;
}

static char *oomAsyncHeapFaultDelay[] = {"2", NULL};
static char *oomAsyncHeapFaultRepeat[] = {"1", NULL};

static MunitParameterEnum oomAsyncParams[] = {
    {TEST_HEAP_FAULT_DELAY, oomAsyncHeapFaultDelay},
    {TEST_HEAP_FAULT_REPEAT, oomAsyncHeapFaultRepeat},
    {NULL, NULL},
};

/* Transient out of memory error happening after send() has returned. */
TEST(send, oomAsync, setUp, tearDown, 0, oomAsyncParams)
{
    struct fixture *f = data;
    SEND(0);
    return MUNIT_OK;
}

/* The backend gets closed while there is a pending write. */
TEST(send, closeDuringWrite, setUp, tearDownDeps, 0, NULL)
{
    struct fixture *f = data;
    struct raft_entry entry;

    /* Set a very large message that is likely to fill the socket buffer.
     * TODO: figure a more deterministic way to choose the value. */
    entry.buf.len = 1024 * 1024 * 8;
    entry.buf.base = raft_malloc(entry.buf.len);

    MESSAGE(0)->type = RAFT_APPEND_ENTRIES;
    MESSAGE(0)->append_entries.entries = &entry;
    MESSAGE(0)->append_entries.n_entries = 1;

    SEND_SUBMIT(0 /* message */, 0 /* rv */, RAFT_CANCELED /* status */);
    TEAR_DOWN_UV;

    raft_free(entry.buf.base);

    return MUNIT_OK;
}

/* The backend gets closed while there is a pending connect request. */
TEST(send, closeDuringConnection, setUp, tearDownDeps, 0, NULL)
{
    struct fixture *f = data;
    SEND_SUBMIT(0 /* message */, 0 /* rv */, RAFT_CANCELED /* status */);
    TEAR_DOWN_UV;
    return MUNIT_OK;
}