| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/event-stream/event_stream_rpc_client.h> |
| 7 |
#include <aws/event-stream/private/event_stream_rpc_test_helper.h> |
| 8 |
|
| 9 |
#include <aws/common/condition_variable.h> |
| 10 |
#include <aws/common/device_random.h> |
| 11 |
#include <aws/common/macros.h> |
| 12 |
#include <aws/common/mutex.h> |
| 13 |
#include <aws/io/channel_bootstrap.h> |
| 14 |
#include <aws/io/event_loop.h> |
| 15 |
#include <aws/io/socket.h> |
| 16 |
|
| 17 |
#include <aws/testing/aws_test_harness.h> |
| 18 |
|
| 19 |
static const char *s_test_host_name = "127.0.0.1"; |
| 20 |
|
| 21 |
struct client_test_data { |
| 22 |
struct aws_allocator *allocator; |
| 23 |
int received_message_flags; |
| 24 |
struct aws_mutex sync_lock; |
| 25 |
struct aws_condition_variable sync_cvar; |
| 26 |
enum aws_event_stream_rpc_message_type received_message_type; |
| 27 |
struct aws_byte_buf received_payload; |
| 28 |
struct aws_event_stream_rpc_server_continuation_token *server_token; |
| 29 |
struct aws_byte_buf last_seen_operation_name; |
| 30 |
bool client_message_sent; |
| 31 |
int client_message_sent_error_code; |
| 32 |
bool client_message_received; |
| 33 |
bool server_message_sent; |
| 34 |
bool server_message_received; |
| 35 |
bool client_token_closed; |
| 36 |
bool server_token_closed; |
| 37 |
}; |
| 38 |
|
| 39 |
struct test_data { |
| 40 |
struct aws_allocator *allocator; |
| 41 |
struct aws_event_loop_group *el_group; |
| 42 |
struct aws_server_bootstrap *server_bootstrap; |
| 43 |
struct aws_client_bootstrap *client_bootstrap; |
| 44 |
struct aws_host_resolver *resolver; |
| 45 |
struct aws_event_stream_rpc_server_listener *listener; |
| 46 |
struct aws_event_stream_rpc_server_connection *server_connection; |
| 47 |
struct aws_event_stream_rpc_client_connection *client_connection; |
| 48 |
aws_event_stream_rpc_server_connection_protocol_message_fn *on_server_message_received; |
| 49 |
aws_event_stream_rpc_server_on_incoming_stream_fn *on_new_server_stream; |
| 50 |
aws_event_stream_rpc_server_stream_continuation_fn *on_new_server_continuation; |
| 51 |
aws_event_stream_rpc_server_stream_continuation_closed_fn *on_server_continuation_closed; |
| 52 |
|
| 53 |
aws_event_stream_rpc_client_connection_protocol_message_fn *on_client_message_received; |
| 54 |
|
| 55 |
struct client_test_data client_test_data; |
| 56 |
|
| 57 |
void *user_data; |
| 58 |
void *server_continuation_user_data; |
| 59 |
struct aws_mutex shutdown_lock; |
| 60 |
struct aws_condition_variable shutdown_cvar; |
| 61 |
struct aws_mutex setup_lock; |
| 62 |
struct aws_condition_variable setup_cvar; |
| 63 |
bool client_setup_completed; |
| 64 |
bool server_setup_completed; |
| 65 |
bool client_connection_shutdown; |
| 66 |
bool server_connection_shutdown_completed; |
| 67 |
bool event_loop_shutdown_completed; |
| 68 |
bool listener_shutdown_completed; |
| 69 |
bool resolver_shutdown_completed; |
| 70 |
}; |
| 71 |
|
| 72 |
static struct test_data s_test_data; |
| 73 |
|
| 74 |
static void s_fixture_on_server_protocol_message( |
| 75 |
struct aws_event_stream_rpc_server_connection *connection, |
| 76 |
const struct aws_event_stream_rpc_message_args *message_args, |
| 77 |
void *user_data) { |
| 78 |
struct test_data *test_data = user_data; |
| 79 |
test_data->on_server_message_received(connection, message_args, test_data->user_data); |
| 80 |
} |
| 81 |
|
| 82 |
static void s_on_stream_server_continuation_shim( |
| 83 |
struct aws_event_stream_rpc_server_continuation_token *token, |
| 84 |
const struct aws_event_stream_rpc_message_args *message_args, |
| 85 |
void *user_data) { |
| 86 |
struct test_data *test_data = user_data; |
| 87 |
test_data->on_new_server_continuation(token, message_args, test_data->server_continuation_user_data); |
| 88 |
} |
| 89 |
|
| 90 |
static void s_stream_server_continuation_closed_shim( |
| 91 |
struct aws_event_stream_rpc_server_continuation_token *token, |
| 92 |
void *user_data) { |
| 93 |
struct test_data *test_data = user_data; |
| 94 |
test_data->on_server_continuation_closed(token, test_data->server_continuation_user_data); |
| 95 |
} |
| 96 |
|
| 97 |
static int s_on_server_incoming_stream_shim( |
| 98 |
struct aws_event_stream_rpc_server_connection *connection, |
| 99 |
struct aws_event_stream_rpc_server_continuation_token *token, |
| 100 |
struct aws_byte_cursor operation_name, |
| 101 |
struct aws_event_stream_rpc_server_stream_continuation_options *continuation_options, |
| 102 |
void *user_data) { |
| 103 |
struct test_data *test_data = user_data; |
| 104 |
|
| 105 |
continuation_options->on_continuation = s_on_stream_server_continuation_shim; |
| 106 |
continuation_options->on_continuation_closed = s_stream_server_continuation_closed_shim; |
| 107 |
continuation_options->user_data = test_data; |
| 108 |
|
| 109 |
if (test_data->on_new_server_stream) { |
| 110 |
test_data->on_new_server_stream( |
| 111 |
connection, token, operation_name, continuation_options, test_data->server_continuation_user_data); |
| 112 |
} |
| 113 |
|
| 114 |
return AWS_OP_SUCCESS; |
| 115 |
} |
| 116 |
|
| 117 |
static int s_fixture_on_new_server_connection( |
| 118 |
struct aws_event_stream_rpc_server_connection *connection, |
| 119 |
int error_code, |
| 120 |
struct aws_event_stream_rpc_connection_options *connection_options, |
| 121 |
void *user_data) { |
| 122 |
(void)error_code; |
| 123 |
|
| 124 |
struct test_data *test_data = user_data; |
| 125 |
test_data->server_connection = connection; |
| 126 |
aws_mutex_lock(&test_data->setup_lock); |
| 127 |
test_data->server_setup_completed = true; |
| 128 |
aws_mutex_unlock(&test_data->setup_lock); |
| 129 |
aws_event_stream_rpc_server_connection_acquire(connection); |
| 130 |
|
| 131 |
connection_options->on_connection_protocol_message = s_fixture_on_server_protocol_message; |
| 132 |
connection_options->on_incoming_stream = s_on_server_incoming_stream_shim; |
| 133 |
connection_options->user_data = user_data; |
| 134 |
|
| 135 |
aws_condition_variable_notify_one(&test_data->setup_cvar); |
| 136 |
|
| 137 |
return AWS_OP_SUCCESS; |
| 138 |
} |
| 139 |
|
| 140 |
static void s_fixture_on_server_connection_shutdown( |
| 141 |
struct aws_event_stream_rpc_server_connection *connection, |
| 142 |
int error_code, |
| 143 |
void *user_data) { |
| 144 |
(void)connection; |
| 145 |
(void)error_code; |
| 146 |
|
| 147 |
struct test_data *test_data = user_data; |
| 148 |
|
| 149 |
aws_mutex_lock(&test_data->shutdown_lock); |
| 150 |
test_data->server_connection_shutdown_completed = true; |
| 151 |
aws_mutex_unlock(&test_data->shutdown_lock); |
| 152 |
aws_condition_variable_notify_one(&test_data->shutdown_cvar); |
| 153 |
} |
| 154 |
|
| 155 |
static bool s_server_connection_shutdown_completed(void *args) { |
| 156 |
struct test_data *test_data = args; |
| 157 |
return test_data->server_connection_shutdown_completed; |
| 158 |
} |
| 159 |
|
| 160 |
static void s_on_listener_destroy(struct aws_event_stream_rpc_server_listener *server, void *user_data) { |
| 161 |
(void)server; |
| 162 |
struct test_data *test_data = user_data; |
| 163 |
aws_mutex_lock(&test_data->shutdown_lock); |
| 164 |
test_data->listener_shutdown_completed = true; |
| 165 |
aws_mutex_unlock(&test_data->shutdown_lock); |
| 166 |
aws_condition_variable_notify_one(&test_data->shutdown_cvar); |
| 167 |
} |
| 168 |
|
| 169 |
static bool s_listener_shutdown_pred(void *arg) { |
| 170 |
struct test_data *test_data = arg; |
| 171 |
return test_data->listener_shutdown_completed; |
| 172 |
} |
| 173 |
|
| 174 |
static void s_event_loop_shutdown_callback(void *user_data) { |
| 175 |
struct test_data *test_data = user_data; |
| 176 |
aws_mutex_lock(&test_data->shutdown_lock); |
| 177 |
test_data->event_loop_shutdown_completed = true; |
| 178 |
aws_mutex_unlock(&test_data->shutdown_lock); |
| 179 |
aws_condition_variable_notify_one(&test_data->shutdown_cvar); |
| 180 |
} |
| 181 |
|
| 182 |
static bool s_event_loop_shutdown_pred(void *arg) { |
| 183 |
struct test_data *test_data = arg; |
| 184 |
return test_data->event_loop_shutdown_completed; |
| 185 |
} |
| 186 |
|
| 187 |
static void s_client_on_connection_setup( |
| 188 |
struct aws_event_stream_rpc_client_connection *connection, |
| 189 |
int error_code, |
| 190 |
void *user_data) { |
| 191 |
(void)error_code; |
| 192 |
struct test_data *test_data = user_data; |
| 193 |
|
| 194 |
aws_mutex_lock(&test_data->setup_lock); |
| 195 |
test_data->client_connection = connection; |
| 196 |
|
| 197 |
if (connection) { |
| 198 |
aws_event_stream_rpc_client_connection_acquire(connection); |
| 199 |
} |
| 200 |
test_data->client_setup_completed = true; |
| 201 |
aws_mutex_unlock(&test_data->setup_lock); |
| 202 |
aws_condition_variable_notify_one(&test_data->setup_cvar); |
| 203 |
} |
| 204 |
|
| 205 |
static void s_client_on_connection_shutdown( |
| 206 |
struct aws_event_stream_rpc_client_connection *connection, |
| 207 |
int error_code, |
| 208 |
void *user_data) { |
| 209 |
(void)connection; |
| 210 |
(void)error_code; |
| 211 |
|
| 212 |
struct test_data *test_data = user_data; |
| 213 |
|
| 214 |
aws_mutex_lock(&test_data->shutdown_lock); |
| 215 |
test_data->client_connection_shutdown = true; |
| 216 |
aws_mutex_unlock(&test_data->shutdown_lock); |
| 217 |
aws_condition_variable_notify_one(&test_data->shutdown_cvar); |
| 218 |
} |
| 219 |
|
| 220 |
static bool s_client_connection_shutdown_completed(void *args) { |
| 221 |
struct test_data *test_data = args; |
| 222 |
return test_data->client_connection_shutdown; |
| 223 |
} |
| 224 |
|
| 225 |
static void s_client_connection_protocol_message( |
| 226 |
struct aws_event_stream_rpc_client_connection *connection, |
| 227 |
const struct aws_event_stream_rpc_message_args *message_args, |
| 228 |
void *user_data) { |
| 229 |
struct test_data *test_data = user_data; |
| 230 |
test_data->on_client_message_received(connection, message_args, test_data->user_data); |
| 231 |
} |
| 232 |
|
| 233 |
static bool s_setup_completed_pred(void *arg) { |
| 234 |
struct test_data *test_data = arg; |
| 235 |
|
| 236 |
return test_data->client_setup_completed && test_data->server_setup_completed; |
| 237 |
} |
| 238 |
|
| 239 |
static void s_resolver_shutdown_completion_callback(void *arg) { |
| 240 |
struct test_data *test_data = arg; |
| 241 |
|
| 242 |
aws_mutex_lock(&test_data->shutdown_lock); |
| 243 |
test_data->resolver_shutdown_completed = true; |
| 244 |
aws_mutex_unlock(&test_data->shutdown_lock); |
| 245 |
aws_condition_variable_notify_one(&test_data->shutdown_cvar); |
| 246 |
} |
| 247 |
|
| 248 |
static bool s_resolver_shutdown_completed_pred(void *arg) { |
| 249 |
struct test_data *test_data = arg; |
| 250 |
|
| 251 |
return test_data->resolver_shutdown_completed; |
| 252 |
} |
| 253 |
|
| 254 |
static int s_fixture_setup(struct aws_allocator *allocator, void *ctx) { |
| 255 |
aws_event_stream_library_init(allocator); |
| 256 |
struct test_data *test_data = ctx; |
| 257 |
AWS_ZERO_STRUCT(*test_data); |
| 258 |
aws_mutex_init(&test_data->setup_lock); |
| 259 |
aws_mutex_init(&test_data->shutdown_lock); |
| 260 |
aws_condition_variable_init(&test_data->setup_cvar); |
| 261 |
aws_condition_variable_init(&test_data->shutdown_cvar); |
| 262 |
|
| 263 |
struct aws_shutdown_callback_options el_shutdown_options = { |
| 264 |
.shutdown_callback_fn = s_event_loop_shutdown_callback, |
| 265 |
.shutdown_callback_user_data = test_data, |
| 266 |
}; |
| 267 |
test_data->el_group = aws_event_loop_group_new_default(allocator, 1, &el_shutdown_options); |
| 268 |
ASSERT_NOT_NULL(test_data->el_group); |
| 269 |
test_data->server_bootstrap = aws_server_bootstrap_new(allocator, test_data->el_group); |
| 270 |
ASSERT_NOT_NULL(test_data->server_bootstrap); |
| 271 |
|
| 272 |
struct aws_shutdown_callback_options host_resolver_shutdown_options = { |
| 273 |
.shutdown_callback_fn = s_resolver_shutdown_completion_callback, |
| 274 |
.shutdown_callback_user_data = test_data, |
| 275 |
}; |
| 276 |
|
| 277 |
struct aws_host_resolver_default_options resolver_options = { |
| 278 |
.el_group = test_data->el_group, |
| 279 |
.max_entries = 1, |
| 280 |
.shutdown_options = &host_resolver_shutdown_options, |
| 281 |
}; |
| 282 |
|
| 283 |
test_data->resolver = aws_host_resolver_new_default(allocator, &resolver_options); |
| 284 |
ASSERT_NOT_NULL(test_data->resolver); |
| 285 |
|
| 286 |
struct aws_client_bootstrap_options client_bootstrap_options = { |
| 287 |
.user_data = test_data, |
| 288 |
.event_loop_group = test_data->el_group, |
| 289 |
.host_resolver = test_data->resolver, |
| 290 |
}; |
| 291 |
test_data->client_bootstrap = aws_client_bootstrap_new(allocator, &client_bootstrap_options); |
| 292 |
ASSERT_NOT_NULL(test_data->client_bootstrap); |
| 293 |
|
| 294 |
ASSERT_SUCCESS(aws_mutex_init(&test_data->shutdown_lock)); |
| 295 |
ASSERT_SUCCESS(aws_condition_variable_init(&test_data->shutdown_cvar)); |
| 296 |
|
| 297 |
ASSERT_SUCCESS(aws_mutex_init(&test_data->client_test_data.sync_lock)); |
| 298 |
ASSERT_SUCCESS(aws_condition_variable_init(&test_data->client_test_data.sync_cvar)); |
| 299 |
test_data->client_test_data.allocator = allocator; |
| 300 |
|
| 301 |
struct aws_socket_options socket_options = { |
| 302 |
.connect_timeout_ms = 3000, |
| 303 |
.domain = AWS_SOCKET_IPV4, |
| 304 |
.type = AWS_SOCKET_STREAM, |
| 305 |
}; |
| 306 |
|
| 307 |
|
| 308 |
uint16_t test_port = 0; |
| 309 |
while (!test_data->listener) { |
| 310 |
aws_device_random_u16(&test_port); |
| 311 |
test_port |= 0x8000; |
| 312 |
|
| 313 |
struct aws_event_stream_rpc_server_listener_options listener_options = { |
| 314 |
.socket_options = &socket_options, |
| 315 |
.host_name = s_test_host_name, |
| 316 |
.port = test_port, |
| 317 |
.bootstrap = test_data->server_bootstrap, |
| 318 |
.user_data = test_data, |
| 319 |
.on_new_connection = s_fixture_on_new_server_connection, |
| 320 |
.on_connection_shutdown = s_fixture_on_server_connection_shutdown, |
| 321 |
.on_destroy_callback = s_on_listener_destroy, |
| 322 |
}; |
| 323 |
|
| 324 |
test_data->listener = aws_event_stream_rpc_server_new_listener(allocator, &listener_options); |
| 325 |
if (!test_data->listener) { |
| 326 |
int error_code = aws_last_error(); |
| 327 |
ASSERT_TRUE(error_code == AWS_IO_SOCKET_ADDRESS_IN_USE || error_code == AWS_ERROR_NO_PERMISSION); |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
test_data->allocator = allocator; |
| 332 |
|
| 333 |
struct aws_event_stream_rpc_client_connection_options connection_options = { |
| 334 |
.socket_options = &socket_options, |
| 335 |
.user_data = test_data, |
| 336 |
.bootstrap = test_data->client_bootstrap, |
| 337 |
.host_name = s_test_host_name, |
| 338 |
.port = test_port, |
| 339 |
.on_connection_setup = s_client_on_connection_setup, |
| 340 |
.on_connection_shutdown = s_client_on_connection_shutdown, |
| 341 |
.on_connection_protocol_message = s_client_connection_protocol_message, |
| 342 |
}; |
| 343 |
|
| 344 |
ASSERT_SUCCESS(aws_event_stream_rpc_client_connection_connect(allocator, &connection_options)); |
| 345 |
|
| 346 |
aws_mutex_lock(&test_data->setup_lock); |
| 347 |
aws_condition_variable_wait_pred(&test_data->setup_cvar, &test_data->setup_lock, s_setup_completed_pred, test_data); |
| 348 |
aws_mutex_unlock(&test_data->setup_lock); |
| 349 |
|
| 350 |
ASSERT_NOT_NULL(&test_data->client_connection); |
| 351 |
return AWS_OP_SUCCESS; |
| 352 |
} |
| 353 |
|
| 354 |
static int s_fixture_shutdown(struct aws_allocator *allocator, int setup_result, void *ctx) { |
| 355 |
(void)allocator; |
| 356 |
struct test_data *test_data = ctx; |
| 357 |
|
| 358 |
if (!setup_result) { |
| 359 |
aws_mutex_lock(&test_data->shutdown_lock); |
| 360 |
aws_condition_variable_wait_pred( |
| 361 |
&test_data->shutdown_cvar, &test_data->shutdown_lock, s_server_connection_shutdown_completed, test_data); |
| 362 |
aws_condition_variable_wait_pred( |
| 363 |
&test_data->shutdown_cvar, &test_data->shutdown_lock, s_client_connection_shutdown_completed, test_data); |
| 364 |
aws_event_stream_rpc_client_connection_release(test_data->client_connection); |
| 365 |
aws_event_stream_rpc_server_connection_release(test_data->server_connection); |
| 366 |
aws_event_stream_rpc_server_listener_release(test_data->listener); |
| 367 |
aws_condition_variable_wait_pred( |
| 368 |
&test_data->shutdown_cvar, &test_data->shutdown_lock, s_listener_shutdown_pred, test_data); |
| 369 |
aws_server_bootstrap_release(test_data->server_bootstrap); |
| 370 |
aws_client_bootstrap_release(test_data->client_bootstrap); |
| 371 |
aws_host_resolver_release(test_data->resolver); |
| 372 |
aws_condition_variable_wait_pred( |
| 373 |
&test_data->shutdown_cvar, &test_data->shutdown_lock, s_resolver_shutdown_completed_pred, test_data); |
| 374 |
aws_event_loop_group_release(test_data->el_group); |
| 375 |
aws_condition_variable_wait_pred( |
| 376 |
&test_data->shutdown_cvar, &test_data->shutdown_lock, s_event_loop_shutdown_pred, test_data); |
| 377 |
aws_mutex_unlock(&test_data->shutdown_lock); |
| 378 |
aws_thread_join_all_managed(); |
| 379 |
aws_mutex_clean_up(&test_data->shutdown_lock); |
| 380 |
aws_condition_variable_clean_up(&test_data->shutdown_cvar); |
| 381 |
aws_mutex_clean_up(&test_data->setup_lock); |
| 382 |
aws_condition_variable_clean_up(&test_data->setup_cvar); |
| 383 |
aws_mutex_clean_up(&test_data->client_test_data.sync_lock); |
| 384 |
aws_condition_variable_clean_up(&test_data->client_test_data.sync_cvar); |
| 385 |
} |
| 386 |
|
| 387 |
aws_event_stream_library_clean_up(); |
| 388 |
|
| 389 |
return AWS_OP_SUCCESS; |
| 390 |
} |
| 391 |
|
| 392 |
static int s_test_event_stream_rpc_client_connection_setup_and_teardown(struct aws_allocator *allocator, void *ctx) { |
| 393 |
struct test_data *test_data = ctx; |
| 394 |
(void)allocator; |
| 395 |
|
| 396 |
|
| 397 |
aws_event_stream_rpc_client_connection_close(test_data->client_connection, AWS_ERROR_SUCCESS); |
| 398 |
aws_event_stream_rpc_server_connection_close(test_data->server_connection, AWS_ERROR_SUCCESS); |
| 399 |
return AWS_OP_SUCCESS; |
| 400 |
} |
| 401 |
|
| 402 |
AWS_TEST_CASE_FIXTURE( |
| 403 |
test_event_stream_rpc_client_connection_setup_and_teardown, |
| 404 |
s_fixture_setup, |
| 405 |
s_test_event_stream_rpc_client_connection_setup_and_teardown, |
| 406 |
s_fixture_shutdown, |
| 407 |
&s_test_data) |
| 408 |
|
| 409 |
static void s_rpc_client_message_flush(int error_code, void *user_data) { |
| 410 |
|
| 411 |
struct client_test_data *client_test_data = user_data; |
| 412 |
aws_mutex_lock(&client_test_data->sync_lock); |
| 413 |
if (error_code == AWS_ERROR_SUCCESS) { |
| 414 |
client_test_data->client_message_sent = true; |
| 415 |
} else { |
| 416 |
client_test_data->client_message_sent_error_code = error_code; |
| 417 |
} |
| 418 |
aws_condition_variable_notify_one(&client_test_data->sync_cvar); |
| 419 |
|
| 420 |
aws_mutex_unlock(&client_test_data->sync_lock); |
| 421 |
} |
| 422 |
|
| 423 |
static void s_rpc_server_message_flush(int error_code, void *user_data) { |
| 424 |
(void)error_code; |
| 425 |
|
| 426 |
struct client_test_data *client_test_data = user_data; |
| 427 |
aws_mutex_lock(&client_test_data->sync_lock); |
| 428 |
client_test_data->server_message_sent = true; |
| 429 |
aws_condition_variable_notify_one(&client_test_data->sync_cvar); |
| 430 |
|
| 431 |
aws_mutex_unlock(&client_test_data->sync_lock); |
| 432 |
} |
| 433 |
|
| 434 |
static bool s_rpc_client_message_transmission_completed_pred(void *arg) { |
| 435 |
struct client_test_data *client_test_data = arg; |
| 436 |
return client_test_data->client_message_sent && client_test_data->server_message_received; |
| 437 |
} |
| 438 |
|
| 439 |
static bool s_rpc_client_message_transmission_error_pred(void *arg) { |
| 440 |
struct client_test_data *client_test_data = arg; |
| 441 |
return client_test_data->client_message_sent_error_code != AWS_ERROR_SUCCESS; |
| 442 |
} |
| 443 |
|
| 444 |
static bool s_rpc_server_message_transmission_completed_pred(void *arg) { |
| 445 |
struct client_test_data *client_test_data = arg; |
| 446 |
return client_test_data->server_message_sent && client_test_data->client_message_received; |
| 447 |
} |
| 448 |
|
| 449 |
static void s_rpc_server_connection_protocol_message( |
| 450 |
struct aws_event_stream_rpc_server_connection *connection, |
| 451 |
const struct aws_event_stream_rpc_message_args *message_args, |
| 452 |
void *user_data) { |
| 453 |
(void)connection; |
| 454 |
|
| 455 |
struct client_test_data *client_test_data = user_data; |
| 456 |
aws_mutex_lock(&client_test_data->sync_lock); |
| 457 |
client_test_data->server_message_received = true; |
| 458 |
client_test_data->received_message_type = message_args->message_type; |
| 459 |
aws_byte_buf_init_copy(&client_test_data->received_payload, client_test_data->allocator, message_args->payload); |
| 460 |
aws_mutex_unlock(&client_test_data->sync_lock); |
| 461 |
aws_condition_variable_notify_one(&client_test_data->sync_cvar); |
| 462 |
} |
| 463 |
|
| 464 |
static void s_rpc_client_connection_protocol_message( |
| 465 |
struct aws_event_stream_rpc_client_connection *connection, |
| 466 |
const struct aws_event_stream_rpc_message_args *message_args, |
| 467 |
void *user_data) { |
| 468 |
(void)connection; |
| 469 |
struct client_test_data *client_test_data = user_data; |
| 470 |
|
| 471 |
aws_mutex_lock(&client_test_data->sync_lock); |
| 472 |
client_test_data->client_message_received = true; |
| 473 |
client_test_data->received_message_type = message_args->message_type; |
| 474 |
client_test_data->received_message_flags = message_args->message_flags; |
| 475 |
aws_byte_buf_init_copy(&client_test_data->received_payload, client_test_data->allocator, message_args->payload); |
| 476 |
aws_mutex_unlock(&client_test_data->sync_lock); |
| 477 |
aws_condition_variable_notify_one(&client_test_data->sync_cvar); |
| 478 |
} |
| 479 |
|
| 480 |
static int s_test_event_stream_rpc_client_connection_connect(struct aws_allocator *allocator, void *ctx) { |
| 481 |
|
| 482 |
struct test_data *test_data = ctx; |
| 483 |
test_data->on_server_message_received = s_rpc_server_connection_protocol_message; |
| 484 |
test_data->on_client_message_received = s_rpc_client_connection_protocol_message; |
| 485 |
|
| 486 |
struct client_test_data client_test_data = { |
| 487 |
.allocator = allocator, |
| 488 |
.sync_cvar = AWS_CONDITION_VARIABLE_INIT, |
| 489 |
.sync_lock = AWS_MUTEX_INIT, |
| 490 |
}; |
| 491 |
|
| 492 |
test_data->user_data = &client_test_data; |
| 493 |
|
| 494 |
struct aws_byte_buf connect_payload = aws_byte_buf_from_c_str("{ \"message\": \" connect message \" }"); |
| 495 |
struct aws_event_stream_rpc_message_args connect_args = { |
| 496 |
.headers_count = 0, |
| 497 |
.headers = NULL, |
| 498 |
.message_type = AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_CONNECT, |
| 499 |
.payload = &connect_payload, |
| 500 |
}; |
| 501 |
|
| 502 |
ASSERT_SUCCESS(aws_event_stream_rpc_client_connection_send_protocol_message( |
| 503 |
test_data->client_connection, &connect_args, s_rpc_client_message_flush, &client_test_data)); |
| 504 |
|
| 505 |
aws_mutex_lock(&client_test_data.sync_lock); |
| 506 |
aws_condition_variable_wait_pred( |
| 507 |
&client_test_data.sync_cvar, |
| 508 |
&client_test_data.sync_lock, |
| 509 |
s_rpc_client_message_transmission_completed_pred, |
| 510 |
&client_test_data); |
| 511 |
|
| 512 |
ASSERT_INT_EQUALS(AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_CONNECT, client_test_data.received_message_type); |
| 513 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 514 |
connect_payload.buffer, |
| 515 |
connect_payload.len, |
| 516 |
client_test_data.received_payload.buffer, |
| 517 |
client_test_data.received_payload.len); |
| 518 |
aws_byte_buf_clean_up(&client_test_data.received_payload); |
| 519 |
|
| 520 |
client_test_data.received_message_type = 0; |
| 521 |
client_test_data.client_message_received = false; |
| 522 |
client_test_data.client_message_sent = false; |
| 523 |
client_test_data.server_message_received = false; |
| 524 |
client_test_data.server_message_sent = false; |
| 525 |
|
| 526 |
connect_args.message_type = AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_CONNECT_ACK; |
| 527 |
connect_args.message_flags = AWS_EVENT_STREAM_RPC_MESSAGE_FLAG_CONNECTION_ACCEPTED; |
| 528 |
|
| 529 |
ASSERT_SUCCESS(aws_event_stream_rpc_server_connection_send_protocol_message( |
| 530 |
test_data->server_connection, &connect_args, s_rpc_server_message_flush, &client_test_data)); |
| 531 |
|
| 532 |
aws_condition_variable_wait_pred( |
| 533 |
&client_test_data.sync_cvar, |
| 534 |
&client_test_data.sync_lock, |
| 535 |
s_rpc_server_message_transmission_completed_pred, |
| 536 |
&client_test_data); |
| 537 |
|
| 538 |
ASSERT_INT_EQUALS(AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_CONNECT_ACK, client_test_data.received_message_type); |
| 539 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 540 |
connect_payload.buffer, |
| 541 |
connect_payload.len, |
| 542 |
client_test_data.received_payload.buffer, |
| 543 |
client_test_data.received_payload.len); |
| 544 |
aws_byte_buf_clean_up(&client_test_data.received_payload); |
| 545 |
|
| 546 |
aws_event_stream_rpc_client_connection_close(test_data->client_connection, AWS_ERROR_SUCCESS); |
| 547 |
aws_event_stream_rpc_server_connection_close(test_data->server_connection, AWS_ERROR_SUCCESS); |
| 548 |
|
| 549 |
aws_mutex_unlock(&client_test_data.sync_lock); |
| 550 |
aws_mutex_clean_up(&client_test_data.sync_lock); |
| 551 |
aws_condition_variable_clean_up(&client_test_data.sync_cvar); |
| 552 |
|
| 553 |
return AWS_OP_SUCCESS; |
| 554 |
} |
| 555 |
|
| 556 |
AWS_TEST_CASE_FIXTURE( |
| 557 |
test_event_stream_rpc_client_connection_connect, |
| 558 |
s_fixture_setup, |
| 559 |
s_test_event_stream_rpc_client_connection_connect, |
| 560 |
s_fixture_shutdown, |
| 561 |
&s_test_data) |
| 562 |
|
| 563 |
static int s_test_event_stream_rpc_client_connection_message_before_connect( |
| 564 |
struct aws_allocator *allocator, |
| 565 |
void *ctx) { |
| 566 |
(void)allocator; |
| 567 |
|
| 568 |
struct test_data *test_data = ctx; |
| 569 |
|
| 570 |
struct aws_byte_buf ping_payload = aws_byte_buf_from_c_str("{ \"message\": \" ping message \" }"); |
| 571 |
struct aws_event_stream_rpc_message_args connect_args = { |
| 572 |
.headers_count = 0, |
| 573 |
.headers = NULL, |
| 574 |
.message_type = AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_PING, |
| 575 |
.payload = &ping_payload, |
| 576 |
}; |
| 577 |
|
| 578 |
aws_event_stream_rpc_client_connection_send_protocol_message( |
| 579 |
test_data->client_connection, &connect_args, s_rpc_client_message_flush, &test_data->client_test_data); |
| 580 |
|
| 581 |
aws_mutex_lock(&test_data->client_test_data.sync_lock); |
| 582 |
aws_condition_variable_wait_pred( |
| 583 |
&test_data->client_test_data.sync_cvar, |
| 584 |
&test_data->client_test_data.sync_lock, |
| 585 |
s_rpc_client_message_transmission_error_pred, |
| 586 |
&test_data->client_test_data); |
| 587 |
|
| 588 |
ASSERT_INT_EQUALS( |
| 589 |
AWS_ERROR_EVENT_STREAM_RPC_PROTOCOL_ERROR, test_data->client_test_data.client_message_sent_error_code); |
| 590 |
|
| 591 |
aws_mutex_unlock(&test_data->client_test_data.sync_lock); |
| 592 |
|
| 593 |
aws_event_stream_rpc_client_connection_close(test_data->client_connection, AWS_ERROR_SUCCESS); |
| 594 |
aws_event_stream_rpc_server_connection_close(test_data->server_connection, AWS_ERROR_SUCCESS); |
| 595 |
|
| 596 |
return AWS_OP_SUCCESS; |
| 597 |
} |
| 598 |
|
| 599 |
AWS_TEST_CASE_FIXTURE( |
| 600 |
test_event_stream_rpc_client_connection_message_before_connect, |
| 601 |
s_fixture_setup, |
| 602 |
s_test_event_stream_rpc_client_connection_message_before_connect, |
| 603 |
s_fixture_shutdown, |
| 604 |
&s_test_data) |
| 605 |
|
| 606 |
static int s_test_event_stream_rpc_client_connection_protocol_message(struct aws_allocator *allocator, void *ctx) { |
| 607 |
|
| 608 |
struct test_data *test_data = ctx; |
| 609 |
test_data->on_server_message_received = s_rpc_server_connection_protocol_message; |
| 610 |
test_data->on_client_message_received = s_rpc_client_connection_protocol_message; |
| 611 |
|
| 612 |
struct client_test_data client_test_data = { |
| 613 |
.allocator = allocator, |
| 614 |
.sync_cvar = AWS_CONDITION_VARIABLE_INIT, |
| 615 |
.sync_lock = AWS_MUTEX_INIT, |
| 616 |
}; |
| 617 |
|
| 618 |
test_data->user_data = &client_test_data; |
| 619 |
|
| 620 |
struct aws_byte_buf connect_payload = aws_byte_buf_from_c_str("{ \"message\": \" connect message \" }"); |
| 621 |
struct aws_event_stream_rpc_message_args connect_args = { |
| 622 |
.headers_count = 0, |
| 623 |
.headers = NULL, |
| 624 |
.message_type = AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_CONNECT, |
| 625 |
.payload = &connect_payload, |
| 626 |
}; |
| 627 |
|
| 628 |
ASSERT_SUCCESS(aws_event_stream_rpc_client_connection_send_protocol_message( |
| 629 |
test_data->client_connection, &connect_args, s_rpc_client_message_flush, &client_test_data)); |
| 630 |
|
| 631 |
aws_mutex_lock(&client_test_data.sync_lock); |
| 632 |
aws_condition_variable_wait_pred( |
| 633 |
&client_test_data.sync_cvar, |
| 634 |
&client_test_data.sync_lock, |
| 635 |
s_rpc_client_message_transmission_completed_pred, |
| 636 |
&client_test_data); |
| 637 |
|
| 638 |
ASSERT_INT_EQUALS(AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_CONNECT, client_test_data.received_message_type); |
| 639 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 640 |
connect_payload.buffer, |
| 641 |
connect_payload.len, |
| 642 |
client_test_data.received_payload.buffer, |
| 643 |
client_test_data.received_payload.len); |
| 644 |
aws_byte_buf_clean_up(&client_test_data.received_payload); |
| 645 |
|
| 646 |
client_test_data.received_message_type = 0; |
| 647 |
client_test_data.client_message_received = false; |
| 648 |
client_test_data.client_message_sent = false; |
| 649 |
client_test_data.server_message_received = false; |
| 650 |
client_test_data.server_message_sent = false; |
| 651 |
|
| 652 |
connect_args.message_type = AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_CONNECT_ACK; |
| 653 |
connect_args.message_flags = AWS_EVENT_STREAM_RPC_MESSAGE_FLAG_CONNECTION_ACCEPTED; |
| 654 |
|
| 655 |
ASSERT_SUCCESS(aws_event_stream_rpc_server_connection_send_protocol_message( |
| 656 |
test_data->server_connection, &connect_args, s_rpc_server_message_flush, &client_test_data)); |
| 657 |
|
| 658 |
aws_condition_variable_wait_pred( |
| 659 |
&client_test_data.sync_cvar, |
| 660 |
&client_test_data.sync_lock, |
| 661 |
s_rpc_server_message_transmission_completed_pred, |
| 662 |
&client_test_data); |
| 663 |
|
| 664 |
ASSERT_INT_EQUALS(AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_CONNECT_ACK, client_test_data.received_message_type); |
| 665 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 666 |
connect_payload.buffer, |
| 667 |
connect_payload.len, |
| 668 |
client_test_data.received_payload.buffer, |
| 669 |
client_test_data.received_payload.len); |
| 670 |
aws_byte_buf_clean_up(&client_test_data.received_payload); |
| 671 |
|
| 672 |
client_test_data.received_message_type = 0; |
| 673 |
client_test_data.client_message_received = false; |
| 674 |
client_test_data.client_message_sent = false; |
| 675 |
client_test_data.server_message_received = false; |
| 676 |
client_test_data.server_message_sent = false; |
| 677 |
|
| 678 |
struct aws_byte_buf ping_payload = |
| 679 |
aws_byte_buf_from_c_str("{ \"message\": \"hello device that will further isolate humans from each other " |
| 680 |
"into an ever increasing digital dystopia.\" }"); |
| 681 |
|
| 682 |
struct aws_event_stream_rpc_message_args ping_args = { |
| 683 |
.headers_count = 0, |
| 684 |
.headers = NULL, |
| 685 |
.message_type = AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_PING, |
| 686 |
.payload = &ping_payload, |
| 687 |
}; |
| 688 |
|
| 689 |
ASSERT_SUCCESS(aws_event_stream_rpc_client_connection_send_protocol_message( |
| 690 |
test_data->client_connection, &ping_args, s_rpc_client_message_flush, &client_test_data)); |
| 691 |
|
| 692 |
aws_condition_variable_wait_pred( |
| 693 |
&client_test_data.sync_cvar, |
| 694 |
&client_test_data.sync_lock, |
| 695 |
s_rpc_client_message_transmission_completed_pred, |
| 696 |
&client_test_data); |
| 697 |
|
| 698 |
ASSERT_INT_EQUALS(AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_PING, client_test_data.received_message_type); |
| 699 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 700 |
ping_payload.buffer, |
| 701 |
ping_payload.len, |
| 702 |
client_test_data.received_payload.buffer, |
| 703 |
client_test_data.received_payload.len); |
| 704 |
aws_byte_buf_clean_up(&client_test_data.received_payload); |
| 705 |
|
| 706 |
ping_args.message_type = AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_PING_RESPONSE; |
| 707 |
|
| 708 |
client_test_data.received_message_type = 0; |
| 709 |
client_test_data.client_message_received = false; |
| 710 |
client_test_data.client_message_sent = false; |
| 711 |
client_test_data.server_message_received = false; |
| 712 |
client_test_data.server_message_sent = false; |
| 713 |
|
| 714 |
ASSERT_SUCCESS(aws_event_stream_rpc_server_connection_send_protocol_message( |
| 715 |
test_data->server_connection, &ping_args, s_rpc_server_message_flush, &client_test_data)); |
| 716 |
|
| 717 |
aws_condition_variable_wait_pred( |
| 718 |
&client_test_data.sync_cvar, |
| 719 |
&client_test_data.sync_lock, |
| 720 |
s_rpc_server_message_transmission_completed_pred, |
| 721 |
&client_test_data); |
| 722 |
|
| 723 |
ASSERT_INT_EQUALS(AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_PING_RESPONSE, client_test_data.received_message_type); |
| 724 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 725 |
ping_payload.buffer, |
| 726 |
ping_payload.len, |
| 727 |
client_test_data.received_payload.buffer, |
| 728 |
client_test_data.received_payload.len); |
| 729 |
aws_byte_buf_clean_up(&client_test_data.received_payload); |
| 730 |
|
| 731 |
aws_event_stream_rpc_client_connection_close(test_data->client_connection, AWS_ERROR_SUCCESS); |
| 732 |
aws_event_stream_rpc_server_connection_close(test_data->server_connection, AWS_ERROR_SUCCESS); |
| 733 |
|
| 734 |
aws_mutex_unlock(&client_test_data.sync_lock); |
| 735 |
aws_mutex_clean_up(&client_test_data.sync_lock); |
| 736 |
aws_condition_variable_clean_up(&client_test_data.sync_cvar); |
| 737 |
|
| 738 |
return AWS_OP_SUCCESS; |
| 739 |
} |
| 740 |
|
| 741 |
AWS_TEST_CASE_FIXTURE( |
| 742 |
test_event_stream_rpc_client_connection_protocol_message, |
| 743 |
s_fixture_setup, |
| 744 |
s_test_event_stream_rpc_client_connection_protocol_message, |
| 745 |
s_fixture_shutdown, |
| 746 |
&s_test_data) |
| 747 |
|
| 748 |
static void s_rpc_client_stream_continuation( |
| 749 |
struct aws_event_stream_rpc_client_continuation_token *token, |
| 750 |
const struct aws_event_stream_rpc_message_args *message_args, |
| 751 |
void *user_data) { |
| 752 |
(void)token; |
| 753 |
struct client_test_data *client_test_data = user_data; |
| 754 |
|
| 755 |
aws_mutex_lock(&client_test_data->sync_lock); |
| 756 |
client_test_data->client_message_received = true; |
| 757 |
client_test_data->received_message_type = message_args->message_type; |
| 758 |
aws_byte_buf_init_copy(&client_test_data->received_payload, client_test_data->allocator, message_args->payload); |
| 759 |
aws_mutex_unlock(&client_test_data->sync_lock); |
| 760 |
aws_condition_variable_notify_one(&client_test_data->sync_cvar); |
| 761 |
} |
| 762 |
|
| 763 |
static void s_rpc_client_stream_continuation_closed( |
| 764 |
struct aws_event_stream_rpc_client_continuation_token *token, |
| 765 |
void *user_data) { |
| 766 |
(void)token; |
| 767 |
|
| 768 |
struct client_test_data *client_test_data = user_data; |
| 769 |
aws_mutex_lock(&client_test_data->sync_lock); |
| 770 |
client_test_data->client_token_closed = true; |
| 771 |
aws_mutex_unlock(&client_test_data->sync_lock); |
| 772 |
aws_condition_variable_notify_one(&client_test_data->sync_cvar); |
| 773 |
} |
| 774 |
|
| 775 |
static bool s_rpc_client_continuation_token_closed_pred(void *arg) { |
| 776 |
struct client_test_data *client_test_data = arg; |
| 777 |
return client_test_data->client_token_closed && client_test_data->server_token_closed; |
| 778 |
} |
| 779 |
|
| 780 |
static int s_rpc_server_on_incoming_stream( |
| 781 |
struct aws_event_stream_rpc_server_connection *connection, |
| 782 |
struct aws_event_stream_rpc_server_continuation_token *token, |
| 783 |
struct aws_byte_cursor operation_name, |
| 784 |
struct aws_event_stream_rpc_server_stream_continuation_options *continuation_options, |
| 785 |
void *user_data) { |
| 786 |
(void)connection; |
| 787 |
(void)continuation_options; |
| 788 |
|
| 789 |
struct client_test_data *client_test_data = user_data; |
| 790 |
client_test_data->server_token = token; |
| 791 |
aws_byte_buf_init_copy_from_cursor( |
| 792 |
&client_test_data->last_seen_operation_name, client_test_data->allocator, operation_name); |
| 793 |
|
| 794 |
return AWS_OP_SUCCESS; |
| 795 |
} |
| 796 |
|
| 797 |
static void s_rpc_server_stream_continuation( |
| 798 |
struct aws_event_stream_rpc_server_continuation_token *token, |
| 799 |
const struct aws_event_stream_rpc_message_args *message_args, |
| 800 |
void *user_data) { |
| 801 |
(void)token; |
| 802 |
|
| 803 |
struct client_test_data *client_test_data = user_data; |
| 804 |
aws_mutex_lock(&client_test_data->sync_lock); |
| 805 |
client_test_data->server_message_received = true; |
| 806 |
client_test_data->received_message_type = message_args->message_type; |
| 807 |
aws_byte_buf_init_copy(&client_test_data->received_payload, client_test_data->allocator, message_args->payload); |
| 808 |
aws_mutex_unlock(&client_test_data->sync_lock); |
| 809 |
aws_condition_variable_notify_one(&client_test_data->sync_cvar); |
| 810 |
} |
| 811 |
|
| 812 |
static void s_rpc_server_stream_continuation_closed( |
| 813 |
struct aws_event_stream_rpc_server_continuation_token *token, |
| 814 |
void *user_data) { |
| 815 |
(void)token; |
| 816 |
struct client_test_data *client_test_data = user_data; |
| 817 |
aws_mutex_lock(&client_test_data->sync_lock); |
| 818 |
client_test_data->server_token_closed = true; |
| 819 |
aws_mutex_unlock(&client_test_data->sync_lock); |
| 820 |
aws_condition_variable_notify_one(&client_test_data->sync_cvar); |
| 821 |
} |
| 822 |
|
| 823 |
static int s_test_event_stream_rpc_client_connection_continuation_flow(struct aws_allocator *allocator, void *ctx) { |
| 824 |
|
| 825 |
struct test_data *test_data = ctx; |
| 826 |
test_data->on_server_message_received = s_rpc_server_connection_protocol_message; |
| 827 |
test_data->on_client_message_received = s_rpc_client_connection_protocol_message; |
| 828 |
test_data->on_new_server_stream = s_rpc_server_on_incoming_stream; |
| 829 |
test_data->on_new_server_continuation = s_rpc_server_stream_continuation; |
| 830 |
test_data->on_server_continuation_closed = s_rpc_server_stream_continuation_closed; |
| 831 |
|
| 832 |
struct client_test_data client_test_data = { |
| 833 |
.allocator = allocator, |
| 834 |
.sync_cvar = AWS_CONDITION_VARIABLE_INIT, |
| 835 |
.sync_lock = AWS_MUTEX_INIT, |
| 836 |
}; |
| 837 |
|
| 838 |
test_data->user_data = &client_test_data; |
| 839 |
test_data->server_continuation_user_data = &client_test_data; |
| 840 |
|
| 841 |
struct aws_byte_buf connect_payload = aws_byte_buf_from_c_str("{ \"message\": \" connect message \" }"); |
| 842 |
struct aws_event_stream_rpc_message_args connect_args = { |
| 843 |
.headers_count = 0, |
| 844 |
.headers = NULL, |
| 845 |
.message_type = AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_CONNECT, |
| 846 |
.payload = &connect_payload, |
| 847 |
}; |
| 848 |
|
| 849 |
ASSERT_SUCCESS(aws_event_stream_rpc_client_connection_send_protocol_message( |
| 850 |
test_data->client_connection, &connect_args, s_rpc_client_message_flush, &client_test_data)); |
| 851 |
|
| 852 |
aws_mutex_lock(&client_test_data.sync_lock); |
| 853 |
aws_condition_variable_wait_pred( |
| 854 |
&client_test_data.sync_cvar, |
| 855 |
&client_test_data.sync_lock, |
| 856 |
s_rpc_client_message_transmission_completed_pred, |
| 857 |
&client_test_data); |
| 858 |
|
| 859 |
ASSERT_INT_EQUALS(AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_CONNECT, client_test_data.received_message_type); |
| 860 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 861 |
connect_payload.buffer, |
| 862 |
connect_payload.len, |
| 863 |
client_test_data.received_payload.buffer, |
| 864 |
client_test_data.received_payload.len); |
| 865 |
aws_byte_buf_clean_up(&client_test_data.received_payload); |
| 866 |
|
| 867 |
client_test_data.received_message_type = 0; |
| 868 |
client_test_data.client_message_received = false; |
| 869 |
client_test_data.client_message_sent = false; |
| 870 |
client_test_data.server_message_received = false; |
| 871 |
client_test_data.server_message_sent = false; |
| 872 |
|
| 873 |
connect_args.message_type = AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_CONNECT_ACK; |
| 874 |
connect_args.message_flags = AWS_EVENT_STREAM_RPC_MESSAGE_FLAG_CONNECTION_ACCEPTED; |
| 875 |
|
| 876 |
ASSERT_SUCCESS(aws_event_stream_rpc_server_connection_send_protocol_message( |
| 877 |
test_data->server_connection, &connect_args, s_rpc_server_message_flush, &client_test_data)); |
| 878 |
|
| 879 |
aws_condition_variable_wait_pred( |
| 880 |
&client_test_data.sync_cvar, |
| 881 |
&client_test_data.sync_lock, |
| 882 |
s_rpc_server_message_transmission_completed_pred, |
| 883 |
&client_test_data); |
| 884 |
|
| 885 |
ASSERT_INT_EQUALS(AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_CONNECT_ACK, client_test_data.received_message_type); |
| 886 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 887 |
connect_payload.buffer, |
| 888 |
connect_payload.len, |
| 889 |
client_test_data.received_payload.buffer, |
| 890 |
client_test_data.received_payload.len); |
| 891 |
aws_byte_buf_clean_up(&client_test_data.received_payload); |
| 892 |
|
| 893 |
client_test_data.received_message_type = 0; |
| 894 |
client_test_data.client_message_received = false; |
| 895 |
client_test_data.client_message_sent = false; |
| 896 |
client_test_data.server_message_received = false; |
| 897 |
client_test_data.server_message_sent = false; |
| 898 |
|
| 899 |
struct aws_event_stream_rpc_client_stream_continuation_options continuation_options = { |
| 900 |
.user_data = &client_test_data, |
| 901 |
.on_continuation = s_rpc_client_stream_continuation, |
| 902 |
.on_continuation_closed = s_rpc_client_stream_continuation_closed, |
| 903 |
}; |
| 904 |
|
| 905 |
struct aws_event_stream_rpc_client_continuation_token *client_token = |
| 906 |
aws_event_stream_rpc_client_connection_new_stream(test_data->client_connection, &continuation_options); |
| 907 |
ASSERT_NOT_NULL(client_token); |
| 908 |
|
| 909 |
struct aws_byte_cursor operation_name = aws_byte_cursor_from_c_str("test_operation"); |
| 910 |
|
| 911 |
struct aws_byte_buf operation_payload = aws_byte_buf_from_c_str("{ \"message\": \" operation payload \" }"); |
| 912 |
struct aws_event_stream_rpc_message_args operation_args = { |
| 913 |
.headers_count = 0, |
| 914 |
.headers = NULL, |
| 915 |
.message_type = AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_APPLICATION_MESSAGE, |
| 916 |
.payload = &operation_payload, |
| 917 |
}; |
| 918 |
|
| 919 |
ASSERT_SUCCESS(aws_event_stream_rpc_client_continuation_activate( |
| 920 |
client_token, operation_name, &operation_args, s_rpc_client_message_flush, &client_test_data)); |
| 921 |
|
| 922 |
aws_condition_variable_wait_pred( |
| 923 |
&client_test_data.sync_cvar, |
| 924 |
&client_test_data.sync_lock, |
| 925 |
s_rpc_client_message_transmission_completed_pred, |
| 926 |
&client_test_data); |
| 927 |
|
| 928 |
ASSERT_INT_EQUALS(AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_APPLICATION_MESSAGE, client_test_data.received_message_type); |
| 929 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 930 |
operation_payload.buffer, |
| 931 |
operation_payload.len, |
| 932 |
client_test_data.received_payload.buffer, |
| 933 |
client_test_data.received_payload.len); |
| 934 |
aws_byte_buf_clean_up(&client_test_data.received_payload); |
| 935 |
|
| 936 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 937 |
operation_name.ptr, |
| 938 |
operation_name.len, |
| 939 |
client_test_data.last_seen_operation_name.buffer, |
| 940 |
client_test_data.last_seen_operation_name.len); |
| 941 |
aws_byte_buf_clean_up(&client_test_data.last_seen_operation_name); |
| 942 |
|
| 943 |
client_test_data.received_message_type = 0; |
| 944 |
client_test_data.client_message_received = false; |
| 945 |
client_test_data.client_message_sent = false; |
| 946 |
client_test_data.server_message_received = false; |
| 947 |
client_test_data.server_message_sent = false; |
| 948 |
|
| 949 |
operation_args.message_flags = AWS_EVENT_STREAM_RPC_MESSAGE_FLAG_TERMINATE_STREAM; |
| 950 |
operation_args.message_type = AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_APPLICATION_ERROR; |
| 951 |
|
| 952 |
ASSERT_SUCCESS(aws_event_stream_rpc_server_continuation_send_message( |
| 953 |
client_test_data.server_token, &operation_args, s_rpc_server_message_flush, &client_test_data)); |
| 954 |
|
| 955 |
aws_condition_variable_wait_pred( |
| 956 |
&client_test_data.sync_cvar, |
| 957 |
&client_test_data.sync_lock, |
| 958 |
s_rpc_server_message_transmission_completed_pred, |
| 959 |
&client_test_data); |
| 960 |
|
| 961 |
aws_condition_variable_wait_pred( |
| 962 |
&client_test_data.sync_cvar, |
| 963 |
&client_test_data.sync_lock, |
| 964 |
s_rpc_client_continuation_token_closed_pred, |
| 965 |
&client_test_data); |
| 966 |
|
| 967 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 968 |
operation_payload.buffer, |
| 969 |
operation_payload.len, |
| 970 |
client_test_data.received_payload.buffer, |
| 971 |
client_test_data.received_payload.len); |
| 972 |
|
| 973 |
ASSERT_INT_EQUALS(AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_APPLICATION_ERROR, client_test_data.received_message_type); |
| 974 |
|
| 975 |
aws_byte_buf_clean_up(&client_test_data.received_payload); |
| 976 |
aws_event_stream_rpc_client_continuation_release(client_token); |
| 977 |
|
| 978 |
aws_event_stream_rpc_client_connection_close(test_data->client_connection, AWS_ERROR_SUCCESS); |
| 979 |
aws_event_stream_rpc_server_connection_close(test_data->server_connection, AWS_ERROR_SUCCESS); |
| 980 |
|
| 981 |
aws_mutex_unlock(&client_test_data.sync_lock); |
| 982 |
aws_mutex_clean_up(&client_test_data.sync_lock); |
| 983 |
aws_condition_variable_clean_up(&client_test_data.sync_cvar); |
| 984 |
|
| 985 |
return AWS_OP_SUCCESS; |
| 986 |
} |
| 987 |
|
| 988 |
AWS_TEST_CASE_FIXTURE( |
| 989 |
test_event_stream_rpc_client_connection_continuation_flow, |
| 990 |
s_fixture_setup, |
| 991 |
s_test_event_stream_rpc_client_connection_continuation_flow, |
| 992 |
s_fixture_shutdown, |
| 993 |
&s_test_data) |
| 994 |
|
| 995 |
static int s_test_event_stream_rpc_client_connection_unactivated_continuation_fails( |
| 996 |
struct aws_allocator *allocator, |
| 997 |
void *ctx) { |
| 998 |
|
| 999 |
struct test_data *test_data = ctx; |
| 1000 |
test_data->on_server_message_received = s_rpc_server_connection_protocol_message; |
| 1001 |
test_data->on_client_message_received = s_rpc_client_connection_protocol_message; |
| 1002 |
test_data->on_new_server_stream = s_rpc_server_on_incoming_stream; |
| 1003 |
test_data->on_new_server_continuation = s_rpc_server_stream_continuation; |
| 1004 |
test_data->on_server_continuation_closed = s_rpc_server_stream_continuation_closed; |
| 1005 |
|
| 1006 |
struct client_test_data client_test_data = { |
| 1007 |
.allocator = allocator, |
| 1008 |
.sync_cvar = AWS_CONDITION_VARIABLE_INIT, |
| 1009 |
.sync_lock = AWS_MUTEX_INIT, |
| 1010 |
}; |
| 1011 |
|
| 1012 |
test_data->user_data = &client_test_data; |
| 1013 |
test_data->server_continuation_user_data = &client_test_data; |
| 1014 |
|
| 1015 |
struct aws_byte_buf connect_payload = aws_byte_buf_from_c_str("{ \"message\": \" connect message \" }"); |
| 1016 |
struct aws_event_stream_rpc_message_args connect_args = { |
| 1017 |
.headers_count = 0, |
| 1018 |
.headers = NULL, |
| 1019 |
.message_type = AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_CONNECT, |
| 1020 |
.payload = &connect_payload, |
| 1021 |
}; |
| 1022 |
|
| 1023 |
ASSERT_SUCCESS(aws_event_stream_rpc_client_connection_send_protocol_message( |
| 1024 |
test_data->client_connection, &connect_args, s_rpc_client_message_flush, &client_test_data)); |
| 1025 |
|
| 1026 |
aws_mutex_lock(&client_test_data.sync_lock); |
| 1027 |
aws_condition_variable_wait_pred( |
| 1028 |
&client_test_data.sync_cvar, |
| 1029 |
&client_test_data.sync_lock, |
| 1030 |
s_rpc_client_message_transmission_completed_pred, |
| 1031 |
&client_test_data); |
| 1032 |
|
| 1033 |
ASSERT_INT_EQUALS(AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_CONNECT, client_test_data.received_message_type); |
| 1034 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 1035 |
connect_payload.buffer, |
| 1036 |
connect_payload.len, |
| 1037 |
client_test_data.received_payload.buffer, |
| 1038 |
client_test_data.received_payload.len); |
| 1039 |
aws_byte_buf_clean_up(&client_test_data.received_payload); |
| 1040 |
|
| 1041 |
client_test_data.received_message_type = 0; |
| 1042 |
client_test_data.client_message_received = false; |
| 1043 |
client_test_data.client_message_sent = false; |
| 1044 |
client_test_data.server_message_received = false; |
| 1045 |
client_test_data.server_message_sent = false; |
| 1046 |
|
| 1047 |
connect_args.message_type = AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_CONNECT_ACK; |
| 1048 |
connect_args.message_flags = AWS_EVENT_STREAM_RPC_MESSAGE_FLAG_CONNECTION_ACCEPTED; |
| 1049 |
|
| 1050 |
ASSERT_SUCCESS(aws_event_stream_rpc_server_connection_send_protocol_message( |
| 1051 |
test_data->server_connection, &connect_args, s_rpc_server_message_flush, &client_test_data)); |
| 1052 |
|
| 1053 |
aws_condition_variable_wait_pred( |
| 1054 |
&client_test_data.sync_cvar, |
| 1055 |
&client_test_data.sync_lock, |
| 1056 |
s_rpc_server_message_transmission_completed_pred, |
| 1057 |
&client_test_data); |
| 1058 |
|
| 1059 |
ASSERT_INT_EQUALS(AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_CONNECT_ACK, client_test_data.received_message_type); |
| 1060 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 1061 |
connect_payload.buffer, |
| 1062 |
connect_payload.len, |
| 1063 |
client_test_data.received_payload.buffer, |
| 1064 |
client_test_data.received_payload.len); |
| 1065 |
aws_byte_buf_clean_up(&client_test_data.received_payload); |
| 1066 |
|
| 1067 |
client_test_data.received_message_type = 0; |
| 1068 |
client_test_data.client_message_received = false; |
| 1069 |
client_test_data.client_message_sent = false; |
| 1070 |
client_test_data.server_message_received = false; |
| 1071 |
client_test_data.server_message_sent = false; |
| 1072 |
|
| 1073 |
struct aws_event_stream_rpc_client_stream_continuation_options continuation_options = { |
| 1074 |
.user_data = &client_test_data, |
| 1075 |
.on_continuation = s_rpc_client_stream_continuation, |
| 1076 |
.on_continuation_closed = s_rpc_client_stream_continuation_closed, |
| 1077 |
}; |
| 1078 |
|
| 1079 |
struct aws_event_stream_rpc_client_continuation_token *client_token = |
| 1080 |
aws_event_stream_rpc_client_connection_new_stream(test_data->client_connection, &continuation_options); |
| 1081 |
ASSERT_NOT_NULL(client_token); |
| 1082 |
|
| 1083 |
struct aws_byte_buf operation_payload = aws_byte_buf_from_c_str("{ \"message\": \" operation payload \" }"); |
| 1084 |
struct aws_event_stream_rpc_message_args operation_args = { |
| 1085 |
.headers_count = 0, |
| 1086 |
.headers = NULL, |
| 1087 |
.message_type = AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_APPLICATION_MESSAGE, |
| 1088 |
.payload = &operation_payload, |
| 1089 |
}; |
| 1090 |
|
| 1091 |
ASSERT_ERROR( |
| 1092 |
AWS_ERROR_EVENT_STREAM_RPC_STREAM_NOT_ACTIVATED, |
| 1093 |
aws_event_stream_rpc_client_continuation_send_message( |
| 1094 |
client_token, &operation_args, s_rpc_client_message_flush, &client_test_data)); |
| 1095 |
|
| 1096 |
aws_event_stream_rpc_client_continuation_release(client_token); |
| 1097 |
|
| 1098 |
aws_event_stream_rpc_client_connection_close(test_data->client_connection, AWS_ERROR_SUCCESS); |
| 1099 |
aws_event_stream_rpc_server_connection_close(test_data->server_connection, AWS_ERROR_SUCCESS); |
| 1100 |
|
| 1101 |
aws_mutex_unlock(&client_test_data.sync_lock); |
| 1102 |
aws_mutex_clean_up(&client_test_data.sync_lock); |
| 1103 |
aws_condition_variable_clean_up(&client_test_data.sync_cvar); |
| 1104 |
|
| 1105 |
return AWS_OP_SUCCESS; |
| 1106 |
} |
| 1107 |
|
| 1108 |
AWS_TEST_CASE_FIXTURE( |
| 1109 |
test_event_stream_rpc_client_connection_unactivated_continuation_fails, |
| 1110 |
s_fixture_setup, |
| 1111 |
s_test_event_stream_rpc_client_connection_unactivated_continuation_fails, |
| 1112 |
s_fixture_shutdown, |
| 1113 |
&s_test_data) |
| 1114 |
|
| 1115 |
static int s_test_event_stream_rpc_client_connection_continuation_send_message_on_closed_fails( |
| 1116 |
struct aws_allocator *allocator, |
| 1117 |
void *ctx) { |
| 1118 |
|
| 1119 |
struct test_data *test_data = ctx; |
| 1120 |
test_data->on_server_message_received = s_rpc_server_connection_protocol_message; |
| 1121 |
test_data->on_client_message_received = s_rpc_client_connection_protocol_message; |
| 1122 |
test_data->on_new_server_stream = s_rpc_server_on_incoming_stream; |
| 1123 |
test_data->on_new_server_continuation = s_rpc_server_stream_continuation; |
| 1124 |
test_data->on_server_continuation_closed = s_rpc_server_stream_continuation_closed; |
| 1125 |
|
| 1126 |
struct client_test_data client_test_data = { |
| 1127 |
.allocator = allocator, |
| 1128 |
.sync_cvar = AWS_CONDITION_VARIABLE_INIT, |
| 1129 |
.sync_lock = AWS_MUTEX_INIT, |
| 1130 |
}; |
| 1131 |
|
| 1132 |
test_data->user_data = &client_test_data; |
| 1133 |
test_data->server_continuation_user_data = &client_test_data; |
| 1134 |
|
| 1135 |
|
| 1136 |
struct aws_byte_buf connect_payload = aws_byte_buf_from_c_str("{ \"message\": \" connect message \" }"); |
| 1137 |
struct aws_event_stream_rpc_message_args connect_args = { |
| 1138 |
.headers_count = 0, |
| 1139 |
.headers = NULL, |
| 1140 |
.message_type = AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_CONNECT, |
| 1141 |
.payload = &connect_payload, |
| 1142 |
}; |
| 1143 |
|
| 1144 |
ASSERT_SUCCESS(aws_event_stream_rpc_client_connection_send_protocol_message( |
| 1145 |
test_data->client_connection, &connect_args, s_rpc_client_message_flush, &client_test_data)); |
| 1146 |
|
| 1147 |
aws_mutex_lock(&client_test_data.sync_lock); |
| 1148 |
|
| 1149 |
|
| 1150 |
aws_condition_variable_wait_pred( |
| 1151 |
&client_test_data.sync_cvar, |
| 1152 |
&client_test_data.sync_lock, |
| 1153 |
s_rpc_client_message_transmission_completed_pred, |
| 1154 |
&client_test_data); |
| 1155 |
|
| 1156 |
ASSERT_INT_EQUALS(AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_CONNECT, client_test_data.received_message_type); |
| 1157 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 1158 |
connect_payload.buffer, |
| 1159 |
connect_payload.len, |
| 1160 |
client_test_data.received_payload.buffer, |
| 1161 |
client_test_data.received_payload.len); |
| 1162 |
aws_byte_buf_clean_up(&client_test_data.received_payload); |
| 1163 |
|
| 1164 |
|
| 1165 |
client_test_data.received_message_type = 0; |
| 1166 |
client_test_data.client_message_received = false; |
| 1167 |
client_test_data.client_message_sent = false; |
| 1168 |
client_test_data.server_message_received = false; |
| 1169 |
client_test_data.server_message_sent = false; |
| 1170 |
|
| 1171 |
connect_args.message_type = AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_CONNECT_ACK; |
| 1172 |
connect_args.message_flags = AWS_EVENT_STREAM_RPC_MESSAGE_FLAG_CONNECTION_ACCEPTED; |
| 1173 |
|
| 1174 |
ASSERT_SUCCESS(aws_event_stream_rpc_server_connection_send_protocol_message( |
| 1175 |
test_data->server_connection, &connect_args, s_rpc_server_message_flush, &client_test_data)); |
| 1176 |
|
| 1177 |
|
| 1178 |
aws_condition_variable_wait_pred( |
| 1179 |
&client_test_data.sync_cvar, |
| 1180 |
&client_test_data.sync_lock, |
| 1181 |
s_rpc_server_message_transmission_completed_pred, |
| 1182 |
&client_test_data); |
| 1183 |
|
| 1184 |
ASSERT_INT_EQUALS(AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_CONNECT_ACK, client_test_data.received_message_type); |
| 1185 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 1186 |
connect_payload.buffer, |
| 1187 |
connect_payload.len, |
| 1188 |
client_test_data.received_payload.buffer, |
| 1189 |
client_test_data.received_payload.len); |
| 1190 |
aws_byte_buf_clean_up(&client_test_data.received_payload); |
| 1191 |
|
| 1192 |
|
| 1193 |
client_test_data.received_message_type = 0; |
| 1194 |
client_test_data.client_message_received = false; |
| 1195 |
client_test_data.client_message_sent = false; |
| 1196 |
client_test_data.server_message_received = false; |
| 1197 |
client_test_data.server_message_sent = false; |
| 1198 |
|
| 1199 |
struct aws_event_stream_rpc_client_stream_continuation_options continuation_options = { |
| 1200 |
.user_data = &client_test_data, |
| 1201 |
.on_continuation = s_rpc_client_stream_continuation, |
| 1202 |
.on_continuation_closed = s_rpc_client_stream_continuation_closed, |
| 1203 |
}; |
| 1204 |
|
| 1205 |
struct aws_event_stream_rpc_client_continuation_token *client_token = |
| 1206 |
aws_event_stream_rpc_client_connection_new_stream(test_data->client_connection, &continuation_options); |
| 1207 |
ASSERT_NOT_NULL(client_token); |
| 1208 |
|
| 1209 |
struct aws_byte_cursor operation_name = aws_byte_cursor_from_c_str("test_operation"); |
| 1210 |
|
| 1211 |
struct aws_byte_buf operation_payload = aws_byte_buf_from_c_str("{ \"message\": \" operation payload \" }"); |
| 1212 |
struct aws_event_stream_rpc_message_args operation_args = { |
| 1213 |
.headers_count = 0, |
| 1214 |
.headers = NULL, |
| 1215 |
.message_type = AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_APPLICATION_MESSAGE, |
| 1216 |
.payload = &operation_payload, |
| 1217 |
}; |
| 1218 |
|
| 1219 |
ASSERT_SUCCESS(aws_event_stream_rpc_client_continuation_activate( |
| 1220 |
client_token, operation_name, &operation_args, s_rpc_client_message_flush, &client_test_data)); |
| 1221 |
|
| 1222 |
|
| 1223 |
aws_condition_variable_wait_pred( |
| 1224 |
&client_test_data.sync_cvar, |
| 1225 |
&client_test_data.sync_lock, |
| 1226 |
s_rpc_client_message_transmission_completed_pred, |
| 1227 |
&client_test_data); |
| 1228 |
|
| 1229 |
ASSERT_INT_EQUALS(AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_APPLICATION_MESSAGE, client_test_data.received_message_type); |
| 1230 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 1231 |
operation_payload.buffer, |
| 1232 |
operation_payload.len, |
| 1233 |
client_test_data.received_payload.buffer, |
| 1234 |
client_test_data.received_payload.len); |
| 1235 |
aws_byte_buf_clean_up(&client_test_data.received_payload); |
| 1236 |
|
| 1237 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 1238 |
operation_name.ptr, |
| 1239 |
operation_name.len, |
| 1240 |
client_test_data.last_seen_operation_name.buffer, |
| 1241 |
client_test_data.last_seen_operation_name.len); |
| 1242 |
aws_byte_buf_clean_up(&client_test_data.last_seen_operation_name); |
| 1243 |
|
| 1244 |
|
| 1245 |
client_test_data.received_message_type = 0; |
| 1246 |
client_test_data.client_message_received = false; |
| 1247 |
client_test_data.client_message_sent = false; |
| 1248 |
client_test_data.server_message_received = false; |
| 1249 |
client_test_data.server_message_sent = false; |
| 1250 |
|
| 1251 |
operation_args.message_flags = AWS_EVENT_STREAM_RPC_MESSAGE_FLAG_TERMINATE_STREAM; |
| 1252 |
operation_args.message_type = AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_APPLICATION_ERROR; |
| 1253 |
|
| 1254 |
ASSERT_SUCCESS(aws_event_stream_rpc_server_continuation_send_message( |
| 1255 |
client_test_data.server_token, &operation_args, s_rpc_server_message_flush, &client_test_data)); |
| 1256 |
|
| 1257 |
|
| 1258 |
aws_condition_variable_wait_pred( |
| 1259 |
&client_test_data.sync_cvar, |
| 1260 |
&client_test_data.sync_lock, |
| 1261 |
s_rpc_server_message_transmission_completed_pred, |
| 1262 |
&client_test_data); |
| 1263 |
|
| 1264 |
|
| 1265 |
aws_condition_variable_wait_pred( |
| 1266 |
&client_test_data.sync_cvar, |
| 1267 |
&client_test_data.sync_lock, |
| 1268 |
s_rpc_client_continuation_token_closed_pred, |
| 1269 |
&client_test_data); |
| 1270 |
|
| 1271 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 1272 |
operation_payload.buffer, |
| 1273 |
operation_payload.len, |
| 1274 |
client_test_data.received_payload.buffer, |
| 1275 |
client_test_data.received_payload.len); |
| 1276 |
|
| 1277 |
ASSERT_INT_EQUALS(AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_APPLICATION_ERROR, client_test_data.received_message_type); |
| 1278 |
|
| 1279 |
aws_byte_buf_clean_up(&client_test_data.received_payload); |
| 1280 |
|
| 1281 |
|
| 1282 |
ASSERT_ERROR( |
| 1283 |
AWS_ERROR_EVENT_STREAM_RPC_STREAM_CLOSED, |
| 1284 |
aws_event_stream_rpc_client_continuation_send_message( |
| 1285 |
client_token, &operation_args, s_rpc_client_message_flush, &client_test_data)); |
| 1286 |
|
| 1287 |
aws_event_stream_rpc_client_continuation_release(client_token); |
| 1288 |
|
| 1289 |
aws_event_stream_rpc_client_connection_close(test_data->client_connection, AWS_ERROR_SUCCESS); |
| 1290 |
aws_event_stream_rpc_server_connection_close(test_data->server_connection, AWS_ERROR_SUCCESS); |
| 1291 |
|
| 1292 |
aws_mutex_unlock(&client_test_data.sync_lock); |
| 1293 |
aws_mutex_clean_up(&client_test_data.sync_lock); |
| 1294 |
aws_condition_variable_clean_up(&client_test_data.sync_cvar); |
| 1295 |
|
| 1296 |
return AWS_OP_SUCCESS; |
| 1297 |
} |
| 1298 |
|
| 1299 |
AWS_TEST_CASE_FIXTURE( |
| 1300 |
test_event_stream_rpc_client_connection_continuation_send_message_on_closed_fails, |
| 1301 |
s_fixture_setup, |
| 1302 |
s_test_event_stream_rpc_client_connection_continuation_send_message_on_closed_fails, |
| 1303 |
s_fixture_shutdown, |
| 1304 |
&s_test_data) |
| 1305 |
|
| 1306 |
static int s_test_event_stream_rpc_client_connection_continuation_duplicated_activate_fails( |
| 1307 |
struct aws_allocator *allocator, |
| 1308 |
void *ctx) { |
| 1309 |
(void)allocator; |
| 1310 |
|
| 1311 |
struct test_data *test_data = ctx; |
| 1312 |
test_data->on_server_message_received = s_rpc_server_connection_protocol_message; |
| 1313 |
test_data->on_client_message_received = s_rpc_client_connection_protocol_message; |
| 1314 |
test_data->on_new_server_stream = s_rpc_server_on_incoming_stream; |
| 1315 |
test_data->on_new_server_continuation = s_rpc_server_stream_continuation; |
| 1316 |
test_data->on_server_continuation_closed = s_rpc_server_stream_continuation_closed; |
| 1317 |
|
| 1318 |
struct client_test_data *client_test_data = &test_data->client_test_data; |
| 1319 |
|
| 1320 |
test_data->user_data = client_test_data; |
| 1321 |
test_data->server_continuation_user_data = client_test_data; |
| 1322 |
|
| 1323 |
struct aws_byte_buf connect_payload = aws_byte_buf_from_c_str("{ \"message\": \" connect message \" }"); |
| 1324 |
struct aws_event_stream_rpc_message_args connect_args = { |
| 1325 |
.headers_count = 0, |
| 1326 |
.headers = NULL, |
| 1327 |
.message_type = AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_CONNECT, |
| 1328 |
.payload = &connect_payload, |
| 1329 |
}; |
| 1330 |
|
| 1331 |
ASSERT_SUCCESS(aws_event_stream_rpc_client_connection_send_protocol_message( |
| 1332 |
test_data->client_connection, &connect_args, s_rpc_client_message_flush, client_test_data)); |
| 1333 |
|
| 1334 |
aws_mutex_lock(&client_test_data->sync_lock); |
| 1335 |
aws_condition_variable_wait_pred( |
| 1336 |
&client_test_data->sync_cvar, |
| 1337 |
&client_test_data->sync_lock, |
| 1338 |
s_rpc_client_message_transmission_completed_pred, |
| 1339 |
client_test_data); |
| 1340 |
|
| 1341 |
ASSERT_INT_EQUALS(AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_CONNECT, client_test_data->received_message_type); |
| 1342 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 1343 |
connect_payload.buffer, |
| 1344 |
connect_payload.len, |
| 1345 |
client_test_data->received_payload.buffer, |
| 1346 |
client_test_data->received_payload.len); |
| 1347 |
aws_byte_buf_clean_up(&client_test_data->received_payload); |
| 1348 |
|
| 1349 |
client_test_data->received_message_type = 0; |
| 1350 |
client_test_data->client_message_received = false; |
| 1351 |
client_test_data->client_message_sent = false; |
| 1352 |
client_test_data->server_message_received = false; |
| 1353 |
client_test_data->server_message_sent = false; |
| 1354 |
|
| 1355 |
connect_args.message_type = AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_CONNECT_ACK; |
| 1356 |
connect_args.message_flags = AWS_EVENT_STREAM_RPC_MESSAGE_FLAG_CONNECTION_ACCEPTED; |
| 1357 |
|
| 1358 |
ASSERT_SUCCESS(aws_event_stream_rpc_server_connection_send_protocol_message( |
| 1359 |
test_data->server_connection, &connect_args, s_rpc_server_message_flush, client_test_data)); |
| 1360 |
|
| 1361 |
aws_condition_variable_wait_pred( |
| 1362 |
&client_test_data->sync_cvar, |
| 1363 |
&client_test_data->sync_lock, |
| 1364 |
s_rpc_server_message_transmission_completed_pred, |
| 1365 |
client_test_data); |
| 1366 |
|
| 1367 |
ASSERT_INT_EQUALS(AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_CONNECT_ACK, client_test_data->received_message_type); |
| 1368 |
ASSERT_BIN_ARRAYS_EQUALS( |
| 1369 |
connect_payload.buffer, |
| 1370 |
connect_payload.len, |
| 1371 |
client_test_data->received_payload.buffer, |
| 1372 |
client_test_data->received_payload.len); |
| 1373 |
aws_byte_buf_clean_up(&client_test_data->received_payload); |
| 1374 |
|
| 1375 |
client_test_data->received_message_type = 0; |
| 1376 |
client_test_data->client_message_received = false; |
| 1377 |
client_test_data->client_message_sent = false; |
| 1378 |
client_test_data->server_message_received = false; |
| 1379 |
client_test_data->server_message_sent = false; |
| 1380 |
|
| 1381 |
struct aws_event_stream_rpc_client_stream_continuation_options continuation_options = { |
| 1382 |
.user_data = client_test_data, |
| 1383 |
.on_continuation = s_rpc_client_stream_continuation, |
| 1384 |
.on_continuation_closed = s_rpc_client_stream_continuation_closed, |
| 1385 |
}; |
| 1386 |
|
| 1387 |
struct aws_event_stream_rpc_client_continuation_token *client_token = |
| 1388 |
aws_event_stream_rpc_client_connection_new_stream(test_data->client_connection, &continuation_options); |
| 1389 |
ASSERT_NOT_NULL(client_token); |
| 1390 |
|
| 1391 |
struct aws_byte_cursor operation_name = aws_byte_cursor_from_c_str("test_operation"); |
| 1392 |
|
| 1393 |
struct aws_byte_buf operation_payload = aws_byte_buf_from_c_str("{ \"message\": \" operation payload \" }"); |
| 1394 |
struct aws_event_stream_rpc_message_args operation_args = { |
| 1395 |
.headers_count = 0, |
| 1396 |
.headers = NULL, |
| 1397 |
.message_type = AWS_EVENT_STREAM_RPC_MESSAGE_TYPE_APPLICATION_MESSAGE, |
| 1398 |
.payload = &operation_payload, |
| 1399 |
}; |
| 1400 |
|
| 1401 |
ASSERT_SUCCESS(aws_event_stream_rpc_client_continuation_activate( |
| 1402 |
client_token, operation_name, &operation_args, s_rpc_client_message_flush, client_test_data)); |
| 1403 |
|
| 1404 |
aws_condition_variable_wait_pred( |
| 1405 |
&client_test_data->sync_cvar, |
| 1406 |
&client_test_data->sync_lock, |
| 1407 |
s_rpc_client_message_transmission_completed_pred, |
| 1408 |
client_test_data); |
| 1409 |
aws_byte_buf_clean_up(&client_test_data->received_payload); |
| 1410 |
aws_byte_buf_clean_up(&client_test_data->last_seen_operation_name); |
| 1411 |
|
| 1412 |
ASSERT_ERROR( |
| 1413 |
AWS_ERROR_INVALID_STATE, |
| 1414 |
aws_event_stream_rpc_client_continuation_activate( |
| 1415 |
client_token, operation_name, &operation_args, s_rpc_client_message_flush, client_test_data)); |
| 1416 |
|
| 1417 |
aws_event_stream_rpc_client_continuation_release(client_token); |
| 1418 |
|
| 1419 |
aws_event_stream_rpc_client_connection_close(test_data->client_connection, AWS_ERROR_SUCCESS); |
| 1420 |
aws_event_stream_rpc_server_connection_close(test_data->server_connection, AWS_ERROR_SUCCESS); |
| 1421 |
|
| 1422 |
aws_condition_variable_wait_pred( |
| 1423 |
&client_test_data->sync_cvar, |
| 1424 |
&client_test_data->sync_lock, |
| 1425 |
s_rpc_client_continuation_token_closed_pred, |
| 1426 |
client_test_data); |
| 1427 |
|
| 1428 |
aws_mutex_unlock(&client_test_data->sync_lock); |
| 1429 |
|
| 1430 |
return AWS_OP_SUCCESS; |
| 1431 |
} |
| 1432 |
|
| 1433 |
AWS_TEST_CASE_FIXTURE( |
| 1434 |
test_event_stream_rpc_client_connection_continuation_duplicated_activate_fails, |
| 1435 |
s_fixture_setup, |
| 1436 |
s_test_event_stream_rpc_client_connection_continuation_duplicated_activate_fails, |
| 1437 |
s_fixture_shutdown, |
| 1438 |
&s_test_data) |