The FreeRADIUS server $Id: a4e7f8c94e8038b7627bcf52356b7ed15955c1d7 $
Loading...
Searching...
No Matches
trunk_tests.c
Go to the documentation of this file.
1#include <freeradius-devel/util/test/acutest.h>
2#include <freeradius-devel/util/test/acutest_helpers.h>
3#include <freeradius-devel/util/syserror.h>
4#include <sys/socket.h>
5
6#define TRUNK_TESTS 1
7#include "trunk.c"
8
9typedef struct {
10 trunk_request_t *treq; //!< Trunk request.
11 bool cancelled; //!< Seen by the cancelled callback.
12 bool completed; //!< Seen by the complete callback.
13 bool failed; //!< Seen by the failed callback.
14 bool freed; //!< Seen by the free callback.
15 bool signal_partial; //!< Muxer should signal that this request is partially written.
16 bool signal_cancel_partial; //!< Muxer should signal that this request is partially cancelled.
17 bool complete_on_write; //!< Muxer signals complete after the write, instead of sent.
18 ///< The request needs no reply, as with replicated packets.
19 int priority; //!< Priority of request
21
22typedef struct {
23 uint64_t cancelled; //!< Count of tests in this run that were cancelled.
24 uint64_t completed; //!< Count of tests in this run that completed.
25 uint64_t failed; //!< Count of tests in this run that failed.
26 uint64_t freed; //!< Count of tests in this run that were freed.
28
29/** A request the muxer has written, waiting for the demuxer
30 *
31 * The muxer keeps written requests in memory, and writes one byte to the
32 * socket pair for each. The demuxer takes one request off the list for
33 * each byte that comes back. Writing the request pointers themselves
34 * through the socket would make every pointer the demuxer reads back
35 * untrusted data.
36 */
37typedef struct {
38 fr_dlist_t entry; //!< Entry in test_conn_t sent.
39 test_proto_request_t *preq; //!< Request that was written.
41
42typedef struct {
43 int fd[2]; //!< Socket pair. fd[1] loops data back to fd[0].
44 fr_dlist_head_t sent; //!< Requests written, in the order they were written.
46
47#define DEBUG_LVL_SET if (acutest_verbose_level_ >= 3) fr_debug_lvl = L_DBG_LVL_4 + 1
48
49/** Write a request to the connection
50 *
51 * @return
52 * - 1 if the request was written.
53 * - 0 if the socket would block.
54 * - -1 on error.
55 */
57{
58 test_sent_t *sent;
59 uint8_t wake = 0;
60 ssize_t slen;
61
62 slen = write(tc->fd[0], &wake, sizeof(wake));
63 if (slen < 0) return -1;
64 if (slen == 0) return 0;
65
66 MEM(sent = talloc(tc, test_sent_t));
67 sent->preq = preq;
68 fr_dlist_insert_tail(&tc->sent, sent);
69
70 return 1;
71}
72
73static void test_mux(UNUSED fr_event_list_t *el, trunk_connection_t *tconn, connection_t *conn, UNUSED void *uctx)
74{
75 trunk_request_t *treq;
76 size_t count = 0;
77 test_conn_t *tc = talloc_get_type_abort(conn->h, test_conn_t);
78 int ret;
79
80 while (trunk_connection_pop_request(&treq, tconn) == 0) {
81 test_proto_request_t *preq = treq->pub.preq;
82 count++;
83
84 /*
85 * Simulate a partial write
86 */
87 if (preq && preq->signal_partial) {
89 preq->signal_partial = false;
90 break;
91 }
92
93 if (acutest_verbose_level_ >= 3) printf("%s - Wrote %p\n", __FUNCTION__, preq);
94
95 ret = test_conn_write(tc, preq);
96 if (ret <= 0) return;
97
98 /*
99 * The request needs no reply. The demuxer skips the byte which was written, because
100 * the request has been freed.
101 */
102 if (preq && preq->complete_on_write) {
104 continue;
105 }
106
108 }
109 TEST_CHECK(count > 0);
110}
111
113{
114 trunk_request_t *treq;
115 size_t count = 0;
116 test_conn_t *tc = talloc_get_type_abort(conn->h, test_conn_t);
117 int ret;
118
119 /*
120 * For cancellation we just do
121 */
122 while ((trunk_connection_pop_cancellation(&treq, tconn) == 0)) {
123 test_proto_request_t *preq = treq->pub.preq;
124 count++;
125
126 /*
127 * Simulate a partial cancel write
128 */
129 if (preq && preq->signal_cancel_partial) {
131 preq->signal_cancel_partial = false;
132 break;
133 }
134
135 if (acutest_verbose_level_ >= 3) printf("%s - Wrote %p\n", __FUNCTION__, preq);
136 ret = test_conn_write(tc, preq);
137 if (ret < 0) {
138 fr_perror("%s - %s", __FUNCTION__, fr_syserror(errno));
139 return;
140 }
141 if (ret == 0) return;
142
144 }
145 TEST_CHECK(count > 0);
146}
147
149{
150 test_conn_t *tc = talloc_get_type_abort(conn->h, test_conn_t);
152 test_sent_t *sent;
153 uint8_t wake;
154
155 while (read(tc->fd[0], &wake, sizeof(wake)) > 0) {
156 sent = fr_dlist_pop_head(&tc->sent);
157 if (!fr_cond_assert(sent)) break;
158
159 preq = sent->preq;
160 talloc_free(sent);
161
162 if (acutest_verbose_level_ >= 3) printf("%s - Read %p\n", __FUNCTION__, preq);
163
164 if (preq->freed) continue;
165
166 /*
167 * Demuxer can handle both normal requests and cancelled ones
168 */
169 switch (preq->treq->pub.state) {
171 break; /* Hack - just ignore it */
172
175 break;
176
179 break;
180
181 default:
182 fr_assert(0);
183 break;
184 }
185 }
186}
187
188static void _conn_io_error(UNUSED fr_event_list_t *el, UNUSED int fd, UNUSED int flags,
189 UNUSED int fd_errno, void *uctx)
190{
191
192 trunk_connection_t *tconn = talloc_get_type_abort(uctx, trunk_connection_t);
193
195}
196
197static void _conn_io_read(UNUSED fr_event_list_t *el, UNUSED int fd, UNUSED int flags, void *uctx)
198{
199 trunk_connection_t *tconn = talloc_get_type_abort(uctx, trunk_connection_t);
201}
202
203static void _conn_io_write(UNUSED fr_event_list_t *el, UNUSED int fd, UNUSED int flags, void *uctx)
204{
205 trunk_connection_t *tconn = talloc_get_type_abort(uctx, trunk_connection_t);
207}
208
211 trunk_connection_event_t notify_on, UNUSED void *uctx)
212{
213 test_conn_t *tc = talloc_get_type_abort(conn->h, test_conn_t);
214 int fd = tc->fd[0];
215
216 switch (notify_on) {
219 break;
220
222 TEST_CHECK(fr_event_fd_insert(conn, NULL, el, fd, _conn_io_read, NULL, _conn_io_error, tconn) == 0);
223 break;
224
226 TEST_CHECK(fr_event_fd_insert(conn, NULL, el, fd, NULL, _conn_io_write, _conn_io_error, tconn) == 0);
227 break;
228
231 break;
232
233 default:
234 fr_assert(0);
235 }
236}
237
238static void test_request_cancel(UNUSED connection_t *conn, void *preq,
239 UNUSED trunk_cancel_reason_t reason, void *uctx)
240{
241 test_proto_stats_t *stats = uctx;
242 test_proto_request_t *our_preq;
243
244 if (!preq) return;
245
246 our_preq = talloc_get_type_abort(preq, test_proto_request_t);
247 our_preq->cancelled = true;
248 if (stats) stats->cancelled++;
249}
250
251static void test_request_complete(UNUSED request_t *request, void *preq, UNUSED void *rctx, void *uctx)
252{
253 test_proto_stats_t *stats = uctx;
254 test_proto_request_t *our_preq;
255
256 if (!preq) return;
257
258 our_preq = talloc_get_type_abort(preq, test_proto_request_t);
259 our_preq->completed = true;
260 if (stats) stats->completed++;
261}
262
263static void test_request_fail(UNUSED request_t *request, void *preq, UNUSED void *rctx, UNUSED trunk_request_state_t state, void *uctx)
264{
265 test_proto_stats_t *stats = uctx;
266 test_proto_request_t *our_preq;
267
268 if (!preq) return;
269
270 our_preq = talloc_get_type_abort(preq, test_proto_request_t);
271 our_preq->failed = true;
272 if (stats) stats->failed++;
273}
274
275static void test_request_free(UNUSED request_t *request, void *preq, void *uctx)
276{
277 test_proto_stats_t *stats = uctx;
278 test_proto_request_t *our_preq;
279
280 if (!preq) return;
281
282 our_preq = talloc_get_type_abort(preq, test_proto_request_t);
283 our_preq->freed = true;
284 if (stats) stats->freed++;
285}
286
287/** Whenever the second socket in a socket pair is readable, read all pending data, and write it back
288 *
289 */
290static void _conn_io_loopback(UNUSED fr_event_list_t *el, int fd, UNUSED int flags, void *uctx)
291{
292 test_conn_t *tc = talloc_get_type_abort(uctx, test_conn_t);
293 static uint8_t buff[1024];
294 static size_t to_write;
295 ssize_t slen;
296
297 fr_assert(fd == tc->fd[1]);
298
299 while (true) {
300 slen = read(fd, buff, sizeof(buff));
301 if (slen <= 0) return;
302
303 to_write = (size_t)slen;
304
305 if (acutest_verbose_level_ >= 3) printf("%s - Read %zu bytes of data\n", __FUNCTION__, (size_t)slen);
306 slen = write(tc->fd[1], buff, (size_t)to_write);
307 if (slen < 0) return;
308
309 if (slen < (ssize_t)to_write) {
310 to_write -= slen;
311 if (acutest_verbose_level_ >= 3) {
312 printf("%s - Partial write %zu bytes left\n", __FUNCTION__, to_write);
313 }
314 return;
315 } else {
316 if (acutest_verbose_level_ >= 3) printf("%s - Wrote %zu bytes of data\n", __FUNCTION__, (size_t)slen);
317 }
318 }
319}
320
321static void _conn_close(UNUSED fr_event_list_t *el, void *h, UNUSED void *uctx)
322{
323 test_conn_t *tc = talloc_get_type_abort(h, test_conn_t);
324
325 talloc_free_children(tc); /* Clear the IO handlers, and any requests not read back */
326
327 close(tc->fd[0]);
328 close(tc->fd[1]);
329
330 talloc_free(tc);
331}
332
333/** Insert I/O handlers that loop any data back round
334 *
335 */
337{
338 test_conn_t *tc = talloc_get_type_abort(h, test_conn_t);
339
340 /*
341 * This always needs to be inserted
342 */
343 TEST_CHECK(fr_event_fd_insert(tc, NULL, el, tc->fd[1], _conn_io_loopback, NULL, NULL, tc) == 0);
344
346}
347
348/** Allocate a basic socket pair
349 *
350 */
351CC_NO_UBSAN(function) /* UBSAN: false positive - public vs private connection_t trips --fsanitize=function*/
352static connection_state_t _conn_init(void **h_out, connection_t *conn, UNUSED void *uctx)
353{
354 test_conn_t *tc;
355
356 MEM(tc = talloc_zero(conn, test_conn_t));
358 socketpair(AF_UNIX, SOCK_STREAM, 0, tc->fd);
359
360 (void) fr_nonblock(tc->fd[0]);
361 (void) fr_nonblock(tc->fd[1]);
362 connection_signal_on_fd(conn, tc->fd[0]);
363 *h_out = tc;
364
366}
367
370 connection_conf_t const *conn_conf,
371 char const *log_prefix, UNUSED void *uctx)
372{
373 connection_conf_t cstat;
374
375 if (!conn_conf) {
376 memset(&cstat, 0, sizeof(cstat));
377 conn_conf = &cstat;
378 }
379 return connection_alloc(tconn, el,
381 .init = _conn_init,
382 .open = _conn_open,
383 .close = _conn_close
384 },
385 conn_conf,
386 log_prefix, tconn);
387}
388
389static fr_cmp_ret_t test_preq_cmp(void const *a, void const *b)
390{
391 test_proto_request_t const *preq_a = a;
392 test_proto_request_t const *preq_b = b;
393
394 return CMP_PREFER_SMALLER(preq_a->priority, preq_b->priority); /* our priorities are NOT request->priority */
395}
396
397static trunk_t *test_setup_trunk(TALLOC_CTX *ctx, fr_event_list_t *el, trunk_conf_t *conf, bool with_cancel_mux, void *uctx)
398{
401 .connection_notify = _conn_notify,
402 .request_prioritise = test_preq_cmp,
403 .request_mux = test_mux,
404 .request_demux = test_demux,
405 .request_cancel = test_request_cancel,
406 .request_complete = test_request_complete,
407 .request_fail = test_request_fail,
408 .request_free = test_request_free
409 };
410
411 /*
412 * Function list is copied, so this is OK.
413 */
414 if (with_cancel_mux) io_funcs.request_cancel_mux = test_cancel_mux;
415
416 return trunk_alloc(ctx, el, &io_funcs, conf, "test_socket_pair", uctx, false, NULL);
417}
418
420{
421 TALLOC_CTX *ctx = talloc_init_const("test");
422 trunk_t *trunk;
424 int events;
425
427 .start = 2,
428 .min = 2
429 };
432 .request_prioritise = fr_pointer_cmp,
433 };
434
436
437 el = fr_event_list_alloc(ctx, NULL, NULL);
438
440
441 trunk = trunk_alloc(ctx, el, &io_funcs, &conf, "test_socket_pair", NULL, false, NULL);
442 TEST_CHECK(trunk != NULL);
443 if (!trunk) return;
444
447 TEST_CHECK(events == 2); /* Two I/O write events, no timers */
450
452 TEST_CHECK(events == 0); /* I/O events should have been cleared */
453
454 talloc_free(trunk);
455 talloc_free(ctx);
456}
457
459{
460 TALLOC_CTX *ctx = talloc_init_const("test");
461 trunk_t *trunk;
463 int events;
465 .start = 2,
466 .min = 2,
467 .conn_conf = &(connection_conf_t){
468 .reconnection_delay = fr_time_delta_from_nsec(NSEC / 2)
469 }
470 };
473 .request_prioritise = fr_pointer_cmp,
474 };
476
477 el = fr_event_list_alloc(ctx, NULL, NULL);
478
479 if (!el) return;
480
482
483 trunk = trunk_alloc(ctx, el, &io_funcs, &conf, "test_socket_pair", NULL, false, NULL);
484 TEST_CHECK(trunk != NULL);
485 if (!trunk) return;
486
488 TEST_CHECK(events == 2); /* Two I/O write events, no timers */
492
494 TEST_CHECK(events == 0); /* I/O events should have been cleared */
495 TEST_MSG("Got %d events", events);
496
498
501 TEST_CHECK(events == 1); /* Two timer events but event loops only adds one to the total*/
502 TEST_MSG("Got %d events", events);
504
506
508 TEST_CHECK(events == 2); /* Two I/O write events, no timers */
510
513 TEST_CHECK(events == 0); /* I/O events should have been cleared */
514
515 talloc_free(trunk);
516 talloc_free(ctx);
517}
518
519CC_NO_UBSAN(function) /* UBSAN: false positive - public vs private connection_t trips --fsanitize=function*/
520static connection_state_t _conn_init_no_signal(void **h_out, connection_t *conn, UNUSED void *uctx)
521{
522 test_conn_t *tc;
523
524 MEM(tc = talloc_zero(conn, test_conn_t));
526 socketpair(AF_UNIX, SOCK_STREAM, 0, tc->fd);
527 *h_out = tc;
528
530}
531
535 char const *log_prefix, void *uctx)
536{
537 return connection_alloc(tconn, el,
539 .init = _conn_init_no_signal,
540 .open = _conn_open,
541 .close = _conn_close
542 },
544 .connection_timeout = fr_time_delta_from_sec(1),
545 .reconnection_delay = fr_time_delta_from_sec(1)
546 },
547 log_prefix, uctx);
548}
549
551{
552 TALLOC_CTX *ctx = talloc_init_const("test");
553 trunk_t *trunk;
555 int events;
556 trunk_connection_t *tconn;
558 .start = 1,
559 .min = 1
560 };
563 .request_prioritise = fr_pointer_cmp,
564 };
565
567
568 el = fr_event_list_alloc(ctx, NULL, NULL);
569
571
572
573 trunk = trunk_alloc(ctx, el, &io_funcs, &conf, "test_socket_pair", NULL, false, NULL);
574 TEST_CHECK(trunk != NULL);
575 if (!trunk) return;
576
577 /*
578 * Trigger connection timeout
579 */
581 TEST_CHECK(fr_event_list_num_timers(el) == 1); /* One timer event for the connection timeout */
583 TEST_CHECK(events == 1); /* We didn't install the I/O events */
584
585 tconn = fr_dlist_head(&trunk->connecting);
586 TEST_CHECK(tconn != NULL);
587 if (tconn == NULL) return;
588
591
592 /*
593 * Timeout should now fire
594 */
596
597 /*
598 * Connection delay not implemented for timed out connections
599 */
602
604 TEST_CHECK(events == 0); /* I/O events should have been cleared */
605
606 talloc_free(trunk);
607 talloc_free(ctx);
608}
609
612 UNUSED connection_conf_t const *conn_conf,
613 char const *log_prefix, void *uctx)
614{
615 return connection_alloc(tconn, el,
617 .init = _conn_init,
618 .open = _conn_open,
619 .close = _conn_close
620 },
622 .connection_timeout = fr_time_delta_from_sec(1),
623 .reconnection_delay = fr_time_delta_from_sec(1)
624 },
625 log_prefix, uctx);
626}
627
629{
630 TALLOC_CTX *ctx = talloc_init_const("test");
631 trunk_t *trunk;
633 int events;
634 trunk_connection_t *tconn;
636 .start = 1,
637 .min = 1,
638 .conn_conf = &(connection_conf_t){
639 .reconnection_delay = fr_time_delta_from_sec(1),
640 .connection_timeout = fr_time_delta_from_sec(1)
641 }
642 };
645 .request_prioritise = fr_pointer_cmp,
646 };
647
649
650 el = fr_event_list_alloc(ctx, NULL, NULL);
652
653 trunk = trunk_alloc(ctx, el, &io_funcs, &conf, "test_socket_pair", NULL, false, NULL);
654 TEST_CHECK(trunk != NULL);
655 if (!trunk) return;
656
657 /*
658 * Trigger connection timeout
659 */
661 TEST_CHECK(fr_event_list_num_timers(el) == 1); /* One timer event for the connection timeout */
663 TEST_CHECK(events == 2); /* We didn't install the I/O events */
665
666 tconn = fr_minmax_heap_min_peek(trunk->active);
667 TEST_CHECK(tconn != NULL);
668 if (tconn == NULL) return;
669
672
673 /*
674 * Trigger reconnection
675 */
678
680 TEST_CHECK(events == 0); /* Reconnect delay not ready to fire yet, no I/O handlers installed */
681 TEST_CHECK(fr_event_list_num_timers(el) == 1); /* One timer event for reconnect delay */
682
685 TEST_CHECK(events == 1); /* Reconnect delay should now be ready to fire */
686
687 fr_event_service(el); /* Services the timer, which then triggers init */
688
691
693 TEST_CHECK(events == 1); /* Should have a pending I/O event and a timer */
694
695 talloc_free(trunk);
696 talloc_free(ctx);
697}
698
699/*
700 * Test basic enqueue and dequeue
701 */
702static void test_enqueue_basic(void)
703{
704 TALLOC_CTX *ctx = talloc_init_const("test");
705 trunk_t *trunk;
708 .start = 1,
709 .min = 1,
710 .manage_interval = fr_time_delta_from_nsec(NSEC / 2)
711 };
713 trunk_request_t *treq = NULL;
714 trunk_enqueue_t rcode;
715
717
718 el = fr_event_list_alloc(ctx, NULL, NULL);
720
721 trunk = test_setup_trunk(ctx, el, &conf, true, NULL);
722
723 /*
724 * Our preq is a pointer to the trunk
725 * request so we don't have to manage
726 * a tree of requests and responses.
727 */
728 preq = talloc_zero(NULL, test_proto_request_t);
729
730 /*
731 * The trunk is active, but there's no
732 * connections.
733 *
734 * We're under the current request limit
735 * so the request should enter the
736 * backlog.
737 */
738 rcode = trunk_request_enqueue(&treq, trunk, NULL, preq, NULL);
739 preq->treq = treq;
741
743
744 /*
745 * Allow the connection to establish
746 */
749
752
753 /*
754 * Should now be active and have a write event
755 * inserted into the event loop.
756 */
758
759 /*
760 * Trunk should be signalled the connection is
761 * writable.
762 *
763 * We should then:
764 * - Pop a request from the pending queue.
765 * - Write the request to the socket pair
766 */
769
771
772 /*
773 * Gives the loopback function a chance
774 * to read the data, and write it back.
775 */
778
779 /*
780 * Trunk should be signalled the connection is
781 * readable.
782 *
783 * We should then:
784 * - Read the (looped back) response.
785 * - Signal the trunk that the connection is readable.
786 */
789
790 TEST_CHECK(preq->completed == true);
791 TEST_CHECK(preq->failed == false);
792 TEST_CHECK(preq->cancelled == false);
793 TEST_CHECK(preq->freed == true);
794 talloc_free(preq);
795
796 talloc_free(trunk);
797 talloc_free(ctx);
798}
799
800/*
801 * Test request cancellations when the connection is in various states
802 */
804{
805 TALLOC_CTX *ctx = talloc_init_const("test");
806 trunk_t *trunk;
809 .start = 1,
810 .min = 1,
811 .manage_interval = fr_time_delta_from_nsec(NSEC / 2)
812 };
814 trunk_request_t *treq = NULL;
815
817
818 el = fr_event_list_alloc(ctx, NULL, NULL);
820
821 trunk = test_setup_trunk(ctx, el, &conf, false, NULL);
822 preq = talloc_zero(NULL, test_proto_request_t);
823 trunk_request_enqueue(&treq, trunk, NULL, preq, NULL);
824
825 TEST_CASE("cancellation via trunk free - TRUNK_REQUEST_STATE_BACKLOG");
826 talloc_free(trunk);
827 TEST_CHECK(preq->completed == false);
828 TEST_CHECK(preq->failed == true);
829 TEST_CHECK(preq->cancelled == false);
830 TEST_CHECK(preq->freed == true);
831 talloc_free(preq);
832
833 TEST_CASE("cancellation via signal - TRUNK_REQUEST_STATE_BACKLOG");
834 trunk = test_setup_trunk(ctx, el, &conf, false, NULL);
835 preq = talloc_zero(NULL, test_proto_request_t);
836 treq = NULL;
837 trunk_request_enqueue(&treq, trunk, NULL, preq, NULL);
838 preq->treq = treq;
841
842 TEST_CHECK(preq->completed == false);
843 TEST_CHECK(preq->failed == false); /* Request/rctx not guaranteed after signal, so can't call fail */
844 TEST_CHECK(preq->cancelled == false);
845 TEST_CHECK(preq->freed == true);
846 talloc_free(preq);
847 talloc_free(trunk);
848
849 TEST_CASE("cancellation via trunk free - TRUNK_REQUEST_STATE_PARTIAL");
850 trunk = test_setup_trunk(ctx, el, &conf, false, NULL);
851 preq = talloc_zero(NULL, test_proto_request_t);
852 preq->signal_partial = true;
853 treq = NULL;
854 trunk_request_enqueue(&treq, trunk, NULL, preq, NULL);
855 preq->treq = treq;
856
857 fr_event_corral(el, test_time_base, false); /* Connect the connection */
859
860 fr_event_corral(el, test_time_base, false); /* Send the request */
862
864
865 talloc_free(trunk);
866
867 TEST_CHECK(preq->completed == false);
868 TEST_CHECK(preq->failed == true);
869 TEST_CHECK(preq->cancelled == true);
870 TEST_CHECK(preq->freed == true);
871 talloc_free(preq);
872
873 TEST_CASE("cancellation via signal - TRUNK_REQUEST_STATE_PARTIAL");
874 trunk = test_setup_trunk(ctx, el, &conf, false, NULL);
875 preq = talloc_zero(NULL, test_proto_request_t);
876 preq->signal_partial = true;
877 treq = NULL;
878 trunk_request_enqueue(&treq, trunk, NULL, preq, NULL);
879 preq->treq = treq;
880
881 fr_event_corral(el, test_time_base, false); /* Connect the connection */
883
884 fr_event_corral(el, test_time_base, false); /* Send the request */
886
890
891 TEST_CHECK(preq->completed == false);
892 TEST_CHECK(preq->failed == false); /* Request/rctx not guaranteed after signal, so can't call fail */
893 TEST_CHECK(preq->cancelled == true);
894 TEST_CHECK(preq->freed == true);
895 talloc_free(preq);
896 talloc_free(trunk);
897
898 TEST_CASE("cancellation via trunk free - TRUNK_REQUEST_STATE_SENT");
899 trunk = test_setup_trunk(ctx, el, &conf, false, NULL);
900 preq = talloc_zero(NULL, test_proto_request_t);
901 treq = NULL;
902 trunk_request_enqueue(&treq, trunk, NULL, preq, NULL);
903 preq->treq = treq;
904
905 fr_event_corral(el, test_time_base, false); /* Connect the connection */
907
908 fr_event_corral(el, test_time_base, false); /* Send the request */
910
912 talloc_free(trunk);
913
914 TEST_CHECK(preq->completed == false);
915 TEST_CHECK(preq->failed == true);
916 TEST_CHECK(preq->cancelled == true);
917 TEST_CHECK(preq->freed == true);
918 talloc_free(preq);
919
920 TEST_CASE("cancellation via signal - TRUNK_REQUEST_STATE_SENT");
921 trunk = test_setup_trunk(ctx, el, &conf, false, NULL);
922 preq = talloc_zero(NULL, test_proto_request_t);
923 treq = NULL;
924 trunk_request_enqueue(&treq, trunk, NULL, preq, NULL);
925 preq->treq = treq;
926
927 fr_event_corral(el, test_time_base, false); /* Connect the connection */
929
930 fr_event_corral(el, test_time_base, false); /* Send the request */
932
936
937 TEST_CHECK(preq->completed == false);
938 TEST_CHECK(preq->failed == false); /* Request/rctx not guaranteed after signal, so can't call fail */
939 TEST_CHECK(preq->cancelled == true);
940 TEST_CHECK(preq->freed == true);
941 talloc_free(preq);
942 talloc_free(trunk);
943
944 TEST_CASE("cancellation via trunk free - TRUNK_REQUEST_STATE_CANCEL_PARTIAL");
945 trunk = test_setup_trunk(ctx, el, &conf, true, NULL);
946 preq = talloc_zero(NULL, test_proto_request_t);
947 preq->signal_cancel_partial = true;
948 treq = NULL;
949 trunk_request_enqueue(&treq, trunk, NULL, preq, NULL);
950 preq->treq = treq;
951
952 fr_event_corral(el, test_time_base, false); /* Connect the connection */
954
955 fr_event_corral(el, test_time_base, false); /* Send the request */
957
961
962 fr_event_corral(el, test_time_base, false); /* Send the cancellation request */
964
966
967 talloc_free(trunk);
968
969 TEST_CHECK(preq->completed == false);
970 TEST_CHECK(preq->failed == false);
971 TEST_CHECK(preq->cancelled == true);
972 TEST_CHECK(preq->freed == true);
973 talloc_free(preq);
974
975 TEST_CASE("cancellation via trunk free - TRUNK_REQUEST_STATE_CANCEL_SENT");
976 trunk = test_setup_trunk(ctx, el, &conf, true, NULL);
977 preq = talloc_zero(NULL, test_proto_request_t);
978 treq = NULL;
979 trunk_request_enqueue(&treq, trunk, NULL, preq, NULL);
980 preq->treq = treq;
981
982 fr_event_corral(el, test_time_base, false); /* Connect the connection */
984
985 fr_event_corral(el, test_time_base, false); /* Send the request */
987
991
992 fr_event_corral(el, test_time_base, false); /* Send the cancellation request */
994
996
997 talloc_free(trunk);
998
999 TEST_CHECK(preq->completed == false);
1000 TEST_CHECK(preq->failed == false);
1001 TEST_CHECK(preq->cancelled == true);
1002 TEST_CHECK(preq->freed == true);
1003 talloc_free(preq);
1004
1005 TEST_CASE("trunk free after TRUNK_REQUEST_STATE_CANCEL_COMPLETE");
1006 trunk = test_setup_trunk(ctx, el, &conf, true, NULL);
1007 preq = talloc_zero(NULL, test_proto_request_t);
1008 treq = NULL;
1009 trunk_request_enqueue(&treq, trunk, NULL, preq, NULL);
1010 preq->treq = treq;
1011
1012 fr_event_corral(el, test_time_base, false); /* Connect the connection */
1014
1015 fr_event_corral(el, test_time_base, false); /* Send the request */
1017
1021
1022 fr_event_corral(el, test_time_base, false); /* Send the cancellation request */
1024
1026
1027 fr_event_corral(el, test_time_base, false); /* Loop the cancel request back round */
1029
1030 fr_event_corral(el, test_time_base, false); /* Read the cancel ACK (such that it is) */
1032
1034
1035 talloc_free(trunk);
1036
1037 TEST_CHECK(preq->completed == false);
1038 TEST_CHECK(preq->failed == false);
1039 TEST_CHECK(preq->cancelled == true);
1040 TEST_CHECK(preq->freed == true);
1041 talloc_free(preq);
1042
1043
1044 talloc_free(ctx);
1045}
1046
1047/*
1048 * Test PARTIAL -> SENT and CANCEL-PARTIAL -> CANCEL-SENT
1049 */
1051{
1052 TALLOC_CTX *ctx = talloc_init_const("test");
1053 trunk_t *trunk;
1055 trunk_conf_t conf = {
1056 .start = 1,
1057 .min = 1,
1058 .manage_interval = fr_time_delta_from_nsec(NSEC / 2)
1059 };
1061 trunk_request_t *treq = NULL;
1062
1064
1065 el = fr_event_list_alloc(ctx, NULL, NULL);
1067
1068 trunk = test_setup_trunk(ctx, el, &conf, true, NULL);
1069 preq = talloc_zero(NULL, test_proto_request_t);
1070 preq->signal_partial = true;
1071 preq->signal_cancel_partial = true;
1072
1073 TEST_CASE("TRUNK_REQUEST_STATE_PARTIAL -> TRUNK_REQUEST_STATE_SENT");
1074
1075 trunk_request_enqueue(&treq, trunk, NULL, preq, NULL);
1076 preq->treq = treq;
1077
1078 fr_event_corral(el, test_time_base, false); /* Connect the connection */
1080
1081 fr_event_corral(el, test_time_base, false); /* Send the request */
1083
1085
1086 fr_event_corral(el, test_time_base, false); /* Complete the partial request */
1088
1090
1093
1094 TEST_CASE("TRUNK_REQUEST_STATE_CANCEL_PARTIAL -> TRUNK_REQUEST_STATE_CANCEL_SENT");
1095
1096 fr_event_corral(el, test_time_base, false); /* Send partial cancel request */
1098
1100
1101 fr_event_corral(el, test_time_base, false); /* Complete the partial cancellation */
1103
1105
1106 fr_event_corral(el, test_time_base, false); /* Loop the cancellation request back */
1108
1110
1111 talloc_free(trunk);
1112
1113 TEST_CHECK(preq->completed == false);
1114 TEST_CHECK(preq->failed == false);
1115 TEST_CHECK(preq->cancelled == true);
1116 TEST_CHECK(preq->freed == true);
1117 talloc_free(preq);
1118
1119 talloc_free(ctx);
1120}
1121
1122/*
1123 * Test PENDING -> COMPLETE and PARTIAL -> COMPLETE when the muxer completes the request
1124 */
1125static void test_complete_from_mux(void)
1126{
1127 TALLOC_CTX *ctx = talloc_init_const("test");
1128 trunk_t *trunk;
1130 trunk_conf_t conf = {
1131 .start = 1,
1132 .min = 1,
1133 .max = 1, /* All requests go to the connection which the test checks */
1134 .manage_interval = fr_time_delta_from_nsec(NSEC / 2)
1135 };
1136 test_proto_request_t *preq, *preq_partial;
1137 trunk_request_t *treq = NULL;
1138 trunk_connection_t *tconn;
1139 fr_time_t written;
1140
1142
1143 el = fr_event_list_alloc(ctx, NULL, NULL);
1145
1146 trunk = test_setup_trunk(ctx, el, &conf, true, NULL);
1147
1148 fr_event_corral(el, test_time_base, false); /* Connect the connection */
1150
1152 tconn = fr_minmax_heap_min_peek(trunk->active);
1153 TEST_ASSERT(tconn != NULL);
1154
1155 TEST_CASE("TRUNK_REQUEST_STATE_PENDING -> TRUNK_REQUEST_STATE_COMPLETE");
1156
1157 preq = talloc_zero(NULL, test_proto_request_t);
1158 preq->complete_on_write = true;
1159
1160 trunk_request_enqueue(&treq, trunk, NULL, preq, NULL);
1161 preq->treq = treq;
1163
1165 test_time_base = written;
1166
1167 fr_event_corral(el, test_time_base, false); /* Write and complete the request */
1169
1171 TEST_CHECK(preq->completed == true);
1172 TEST_CHECK(preq->failed == false);
1173 TEST_CHECK(preq->freed == true);
1174
1175 TEST_MSG("last_write_success is set when the muxer completes the request");
1176 TEST_CHECK(fr_time_eq(tconn->pub.last_write_success, written));
1177 TEST_CHECK(fr_time_eq(trunk->pub.last_write_success, written));
1178
1179 TEST_CASE("TRUNK_REQUEST_STATE_PARTIAL -> TRUNK_REQUEST_STATE_COMPLETE");
1180
1181 /*
1182 * Don't free the first request yet. The byte which the muxer wrote for it comes back on
1183 * the next service, and the demuxer reads the request to see that it was freed.
1184 */
1185 preq_partial = talloc_zero(NULL, test_proto_request_t);
1186 preq_partial->signal_partial = true;
1187 preq_partial->complete_on_write = true;
1188
1189 treq = NULL;
1190 trunk_request_enqueue(&treq, trunk, NULL, preq_partial, NULL);
1191 preq_partial->treq = treq;
1192
1194 test_time_base = written;
1195
1196 fr_event_corral(el, test_time_base, false); /* Write part of the request */
1198
1200 TEST_CHECK(preq_partial->completed == false);
1201
1203 test_time_base = written;
1204
1205 fr_event_corral(el, test_time_base, false); /* Write the rest of the request, and complete it */
1207
1209 TEST_CHECK(preq_partial->completed == true);
1210 TEST_CHECK(preq_partial->failed == false);
1211 TEST_CHECK(preq_partial->freed == true);
1212
1213 TEST_MSG("last_write_success is set when the muxer completes a partial request");
1214 TEST_CHECK(fr_time_eq(tconn->pub.last_write_success, written));
1215 TEST_CHECK(fr_time_eq(trunk->pub.last_write_success, written));
1216
1217 fr_event_corral(el, test_time_base, false); /* Read back the bytes, which the demuxer skips */
1219
1220 talloc_free(trunk);
1221 talloc_free(preq);
1222 talloc_free(preq_partial);
1223
1224 talloc_free(ctx);
1225}
1226
1227/*
1228 * Test calling reconnect with requests in each different state
1229 */
1231{
1232 TALLOC_CTX *ctx = talloc_init_const("test");
1233 trunk_t *trunk;
1235 trunk_conf_t conf = {
1236 .start = 2,
1237 .min = 2,
1238 .manage_interval = fr_time_delta_from_nsec(NSEC / 2),
1239 .conn_conf = &(connection_conf_t){
1240 .reconnection_delay = fr_time_delta_from_nsec(NSEC / 10)
1241 },
1242 .backlog_on_failed_conn = true
1243 };
1245 trunk_request_t *treq = NULL;
1246 trunk_connection_t *tconn;
1247
1250
1251 el = fr_event_list_alloc(ctx, NULL, NULL);
1253
1254 trunk = test_setup_trunk(ctx, el, &conf, true, NULL);
1255 preq = talloc_zero(ctx, test_proto_request_t);
1256 preq->signal_partial = true;
1257 preq->signal_cancel_partial = true;
1258
1259 fr_event_corral(el, test_time_base, false); /* Connect the connection(s) */
1261
1262 TEST_CASE("dequeue on reconnect - TRUNK_REQUEST_STATE_PENDING");
1263
1265
1266 trunk_request_enqueue(&treq, trunk, NULL, preq, NULL);
1267 preq->treq = treq;
1268
1269 tconn = treq->pub.tconn; /* Store the conn the request was assigned to */
1271
1273
1274 /*
1275 * Should be reassigned to the other connection
1276 */
1277 TEST_CHECK(tconn != treq->pub.tconn);
1279
1280 /*
1281 * Should be reassigned to the backlog
1282 */
1285 TEST_CHECK(!treq->pub.tconn);
1286
1287 TEST_CASE("cancel on reconnect - TRUNK_REQUEST_STATE_PARTIAL");
1288
1289 /*
1290 * Allow the connections to reconnect
1291 */
1294 fr_event_service(el); /* run management function */
1296 fr_event_service(el); /* service any I/O callbacks */
1297
1298 /*
1299 * Request should now be assigned back to one of the reconnected
1300 * connections.
1301 */
1303 TEST_CHECK(treq->pub.tconn != NULL);
1304
1306 fr_event_corral(el, test_time_base, false); /* Send the request (partially) */
1308
1310
1311 /*
1312 * Reconnect the connection.
1313 *
1314 * preq should pass through the cancel function,
1315 * then be re-assigned.
1316 */
1317 tconn = treq->pub.tconn;
1319
1320 TEST_CHECK(preq->completed == false);
1321 TEST_CHECK(preq->failed == false);
1322 TEST_CHECK(preq->cancelled == true);
1323 TEST_CHECK(preq->freed == false);
1324
1325 preq->cancelled = false; /* Reset */
1326
1328 TEST_CHECK(tconn != treq->pub.tconn); /* Ensure it moved */
1329
1330 TEST_CASE("cancel on reconnect - TRUNK_REQUEST_STATE_SENT");
1331
1332 /*
1333 * Sent the request (fully)
1334 */
1335 fr_event_corral(el, test_time_base, false); /* Send the request (partially) */
1337
1338 /*
1339 * The above indeed appears to send the request partially;
1340 * this appears to be required to send it fully, judging by
1341 * the following check, which fails without it.
1342 */
1343 fr_event_corral(el, test_time_base, false); /* Send the request (partially) */
1346
1347 tconn = treq->pub.tconn;
1349
1351
1352 /*
1353 * Allow the connections to reconnect
1354 * and send the request.
1355 */
1359 TEST_CHECK(tconn != treq->pub.tconn); /* Ensure it moved */
1360
1361 TEST_CHECK(preq->completed == false);
1362 TEST_CHECK(preq->failed == false);
1363 TEST_CHECK(preq->cancelled == true);
1364 TEST_CHECK(preq->freed == false);
1365
1366 preq->cancelled = false; /* Reset */
1367
1368 TEST_CASE("free on reconnect - TRUNK_REQUEST_STATE_CANCEL");
1369
1370 /*
1371 * Signal the request should be cancelled
1372 */
1375
1376 /*
1377 * Requests in the cancel state, are
1378 * freed instead of being moved between
1379 * connections.
1380 */
1381 trunk_connection_signal_reconnect(tconn, CONNECTION_FAILED); /* treq->pub.tconn, now invalid due to cancel */
1382
1386
1390
1394
1395 TEST_CHECK(preq->completed == false);
1396 TEST_CHECK(preq->failed == false);
1397 TEST_CHECK(preq->cancelled == true);
1398 TEST_CHECK(preq->freed == true);
1399
1400 /*
1401 * Allow the connection we just reconnected
1402 * to open so it doesn't interfere with
1403 * the next test.
1404 */
1408
1409 TEST_CASE("free on reconnect - TRUNK_REQUEST_STATE_CANCEL_PARTIAL");
1410
1411 /*
1412 * Queue up a new request, and get it to the cancel-partial state.
1413 */
1414 preq = talloc_zero(ctx, test_proto_request_t);
1415 preq->signal_cancel_partial = true;
1416 treq = NULL;
1417 trunk_request_enqueue(&treq, trunk, NULL, preq, NULL);
1418 preq->treq = treq;
1419
1421
1422 /*
1423 * Sent the request (fully)
1424 */
1426 fr_event_corral(el, test_time_base, false); /* Send the request (fully) */
1428
1430 trunk_request_signal_cancel(treq); /* Cancel the request */
1431
1433
1434 /*
1435 * Transition to cancel partial
1436 */
1440
1442
1443 /*
1444 * Trigger a reconnection
1445 */
1447
1451
1452 TEST_CHECK(preq->completed == false);
1453 TEST_CHECK(preq->failed == false);
1454 TEST_CHECK(preq->cancelled == true);
1455 TEST_CHECK(preq->freed == true);
1456
1457 /*
1458 * Allow the connection we just reconnected
1459 * top open so it doesn't interfere with
1460 * the next test.
1461 */
1465
1466 TEST_CASE("free on reconnect - TRUNK_REQUEST_STATE_CANCEL_SENT");
1467
1468 /*
1469 * Queue up a new request, and get it to the cancel-sent state.
1470 */
1471 preq = talloc_zero(NULL, test_proto_request_t);
1472 treq = NULL;
1473 trunk_request_enqueue(&treq, trunk, NULL, preq, NULL);
1474 preq->treq = treq;
1475
1477
1478 /*
1479 * Sent the request (fully)
1480 */
1482 fr_event_corral(el, test_time_base, false); /* Send the request (fully) */
1484
1486 trunk_request_signal_cancel(treq); /* Cancel the request */
1487
1489
1490 /*
1491 * Transition to cancel
1492 */
1496
1498
1499 /*
1500 * Trigger a reconnection
1501 */
1503
1507
1508 TEST_CHECK(preq->completed == false);
1509 TEST_CHECK(preq->failed == false);
1510 TEST_CHECK(preq->cancelled == true);
1511 TEST_CHECK(preq->freed == true);
1512
1513 talloc_free(preq);
1514
1515 talloc_free(ctx);
1516}
1517
1519{
1520 TALLOC_CTX *ctx = talloc_init_const("test");
1521 trunk_t *trunk;
1523 trunk_conf_t conf = {
1524 .start = 0,
1525 .min = 0, /* No connections on start */
1526 .manage_interval = fr_time_delta_from_nsec(NSEC / 2)
1527 };
1529 trunk_request_t *treq_a = NULL, *treq_b = NULL, *treq_c = NULL;
1530
1532
1533 el = fr_event_list_alloc(ctx, NULL, NULL);
1535
1536 /* Need to provide a timer starting value above zero */
1538
1539 trunk = test_setup_trunk(ctx, el, &conf, true, NULL);
1540 preq = talloc_zero(NULL, test_proto_request_t);
1541
1542 TEST_CASE("C0 - Enqueue should spawn");
1543 trunk_request_enqueue(&treq_a, trunk, NULL, preq, NULL);
1544
1545 /*
1546 * This causes the event associated with the request left on
1547 * the backlog queue to be handled, which (along with the other
1548 * corral; service sequence, makes the checks all pass.
1549 */
1552
1554
1555 TEST_CASE("C1 connecting, !max_req_per_conn - Enqueue MUST NOT spawn");
1556 trunk_request_enqueue(&treq_b, trunk, NULL, preq, NULL);
1557
1559
1560 /*
1561 * Allow the connections to open
1562 */
1565
1567
1568 TEST_CASE("C1 active, !max_req_per_conn - Enqueue MUST NOT spawn");
1569 trunk_request_enqueue(&treq_c, trunk, NULL, preq, NULL);
1570
1573
1574 talloc_free(ctx);
1575 talloc_free(preq);
1576}
1577
1579{
1580 TALLOC_CTX *ctx = talloc_init_const("test");
1581 trunk_t *trunk;
1583 trunk_conf_t conf = {
1584 .start = 2,
1585 .min = 2, /* No connections on start */
1586 .manage_interval = fr_time_delta_from_nsec(NSEC / 2)
1587 };
1589 trunk_connection_t *tconn;
1590 trunk_request_t *treq_a = NULL, *treq_b = NULL, *treq_c = NULL;
1591
1593
1594 el = fr_event_list_alloc(ctx, NULL, NULL);
1596
1597 trunk = test_setup_trunk(ctx, el, &conf, true, NULL);
1598 preq = talloc_zero(NULL, test_proto_request_t);
1599 printf("Rebalance %p\n", preq);
1600
1601 /*
1602 * Allow the connections to open
1603 */
1606
1607 /*
1608 * Mark one of the connections as full, and
1609 * enqueue three requests on the other.
1610 */
1611 tconn = fr_minmax_heap_min_peek(trunk->active);
1612
1613 TEST_CASE("C2 connected, R0 - Signal inactive");
1615
1616
1617 trunk_request_enqueue(&treq_a, trunk, NULL, preq, NULL);
1618 trunk_request_enqueue(&treq_b, trunk, NULL, preq, NULL);
1619 trunk_request_enqueue(&treq_c, trunk, NULL, preq, NULL);
1620
1621 TEST_CASE("C1 connected, C2 inactive, R3 - Enqueued");
1624
1625 /*
1626 * Now mark the previous connection as
1627 * active. It should receive at least
1628 * one of the requests.
1629 */
1630 TEST_CASE("C2 active, R3 - Signal active, should balance");
1632
1635
1636 talloc_free(ctx);
1637 talloc_free(preq);
1638}
1639
1640/** Get the two active connections of a trunk which has two connections
1641 *
1642 */
1644{
1646
1647 *a = fr_minmax_heap_iter_init(trunk->active, &iter);
1648 *b = fr_minmax_heap_iter_next(trunk->active, &iter);
1649}
1650
1652{
1653 TALLOC_CTX *ctx = talloc_init_const("test");
1654 trunk_t *trunk;
1656 trunk_conf_t conf = {
1657 .start = 2,
1658 .min = 2,
1659 .max = 2,
1660 .manage_interval = fr_time_delta_from_nsec(NSEC / 2),
1661 .sort = TRUNK_SORT_MRU
1662 };
1663 trunk_connection_t *a, *b;
1664
1666
1667 el = fr_event_list_alloc(ctx, NULL, NULL);
1669
1670 trunk = test_setup_trunk(ctx, el, &conf, true, NULL);
1671
1672 /*
1673 * Allow the connections to open
1674 */
1677
1679 test_two_active(trunk, &a, &b);
1680 TEST_ASSERT(a && b);
1681
1682 TEST_CASE("time_window is zero, and the connection with the most recent reply sorts first");
1685 CONN_REORDER(a);
1686 CONN_REORDER(b);
1690
1691 TEST_CASE("time_window is zero, and a connection which never replied sorts last");
1693 CONN_REORDER(a);
1695
1696 TEST_CASE("time_window is zero, and replies which differ by one nanosecond are not equal");
1699
1700 TEST_CASE("time_window is zero, and equal reply times order by queue depth");
1703
1704 talloc_free(ctx);
1705}
1706
1708{
1709 TALLOC_CTX *ctx = talloc_init_const("test");
1710 trunk_t *trunk;
1712 trunk_conf_t conf = {
1713 .start = 2,
1714 .min = 2,
1715 .max = 2,
1716 .manage_interval = fr_time_delta_from_nsec(NSEC / 2),
1717 .sort = TRUNK_SORT_MRU,
1718 .time_window = fr_time_delta_from_sec(1)
1719 };
1720 trunk_connection_t *a, *b;
1721
1723
1724 el = fr_event_list_alloc(ctx, NULL, NULL);
1726
1727 trunk = test_setup_trunk(ctx, el, &conf, true, NULL);
1728
1729 /*
1730 * Allow the connections to open
1731 */
1734
1736 test_two_active(trunk, &a, &b);
1737 TEST_ASSERT(a && b);
1738
1739 TEST_CASE("Replies which are less than time_window apart are equal");
1740 a->pub.last_read_success = fr_time_wrap((int64_t) 10 * NSEC);
1744
1745 TEST_CASE("Replies which are more than time_window apart sort by the time of the reply");
1749
1750 TEST_CASE("A connection which never replied sorts after a connection which replied");
1753
1754 TEST_CASE("Two connections which never replied sort by queue depth");
1757
1758 talloc_free(ctx);
1759}
1760
1762{
1763 TALLOC_CTX *ctx = talloc_init_const("test");
1764 trunk_t *trunk;
1766 trunk_conf_t conf = {
1767 .start = 2,
1768 .min = 2,
1769 .max = 2,
1770 .manage_interval = fr_time_delta_from_nsec(NSEC / 2),
1771 .sort = TRUNK_SORT_QUEUE
1772 };
1773 trunk_connection_t *a, *b;
1774
1776
1777 el = fr_event_list_alloc(ctx, NULL, NULL);
1779
1780 trunk = test_setup_trunk(ctx, el, &conf, true, NULL);
1781
1782 /*
1783 * Allow the connections to open
1784 */
1787
1789 test_two_active(trunk, &a, &b);
1790 TEST_ASSERT(a && b);
1791
1792 TEST_CASE("sort = queue selects the queue ordering");
1794
1795 TEST_CASE("sort = queue ignores the time of the last reply");
1796 a->pub.last_read_success = fr_time_wrap((int64_t) 10 * NSEC);
1797 b->pub.last_read_success = fr_time_wrap((int64_t) 100 * NSEC);
1798 TEST_CHECK(trunk->funcs.connection_prioritise(a, b) == 0);
1799 TEST_CHECK(trunk->funcs.connection_prioritise(b, a) == 0);
1800
1801 talloc_free(ctx);
1802
1803 /*
1804 * sort = mru is the default, because the value of TRUNK_SORT_MRU is zero.
1805 */
1806 conf.sort = TRUNK_SORT_MRU;
1807 ctx = talloc_init_const("test");
1808 el = fr_event_list_alloc(ctx, NULL, NULL);
1810 trunk = test_setup_trunk(ctx, el, &conf, true, NULL);
1811
1812 TEST_CASE("sort = mru selects the ordering by the last reply");
1814
1815 talloc_free(ctx);
1816}
1817
1819{
1820 TALLOC_CTX *ctx = talloc_init_const("test");
1821 trunk_t *trunk;
1823 trunk_conf_t conf = {
1824 .start = 2,
1825 .min = 2,
1826 .max = 2,
1827 .manage_interval = fr_time_delta_from_nsec(NSEC / 2),
1828 .sort = TRUNK_SORT_LRU,
1829 .time_window = fr_time_delta_from_sec(1)
1830 };
1831 test_proto_request_t *preq[4];
1832 trunk_request_t *treq[4] = { NULL, NULL, NULL, NULL };
1833 trunk_connection_t *a, *b, *older, *newer;
1834 size_t i;
1835
1837
1838 el = fr_event_list_alloc(ctx, NULL, NULL);
1840
1841 trunk = test_setup_trunk(ctx, el, &conf, true, NULL);
1842 for (i = 0; i < NUM_ELEMENTS(preq); i++) preq[i] = talloc_zero(NULL, test_proto_request_t);
1843
1844 /*
1845 * Allow the connections to open
1846 */
1849
1851 test_two_active(trunk, &a, &b);
1852 TEST_ASSERT(a && b);
1853
1854 TEST_CASE("sort = lru selects the last write ordering");
1856
1857 TEST_CASE("The connection with the older last_write_success sorts first");
1858 a->pub.last_write_success = fr_time_wrap((int64_t) 10 * NSEC);
1859 b->pub.last_write_success = fr_time_wrap((int64_t) 20 * NSEC);
1862
1863 TEST_CASE("Writes which are not more than time_window apart are equal");
1867
1868 TEST_CASE("Writes which are more than time_window apart sort by the time of the write");
1872
1873 TEST_CASE("time_window is zero, and writes which differ by one nanosecond are not equal");
1878
1879 TEST_CASE("Equal write times order by queue depth");
1882
1883 /*
1884 * Set the write times, and reorder the heap. The test does not run the event loop, so the
1885 * requests wait to be written.
1886 */
1887 a->pub.last_write_success = fr_time_wrap((int64_t) 10 * NSEC);
1888 b->pub.last_write_success = fr_time_wrap((int64_t) 1000 * NSEC);
1889 CONN_REORDER(a);
1890 CONN_REORDER(b);
1891 older = a;
1892 newer = b;
1893 TEST_ASSERT(fr_minmax_heap_min_peek(trunk->active) == older);
1894
1895 TEST_CASE("The first request goes to the connection with the older write");
1896 trunk_request_enqueue(&treq[0], trunk, NULL, preq[0], NULL);
1897 preq[0]->treq = treq[0];
1899
1900 TEST_CASE("A connection with a request waiting to be written sorts after a connection with none");
1902 TEST_CHECK(fr_minmax_heap_min_peek(trunk->active) == newer);
1903
1904 TEST_CASE("The second request goes to the connection which has no request waiting to be written");
1905 trunk_request_enqueue(&treq[1], trunk, NULL, preq[1], NULL);
1906 preq[1]->treq = treq[1];
1908
1909 TEST_CASE("A burst of four requests, with no write between the requests, goes to both connections equally");
1910 trunk_request_enqueue(&treq[2], trunk, NULL, preq[2], NULL);
1911 preq[2]->treq = treq[2];
1912 trunk_request_enqueue(&treq[3], trunk, NULL, preq[3], NULL);
1913 preq[3]->treq = treq[3];
1916
1917 talloc_free(ctx);
1918 for (i = 0; i < NUM_ELEMENTS(preq); i++) talloc_free(preq[i]);
1919}
1920
1922{
1923 TALLOC_CTX *ctx = talloc_init_const("test");
1924 trunk_t *trunk;
1926 trunk_conf_t conf = {
1927 .start = 2,
1928 .min = 2,
1929 .max = 2,
1930 .manage_interval = fr_time_delta_from_nsec(NSEC / 2),
1931 .sort = TRUNK_SORT_MRS,
1932 .time_window = fr_time_delta_from_sec(1)
1933 };
1934 test_proto_request_t *preq[2];
1935 trunk_request_t *treq[2] = { NULL, NULL };
1936 trunk_connection_t *a, *b, *older, *newer;
1937 size_t i;
1938
1940
1941 el = fr_event_list_alloc(ctx, NULL, NULL);
1943
1944 trunk = test_setup_trunk(ctx, el, &conf, true, NULL);
1945 for (i = 0; i < NUM_ELEMENTS(preq); i++) preq[i] = talloc_zero(NULL, test_proto_request_t);
1946
1947 /*
1948 * Allow the connections to open
1949 */
1952
1954 test_two_active(trunk, &a, &b);
1955 TEST_ASSERT(a && b);
1956
1957 TEST_CASE("sort = mrs selects the most recent sent ordering");
1959
1960 TEST_CASE("The connection with the larger mrs_time sorts first");
1961 a->pub.mrs_time = fr_time_wrap((int64_t) 20 * NSEC);
1962 b->pub.mrs_time = fr_time_wrap((int64_t) 10 * NSEC);
1965
1966 TEST_CASE("Times which are not more than time_window apart are equal");
1970
1971 TEST_CASE("Times which are more than time_window apart sort by mrs_time");
1975
1976 TEST_CASE("time_window is zero, and times which differ by one nanosecond are not equal");
1981
1982 TEST_CASE("Equal times order by queue depth");
1983 b->pub.mrs_time = a->pub.mrs_time;
1985
1986 /*
1987 * Set the times, and reorder the heap. The test does not run the event loop, so the requests
1988 * wait to be written.
1989 */
1990 a->pub.mrs_time = fr_time_wrap((int64_t) 1000 * NSEC);
1991 b->pub.mrs_time = fr_time_wrap((int64_t) 10 * NSEC);
1992 CONN_REORDER(a);
1993 CONN_REORDER(b);
1994 newer = a;
1995 older = b;
1996 TEST_ASSERT(fr_minmax_heap_min_peek(trunk->active) == newer);
1997
1998 TEST_CASE("The first two requests go to the connection with the larger mrs_time");
1999 trunk_request_enqueue(&treq[0], trunk, NULL, preq[0], NULL);
2000 preq[0]->treq = treq[0];
2001 trunk_request_enqueue(&treq[1], trunk, NULL, preq[1], NULL);
2002 preq[1]->treq = treq[1];
2005
2006 TEST_CASE("When the times become equal, the connection with fewer requests sorts first");
2007 older->pub.mrs_time = newer->pub.mrs_time;
2008 CONN_REORDER(older);
2009 TEST_CHECK(_trunk_connection_order_by_mrs(older, newer) < 0);
2010 TEST_CHECK(fr_minmax_heap_min_peek(trunk->active) == older);
2011
2012 talloc_free(ctx);
2013 for (i = 0; i < NUM_ELEMENTS(preq); i++) talloc_free(preq[i]);
2014}
2015
2017{
2018 TALLOC_CTX *ctx = talloc_init_const("test");
2019 trunk_t *trunk;
2021 trunk_conf_t conf = {
2022 .start = 2,
2023 .min = 2,
2024 .max = 2,
2025 .manage_interval = fr_time_delta_from_nsec(NSEC / 2),
2026 .sort = TRUNK_SORT_RTO
2027 };
2028 test_proto_request_t *preq[5];
2029 trunk_request_t *treq[5] = { NULL, NULL, NULL, NULL, NULL };
2030 trunk_connection_t *a, *b;
2031 size_t i;
2032
2034
2035 el = fr_event_list_alloc(ctx, NULL, NULL);
2037
2038 trunk = test_setup_trunk(ctx, el, &conf, true, NULL);
2039 for (i = 0; i < NUM_ELEMENTS(preq); i++) preq[i] = talloc_zero(NULL, test_proto_request_t);
2040
2041 /*
2042 * Allow the connections to open
2043 */
2046
2048 test_two_active(trunk, &a, &b);
2049 TEST_ASSERT(a && b);
2050
2051 TEST_CASE("sort = rto selects the RTO ordering");
2053
2054 TEST_CASE("A new connection has an RTT and an RTTVAR of 10ms");
2059
2060 TEST_CASE("Connections with the same RTT, RTTVAR and queue depth are equal");
2062
2063 TEST_CASE("With equal queue depth, the connection with the smaller RTT sorts first");
2068
2069 TEST_CASE("With equal RTT, the connection with the smaller RTTVAR sorts first");
2076
2077 TEST_CASE("The RTO is the RTT plus four times the RTTVAR");
2079 a->pub.rtt.rttvar = fr_time_delta_from_msec(10); /* RTO is 50ms */
2081 b->pub.rtt.rttvar = fr_time_delta_wrap(0); /* RTO is 49ms */
2083 b->pub.rtt.rtt = fr_time_delta_from_msec(51); /* RTO is 51ms */
2085
2086 TEST_CASE("time_window does not apply");
2090
2091 /*
2092 * Set the round trip times, and reorder the heap. The test does not run the event loop, so the
2093 * requests wait to be written. The estimates for the next request are:
2094 *
2095 * - connection a: 10ms, 20ms, 30ms, 40ms, 50ms...
2096 * - connection b: 25ms, 50ms...
2097 */
2102 CONN_REORDER(a);
2103 CONN_REORDER(b);
2105
2106 TEST_CASE("The first request goes to the faster connection");
2107 trunk_request_enqueue(&treq[0], trunk, NULL, preq[0], NULL);
2108 preq[0]->treq = treq[0];
2111
2112 TEST_CASE("The second request still goes to the faster connection, 20ms is less than 25ms");
2113 trunk_request_enqueue(&treq[1], trunk, NULL, preq[1], NULL);
2114 preq[1]->treq = treq[1];
2116
2117 TEST_CASE("The third request goes to the slower connection, 30ms is more than 25ms");
2118 trunk_request_enqueue(&treq[2], trunk, NULL, preq[2], NULL);
2119 preq[2]->treq = treq[2];
2121
2122 TEST_CASE("The next two requests go to the faster connection, 30ms and 40ms are less than 50ms");
2123 trunk_request_enqueue(&treq[3], trunk, NULL, preq[3], NULL);
2124 preq[3]->treq = treq[3];
2125 trunk_request_enqueue(&treq[4], trunk, NULL, preq[4], NULL);
2126 preq[4]->treq = treq[4];
2129
2130 talloc_free(ctx);
2131 for (i = 0; i < NUM_ELEMENTS(preq); i++) talloc_free(preq[i]);
2132}
2133
2135{
2136 TALLOC_CTX *ctx = talloc_init_const("test");
2137 trunk_t *trunk;
2139 trunk_conf_t conf = {
2140 .start = 2,
2141 .min = 2,
2142 .max = 2,
2143 .manage_interval = fr_time_delta_from_nsec(NSEC / 2),
2144 .sort = TRUNK_SORT_LRU
2145 };
2147 trunk_request_t *treq = NULL;
2148 trunk_connection_t *x, *y;
2149
2151
2152 el = fr_event_list_alloc(ctx, NULL, NULL);
2154
2155 trunk = test_setup_trunk(ctx, el, &conf, true, NULL);
2156 preq = talloc_zero(NULL, test_proto_request_t);
2157
2158 /*
2159 * Allow the connections to open
2160 */
2163
2165 test_two_active(trunk, &x, &y);
2166 TEST_ASSERT(x && y);
2167
2168 /*
2169 * Connection "y" wrote long after the time at which the test writes the request. Connection
2170 * "x" has the older write, so the request goes to connection "x".
2171 */
2173 y->pub.last_write_success = fr_time_wrap((int64_t) 1000 * NSEC);
2174 CONN_REORDER(x);
2175 CONN_REORDER(y);
2177
2178 trunk_request_enqueue(&treq, trunk, NULL, preq, NULL);
2179 preq->treq = treq;
2181
2182 TEST_CASE("Before the write, the connection with no waiting request sorts first");
2184
2185 /*
2186 * Write the request. The request moves from pending to sent, so connection "x" has no
2187 * request waiting to be written, and has a write older than the write on connection "y".
2188 */
2193
2194 TEST_CASE("After the write, the heap is reordered, and the connection with the older write sorts first");
2198
2199 talloc_free(ctx);
2200 talloc_free(preq);
2201}
2202
2204{
2205 TALLOC_CTX *ctx = talloc_init_const("test");
2206 trunk_t *trunk;
2208 trunk_conf_t conf = {
2209 .start = 2,
2210 .min = 2,
2211 .max = 2,
2212 .manage_interval = fr_time_delta_from_nsec(NSEC / 2),
2213 .sort = TRUNK_SORT_MRU
2214 };
2216 trunk_connection_t *first, *second;
2217 trunk_request_t *treq = NULL;
2218
2220
2221 el = fr_event_list_alloc(ctx, NULL, NULL);
2223
2224 trunk = test_setup_trunk(ctx, el, &conf, true, NULL);
2225 preq = talloc_zero(NULL, test_proto_request_t);
2226
2227 /*
2228 * Allow the connections to open
2229 */
2232
2234
2235 /*
2236 * Send the request on the connection which is not at the top of the heap, and take the
2237 * connection at the top out of service.
2238 */
2239 first = fr_minmax_heap_min_peek(trunk->active);
2240 TEST_ASSERT(first != NULL);
2242 second = fr_minmax_heap_min_peek(trunk->active);
2243 TEST_ASSERT(second && (first != second));
2244
2245 trunk_request_enqueue(&treq, trunk, NULL, preq, NULL);
2246 preq->treq = treq;
2247
2248 /*
2249 * Write the request, loop the request back, and then read the reply.
2250 */
2257
2258 TEST_CHECK(preq->completed == true);
2261
2262 /*
2263 * Both connections hold no requests. The connection which replied sorts first, because
2264 * the heap was reordered after the demux function returned.
2265 */
2266 TEST_CASE("The connection which replied sorts first after the reply");
2270 TEST_CHECK(fr_minmax_heap_min_peek(trunk->active) == second);
2271
2272 talloc_free(ctx);
2273 talloc_free(preq);
2274}
2275
2276#define ALLOC_REQ(_id) \
2277do { \
2278 treq_##_id = trunk_request_alloc(trunk, NULL); \
2279 preq_##_id = talloc_zero(ctx, test_proto_request_t); \
2280 preq_##_id->treq = treq_##_id; \
2281 preq_##_id->priority = next_prio++; \
2282} while (0)
2283
2285{
2286 TALLOC_CTX *ctx = talloc_init_const("test");
2287 trunk_t *trunk;
2289 trunk_conf_t conf = {
2290 .start = 0, /* No connections on start */
2291 .min = 0,
2292 .max = 2,
2293 .max_req_per_conn = 2,
2294 .target_req_per_conn = 2, /* One request per connection */
2295 .manage_interval = fr_time_delta_from_nsec(NSEC / 2)
2296 };
2297 test_proto_request_t *preq_a, *preq_b, *preq_c, *preq_d, *preq_e;
2298 trunk_request_t *treq_a = NULL, *treq_b = NULL, *treq_c = NULL, *treq_d = NULL, *treq_e = NULL;
2299 int next_prio = 0;
2300
2302
2303 el = fr_event_list_alloc(ctx, NULL, NULL);
2305
2306 /* Need to provide a timer starting value above zero */
2308
2309 trunk = test_setup_trunk(ctx, el, &conf, true, NULL);
2310 TRUNK_VERIFY(trunk);
2311
2312 /*
2313 * Queuing a request should start a connection.
2314 */
2315 TEST_CASE("C0, R1 - Enqueue should spawn");
2316 ALLOC_REQ(a);
2317 TEST_CHECK(trunk_request_enqueue(&treq_a, trunk, NULL, preq_a, NULL) == TRUNK_ENQUEUE_IN_BACKLOG);
2318 TRUNK_VERIFY(trunk);
2319
2320 /*
2321 * Like test_connection_start_on_enqueue(), you have to process the backlog
2322 * to start the chain of events.
2323 */
2327
2329 TRUNK_VERIFY(trunk);
2330
2331 /*
2332 * Queuing another request should *NOT* start another connection
2333 */
2334 TEST_CASE("C1 connecting, R2 - MUST NOT spawn");
2335 ALLOC_REQ(b);
2336 TEST_CHECK(trunk_request_enqueue(&treq_b, trunk, NULL, preq_b, NULL) == TRUNK_ENQUEUE_IN_BACKLOG);
2338 TRUNK_VERIFY(trunk);
2339
2340 TEST_CASE("C1 connecting, R3 - MUST NOT spawn");
2341 ALLOC_REQ(c);
2342 TEST_CHECK(trunk_request_enqueue(&treq_c, trunk, NULL, preq_c, NULL) == TRUNK_ENQUEUE_IN_BACKLOG);
2344 TRUNK_VERIFY(trunk);
2345
2346 TEST_CASE("C1 connecting, R4 - MUST NOT spawn");
2347 ALLOC_REQ(d);
2348 TEST_CHECK(trunk_request_enqueue(&treq_d, trunk, NULL, preq_d, NULL) == TRUNK_ENQUEUE_IN_BACKLOG);
2350 TRUNK_VERIFY(trunk);
2351
2352 TEST_CASE("C1 connecting, R5 - MUST NOT spawn, NO CAPACITY");
2353 ALLOC_REQ(e);
2354 TEST_CHECK(trunk_request_enqueue(&treq_e, trunk, NULL, preq_e, NULL) == TRUNK_ENQUEUE_NO_CAPACITY);
2356 TRUNK_VERIFY(trunk);
2357
2358 /*
2359 * Allowing connection to open
2360 */
2363
2364 TEST_CASE("C1 active, R4 - Check pending 2");
2367 TRUNK_VERIFY(trunk);
2368
2369 /*
2370 * Sending requests
2371 */
2374
2375 TEST_CASE("C1 active, R4 - Check sent 2");
2377 TRUNK_VERIFY(trunk);
2378
2379 /*
2380 * Looping I/O
2381 */
2385
2386 /*
2387 * Receiving responses
2388 */
2392
2393 TEST_CHECK(preq_a->completed == true);
2394 TEST_CHECK(preq_a->failed == false);
2395 TEST_CHECK(preq_a->cancelled == false);
2396 TEST_CHECK(preq_a->freed == true);
2397
2398 TEST_CHECK(preq_b->completed == true);
2399 TEST_CHECK(preq_b->failed == false);
2400 TEST_CHECK(preq_b->cancelled == false);
2401 TEST_CHECK(preq_b->freed == true);
2402
2405 TRUNK_VERIFY(trunk);
2406
2407 TEST_CASE("C1 active, R0 - Check complete 2, pending 0");
2408
2409 /*
2410 * Sending requests
2411 */
2414
2415 /*
2416 * Looping I/O
2417 */
2420
2421 /*
2422 * Receiving responses
2423 */
2426
2427 TEST_CHECK(preq_c->completed == true);
2428 TEST_CHECK(preq_c->failed == false);
2429 TEST_CHECK(preq_c->cancelled == false);
2430 TEST_CHECK(preq_c->freed == true);
2431
2432 TEST_CHECK(preq_d->completed == true);
2433 TEST_CHECK(preq_d->failed == false);
2434 TEST_CHECK(preq_d->cancelled == false);
2435 TEST_CHECK(preq_d->freed == true);
2436
2438 TRUNK_VERIFY(trunk);
2439
2440 talloc_free(trunk);
2441 talloc_free(ctx);
2442}
2443
2445{
2446 TALLOC_CTX *ctx = talloc_init_const("test");
2447 trunk_t *trunk;
2449 trunk_conf_t conf = {
2450 .start = 0, /* No connections on start */
2451 .min = 0,
2452 .max = 0,
2453 .max_req_per_conn = 0,
2454 .target_req_per_conn = 2, /* One request per connection */
2455 .manage_interval = fr_time_delta_from_nsec(NSEC / 10)
2456 };
2457
2458 test_proto_request_t *preq_a, *preq_b, *preq_c;
2459 trunk_request_t *treq_a = NULL, *treq_b = NULL, *treq_c = NULL;
2460 test_proto_stats_t stats;
2461 int next_prio = 0;
2462
2464
2465 el = fr_event_list_alloc(ctx, NULL, NULL);
2467
2468 /* Need to provide a timer starting value above zero */
2470
2471 memset(&stats, 0, sizeof(stats));
2472 trunk = test_setup_trunk(ctx, el, &conf, true, &stats);
2473
2474 /*
2475 * Queuing a request should start a connection.
2476 */
2477 TEST_CASE("C0, R1 - Enqueue should spawn");
2478 ALLOC_REQ(a);
2479 TEST_CHECK(trunk_request_enqueue(&treq_a, trunk, NULL, preq_a, NULL) == TRUNK_ENQUEUE_IN_BACKLOG);
2480
2481 /*
2482 * Processing the event associated with the backlog creates
2483 * the connection in connecting state..
2484 */
2487
2489
2490 TEST_CASE("C1 connecting, R2 - MUST NOT spawn");
2491 ALLOC_REQ(b);
2492 TEST_CHECK(trunk_request_enqueue(&treq_b, trunk, NULL, preq_b, NULL) == TRUNK_ENQUEUE_IN_BACKLOG);
2495
2496 /*
2497 * Open connection
2498 */
2501
2503
2504 TEST_CASE("C1 connected, R3 - should spawn");
2505 ALLOC_REQ(c);
2506 TEST_CHECK(trunk_request_enqueue(&treq_c, trunk, NULL, preq_c, NULL) == TRUNK_ENQUEUE_OK);
2508
2511
2515
2516 /*
2517 * Complete requests
2518 */
2520
2523
2525
2526 TEST_CASE("C1 connected, C2 connecting, R2 - MUST NOT spawn");
2529
2530 /*
2531 * Finish the last request, should close one connection
2532 */
2535
2537
2538 TEST_CASE("C1 connected, R0");
2541
2542 /*
2543 * Requests now done, should close another connection
2544 */
2547
2549
2550 TEST_CASE("C0, R0");
2552
2553 TEST_CHECK(stats.completed == 3);
2554 TEST_CHECK(stats.failed == 0);
2555 TEST_CHECK(stats.cancelled == 0);
2556 TEST_CHECK(stats.freed == 3);
2557
2558 /*
2559 * Queuing a request should start a connection.
2560 */
2561 TEST_CASE("C0, R1 - Enqueue should spawn");
2562 ALLOC_REQ(a);
2563 TEST_CHECK(trunk_request_enqueue(&treq_a, trunk, NULL, preq_a, NULL) == TRUNK_ENQUEUE_IN_BACKLOG);
2564
2565 /*
2566 * ...once the event associated with the backlogged request is handled.
2567 */
2570
2572
2573 TEST_CASE("C1 connecting, R2 - MUST NOT spawn");
2574 ALLOC_REQ(b);
2575 TEST_CHECK(trunk_request_enqueue(&treq_b, trunk, NULL, preq_b, NULL) == TRUNK_ENQUEUE_IN_BACKLOG);
2578
2579 /*
2580 * Open connection
2581 */
2584
2586
2587 TEST_CASE("C1 connected, R3 - should spawn");
2588 ALLOC_REQ(c);
2589 TEST_CHECK(trunk_request_enqueue(&treq_c, trunk, NULL, preq_c, NULL) == TRUNK_ENQUEUE_OK);
2591
2594
2598
2599 talloc_free(trunk);
2600 talloc_free(ctx);
2601}
2602
2603/*
2604 * Test the global trunk state transitions: PENDING -> ACTIVE -> FAILED.
2605 */
2607{
2608 TALLOC_CTX *ctx = talloc_init_const("test");
2609 trunk_t *trunk;
2611 int events;
2612 trunk_conf_t conf = {
2613 .start = 2,
2614 .min = 2,
2615 .conn_conf = &(connection_conf_t){
2616 /*
2617 * Long, so failed connections stay CLOSED
2618 * while we check the FAILED state.
2619 */
2620 .reconnection_delay = fr_time_delta_from_sec(10)
2621 }
2622 };
2625 .request_prioritise = fr_pointer_cmp,
2626 };
2627
2629
2630 el = fr_event_list_alloc(ctx, NULL, NULL);
2632
2633 trunk = trunk_alloc(ctx, el, &io_funcs, &conf, "test_socket_pair", NULL, false, NULL);
2634 TEST_CHECK(trunk != NULL);
2635 if (!trunk) return;
2636
2637 /*
2638 * start = 2 spawns two connections which are connecting. No
2639 * active connections yet, so the trunk is PENDING.
2640 */
2641 TEST_CASE("PENDING - connections are connecting");
2644
2645 /*
2646 * Let the connections open. The trunk becomes ACTIVE.
2647 */
2649 TEST_CHECK(events == 2); /* Two I/O write events, no timers */
2651
2652 TEST_CASE("ACTIVE - connections are active");
2655
2656 /*
2657 * Fail all connections. With a long reconnection_delay they stay
2658 * CLOSED, so the trunk becomes FAILED.
2659 */
2660 TEST_CASE("FAILED - all connections closed / in reconnect backoff");
2662
2663 (void) fr_event_corral(el, test_time_base, false);
2665
2669
2670 talloc_free(trunk);
2671 talloc_free(ctx);
2672}
2673
2674/*
2675 * Test the global trunk FULL state: no active connections, but the
2676 * maximum permitted connections are all connected and full.
2677 */
2678static void test_trunk_state_full(void)
2679{
2680 TALLOC_CTX *ctx = talloc_init_const("test");
2681 trunk_t *trunk;
2683 trunk_conf_t conf = {
2684 .start = 0, /* No connections on start */
2685 .min = 0,
2686 .max = 1,
2687 .max_req_per_conn = 1,
2688 .target_req_per_conn = 1,
2689 .manage_interval = fr_time_delta_from_nsec(NSEC / 2)
2690 };
2691 test_proto_request_t *preq_a;
2692 trunk_request_t *treq_a = NULL;
2693 int next_prio = 0;
2694
2696
2697 el = fr_event_list_alloc(ctx, NULL, NULL);
2699
2700 /* Need to provide a timer starting value above zero */
2702
2703 trunk = test_setup_trunk(ctx, el, &conf, true, NULL);
2704
2705 /*
2706 * No connections at all -> IDLE.
2707 */
2708 TEST_CASE("IDLE - no connections");
2710
2711 /*
2712 * Enqueue a request. It enters the backlog and spawns a connection.
2713 */
2714 ALLOC_REQ(a);
2715 TEST_CHECK(trunk_request_enqueue(&treq_a, trunk, NULL, preq_a, NULL) == TRUNK_ENQUEUE_IN_BACKLOG);
2716
2720
2721 /*
2722 * The connection spawned by the backlog is connecting -> PENDING.
2723 */
2724 TEST_CASE("PENDING - connection connecting");
2727
2728 /*
2729 * Open the connection. The backlogged request is assigned to it,
2730 * which fills it (max_req_per_conn == 1). With max == 1 and that
2731 * one connection full (and none active or connecting), the trunk
2732 * is FULL.
2733 */
2736
2737 TEST_CASE("FULL - the maximum number of connections are all full");
2741
2742 talloc_free(trunk);
2743 talloc_free(ctx);
2744}
2745
2746#undef fr_time /* Need to the real time */
2748{
2749 TALLOC_CTX *ctx = talloc_init_const("test");
2750 trunk_t *trunk;
2752 int events;
2753 trunk_conf_t conf = {
2754 .start = 1,
2755 .min = 1,
2756 .max = 0,
2757 .max_req_per_conn = 0,
2758 .target_req_per_conn = 0, /* One request per connection */
2759 .req_pool_headers = 1,
2760 .req_pool_size = sizeof(test_proto_request_t),
2761 .manage_interval = fr_time_delta_from_nsec(NSEC / 2)
2762 };
2763 size_t i = 0, requests = 100000;
2764 fr_time_t enqueue_start, enqueue_stop, io_start, io_stop;
2765 fr_time_delta_t enqueue_time, io_time, total_time;
2766 trunk_request_t **treq_array;
2767 test_proto_request_t **preq_array;
2768 test_proto_stats_t stats;
2769
2771
2772 el = fr_event_list_alloc(ctx, NULL, NULL);
2774
2775 /* Need to provide a timer starting value above zero */
2777
2778 memset(&stats, 0, sizeof(stats));
2779 trunk = test_setup_trunk(ctx, el, &conf, true, &stats);
2780
2781 /*
2782 * Open the connections
2783 */
2786
2787 /*
2788 * Build up a cache of requests
2789 * This prevents all mallocs on request enqueue.
2790 *
2791 * When the server's running, this does represent
2792 * close to what we'd have as a steady state.
2793 */
2794 MEM(treq_array = talloc_array(ctx, trunk_request_t *, requests));
2795 for (i = 0; i < requests; i++) treq_array[i] = trunk_request_alloc(trunk, NULL);
2796 for (i = 0; i < requests; i++) trunk_request_free(&treq_array[i]);
2797
2798 MEM(preq_array = talloc_array(ctx, test_proto_request_t *, requests));
2799
2801
2802 TEST_CASE("Enqueue requests");
2803 enqueue_start = fr_time();
2804// ProfilerStart(getenv("FR_PROFILE"));
2805 for (i = 0; i < requests; i++) {
2806 trunk_request_t *treq;
2807 test_proto_request_t *preq = NULL;
2808
2809 treq = trunk_request_alloc(trunk, NULL);
2810 preq = talloc_zero(treq, test_proto_request_t);
2811 preq->treq = treq;
2812 trunk_request_enqueue(&treq, trunk, NULL, preq, NULL);
2813 }
2814 enqueue_stop = fr_time();
2815 enqueue_time = fr_time_sub(enqueue_stop, enqueue_start);
2816 if (acutest_verbose_level_ >= 1) {
2817 INFO("Enqueue time %pV (%u rps) (%"PRIu64"/%"PRIu64")",
2818 fr_box_time_delta(enqueue_time),
2819 (uint32_t)((float) requests / ((float)(fr_time_delta_unwrap(enqueue_time)) / NSEC)),
2820 trunk->pub.req_alloc_new, trunk->pub.req_alloc_reused);
2821 }
2822
2823 TEST_CASE("Perform I/O operations");
2824 io_start = fr_time();
2825 while (true) {
2827 if (!events) break;
2830 }
2831 io_stop = fr_time();
2832 io_time = fr_time_sub(io_stop, io_start);
2833
2834 if (acutest_verbose_level_ >= 1) {
2835 INFO("I/O time %pV (%u rps)",
2836 fr_box_time_delta(io_time),
2837 (uint32_t)((float) requests / ((float)(fr_time_delta_unwrap(io_time)) / NSEC)));
2838 }
2839
2840 if (acutest_verbose_level_ >= 1) {
2841 total_time = fr_time_sub(io_stop, enqueue_start);
2842 INFO("Total time %pV (%u rps)",
2843 fr_box_time_delta(total_time),
2844 (uint32_t)((float) requests / ((float)(fr_time_delta_unwrap(total_time)) / NSEC)));
2845 }
2846
2847 TEST_CHECK_LEN(stats.completed, requests);
2848 TEST_CHECK_LEN(stats.failed, 0);
2849 TEST_CHECK_LEN(stats.cancelled, 0);
2850 TEST_CHECK_LEN(stats.freed, requests);
2851
2852// ProfilerStop();
2853
2854 talloc_free(ctx);
2855}
2856
2857/*
2858 * Connection spawning
2859 */
2861 /*
2862 * Basic tests
2863 */
2864 { "Basic - Alloc then free", test_socket_pair_alloc_then_free },
2865 { "Basic - Alloc then reconnect then free", test_socket_pair_alloc_then_reconnect_then_free },
2866
2867 /*
2868 * Connection timeout
2869 */
2870 { "Timeouts - Connection", test_socket_pair_alloc_then_connect_timeout },
2871 { "Timeouts - Reconnect delay", test_socket_pair_alloc_then_reconnect_check_delay },
2872
2873 /*
2874 * Basic enqueue/dequeue
2875 */
2876 { "Enqueue - Basic", test_enqueue_basic },
2877 { "Enqueue - Cancellation points", test_enqueue_cancellation_points },
2878 { "Enqueue - Partial state transitions", test_partial_to_complete_states },
2879 { "Enqueue - Complete from the muxer", test_complete_from_mux },
2880 { "Requeue - On reconnect", test_requeue_on_reconnect },
2881
2882 /*
2883 * Rebalance
2884 */
2885 { "Rebalance - Connection rebalance", test_connection_rebalance_requests },
2886
2887 /*
2888 * Connection ordering
2889 */
2890 { "Order - time_window is zero", test_connection_order_zero_window },
2891 { "Order - time_window", test_connection_order_window },
2892 { "Order - sort", test_connection_order_sort },
2893 { "Order - sort = lru", test_connection_order_lru },
2894 { "Order - sort = lru, reorder after write", test_connection_order_lru_after_write },
2895 { "Order - sort = mrs", test_connection_order_mrs },
2896 { "Order - sort = rto", test_connection_order_rto },
2897 { "Order - Reordered after demux", test_connection_order_after_demux },
2898
2899 /*
2900 * Connection spawning tests
2901 */
2902 { "Spawn - Test connection start on enqueue", test_connection_start_on_enqueue },
2903 { "Spawn - Connection levels max", test_connection_levels_max },
2904 { "Spawn - Connection levels alternating edges",test_connection_levels_alternating_edges },
2905
2906 /*
2907 * Trunk state transitions
2908 */
2909 { "State - Pending, active, failed", test_trunk_state_pending_active_failed },
2910 { "State - Full", test_trunk_state_full },
2911
2912 /*
2913 * Performance tests
2914 */
2915 { "Speed Test - Enqueue, and I/O", test_enqueue_and_io_speed },
2917};
#define TEST_CHECK(cond)
Definition acutest.h:101
static int acutest_verbose_level_
Definition acutest.h:478
#define TEST_CASE(name)
Definition acutest.h:203
#define TEST_ASSERT(cond)
Definition acutest.h:125
#define TEST_TERMINATOR
Definition acutest.h:77
#define TEST_MSG(...)
Definition acutest.h:234
#define TEST_CHECK_LEN(_got, _exp)
static uint8_t request[4]
#define CMP_PREFER_SMALLER(_a, _b)
Evaluates to +1 for a > b, and -1 for a < b.
Definition build.h:122
#define CC_NO_UBSAN(_sanitize)
Definition build.h:540
#define UNUSED
Definition build.h:415
#define NUM_ELEMENTS(_t)
Definition build.h:443
fr_cmp_ret_t
Result of an ordering comparison.
Definition build.h:113
void fr_talloc_fault_setup(void)
Register talloc fault handlers.
Definition debug.c:1047
#define fr_cond_assert(_x)
Calls panic_action ifndef NDEBUG, else logs error and evaluates to value of _x.
Definition debug.h:177
#define MEM(x)
Definition debug.h:38
static void * fr_dlist_head(fr_dlist_head_t const *list_head)
Return the HEAD item of a list or NULL if the list is empty.
Definition dlist.h:468
static void * fr_dlist_pop_head(fr_dlist_head_t *list_head)
Remove the head item in a list.
Definition dlist.h:654
static int fr_dlist_insert_tail(fr_dlist_head_t *list_head, void *ptr)
Insert an item into the tail of a list.
Definition dlist.h:360
#define fr_dlist_talloc_init(_head, _type, _field)
Initialise the head structure of a doubly linked list.
Definition dlist.h:257
Head of a doubly linked list.
Definition dlist.h:51
Entry in a doubly linked list.
Definition dlist.h:41
#define fr_event_fd_insert(...)
Definition event.h:251
@ FR_EVENT_FILTER_IO
Combined filter for read/write functions/.
Definition event.h:87
talloc_free(hp)
void fr_event_service(fr_event_list_t *el)
Service any outstanding timer or file descriptor events.
Definition event.c:2441
int fr_event_corral(fr_event_list_t *el, fr_time_t now, bool wait)
Gather outstanding timer and file descriptor events.
Definition event.c:2128
uint64_t fr_event_list_num_timers(fr_event_list_t *el)
Return the number of timer events currently scheduled.
Definition event.c:591
#define fr_time()
Definition event.c:61
fr_event_list_t * fr_event_list_alloc(TALLOC_CTX *ctx, fr_event_status_cb_t status, void *status_uctx)
Initialise a new event list.
Definition event.c:2648
int fr_event_fd_delete(fr_event_list_t *el, int fd, fr_event_filter_t filter)
Remove a file descriptor from the event loop.
Definition event.c:1244
Stores all information relating to an event list.
Definition event.c:415
unsigned int uint32_t
long int ssize_t
unsigned char uint8_t
unsigned long int size_t
void * fr_minmax_heap_iter_next(fr_minmax_heap_t *hp, fr_minmax_heap_iter_t *iter)
Get the next entry in a minmax heap.
void * fr_minmax_heap_min_peek(fr_minmax_heap_t *hp)
void * fr_minmax_heap_iter_init(fr_minmax_heap_t *hp, fr_minmax_heap_iter_t *iter)
Iterate over entries in a minmax heap.
unsigned int fr_minmax_heap_iter_t
Definition minmax_heap.h:39
fr_cmp_ret_t fr_pointer_cmp(void const *a, void const *b)
Compares two pointers.
Definition misc.c:449
int fr_nonblock(UNUSED int fd)
Definition misc.c:293
static const trunk_io_funcs_t io_funcs
Definition bio.c:3598
#define fr_assert(_expr)
Definition rad_assert.h:37
#define INFO(fmt,...)
Definition radict.c:67
static fr_event_list_t * events
Definition radsniff.c:58
static rs_t * conf
Definition radsniff.c:52
fr_time_delta_t rttvar
RTT variation.
Definition rtt.h:50
fr_time_delta_t rtt
smoothed round trip time
Definition rtt.h:49
uint64_t connection_get_num_timed_out(connection_t const *conn)
Return the number of times this connection has timed out whilst connecting.
Definition connection.c:598
uint64_t connection_get_num_reconnected(connection_t const *conn)
Return the number of times we've attempted to establish or re-establish this connection.
Definition connection.c:586
void connection_signal_reconnect(connection_t *conn, connection_reason_t reason)
Asynchronously signal the connection should be reconnected.
int connection_signal_on_fd(connection_t *conn, int fd)
Setup the connection to change states to connected or failed based on I/O events.
connection_t * connection_alloc(TALLOC_CTX *ctx, fr_event_list_t *el, connection_funcs_t const *funcs, connection_conf_t const *conf, char const *log_prefix, void const *uctx)
Allocate a new connection.
@ CONNECTION_FAILED
Connection is being reconnected because it failed.
Definition connection.h:89
connection_state_t
Definition connection.h:47
@ CONNECTION_STATE_CONNECTED
File descriptor is open (ready for writing).
Definition connection.h:54
@ CONNECTION_STATE_CONNECTING
Waiting for connection to establish.
Definition connection.h:52
Holds a complete set of functions for a connection.
Definition connection.h:199
static char buff[sizeof("18446744073709551615")+3]
Definition size_tests.c:37
static fr_time_t test_time(void)
Definition slab_tests.c:43
static fr_time_t test_time_base
Definition slab_tests.c:42
return count
Definition module.c:173
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
Definition syserror.c:243
static TALLOC_CTX * talloc_init_const(char const *name)
Allocate a top level chunk with a constant name.
Definition talloc.h:127
static fr_time_delta_t fr_time_delta_from_msec(int64_t msec)
Definition time.h:584
static fr_time_t fr_time_add_time_delta(fr_time_t a, fr_time_delta_t b)
Definition time.h:173
static int64_t fr_time_delta_unwrap(fr_time_delta_t time)
Definition time.h:154
static fr_time_delta_t fr_time_delta_from_sec(int64_t sec)
Definition time.h:599
#define fr_time_delta_wrap(_time)
Definition time.h:152
#define fr_time_wrap(_time)
Definition time.h:145
#define fr_time_eq(_a, _b)
Definition time.h:241
#define fr_time_delta_eq(_a, _b)
Definition time.h:287
#define NSEC
Definition time.h:379
#define fr_time_gt(_a, _b)
Definition time.h:237
static fr_time_delta_t fr_time_delta_from_nsec(int64_t nsec)
Definition time.h:572
static fr_time_t fr_time_sub_time_delta(fr_time_t a, fr_time_delta_t b)
Definition time.h:213
#define fr_time_sub(_a, _b)
Subtract one time from another.
Definition time.h:229
#define fr_time_lt(_a, _b)
Definition time.h:239
A time delta, a difference in time measured in nanoseconds.
Definition time.h:80
"server local" time.
Definition time.h:69
void fr_timer_list_set_time_func(fr_timer_list_t *tl, fr_event_time_source_t func)
Override event list time source.
Definition timer.c:1333
A management API for bonding multiple connections together.
#define CONN_REORDER(_tconn)
Reorder the connections in the active heap.
Definition trunk.c:827
int trunk_connection_pop_cancellation(trunk_request_t **treq_out, trunk_connection_t *tconn)
Pop a cancellation request off a connection's cancellation queue.
Definition trunk.c:4025
trunk_conf_t conf
Trunk common configuration.
Definition trunk.c:228
void trunk_reconnect(trunk_t *trunk, int states, connection_reason_t reason)
Force the trunk to re-establish its connections.
Definition trunk.c:4996
trunk_io_funcs_t funcs
I/O functions.
Definition trunk.c:280
void trunk_request_signal_partial(trunk_request_t *treq)
Signal a partial write.
Definition trunk.c:2150
void trunk_request_signal_cancel_sent(trunk_request_t *treq)
Signal that a remote server has been notified of the cancellation.
Definition trunk.c:2397
void trunk_connection_signal_readable(trunk_connection_t *tconn)
Signal that a trunk connection is readable.
Definition trunk.c:4111
static fr_cmp_ret_t _trunk_connection_order_by_last_reply(void const *one, void const *two)
Order connections by the time of the last successful reply (MRU), and then by queue depth.
Definition trunk.c:5156
trunk_request_t * trunk_request_alloc(trunk_t *trunk, request_t *request)
(Pre-)Allocate a new trunk request
Definition trunk.c:2615
static fr_cmp_ret_t _trunk_connection_order_by_last_write(void const *one, void const *two)
Order connections by the number of requests waiting to be written, and then by the time of the last w...
Definition trunk.c:5280
uint32_t trunk_request_count_by_connection(trunk_connection_t const *tconn, int req_state)
Return the count number of requests associated with a trunk connection.
Definition trunk.c:3036
struct trunk_request_pub_s pub
Public fields in the trunk request.
Definition trunk.c:100
struct trunk_pub_s pub
Public fields in the trunk connection.
Definition trunk.c:220
trunk_enqueue_t trunk_request_enqueue(trunk_request_t **treq_out, trunk_t *trunk, request_t *request, void *preq, void *rctx)
Enqueue a request that needs data written to the trunk.
Definition trunk.c:2730
static fr_cmp_ret_t _trunk_connection_order_by_rto(void const *one, void const *two)
Order connections by the estimated time for a new request to complete.
Definition trunk.c:5244
void trunk_request_signal_cancel_complete(trunk_request_t *treq)
Signal that a remote server acked our cancellation.
Definition trunk.c:2421
int trunk_connection_pop_request(trunk_request_t **treq_out, trunk_connection_t *tconn)
Pop a request off a connection's pending queue.
Definition trunk.c:4073
fr_dlist_head_t connecting
Connections which are not yet in the open state.
Definition trunk.c:246
void trunk_request_signal_cancel(trunk_request_t *treq)
Cancel a trunk request.
Definition trunk.c:2289
struct trunk_connection_pub_s pub
Public fields in the trunk connection.
Definition trunk.c:138
uint16_t trunk_connection_count_by_state(trunk_t *trunk, int conn_state)
Return the count number of connections in the specified states.
Definition trunk.c:3012
trunk_t * trunk_alloc(TALLOC_CTX *ctx, fr_event_list_t *el, trunk_io_funcs_t const *funcs, trunk_conf_t const *conf, char const *log_prefix, void const *uctx, bool delay_start, fr_pair_list_t *trigger_args)
Allocate a new collection of connections.
Definition trunk.c:5407
fr_minmax_heap_t * active
Connections which can service requests.
Definition trunk.c:248
void trunk_request_free(trunk_request_t **treq_to_free)
If the trunk request is freed then update the target requests.
Definition trunk.c:2459
static fr_cmp_ret_t _trunk_connection_order_by_shortest_queue(void const *one, void const *two)
Order connections by queue depth.
Definition trunk.c:5125
static fr_cmp_ret_t _trunk_connection_order_by_mrs(void const *one, void const *two)
Order connections by the most recent sent time which had a reply (MRS), and then by queue depth.
Definition trunk.c:5198
void trunk_connection_signal_active(trunk_connection_t *tconn)
Signal a trunk connection is no longer full.
Definition trunk.c:4150
void trunk_connection_signal_inactive(trunk_connection_t *tconn)
Signal a trunk connection cannot accept more requests.
Definition trunk.c:4127
uint64_t trunk_request_count_by_state(trunk_t *trunk, int conn_state, int req_state)
Return a count of requests on a connection in a specific state.
Definition trunk.c:4784
void trunk_request_signal_cancel_partial(trunk_request_t *treq)
Signal a partial cancel write.
Definition trunk.c:2373
void trunk_request_signal_sent(trunk_request_t *treq)
Signal that the request was written to a connection successfully.
Definition trunk.c:2171
void trunk_request_signal_complete(trunk_request_t *treq)
Signal that a trunk request is complete.
Definition trunk.c:2215
void trunk_connection_signal_reconnect(trunk_connection_t *tconn, connection_reason_t reason)
Signal a trunk connection is no longer viable.
Definition trunk.c:4189
void trunk_connection_signal_writable(trunk_connection_t *tconn)
Signal that a trunk connection is writable.
Definition trunk.c:4093
Associates request queues with a connection.
Definition trunk.c:137
Wraps a normal request.
Definition trunk.c:99
Main trunk management handle.
Definition trunk.c:219
fr_time_t _CONST last_write_success
Last time we wrote to the connection.
Definition trunk.h:353
#define TRUNK_VERIFY(_trunk)
Definition trunk.h:977
trunk_connection_event_t
What type of I/O events the trunk connection is currently interested in receiving.
Definition trunk.h:82
@ TRUNK_CONN_EVENT_BOTH
Trunk should be notified if a connection is readable or writable.
Definition trunk.h:89
@ TRUNK_CONN_EVENT_WRITE
Trunk should be notified if a connection is writable.
Definition trunk.h:87
@ TRUNK_CONN_EVENT_NONE
Don't notify the trunk on connection state changes.
Definition trunk.h:83
@ TRUNK_CONN_EVENT_READ
Trunk should be notified if a connection is readable.
Definition trunk.h:85
fr_time_delta_t time_window
Connections within time_window responses (MRU) or time_window requests (LRU) are considered to be equ...
Definition trunk.h:296
#define TRUNK_REQUEST_STATE_ALL
All request states.
Definition trunk.h:222
fr_heap_cmp_t connection_prioritise
Ordering function for connections.
Definition trunk.h:782
fr_time_t mrs_time
Most recent sent time which had a reply.
Definition trunk.h:416
@ TRUNK_CONN_FULL
Connection is full and can't accept any more requests.
Definition trunk.h:105
@ TRUNK_CONN_CONNECTING
Connection is connecting.
Definition trunk.h:100
@ TRUNK_CONN_CLOSED
Connection was closed, either explicitly or due to failure.
Definition trunk.h:104
@ TRUNK_CONN_ACTIVE
Connection is connected and ready to service requests.
Definition trunk.h:101
@ TRUNK_STATE_PENDING
Trunk has connections, but none are usable yet; connections are being opened (INIT / CONNECTING).
Definition trunk.h:67
@ TRUNK_STATE_FAILED
Trunk has connections, but they have all failed and are closed / in reconnect backoff.
Definition trunk.h:73
@ TRUNK_STATE_ACTIVE
Trunk has at least one active connection which can service requests.
Definition trunk.h:65
@ TRUNK_STATE_FULL
Trunk has no active connections, but has one or more connected connections which are all full (at cap...
Definition trunk.h:69
@ TRUNK_STATE_IDLE
Trunk has no connections.
Definition trunk.h:64
@ TRUNK_SORT_QUEUE
Order by queue depth only.
Definition trunk.h:164
@ TRUNK_SORT_MRU
Most Recently Used (MRU): order by the time of the last successful reply, then by queue depth.
Definition trunk.h:165
@ TRUNK_SORT_MRS
Most Recent Sent (MRS): order by the larger mrs_time, then by queue depth.
Definition trunk.h:170
@ TRUNK_SORT_LRU
Least Recently Used (LRU): order by fewer requests in the pending and partial states,...
Definition trunk.h:167
@ TRUNK_SORT_RTO
Order by the smaller estimated time for a new request to complete.
Definition trunk.h:172
uint64_t _CONST req_alloc_reused
How many requests were reused.
Definition trunk.h:367
trunk_request_state_t _CONST state
Which list the request is now located in.
Definition trunk.h:382
fr_time_t _CONST last_write_success
Last time we wrote to the connection.
Definition trunk.h:408
trunk_connection_t *_CONST tconn
Connection this request belongs to.
Definition trunk.h:386
trunk_connection_alloc_t connection_alloc
Allocate a new connection_t.
Definition trunk.h:778
fr_time_t _CONST last_read_success
Last time we read from the connection.
Definition trunk.h:410
trunk_enqueue_t
Definition trunk.h:175
@ TRUNK_ENQUEUE_OK
Operation was successful.
Definition trunk.h:177
@ TRUNK_ENQUEUE_NO_CAPACITY
At maximum number of connections, and no connection has capacity.
Definition trunk.h:178
@ TRUNK_ENQUEUE_IN_BACKLOG
Request should be enqueued in backlog.
Definition trunk.h:176
#define TRUNK_CONN_ALL
All connection states.
Definition trunk.h:121
uint64_t _CONST req_alloc_new
How many requests we've allocated.
Definition trunk.h:365
connection_t *_CONST conn
The underlying connection.
Definition trunk.h:406
fr_rtt_t rtt
Smoothed round trip time, and variation.
Definition trunk.h:418
void *_CONST preq
Data for the muxer to write to the connection.
Definition trunk.h:388
trunk_request_state_t
Used for sanity checks and to simplify freeing.
Definition trunk.h:188
@ TRUNK_REQUEST_STATE_PARTIAL
Some of the request was written to the socket, more of it should be written later.
Definition trunk.h:197
@ TRUNK_REQUEST_STATE_CANCEL_SENT
We've informed the remote server that the request has been cancelled.
Definition trunk.h:212
@ TRUNK_REQUEST_STATE_CANCEL
A request on a particular socket was cancel.
Definition trunk.h:211
@ TRUNK_REQUEST_STATE_CANCEL_PARTIAL
We partially wrote a cancellation request.
Definition trunk.h:214
@ TRUNK_REQUEST_STATE_BACKLOG
In the backlog.
Definition trunk.h:194
@ TRUNK_REQUEST_STATE_PENDING
In the queue of a connection and is pending writing.
Definition trunk.h:195
@ TRUNK_REQUEST_STATE_SENT
Was written to a socket. Waiting for a response.
Definition trunk.h:199
trunk_request_cancel_mux_t request_cancel_mux
!< Read one or more requests from a connection.
Definition trunk.h:791
trunk_cancel_reason_t
Reasons for a request being cancelled.
Definition trunk.h:56
trunk_state_t _CONST state
Current state of the trunk.
Definition trunk.h:370
Common configuration parameters for a trunk.
Definition trunk.h:251
I/O functions to pass to trunk_alloc.
Definition trunk.h:777
static void test_connection_order_lru(void)
trunk_request_t * treq
Trunk request.
Definition trunk_tests.c:10
#define DEBUG_LVL_SET
Definition trunk_tests.c:47
static void test_connection_order_mrs(void)
fr_dlist_head_t sent
Requests written, in the order they were written.
Definition trunk_tests.c:44
TEST_LIST
static void test_demux(UNUSED fr_event_list_t *el, UNUSED trunk_connection_t *tconn, connection_t *conn, UNUSED void *uctx)
uint64_t cancelled
Count of tests in this run that were cancelled.
Definition trunk_tests.c:23
static void test_trunk_state_full(void)
uint64_t freed
Count of tests in this run that were freed.
Definition trunk_tests.c:26
bool complete_on_write
Muxer signals complete after the write, instead of sent.
Definition trunk_tests.c:17
int fd[2]
Socket pair. fd[1] loops data back to fd[0].
Definition trunk_tests.c:43
static connection_t * test_setup_socket_pair_connection_alloc(trunk_connection_t *tconn, fr_event_list_t *el, connection_conf_t const *conn_conf, char const *log_prefix, UNUSED void *uctx)
static void test_enqueue_and_io_speed(void)
bool cancelled
Seen by the cancelled callback.
Definition trunk_tests.c:11
static void _conn_io_error(UNUSED fr_event_list_t *el, UNUSED int fd, UNUSED int flags, UNUSED int fd_errno, void *uctx)
static void test_complete_from_mux(void)
static void _conn_io_loopback(UNUSED fr_event_list_t *el, int fd, UNUSED int flags, void *uctx)
Whenever the second socket in a socket pair is readable, read all pending data, and write it back.
static void test_mux(UNUSED fr_event_list_t *el, trunk_connection_t *tconn, connection_t *conn, UNUSED void *uctx)
Definition trunk_tests.c:73
static void test_trunk_state_pending_active_failed(void)
static void test_socket_pair_alloc_then_connect_timeout(void)
static void test_cancel_mux(UNUSED fr_event_list_t *el, trunk_connection_t *tconn, connection_t *conn, UNUSED void *uctx)
static void _conn_io_write(UNUSED fr_event_list_t *el, UNUSED int fd, UNUSED int flags, void *uctx)
static void test_partial_to_complete_states(void)
fr_dlist_t entry
Entry in test_conn_t sent.
Definition trunk_tests.c:38
static connection_t * test_setup_socket_pair_1s_timeout_connection_alloc(trunk_connection_t *tconn, fr_event_list_t *el, UNUSED connection_conf_t const *conf, char const *log_prefix, void *uctx)
static void test_request_free(UNUSED request_t *request, void *preq, void *uctx)
static void test_socket_pair_alloc_then_free(void)
bool failed
Seen by the failed callback.
Definition trunk_tests.c:13
static void test_request_fail(UNUSED request_t *request, void *preq, UNUSED void *rctx, UNUSED trunk_request_state_t state, void *uctx)
static void test_connection_rebalance_requests(void)
static connection_state_t _conn_init_no_signal(void **h_out, connection_t *conn, UNUSED void *uctx)
static connection_t * test_setup_socket_pair_1s_reconnection_delay_alloc(trunk_connection_t *tconn, fr_event_list_t *el, UNUSED connection_conf_t const *conn_conf, char const *log_prefix, void *uctx)
static void test_connection_order_zero_window(void)
static void test_connection_levels_max(void)
static void test_socket_pair_alloc_then_reconnect_check_delay(void)
static void test_connection_order_sort(void)
bool freed
Seen by the free callback.
Definition trunk_tests.c:14
static void test_connection_order_window(void)
uint64_t failed
Count of tests in this run that failed.
Definition trunk_tests.c:25
test_proto_request_t * preq
Request that was written.
Definition trunk_tests.c:39
static void _conn_io_read(UNUSED fr_event_list_t *el, UNUSED int fd, UNUSED int flags, void *uctx)
bool completed
Seen by the complete callback.
Definition trunk_tests.c:12
static void _conn_notify(trunk_connection_t *tconn, connection_t *conn, fr_event_list_t *el, trunk_connection_event_t notify_on, UNUSED void *uctx)
#define ALLOC_REQ(_id)
int priority
Priority of request.
Definition trunk_tests.c:19
static void test_socket_pair_alloc_then_reconnect_then_free(void)
static trunk_t * test_setup_trunk(TALLOC_CTX *ctx, fr_event_list_t *el, trunk_conf_t *conf, bool with_cancel_mux, void *uctx)
static connection_state_t _conn_open(fr_event_list_t *el, void *h, UNUSED void *uctx)
Insert I/O handlers that loop any data back round.
static void test_enqueue_cancellation_points(void)
static void test_connection_order_after_demux(void)
static fr_cmp_ret_t test_preq_cmp(void const *a, void const *b)
static void test_requeue_on_reconnect(void)
static void test_connection_start_on_enqueue(void)
static void test_connection_order_lru_after_write(void)
static void test_connection_order_rto(void)
bool signal_partial
Muxer should signal that this request is partially written.
Definition trunk_tests.c:15
static void test_request_complete(UNUSED request_t *request, void *preq, UNUSED void *rctx, void *uctx)
static int test_conn_write(test_conn_t *tc, test_proto_request_t *preq)
Write a request to the connection.
Definition trunk_tests.c:56
static void test_request_cancel(UNUSED connection_t *conn, void *preq, UNUSED trunk_cancel_reason_t reason, void *uctx)
static void test_enqueue_basic(void)
uint64_t completed
Count of tests in this run that completed.
Definition trunk_tests.c:24
static void test_connection_levels_alternating_edges(void)
static void _conn_close(UNUSED fr_event_list_t *el, void *h, UNUSED void *uctx)
static connection_state_t _conn_init(void **h_out, connection_t *conn, UNUSED void *uctx)
Allocate a basic socket pair.
bool signal_cancel_partial
Muxer should signal that this request is partially cancelled.
Definition trunk_tests.c:16
static void test_two_active(trunk_t *trunk, trunk_connection_t **a, trunk_connection_t **b)
Get the two active connections of a trunk which has two connections.
A request the muxer has written, waiting for the demuxer.
Definition trunk_tests.c:37
static fr_event_list_t * el
void fr_perror(char const *fmt,...)
Print the current error to stderr with a prefix.
Definition strerror.c:737
#define fr_box_time_delta(_val)
Definition value.h:397