From c9ba53a3ef72f694823b8a68cb0380b09b97def4 Mon Sep 17 00:00:00 2001
From: Nick Mathewson <nickm@torproject.org>
Date: Mon, 27 Feb 2012 17:57:01 -0500
Subject: [PATCH] Remove an unsafe programming practice from test-fdleak; add
 XX comments

It's a bad idea to have a read callback read a fixed amount; Instead,
we should drain it, or have some way to know that more data is
arrived.  Not unsafe in this case, but let's not encourage people to
learn bad habits.

This commit also notes some other stuff to fix.
---
 test/test-fdleak.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)

diff --git a/test/test-fdleak.c b/test/test-fdleak.c
index 7b896bcd..5242e1e2 100644
--- a/test/test-fdleak.c
+++ b/test/test-fdleak.c
@@ -41,12 +41,13 @@
 
 #include "event2/event.h"
 #include "event2/bufferevent.h"
+#include "event2/buffer.h"
 #include "event2/listener.h"
 
 /* This test opens a server socket, and many client sockets which connect to
    the server socket many times. It sets a low number for the max open file
    limit to catch any file descriptor leaks. */
-
+/* XXX This keeps two instances of this test from running in parallel. */
 #define PORT 31456
 
 /* Number of requests to make. Setting this too high might result in the machine
@@ -71,16 +72,20 @@ Server functions
 static void
 server_read_cb(struct bufferevent *bev, void *ctx)
 {
-	unsigned char tmp;
-	bufferevent_read(bev, &tmp, 1);
-	bufferevent_write(bev, &tmp, 1);
+	while (evbuffer_get_length(bufferevent_get_input(bev))) {
+		unsigned char tmp;
+		bufferevent_read(bev, &tmp, 1);
+		bufferevent_write(bev, &tmp, 1);
+	}
 }
 
 /* Wait for an EOF and then free the bufferevent */
 static void
 server_write_cb(struct bufferevent *bev, short events, void *ctx)
+/*XXX rename */
 {
 	if (events & BEV_EVENT_ERROR) {
+		/* XXX won't capture net errors on windows */
 		perror("Error from bufferevent");
 		exit(1);
 	} else if (events & (BEV_EVENT_EOF | BEV_EVENT_ERROR)) {
@@ -159,6 +164,7 @@ client_read_cb(struct bufferevent *bev, void *ctx)
 /* Send a byte to the server. */
 static void
 client_write_cb(struct bufferevent *bev, short events, void *ctx)
+/*XXX rename */
 {
 	if (events & BEV_EVENT_CONNECTED) {
 		unsigned char tmp = 'A';
@@ -178,9 +184,11 @@ start_client(struct event_base *base)
 	struct bufferevent *bev = bufferevent_socket_new(base, -1,
                                                          BEV_OPT_CLOSE_ON_FREE);
 	bufferevent_setcb(bev, client_read_cb, NULL, client_write_cb, NULL);
-
+	fprintf(stdout, ".");
+	fflush(stdout);
 	if (bufferevent_socket_connect(bev, (struct sockaddr *)&sin,
                                        sizeof(sin)) < 0) {
+		/* XXX won't capture net errors on windows */
 		perror("Could not connect!");
 		bufferevent_free(bev);
 		exit(2);
@@ -216,3 +224,5 @@ main(int argc, char **argv)
 
 	return 0;
 }
+
+/* XXX why does this test cause so much latency sometimes (OSX 10.5)? */
-- 
2.40.0