...
authoralex <[email protected]>
Mon, 28 Mar 2022 17:13:23 +0000 (10:13 -0700)
committeralex <[email protected]>
Mon, 28 Mar 2022 17:13:23 +0000 (10:13 -0700)
src/net/queue.c
src/net/wait.c
test/unit/Makefile.am
test/unit/net.tests.c

index eafc7ece9c0f78ba623d734438b2b666cdab7fc9..f4f08f061a639f63201a89f5d3f950c16163e695 100644 (file)
@@ -3,6 +3,10 @@
 int net_queue(int epoll_fd, int sock, struct peer *info) {
        struct epoll_event ev;
 
+       if(epoll_fd<0) { return -1; }
+       if(sock<0) { return -1; }
+       if(NULL==info) { return -1; }
+
        ev.events = EPOLLIN | EPOLLOUT | EPOLLET;
        ev.data.ptr = info;
        ev.data.fd = sock;
index beb4af31a8a475c974d654a3fa085be0f0c5d443..8a962768d60af043a92463d5395d469c5294559f 100644 (file)
@@ -17,9 +17,11 @@ int net_wait(int sock, struct peer *info, void *buf, size_t buf_size) {
 
        i = recv(sock,p,size,0);
        if(-1==i) {
-               if((errno==EAGAIN)||(errno==EWOULDBLOCK)) { return 0; }
-               perror("recv");
-               return -1;
+               if(!((errno==EAGAIN)||(errno==EWOULDBLOCK))) {
+                       perror("recv");
+                       return -1;
+               }
+               i = 0;
        }
 
        if(i!=size) { goto cached; }
@@ -51,7 +53,7 @@ static void *use_cached(struct peer *info,void *p,size_t *size) {
                        free(info->in);
                        info->in = NULL;
 
-                       *size -= info->in_size;
+                       (*size) -= info->in_size;
                        p += info->in_size;
                }
        }
index 9a37ff4a8b1d866821cd05a6c23e459b7b2a14c0..ea1d39f4b2a41d2ca73b6bfaef2501397826485b 100644 (file)
@@ -120,6 +120,7 @@ net_tests_SOURCES = \
        $(common_SOURCES) \
        net.tests.c \
        $(top_srcdir)/src/net/cache.c \
+       $(top_srcdir)/src/net/queue.c \
        $(top_srcdir)/src/net/send.c \
        $(top_srcdir)/src/net/wait.c \
        $(top_srcdir)/src/peer/free.c \
index 0fabe6dba73d0751bfacffcaaa0a932eae147b4a..eeb6014b0065a698891e780b7ada8d27ed72e8a4 100644 (file)
@@ -5,6 +5,7 @@
 int main();
 static void setup_sockets();
 static void net_cache_basic_test();
+static void net_queue_basic_test();
 static void net_send_basic_test();
 static void net_wait_basic_test();
 
@@ -15,6 +16,7 @@ int main() {
        setup_env();
 
        net_cache_basic_test();
+       net_queue_basic_test();
        net_send_basic_test();
        net_wait_basic_test();
 
@@ -66,6 +68,28 @@ static void net_cache_basic_test() {
        free(p1);
 }
 
+static void net_queue_basic_test() {
+       int epoll_fd;
+       int sock;
+       struct peer *info;
+
+       assert(1==peer_init(&info));
+       
+       epoll_fd = epoll_create1(0);
+       assert(epoll_fd!=-1);
+
+       sock = 0;
+
+       assert(-1==net_queue(-1,-1,NULL));
+       assert(-1==net_queue(epoll_fd,-1,NULL));
+       assert(-1==net_queue(epoll_fd,sock,NULL));
+
+       assert(1==net_queue(epoll_fd,sock,info));
+
+       peer_free(info);
+       close(epoll_fd);
+}
+
 static void net_send_basic_test() {
        struct peer *info;
        char buf[100];
@@ -100,6 +124,7 @@ static void net_wait_basic_test() {
        struct peer *info;
        char buf[100];
        char expected[100];
+       int i, increment;
 
        setup_sockets();
 
@@ -133,6 +158,22 @@ static void net_wait_basic_test() {
        assert(1==net_wait(read_sock,info,buf,100));
        assert(info->in==NULL);
        assert(memcmp(buf,expected,100)==0);
+
+       i = 0;
+       info->in_size = 0;
+       while(i<99) {
+               assert(0==net_wait(read_sock,info,buf,100));
+               increment = rand()%2;
+               i += increment;
+               assert(((increment>0)?1:0)==write(write_sock,expected,increment));
+               assert(0==net_wait(read_sock,info,buf,100));
+               assert(info->in_size==i);
+       }
+       
+       assert(1==write(write_sock,expected,1));
+       assert(1==net_wait(read_sock,info,buf,100));
+       assert(info->in==NULL);
+       assert(memcmp(buf,expected,100)==0);
        
        close(read_sock);
        close(write_sock);