stream: fix small nit in select hack, add test

This commit is contained in:
Fedor Indutny 2013-04-26 23:43:28 +04:00 committed by Ben Noordhuis
parent 2400716d87
commit ac4e7e7ff2
5 changed files with 97 additions and 4 deletions

View File

@ -88,6 +88,7 @@ TESTS= \
test/test-loop-stop.o \
test/test-multiple-listen.o \
test/test-mutexes.o \
test/test-osx-select.o \
test/test-pass-always.o \
test/test-ping-pong.o \
test/test-pipe-bind-error.o \

View File

@ -202,12 +202,14 @@ static void uv__stream_osx_select(void* arg) {
if (FD_ISSET(fd, &swrite))
events |= UV__POLLOUT;
uv_mutex_lock(&s->mutex);
s->events |= events;
uv_mutex_unlock(&s->mutex);
assert(events != 0 || FD_ISSET(s->int_fd, &sread));
if (events != 0) {
uv_mutex_lock(&s->mutex);
s->events |= events;
if (events != 0)
uv_async_send(&s->async);
uv_mutex_unlock(&s->mutex);
}
}
}

View File

@ -220,6 +220,9 @@ TEST_DECLARE (we_get_signal)
TEST_DECLARE (we_get_signals)
TEST_DECLARE (signal_multiple_loops)
#endif
#ifdef __APPLE__
TEST_DECLARE (osx_select)
#endif
HELPER_DECLARE (tcp4_echo_server)
HELPER_DECLARE (tcp6_echo_server)
HELPER_DECLARE (udp4_echo_server)
@ -440,6 +443,10 @@ TASK_LIST_START
TEST_ENTRY (signal_multiple_loops)
#endif
#ifdef __APPLE__
TEST_ENTRY (osx_select)
#endif
TEST_ENTRY (fs_file_noent)
TEST_ENTRY (fs_file_nametoolong)
TEST_ENTRY (fs_file_loop)

82
test/test-osx-select.c Normal file
View File

@ -0,0 +1,82 @@
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "uv.h"
#include "task.h"
#ifdef __APPLE__
#include <sys/ioctl.h>
#include <string.h>
static int read_count;
static uv_buf_t alloc_cb(uv_handle_t* handle, size_t size) {
static char buf[1024];
return uv_buf_init(buf, ARRAY_SIZE(buf));
}
static void read_cb(uv_stream_t* stream, ssize_t nread, uv_buf_t buf) {
fprintf(stdout, "got data %d\n", ++read_count);
if (read_count == 3)
uv_close((uv_handle_t*) stream, NULL);
}
TEST_IMPL(osx_select) {
int r;
int fd;
size_t i;
size_t len;
const char* str;
uv_tty_t tty;
fd = open("/dev/tty", O_RDONLY);
ASSERT(fd >= 0);
r = uv_tty_init(uv_default_loop(), &tty, fd, 1);
ASSERT(r == 0);
uv_read_start((uv_stream_t*) &tty, alloc_cb, read_cb);
// Emulate user-input
str = "got some input\n"
"with a couple of lines\n"
"feel pretty happy\n";
for (i = 0, len = strlen(str); i < len; i++) {
r = ioctl(fd, TIOCSTI, str + i);
ASSERT(r == 0);
}
uv_run(uv_default_loop(), UV_RUN_DEFAULT);
ASSERT(read_count == 3);
MAKE_VALGRIND_HAPPY();
return 0;
}
#endif /* __APPLE__ */

1
uv.gyp
View File

@ -309,6 +309,7 @@
'test/test-loop-stop.c',
'test/test-walk-handles.c',
'test/test-multiple-listen.c',
'test/test-osx-select.c',
'test/test-pass-always.c',
'test/test-ping-pong.c',
'test/test-pipe-bind-error.c',