1 /*
2  * Copyright (c) 2020 Fastly, Kazuho Oku
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to
6  * deal in the Software without restriction, including without limitation the
7  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8  * sell copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20  * IN THE SOFTWARE.
21  */
22 #include <stdlib.h>
23 #include "../../test.h"
24 #include "../../../../lib/http3/server.c"
25 
26 struct sched_node_t {
27     struct st_h2o_http3_req_scheduler_node_t super;
28     unsigned id;
29 };
30 
compare_node(struct st_h2o_http3_req_scheduler_t * sched,const struct st_h2o_http3_req_scheduler_node_t * _x,const struct st_h2o_http3_req_scheduler_node_t * _y)31 static int compare_node(struct st_h2o_http3_req_scheduler_t *sched, const struct st_h2o_http3_req_scheduler_node_t *_x,
32                         const struct st_h2o_http3_req_scheduler_node_t *_y)
33 {
34     struct sched_node_t *x = (void *)_x, *y = (void *)_y;
35     if (x->id < y->id) {
36         return -1;
37     } else if (x->id > y->id) {
38         return 1;
39     } else {
40         return 0;
41     }
42 }
43 
get_top_node(struct st_h2o_http3_req_scheduler_t * sched)44 static struct sched_node_t *get_top_node(struct st_h2o_http3_req_scheduler_t *sched)
45 {
46     h2o_linklist_t *anchor = &sched->active.urgencies[sched->active.smallest_urgency].high;
47     if (h2o_linklist_is_empty(anchor)) {
48         anchor = &sched->active.urgencies[sched->active.smallest_urgency].low;
49         assert(!h2o_linklist_is_empty(anchor));
50     }
51     return H2O_STRUCT_FROM_MEMBER(struct sched_node_t, super.link, anchor->next);
52 }
53 
run_once(struct st_h2o_http3_req_scheduler_t * sched)54 static void run_once(struct st_h2o_http3_req_scheduler_t *sched)
55 {
56     struct sched_node_t *top = get_top_node(sched);
57     ++top->super.call_cnt;
58     req_scheduler_setup_for_next(sched, &top->super, compare_node);
59 }
60 
test_scheduler(void)61 static void test_scheduler(void)
62 {
63     struct st_h2o_http3_req_scheduler_t sched;
64 
65     req_scheduler_init(&sched);
66     ok(sched.active.smallest_urgency == H2O_ABSPRIO_NUM_URGENCY_LEVELS);
67 
68     /* add three nodes in non-sequential order, check that they are ordered in sequence */
69     struct sched_node_t node1 = {.super = {.priority = {.urgency = 1}}, .id = 1};
70     struct sched_node_t node4 = {.super = {.priority = {.urgency = 1}}, .id = 4};
71     struct sched_node_t node5 = {.super = {.priority = {.urgency = 1}}, .id = 5};
72     req_scheduler_activate(&sched, &node1.super, compare_node);
73     req_scheduler_activate(&sched, &node5.super, compare_node);
74     req_scheduler_activate(&sched, &node4.super, compare_node);
75     ok(get_top_node(&sched) == &node1);
76     ok(get_top_node(&sched)->super.link.next == &node4.super.link);
77     ok(get_top_node(&sched)->super.link.next->next == &node5.super.link);
78     ok(get_top_node(&sched)->super.link.next->next->next == &sched.active.urgencies[1].high);
79 
80     /* running the top node (which is non-sequential) does not change the order */
81     run_once(&sched);
82     ok(get_top_node(&sched) == &node1);
83 
84     /* retire the top node, check that the id=4 is promoted */
85     req_scheduler_deactivate(&sched, &node1.super);
86     ok(get_top_node(&sched) == &node4);
87     ok(get_top_node(&sched)->super.link.next == &node5.super.link);
88     ok(get_top_node(&sched)->super.link.next->next == &sched.active.urgencies[1].high);
89 
90     /* add node2, 3 that are incremental, their initial slots will be in "high", in sequential order */
91     struct sched_node_t node2 = {.super = {.priority = {.urgency = 1, .incremental = 1}}, .id = 2};
92     struct sched_node_t node3 = {.super = {.priority = {.urgency = 1, .incremental = 1}}, .id = 3};
93     req_scheduler_activate(&sched, &node2.super, compare_node);
94     req_scheduler_activate(&sched, &node3.super, compare_node);
95     ok(get_top_node(&sched) == &node2);
96     ok(get_top_node(&sched)->super.link.next == &node3.super.link);
97     ok(get_top_node(&sched)->super.link.next->next == &node4.super.link);
98     ok(get_top_node(&sched)->super.link.next->next->next == &node5.super.link);
99     ok(get_top_node(&sched)->super.link.next->next->next->next == &sched.active.urgencies[1].high);
100 
101     /* run the top node multiple times; node2, node3 are moved to lower, but node4 stays at the top */
102     run_once(&sched);
103     ok(get_top_node(&sched) == &node3);
104     run_once(&sched);
105     ok(get_top_node(&sched) == &node4);
106     for (int i = 0; i < 3; ++i) {
107         run_once(&sched);
108         ok(get_top_node(&sched) == &node4);
109     }
110 
111     /* retire the non-incremental ones, check that the incremental ones are invoked one by one */
112     req_scheduler_deactivate(&sched, &node4.super);
113     req_scheduler_deactivate(&sched, &node5.super);
114     for (int i = 0; i < 4; ++i) {
115         ok(get_top_node(&sched) == &node2);
116         run_once(&sched);
117         ok(get_top_node(&sched) == &node3);
118         run_once(&sched);
119     }
120     ok(get_top_node(&sched) == &node2);
121 
122     /* node at a higher urgency level cuts in */
123     struct sched_node_t node6 = {.super = {.priority = {.urgency = 0}}, .id = 6};
124     req_scheduler_activate(&sched, &node6.super, compare_node);
125     ok(get_top_node(&sched) == &node6);
126     run_once(&sched);
127     ok(get_top_node(&sched) == &node6);
128 
129     /* retire all nodes */
130     req_scheduler_deactivate(&sched, &node2.super);
131     req_scheduler_deactivate(&sched, &node3.super);
132     req_scheduler_deactivate(&sched, &node6.super);
133     ok(sched.active.smallest_urgency == H2O_ABSPRIO_NUM_URGENCY_LEVELS);
134 }
135 
test_lib__http3_server(void)136 void test_lib__http3_server(void)
137 {
138     subtest("scheduler", test_scheduler);
139 }
140