From 3a17aeed7a481e614c65925cbe07b286ae257e54 Mon Sep 17 00:00:00 2001 From: Niels Provos Date: Sat, 26 Apr 2008 01:00:44 +0000 Subject: [PATCH] fix a bug in buffrevent read water marks and add a test for them svn:r731 --- ChangeLog | 1 + evbuffer.c | 3 +- test/regress.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 2 deletions(-) diff --git a/ChangeLog b/ChangeLog index edc10261..35c9a90e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -72,6 +72,7 @@ Changes in current version: o New functions (event_assign, event_new, event_free) for use by apps that want to be safely threadsafe, or want to remain ignorant of the contents of struct event. o introduce bufferevent_read_buffer; allows reading without memory copy. o expose bufferevent_setwatermark via header files and fix high watermark on read + o fix a bug in buffrevent read water marks and add a test for them Changes in 1.4.0: o allow \r or \n individually to separate HTTP headers instead of the standard "\r\n"; from Charles Kerr. diff --git a/evbuffer.c b/evbuffer.c index 51ee6c06..9b99e536 100644 --- a/evbuffer.c +++ b/evbuffer.c @@ -147,9 +147,8 @@ bufferevent_readcb(evutil_socket_t fd, short event, void *arg) struct evbuffer *buf = bufev->input; event_del(&bufev->ev_read); - /* Now schedule a callback for us */ + /* Now schedule a callback for us when the buffer changes */ evbuffer_setcb(buf, bufferevent_read_pressure_cb, bufev); - return; } /* Invoke the user callback - must always be called last */ diff --git a/test/regress.c b/test/regress.c index 9c2b7226..55d68609 100644 --- a/test/regress.c +++ b/test/regress.c @@ -1302,6 +1302,10 @@ test_evbuffer_find(void) evbuffer_free(buf); } +/* + * simple bufferevent test + */ + static void readcb(struct bufferevent *bev, void *arg) { @@ -1365,6 +1369,80 @@ test_bufferevent(void) cleanup_test(); } +/* + * test watermarks and bufferevent + */ + +static void +wm_readcb(struct bufferevent *bev, void *arg) +{ + struct evbuffer *evbuf = evbuffer_new(); + int len = EVBUFFER_LENGTH(bev->input); + static int nread; + + assert(len >= 10 && len <= 20); + + assert(evbuf != NULL); + + /* gratuitous test of bufferevent_read_buffer */ + bufferevent_read_buffer(bev, evbuf); + + nread += len; + if (nread == 65000) { + bufferevent_disable(bev, EV_READ); + test_ok++; + } + + evbuffer_free(evbuf); +} + +static void +wm_writecb(struct bufferevent *bev, void *arg) +{ + if (EVBUFFER_LENGTH(bev->output) == 0) + test_ok++; +} + +static void +wm_errorcb(struct bufferevent *bev, short what, void *arg) +{ + test_ok = -2; +} + +static void +test_bufferevent_watermarks(void) +{ + struct bufferevent *bev1, *bev2; + char buffer[65000]; + int i; + + setup_test("Bufferevent Watermarks: "); + + bev1 = bufferevent_new(pair[0], NULL, wm_writecb, wm_errorcb, NULL); + bev2 = bufferevent_new(pair[1], wm_readcb, NULL, wm_errorcb, NULL); + + bufferevent_disable(bev1, EV_READ); + bufferevent_enable(bev2, EV_READ); + + for (i = 0; i < sizeof(buffer); i++) + buffer[i] = i; + + bufferevent_write(bev1, buffer, sizeof(buffer)); + + /* limit the reading on the receiving bufferevent */ + bufferevent_setwatermark(bev2, EV_READ, 10, 20); + + event_dispatch(); + + bufferevent_free(bev1); + bufferevent_free(bev2); + + if (test_ok != 2) + test_ok = 0; + + cleanup_test(); +} + struct test_pri_event { struct event ev; int count; @@ -1865,6 +1943,7 @@ main (int argc, char **argv) test_evbuffer_find(); test_bufferevent(); + test_bufferevent_watermarks(); test_free_active_base(); -- 2.40.0