Skip to content

Instantly share code, notes, and snippets.

@saghul
Created July 8, 2014 17:39
Show Gist options
  • Save saghul/989ec675c839ae9a6bef to your computer and use it in GitHub Desktop.
Save saghul/989ec675c839ae9a6bef to your computer and use it in GitHub Desktop.
diff --git a/test/test-tcp-write-queue-order.c b/test/test-tcp-write-queue-order.c
index c6b1808..ccc8fcc 100644
--- a/test/test-tcp-write-queue-order.c
+++ b/test/test-tcp-write-queue-order.c
@@ -26,11 +26,9 @@
#include "uv.h"
#include "task.h"
-#define FILL_COUNT 100000
-#define REST_COUNT 100
-#define TOTAL_COUNT (FILL_COUNT + REST_COUNT)
+#define REQ_COUNT 5000
+#define CANCEL_THRESHOLD 5
-static uv_timer_t timer;
static uv_tcp_t server;
static uv_tcp_t client;
static uv_tcp_t incoming;
@@ -39,29 +37,31 @@ static int close_cb_called;
static int connection_cb_called;
static int write_callbacks;
static int write_cancelled_callbacks;
+static int closing;
-static uv_write_t write_requests[TOTAL_COUNT];
+static uv_write_t write_requests[REQ_COUNT];
static void close_cb(uv_handle_t* handle) {
close_cb_called++;
}
-void timer_cb(uv_timer_t* handle) {
- uv_close((uv_handle_t*) &client, close_cb);
- uv_close((uv_handle_t*) &server, close_cb);
- uv_close((uv_handle_t*) &incoming, close_cb);
-}
void write_cb(uv_write_t* req, int status) {
- ASSERT(write_cancelled_callbacks == 0);
- write_callbacks++;
+ if (status == 0) {
+ if (write_callbacks++ > CANCEL_THRESHOLD && !closing) {
+ closing = 1;
+ uv_close((uv_handle_t*) &client, close_cb);
+ uv_close((uv_handle_t*) &server, close_cb);
+ uv_close((uv_handle_t*) &incoming, close_cb);
+ }
+ } else {
+ /* on Windows I see UV_ECONNABORTED here */
+ write_cancelled_callbacks++;
+ }
+
}
-void write_cb_cancelled(uv_write_t* req, int status) {
- ASSERT(status == UV_ECANCELED);
- write_cancelled_callbacks++;
-}
static void connect_cb(uv_connect_t* req, int status) {
static char base[1024];
@@ -74,19 +74,10 @@ static void connect_cb(uv_connect_t* req, int status) {
buf = uv_buf_init(base, sizeof(base));
- for (i = 0; i < FILL_COUNT; i++) {
+ for (i = 0; i < REQ_COUNT; i++) {
r = uv_write(&write_requests[i], req->handle, &buf, 1, write_cb);
ASSERT(r == 0);
}
-
- for (i = 0; i < REST_COUNT; i++) {
- r = uv_write(&write_requests[FILL_COUNT + i],
- req->handle,
- &buf,
- 1,
- write_cb_cancelled);
- ASSERT(r == 0);
- }
}
@@ -125,16 +116,13 @@ TEST_IMPL(tcp_write_queue_order) {
(struct sockaddr*) &addr,
connect_cb));
- ASSERT(0 == uv_timer_init(uv_default_loop(), &timer));
- ASSERT(0 == uv_timer_start(&timer, timer_cb, 100, 0));
-
ASSERT(0 == uv_run(uv_default_loop(), UV_RUN_DEFAULT));
ASSERT(connect_cb_called == 1);
ASSERT(close_cb_called == 3);
ASSERT(connection_cb_called == 1);
- ASSERT(write_callbacks == FILL_COUNT);
- ASSERT(write_cancelled_callbacks == REST_COUNT);
+ ASSERT(write_callbacks + write_cancelled_callbacks == REQ_COUNT);
+ ASSERT(write_callbacks > CANCEL_THRESHOLD);
MAKE_VALGRIND_HAPPY();
return 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment