Skip to content
Snippets Groups Projects
Commit 7e23ca79 authored by Vladislav Shpilevoy's avatar Vladislav Shpilevoy
Browse files

test: stop using swim_transport.addr as in-param

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
parent 362c195c
No related branches found
No related tags found
No related merge requests found
......@@ -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);
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment