Skip to content

Commit

Permalink
fix dispatch queue test flag
Browse files Browse the repository at this point in the history
  • Loading branch information
xiazhvera committed Nov 8, 2024
1 parent 69c22b4 commit 1b79cbd
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 25 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -646,8 +646,7 @@ All exported functions, simply shim into the v-table and return.

We include a cross-platform API for sockets. We support TCP and UDP using IPv4 and IPv6, and Unix Domain sockets. On Windows,
we use Named Pipes to support the functionality of Unix Domain sockets. On Windows, this is implemented with winsock2, and on
all unix platforms we use the posix API. We can also enable Apple Network Framework along with the Apple Dispatch Queue by setting
the preprocessing flag `AWS_USE_DISPATCH_QUEUE`. Then we will use Apple Network Framework on Apple platforms.
all unix platforms we use the posix API. Then we will use Apple Network Framework on Apple platforms.

Upon a connection being established, the new socket (either as the result of a `connect()` or `start_accept()` call)
will not be attached to any event loops. It is your responsibility to register it with an event loop to begin receiving
Expand Down
7 changes: 3 additions & 4 deletions include/aws/io/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,9 @@ AWS_IO_API void aws_socket_clean_up(struct aws_socket *socket);
* In TCP, LOCAL and VSOCK this function will not block. If the return value is successful, then you must wait on the
* `on_connection_result()` callback to be invoked before using the socket.
*
* The function will failed with error if the endpoint is invalid. Except for Apple Network Framework (with
* AWS_USE_DISPATCH_QUEUE enabled). In Apple network framework, as connect is an async api, we would not know if the
* local endpoint is valid until we have the connection state returned in callback. The error will returned in
* `on_connection_result` callback
* The function will failed with error if the endpoint is invalid, except for Apple Network Framework. In Apple network
* framework, as connect is an async api, we would not know if the local endpoint is valid until we have the connection
* state returned in callback. The error will returned in `on_connection_result` callback
*
* If an event_loop is provided for UDP sockets, a notification will be sent on
* on_connection_result in the event-loop's thread. Upon completion, the socket will already be assigned
Expand Down
4 changes: 0 additions & 4 deletions source/darwin/dispatch_queue_event_loop.c
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,6 @@ static void s_cancel_task(struct aws_event_loop *event_loop, struct aws_task *ta
static int s_connect_to_dispatch_queue(struct aws_event_loop *event_loop, struct aws_io_handle *handle) {
(void)event_loop;
(void)handle;
#ifdef AWS_USE_DISPATCH_QUEUE
AWS_PRECONDITION(handle->set_queue && handle->clear_queue);

AWS_LOGF_TRACE(
Expand All @@ -475,7 +474,6 @@ static int s_connect_to_dispatch_queue(struct aws_event_loop *event_loop, struct
(void *)handle->data.handle);
struct dispatch_loop *dispatch_loop = event_loop->impl_data;
handle->set_queue(handle, dispatch_loop->dispatch_queue);
#endif // #ifdef AWS_USE_DISPATCH_QUEUE
return AWS_OP_SUCCESS;
}

Expand All @@ -485,9 +483,7 @@ static int s_unsubscribe_from_io_events(struct aws_event_loop *event_loop, struc
"id=%p: un-subscribing from events on handle %p",
(void *)event_loop,
(void *)handle->data.handle);
#ifdef AWS_USE_DISPATCH_QUEUE
handle->clear_queue(handle);
#endif
return AWS_OP_SUCCESS;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ add_test_case(cleanup_in_write_cb_doesnt_explode)
add_test_case(sock_write_cb_is_async)
add_test_case(socket_validate_port)

if(NOT AWS_USE_DISPATCH_QUEUE)
if(NOT AWS_EVENT_LOOP_DISPATCH_QUEUE_OVERRIDE)
# Apple Network Framework does not support bind+connect
add_test_case(udp_bind_connect_communication)
# The read/write will always run a different thread for Apple Network Framework
Expand Down
21 changes: 7 additions & 14 deletions tests/socket_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,6 @@
# include <linux/vm_sockets.h>
#endif

// DEBUG WIP
#ifdef AWS_USE_DISPATCH_QUEUE
static bool s_use_dispatch_queue = true;
#else
static bool s_use_dispatch_queue = false;
#endif

struct local_listener_args {
struct aws_socket *incoming;
struct aws_mutex *mutex;
Expand Down Expand Up @@ -254,7 +247,7 @@ static int s_test_socket_ex(

// The Apple Network Framework always require a "start listener/start connection"
// for setup a server socket
if (options->type == AWS_SOCKET_STREAM || s_use_dispatch_queue) {
if (options->type == AWS_SOCKET_STREAM || aws_event_loop_get_default_type() == AWS_ELT_DISPATCH_QUEUE) {
ASSERT_SUCCESS(aws_socket_listen(&listener, 1024));
ASSERT_SUCCESS(aws_socket_start_accept(&listener, event_loop, s_local_listener_incoming, &listener_args));
}
Expand All @@ -269,7 +262,7 @@ static int s_test_socket_ex(
}
ASSERT_SUCCESS(aws_socket_connect(&outgoing, endpoint, event_loop, s_local_outgoing_connection, &outgoing_args));

if (listener.options.type == AWS_SOCKET_STREAM || s_use_dispatch_queue) {
if (listener.options.type == AWS_SOCKET_STREAM || aws_event_loop_get_default_type() == AWS_ELT_DISPATCH_QUEUE) {
ASSERT_SUCCESS(aws_mutex_lock(&mutex));
ASSERT_SUCCESS(
aws_condition_variable_wait_pred(&condition_variable, &mutex, s_incoming_predicate, &listener_args));
Expand All @@ -282,7 +275,7 @@ static int s_test_socket_ex(

struct aws_socket *server_sock = &listener;

if (options->type == AWS_SOCKET_STREAM || s_use_dispatch_queue) {
if (options->type == AWS_SOCKET_STREAM || aws_event_loop_get_default_type() == AWS_ELT_DISPATCH_QUEUE) {
ASSERT_TRUE(listener_args.incoming_invoked);
ASSERT_FALSE(listener_args.error_invoked);
server_sock = listener_args.incoming;
Expand Down Expand Up @@ -493,7 +486,7 @@ static int s_test_socket_udp_dispatch_queue(
ASSERT_SUCCESS(aws_mutex_unlock(&mutex));
ASSERT_INT_EQUALS(AWS_OP_SUCCESS, io_args.error_code);

if (listener.options.type == AWS_SOCKET_STREAM || s_use_dispatch_queue) {
if (listener.options.type == AWS_SOCKET_STREAM || aws_event_loop_get_default_type() == AWS_ELT_DISPATCH_QUEUE) {
ASSERT_SUCCESS(aws_mutex_lock(&mutex));
ASSERT_SUCCESS(
aws_condition_variable_wait_pred(&condition_variable, &mutex, s_incoming_predicate, &listener_args));
Expand Down Expand Up @@ -593,7 +586,7 @@ static int s_test_socket(
struct aws_socket_options *options,
struct aws_socket_endpoint *endpoint) {

if (s_use_dispatch_queue && options->type == AWS_SOCKET_DGRAM)
if (aws_event_loop_get_default_type() == AWS_ELT_DISPATCH_QUEUE && options->type == AWS_SOCKET_DGRAM)
return s_test_socket_udp_dispatch_queue(allocator, options, endpoint);
else
return s_test_socket_ex(allocator, options, NULL, endpoint);
Expand Down Expand Up @@ -1056,7 +1049,7 @@ static int s_test_outgoing_local_sock_errors(struct aws_allocator *allocator, vo

int socket_connect_result = aws_socket_connect(&outgoing, &endpoint, event_loop, s_null_sock_connection, &args);
// As Apple network framework has a async API design, we would not get the error back on connect
if (!s_use_dispatch_queue) {
if(aws_event_loop_get_default_type() != AWS_ELT_DISPATCH_QUEUE){
ASSERT_FAILS(socket_connect_result);
ASSERT_TRUE(
aws_last_error() == AWS_IO_SOCKET_CONNECTION_REFUSED || aws_last_error() == AWS_ERROR_FILE_INVALID_PATH);
Expand Down Expand Up @@ -1270,7 +1263,7 @@ static int s_test_bind_on_zero_port(

ASSERT_SUCCESS(aws_socket_get_bound_address(&incoming, &local_address1));

if (s_use_dispatch_queue) {
if (aws_event_loop_get_default_type() == AWS_ELT_DISPATCH_QUEUE) {
struct aws_mutex mutex = AWS_MUTEX_INIT;
struct aws_condition_variable condition_variable = AWS_CONDITION_VARIABLE_INIT;

Expand Down

0 comments on commit 1b79cbd

Please sign in to comment.