From: Vladislav Shpilevoy <v.shpilevoy@tarantool.org> To: tarantool-patches@dev.tarantool.org, sergepetrenko@tarantool.org Subject: [Tarantool-patches] [PATCH 01/10] test: stop using swim_transport.addr as in-param Date: Tue, 1 Dec 2020 00:56:09 +0100 [thread overview] Message-ID: <5625dfe8e8a67e907561d715c26b87fe46d28a8d.1606780408.git.v.shpilevoy@tarantool.org> (raw) In-Reply-To: <cover.1606780408.git.v.shpilevoy@tarantool.org> SWIM unit tests contain special libraries for emulating event loop and network: swim_test_ev and swim_test_transport. They provide API similar to libev and to network part of libc, which internally is implemented entirely in user-space and allows to simulate all kinds of errors, any time durations, etc. These test libraries are going to be re-used for Raft unit tests. But for that it is necessary to detach them from all SWIM dependencies. -- One of the dependencies - swim_transport.addr, which was used in swim_transport_send() as an input parameter. swim_transport_send() simulates sendto(), and it can't be generalized while it depends on source address being passed explicitly. This patch makes swim_transport_send() deduct the source address by the file descriptor number. There is a couple of new functions for that: swim_test_sockaddr_in_to_fd and swim_test_fd_to_sockaddr_in. They were inlined earlier, but it seems the fd <-> source address translation is used often enough to extract these functions. Part of #5303 --- test/unit/swim_test_transport.c | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/test/unit/swim_test_transport.c b/test/unit/swim_test_transport.c index 334ac926e..9fad351b5 100644 --- a/test/unit/swim_test_transport.c +++ b/test/unit/swim_test_transport.c @@ -51,6 +51,25 @@ enum { FAKE_FD_NUMBER = 1000, }; +static inline int +swim_test_sockaddr_in_to_fd(const struct sockaddr_in *addr) +{ + assert(addr->sin_family == AF_INET); + return ntohs(addr->sin_port) + FAKE_FD_BASE; +} + +static inline void +swim_test_fd_to_sockaddr_in(int fd, struct sockaddr_in *addr) +{ + *addr = (struct sockaddr_in){ + .sin_family = AF_INET, + .sin_port = htons(fd) - FAKE_FD_BASE, + .sin_addr = { + .s_addr = htonl(INADDR_LOOPBACK), + }, + }; +} + /** UDP packet wrapper. It is stored in send/recv queues. */ struct swim_test_packet { /** Source address. */ @@ -289,8 +308,10 @@ swim_transport_send(struct swim_transport *transport, const void *data, */ (void) addr_size; assert(addr->sa_family == AF_INET); + struct sockaddr_in src_addr; + swim_test_fd_to_sockaddr_in(transport->fd, &src_addr); struct swim_test_packet *p = - swim_test_packet_new(data, size, &transport->addr, + swim_test_packet_new(data, size, &src_addr, (const struct sockaddr_in *) addr); struct swim_fd *src = &swim_fd[transport->fd - FAKE_FD_BASE]; assert(src->is_opened); @@ -328,7 +349,7 @@ swim_transport_bind(struct swim_transport *transport, { assert(addr->sa_family == AF_INET); const struct sockaddr_in *new_addr = (const struct sockaddr_in *) addr; - int new_fd = ntohs(new_addr->sin_port) + FAKE_FD_BASE; + int new_fd = swim_test_sockaddr_in_to_fd(new_addr); int old_fd = transport->fd; if (old_fd == new_fd) { transport->addr = *new_addr; @@ -406,7 +427,8 @@ swim_fd_send_packet(struct swim_fd *fd) } swim_test_packet_delete(p); } else { - dst = &swim_fd[ntohs(p->dst.sin_port)]; + int fdnum = swim_test_sockaddr_in_to_fd(&p->dst); + dst = &swim_fd[fdnum - FAKE_FD_BASE]; swim_move_packet(fd, dst, p); } } -- 2.24.3 (Apple Git-128)
next prev parent reply other threads:[~2020-11-30 23:56 UTC|newest] Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top 2020-11-30 23:56 [Tarantool-patches] [PATCH 00/10] Raft module, part 3 - fake network and libev for Raft and SWIM Vladislav Shpilevoy 2020-11-30 23:56 ` Vladislav Shpilevoy [this message] 2020-11-30 23:56 ` [Tarantool-patches] [PATCH 10/10] fakesys: move fakeev to fakesys library Vladislav Shpilevoy 2020-11-30 23:56 ` [Tarantool-patches] [PATCH 02/10] test: factor out swim from libc emulation funcs Vladislav Shpilevoy 2020-11-30 23:56 ` [Tarantool-patches] [PATCH 03/10] test: rename fake libc network methods to fakenet Vladislav Shpilevoy 2020-11-30 23:56 ` [Tarantool-patches] [PATCH 04/10] test: move fake network code to fakenet.c/.h files Vladislav Shpilevoy 2020-11-30 23:56 ` [Tarantool-patches] [PATCH 05/10] test: factor out swim from " Vladislav Shpilevoy 2020-11-30 23:56 ` [Tarantool-patches] [PATCH 06/10] fakesys: introduce fake system library Vladislav Shpilevoy 2020-11-30 23:56 ` [Tarantool-patches] [PATCH 07/10] test: rename fake libev methods to fakeev Vladislav Shpilevoy 2020-11-30 23:56 ` [Tarantool-patches] [PATCH 08/10] test: move fake libev code to fakeev.c/.h files Vladislav Shpilevoy 2020-11-30 23:56 ` [Tarantool-patches] [PATCH 09/10] test: factor out swim from fakeev.h/.c files Vladislav Shpilevoy 2020-12-01 13:42 ` [Tarantool-patches] [PATCH 00/10] Raft module, part 3 - fake network and libev for Raft and SWIM Serge Petrenko 2020-12-01 22:33 ` Vladislav Shpilevoy 2020-12-04 0:35 ` Alexander V. Tikhonov 2020-12-04 22:53 ` Vladislav Shpilevoy
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=5625dfe8e8a67e907561d715c26b87fe46d28a8d.1606780408.git.v.shpilevoy@tarantool.org \ --to=v.shpilevoy@tarantool.org \ --cc=sergepetrenko@tarantool.org \ --cc=tarantool-patches@dev.tarantool.org \ --subject='Re: [Tarantool-patches] [PATCH 01/10] test: stop using swim_transport.addr as in-param' \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox