diff --git a/runtime/streams.cpp b/runtime/streams.cpp index 04fe728117..f49feed3f8 100644 --- a/runtime/streams.cpp +++ b/runtime/streams.cpp @@ -320,9 +320,15 @@ Optional f$stream_select(mixed& read, mixed& write, mixed& except, cons return false; } - // TODO use pselect + struct timespec ts, *timeout_ts = nullptr; + if (timeout) { + ts.tv_sec = timeout->tv_sec; + ts.tv_nsec = static_cast(timeout->tv_usec) * 1000; + timeout_ts = &ts; + } + dl::enter_critical_section(); // OK - int32_t select_result = select(nfds + 1, &rfds, &wfds, &efds, timeout); + int32_t select_result = pselect(nfds + 1, &rfds, &wfds, &efds, timeout_ts, nullptr); dl::leave_critical_section(); if (select_result == -1) { diff --git a/tests/cpp/runtime/runtime-tests.cmake b/tests/cpp/runtime/runtime-tests.cmake index 731da98fe6..d932e7d597 100644 --- a/tests/cpp/runtime/runtime-tests.cmake +++ b/tests/cpp/runtime/runtime-tests.cmake @@ -21,7 +21,8 @@ prepend(RUNTIME_TESTS_SOURCES ${BASE_DIR}/tests/cpp/runtime/ memory_resource/unsynchronized_pool_resource-test.cpp string-list-test.cpp string-test.cpp - zstd-test.cpp) + zstd-test.cpp + stream-select-test.cpp) allow_deprecated_declarations_for_apple(${BASE_DIR}/tests/cpp/runtime/inter-process-mutex-test.cpp) vk_add_unittest(runtime "${RUNTIME_LIBS};${RUNTIME_LINK_TEST_LIBS}" ${RUNTIME_TESTS_SOURCES}) diff --git a/tests/cpp/runtime/stream-select-test.cpp b/tests/cpp/runtime/stream-select-test.cpp new file mode 100644 index 0000000000..eda3185edb --- /dev/null +++ b/tests/cpp/runtime/stream-select-test.cpp @@ -0,0 +1,22 @@ +#include +#include "runtime/streams.h" +#include "runtime/critical_section.h" + +TEST(stream_select, udp_writable) { + mixed err_no; + mixed err_desc; + Stream udp_stream = f$stream_socket_client( + CONST_STRING("udp://127.0.0.1:9"), err_no, err_desc, 1.0); + + ASSERT_TRUE(f$boolval(udp_stream)); + + mixed read_arr = array(); + mixed write_arr = array::create(udp_stream); + mixed except_arr = array(); + + Optional res = f$stream_select(read_arr, write_arr, except_arr, 0, 0); + ASSERT_TRUE(res.has_value()); + ASSERT_EQ(res.val(), 1); + + f$fclose(udp_stream); +}