|
| 1 | +/* |
| 2 | + * (C) 2020 The University of Chicago |
| 3 | + * |
| 4 | + * See COPYRIGHT in top-level directory. |
| 5 | + */ |
| 6 | +#include <stdio.h> |
| 7 | +#include <margo.h> |
| 8 | +#include <margo-hg-shim.h> |
| 9 | +#include <mercury_proc_string.h> |
| 10 | +#include <mercury_macros.h> |
| 11 | +#include "helper-server.h" |
| 12 | +#include "munit/munit.h" |
| 13 | +#include "munit/munit-goto.h" |
| 14 | + |
| 15 | +#define P(__msg__) printf("%s\n", __msg__); fflush(stdout) |
| 16 | + |
| 17 | +DECLARE_MARGO_RPC_HANDLER(rpc_ult) |
| 18 | +static void rpc_ult(hg_handle_t handle) |
| 19 | +{ |
| 20 | + margo_respond(handle, NULL); |
| 21 | + margo_destroy(handle); |
| 22 | + return; |
| 23 | +} |
| 24 | +DEFINE_MARGO_RPC_HANDLER(rpc_ult) |
| 25 | + |
| 26 | +static int svr_init_fn(margo_instance_id mid, void* arg) |
| 27 | +{ |
| 28 | + (void)arg; |
| 29 | + MARGO_REGISTER(mid, "rpc", void, void, rpc_ult); |
| 30 | + return (0); |
| 31 | +} |
| 32 | + |
| 33 | +struct test_context { |
| 34 | + margo_instance_id mid; |
| 35 | + int remote_pid; |
| 36 | + char remote_addr[256]; |
| 37 | +}; |
| 38 | + |
| 39 | +static void* test_context_setup(const MunitParameter params[], void* user_data) |
| 40 | +{ |
| 41 | + (void)params; |
| 42 | + (void)user_data; |
| 43 | + struct test_context* ctx = calloc(1, sizeof(*ctx)); |
| 44 | + |
| 45 | + const char* protocol = munit_parameters_get(params, "protocol"); |
| 46 | + hg_size_t remote_addr_size = 256; |
| 47 | + |
| 48 | + const char* config = "{" |
| 49 | + "\"rpc_pool\":\"my_rpc_pool\"," |
| 50 | + "\"progress_pool\":\"my_progress_pool\"," |
| 51 | + "\"argobots\": {" |
| 52 | + "\"pools\": [" |
| 53 | + "{ \"name\":\"my_rpc_pool\", \"kind\":\"fifo_wait\" }," |
| 54 | + "{ \"name\":\"my_progress_pool\", \"kind\":\"fifo_wait\" }" |
| 55 | + "]," |
| 56 | + "\"xstreams\": [" |
| 57 | + "{ \"name\":\"my_progress_xstream\"," |
| 58 | + "\"scheduler\": {\"type\":\"basic_wait\", \"pools\":[\"my_progress_pool\"]}" |
| 59 | + "}," |
| 60 | + "{ \"name\":\"my_rpc_xstream\"," |
| 61 | + "\"scheduler\": {\"type\":\"basic_wait\", \"pools\":[\"my_rpc_pool\"]}" |
| 62 | + "}" |
| 63 | + "]," |
| 64 | + "}" |
| 65 | + "}"; |
| 66 | + |
| 67 | + struct margo_init_info init_info = {0}; |
| 68 | + init_info.json_config = config; |
| 69 | + ctx->remote_pid = HS_start(protocol, &init_info, svr_init_fn, NULL, NULL, |
| 70 | + &(ctx->remote_addr[0]), &remote_addr_size); |
| 71 | + munit_assert_int(ctx->remote_pid, >, 0); |
| 72 | + |
| 73 | + ctx->mid = margo_init_ext(protocol, MARGO_SERVER_MODE, &init_info); |
| 74 | + if(!ctx->mid) { |
| 75 | + HS_stop(ctx->remote_pid, 0); |
| 76 | + } |
| 77 | + munit_assert_not_null(ctx->mid); |
| 78 | + |
| 79 | + return ctx; |
| 80 | +} |
| 81 | + |
| 82 | +static void test_context_tear_down(void* fixture) |
| 83 | +{ |
| 84 | + struct test_context* ctx = (struct test_context*)fixture; |
| 85 | + |
| 86 | + hg_addr_t remote_addr = HG_ADDR_NULL; |
| 87 | + margo_addr_lookup(ctx->mid, ctx->remote_addr, &remote_addr); |
| 88 | + margo_shutdown_remote_instance(ctx->mid, remote_addr); |
| 89 | + margo_addr_free(ctx->mid, remote_addr); |
| 90 | + HS_stop(ctx->remote_pid, 0); |
| 91 | + margo_finalize(ctx->mid); |
| 92 | + |
| 93 | + free(ctx); |
| 94 | +} |
| 95 | + |
| 96 | +static MunitResult test_migrate_progress_and_forward(const MunitParameter params[], |
| 97 | + void* data) |
| 98 | +{ |
| 99 | + (void)params; |
| 100 | + (void)data; |
| 101 | + hg_return_t hret[5] = {0,0,0,0,0}; |
| 102 | + int ret[5] = {0}; |
| 103 | + hg_handle_t handle = HG_HANDLE_NULL; |
| 104 | + hg_addr_t addr = HG_ADDR_NULL; |
| 105 | + |
| 106 | + struct test_context* ctx = (struct test_context*)data; |
| 107 | + |
| 108 | + // "rpc" is registered on the server, everything should be fine |
| 109 | + hg_id_t rpc_id = MARGO_REGISTER(ctx->mid, "rpc", void, void, NULL); |
| 110 | + |
| 111 | + hret[0] = margo_addr_lookup(ctx->mid, ctx->remote_addr, &addr); |
| 112 | + if(hret[0] != HG_SUCCESS) goto cleanup; |
| 113 | + hret[1] = margo_create(ctx->mid, addr, rpc_id, &handle); |
| 114 | + if(hret[1] != HG_SUCCESS) goto cleanup; |
| 115 | + hret[2] = margo_forward(handle, NULL); |
| 116 | + if(hret[2] != HG_SUCCESS) goto cleanup; |
| 117 | + hret[3] = margo_destroy(handle); |
| 118 | + if(hret[3] != HG_SUCCESS) goto cleanup; |
| 119 | + |
| 120 | + // create new pool and ES |
| 121 | + struct margo_pool_info pool_info = {0}; |
| 122 | + ret[0] = margo_add_pool_from_json(ctx->mid, |
| 123 | + "{ \"name\":\"my_new_progress_pool\", \"kind\":\"fifo_wait\" }", |
| 124 | + &pool_info); |
| 125 | + struct margo_xstream_info es_info = {0}; |
| 126 | + ret[1] = margo_add_xstream_from_json(ctx->mid, |
| 127 | + "{ \"name\":\"my_new_progress_xstream\"," |
| 128 | + "\"scheduler\": {\"type\":\"basic_wait\", \"pools\":[\"my_new_progress_pool\"]}" |
| 129 | + "}", |
| 130 | + &es_info); |
| 131 | + // migrate the progress loop |
| 132 | + ret[2] = margo_migrate_progress_loop(ctx->mid, pool_info.index); |
| 133 | + // erase old pool and ES |
| 134 | + ret[3] = margo_remove_xstream_by_name(ctx->mid, "my_progress_xstream"); |
| 135 | + ret[4] = margo_remove_pool_by_name(ctx->mid, "my_progress_pool"); |
| 136 | + |
| 137 | + // send another RPC |
| 138 | + hret[1] = margo_create(ctx->mid, addr, rpc_id, &handle); |
| 139 | + if(hret[1] != HG_SUCCESS) goto cleanup; |
| 140 | + hret[2] = margo_forward(handle, NULL); |
| 141 | + if(hret[2] != HG_SUCCESS) goto cleanup; |
| 142 | + |
| 143 | +cleanup: |
| 144 | + hret[3] = margo_destroy(handle); |
| 145 | + hret[4] = margo_addr_free(ctx->mid, addr); |
| 146 | + |
| 147 | + munit_assert_int_goto(hret[0], ==, HG_SUCCESS, error); |
| 148 | + munit_assert_int_goto(hret[1], ==, HG_SUCCESS, error); |
| 149 | + munit_assert_int_goto(hret[2], ==, HG_SUCCESS, error); |
| 150 | + munit_assert_int_goto(hret[3], ==, HG_SUCCESS, error); |
| 151 | + munit_assert_int_goto(hret[4], ==, HG_SUCCESS, error); |
| 152 | + munit_assert_int_goto(ret[0], ==, 0, error); |
| 153 | + munit_assert_int_goto(ret[1], ==, 0, error); |
| 154 | + munit_assert_int_goto(ret[2], ==, 0, error); |
| 155 | + munit_assert_int_goto(ret[3], ==, 0, error); |
| 156 | + munit_assert_int_goto(ret[4], ==, 0, error); |
| 157 | + return MUNIT_OK; |
| 158 | + |
| 159 | +error: |
| 160 | + return MUNIT_FAIL; |
| 161 | +} |
| 162 | + |
| 163 | +static char* protocol_params[] = {"na+sm", NULL}; |
| 164 | + |
| 165 | +static MunitParameterEnum test_params[] |
| 166 | + = {{"protocol", protocol_params}, |
| 167 | + {NULL, NULL}}; |
| 168 | + |
| 169 | +static MunitParameterEnum test_params2[] |
| 170 | + = {{"protocol", protocol_params}, |
| 171 | + {NULL, NULL}}; |
| 172 | + |
| 173 | +static MunitTest test_suite_tests[] = { |
| 174 | + {(char*)"/forward", test_migrate_progress_and_forward, test_context_setup, |
| 175 | + test_context_tear_down, MUNIT_TEST_OPTION_NONE, test_params}, |
| 176 | + {NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL}}; |
| 177 | + |
| 178 | +static const MunitSuite test_suite |
| 179 | + = {(char*)"/margo", test_suite_tests, NULL, 1, MUNIT_SUITE_OPTION_NONE}; |
| 180 | + |
| 181 | +int main(int argc, char* argv[MUNIT_ARRAY_PARAM(argc + 1)]) |
| 182 | +{ |
| 183 | + return munit_suite_main(&test_suite, NULL, argc, argv); |
| 184 | +} |
0 commit comments