1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
/* libcr_ipc/tests/test_rpc.c - Tests for <libcr_ipc/rpc.h>
*
* Copyright (C) 2025 Luke T. Shumaker <lukeshu@lukeshu.com>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#include <libcr/coroutine.h>
#include <libcr_ipc/rpc.h>
#include "test.h"
CR_RPC_DECLARE(intrpc, int, int)
/* Test that the RPC is fair, have worker1 start waiting first, and
* ensure that it gets the first request. */
COROUTINE cr_caller(void *_ch) {
intrpc_t *ch = _ch;
cr_begin();
int resp = intrpc_send_req(ch, 1);
test_assert(resp == 2);
resp = intrpc_send_req(ch, 3);
test_assert(resp == 4);
cr_exit();
}
COROUTINE cr_worker1(void *_ch) {
intrpc_t *ch = _ch;
cr_begin();
intrpc_req_t req = intrpc_recv_req(ch);
test_assert(req.req == 1);
intrpc_send_resp(req, 2);
cr_exit();
}
COROUTINE cr_worker2(void *_ch) {
intrpc_t *ch = _ch;
cr_begin();
intrpc_req_t req = intrpc_recv_req(ch);
test_assert(req.req == 3);
intrpc_send_resp(req, 4);
cr_exit();
}
int main() {
intrpc_t ch = {0};
coroutine_add("worker1", cr_worker1, &ch);
coroutine_add("caller", cr_caller, &ch);
coroutine_add("worker2", cr_worker2, &ch);
coroutine_main();
return 0;
}
|