From fe47003d06e4f036de6874c1d00098cd6a687de5 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Tue, 5 May 2009 16:52:37 +0000 Subject: [PATCH] Make unit tests for bufferevent_async compile and _almost_ work. Either I need to make the callbacks get deferred in a base with no events (doable), or I need to make it okay to call launch_read from inside the callback for read (tricky). svn:r1277 --- Makefile.am | 4 +++- buffer_iocp.c | 4 ++-- bufferevent_async.c | 14 ++++++++++---- event-internal.h | 2 ++ event_iocp.c | 3 ++- iocp-internal.h | 1 + test/regress_iocp.c | 12 ++++++++---- 7 files changed, 28 insertions(+), 12 deletions(-) diff --git a/Makefile.am b/Makefile.am index 7701dffd..fd48f31e 100644 --- a/Makefile.am +++ b/Makefile.am @@ -57,6 +57,7 @@ EXTRA_DIST = \ evthread_win32.c \ evthread_pthread.c \ whatsnew-2.0.txt \ + bufferevent_async.c \ WIN32-Code/config.h \ WIN32-Code/win32.c \ WIN32-Code/tree.h \ @@ -75,7 +76,8 @@ SUBDIRS = . include sample test if BUILD_WIN32 SYS_LIBS = -lws2_32 -SYS_SRC = WIN32-Code/win32.c evthread_win32.c buffer_iocp.c event_iocp.c +SYS_SRC = WIN32-Code/win32.c evthread_win32.c buffer_iocp.c event_iocp.c \ + bufferevent_async.c SYS_INCLUDES = -IWIN32-Code else diff --git a/buffer_iocp.c b/buffer_iocp.c index bae08434..620efd8d 100644 --- a/buffer_iocp.c +++ b/buffer_iocp.c @@ -120,6 +120,7 @@ read_completed(struct event_overlapped *eo, uintptr_t _, ssize_t nBytes) struct evbuffer_chain *chain = buf_o->first_pinned; EVBUFFER_LOCK(evbuf, EVTHREAD_WRITE); + buf->read_in_progress = 0; evbuffer_unfreeze(evbuf, 0); if (chain == evbuf->previous_to_last) { @@ -139,7 +140,6 @@ read_completed(struct event_overlapped *eo, uintptr_t _, ssize_t nBytes) pin_release(eo, EVBUFFER_MEM_PINNED_R); - buf->read_in_progress = 0; _evbuffer_decref_and_unlock(evbuf); } @@ -153,10 +153,10 @@ write_completed(struct event_overlapped *eo, uintptr_t _, ssize_t nBytes) struct evbuffer *evbuf = &buf->buffer; EVBUFFER_LOCK(evbuf, EVTHREAD_WRITE); + buf->write_in_progress = 0; evbuffer_unfreeze(evbuf, 1); evbuffer_drain(evbuf, nBytes); pin_release(eo,EVBUFFER_MEM_PINNED_W); - buf->write_in_progress = 0; _evbuffer_decref_and_unlock(evbuf); } diff --git a/bufferevent_async.c b/bufferevent_async.c index fe5cad89..88f87576 100644 --- a/bufferevent_async.c +++ b/bufferevent_async.c @@ -105,7 +105,7 @@ bev_async_consider_writing(struct bufferevent_async *b) if (!evbuffer_get_length(b->bev.bev.output)) return; - /* XXXX doesn't respect low-water mark very well. */ + /* XXXX doesn't respect low-water mark very well. */ if (evbuffer_launch_write(b->bev.bev.output, -1)) { assert(0);/* XXX act sensibly. */ } else { @@ -118,6 +118,7 @@ bev_async_consider_reading(struct bufferevent_async *b) { size_t cur_size; size_t read_high; + size_t at_most; /* Don't read if there is a read in progress, or we do not * want to read. */ if (b->read_in_progress || !(b->bev.bev.enabled&EV_READ)) @@ -126,10 +127,15 @@ bev_async_consider_reading(struct bufferevent_async *b) /* Don't read if we're full */ cur_size = evbuffer_get_length(b->bev.bev.input); read_high = b->bev.bev.wm_read.high; - if (cur_size >= read_high) - return; + if (read_high) { + if (cur_size >= read_high) + return; + at_most = read_high - cur_size; + } else { + at_most = 16384; /* FIXME totally magic. */ + } - if (evbuffer_launch_read(b->bev.bev.input, read_high-cur_size)) { + if (evbuffer_launch_read(b->bev.bev.input, at_most)) { assert(0); } else { b->read_in_progress = 1; diff --git a/event-internal.h b/event-internal.h index 7a6bf8c6..03ccced5 100644 --- a/event-internal.h +++ b/event-internal.h @@ -32,6 +32,8 @@ extern "C" { #endif #include "event-config.h" +#include +#include "event2/event_struct.h" #include "minheap-internal.h" #include "evsignal-internal.h" #include "mm-internal.h" diff --git a/event_iocp.c b/event_iocp.c index 18021aaa..f5eb185e 100644 --- a/event_iocp.c +++ b/event_iocp.c @@ -33,6 +33,7 @@ #include "iocp-internal.h" #include "log-internal.h" #include "mm-internal.h" +#include "event-internal.h" #define NOTIFICATION_KEY ((ULONG_PTR)-1) @@ -195,7 +196,7 @@ event_iocp_activate_overlapped( return (r==0) ? -1 : 0; } -struct event_iocp * +struct event_iocp_port * event_base_get_iocp(struct event_base *base) { #ifdef WIN32 diff --git a/iocp-internal.h b/iocp-internal.h index 1dcdbc23..1470620d 100644 --- a/iocp-internal.h +++ b/iocp-internal.h @@ -145,6 +145,7 @@ int event_iocp_activate_overlapped(struct event_iocp_port *port, struct event_overlapped *o, uintptr_t key, ev_uint32_t n_bytes); +struct event_base; struct event_iocp_port *event_base_get_iocp(struct event_base *base); #ifdef __cplusplus diff --git a/test/regress_iocp.c b/test/regress_iocp.c index 35114cd7..7c217c84 100644 --- a/test/regress_iocp.c +++ b/test/regress_iocp.c @@ -176,14 +176,18 @@ test_iocp_evbuffer(void *ptr) for (i=0;i<10;++i) evbuffer_add(wbuf, junk, sizeof(junk)); - tt_assert(!evbuffer_launch_read(rbuf, 2048)); + tt_assert(!evbuffer_get_length(rbuf)); tt_assert(!evbuffer_launch_write(wbuf, 512)); + tt_assert(!evbuffer_launch_read(rbuf, 2048)); #ifdef WIN32 /* FIXME this is stupid. */ Sleep(1000); #endif + tt_int_op(evbuffer_get_length(rbuf),==,512); + + /* FIXME Actually test some stuff here. */ end: @@ -220,9 +224,9 @@ test_iocp_bufferevent_async(void *ptr) #endif bea1 = bufferevent_async_new(data->base, data->pair[0], - BEV_OPT_DEFER_CALLBACKS); + 0); /* We'd defer callbacks, but that would need a base. */ bea2 = bufferevent_async_new(data->base, data->pair[1], - BEV_OPT_DEFER_CALLBACKS); + 0); tt_assert(bea1); tt_assert(bea2); @@ -239,7 +243,7 @@ test_iocp_bufferevent_async(void *ptr) #endif n = bufferevent_read(bea2, buf, sizeof(buf)-1); buf[n]='\0'; - tt_assert(!strcmp(buf, "Hello world")); + tt_str_op(buf, ==, "Hello world"); end: /* FIXME: free stuff. */; -- 2.50.1