mirror of
https://github.com/cesanta/mongoose.git
synced 2026-11-08 16:11:18 +00:00
improve RTX tests for coverage and resiliency
This commit is contained in:
1 parent
aaf202ba2f
commit
f59a3f71ed
3 files changed
+100
-63
No files matched your search
+16
-8
@@ -5,6 +5,9 @@
|
||||
|
||||
#include "mip_x_test.c"
|
||||
|
||||
static bool drop_tcp_segment(const void *buf, size_t len,
|
||||
struct mg_tcpip_if *ifp);
|
||||
|
||||
struct port_driver_data {
|
||||
int sockfd;
|
||||
struct sockaddr_in port_addr;
|
||||
@@ -16,6 +19,7 @@ static size_t sock_rx(void *buf, size_t len, struct mg_tcpip_if *ifp) {
|
||||
ssize_t received = recv(((struct port_driver_data *)ifp->driver_data)->sockfd, buf, len, 0);
|
||||
usleep(1); // This is to avoid 100% CPU
|
||||
if (received < 0) return 0;
|
||||
if (drop_tcp_segment(buf, (size_t) received, ifp)) return 0;
|
||||
return (size_t) received;
|
||||
}
|
||||
|
||||
@@ -37,15 +41,19 @@ static bool is_tcp_segment(const void *buf, size_t len) {
|
||||
return (tcp->flags & (TH_SYN | TH_FIN)) == 0;
|
||||
}
|
||||
|
||||
static size_t sock_tx(const void *buf, size_t len, struct mg_tcpip_if *ifp) {
|
||||
static bool drop_tcp_segment(const void *buf, size_t len,
|
||||
struct mg_tcpip_if *ifp) {
|
||||
uint32_t random;
|
||||
if (ifp->enable_tcp_retransmit && is_tcp_segment(buf, len)) { // introduce a 30% segment loss
|
||||
mg_random(&random, sizeof(random));
|
||||
if (random % 10 < 3) {
|
||||
MG_DEBUG(("dropping TCP segment"));
|
||||
return len;
|
||||
}
|
||||
}
|
||||
if (!ifp->enable_tcp_retransmit || !is_tcp_segment(buf, len)) return false;
|
||||
// Introduce a 30% segment loss
|
||||
mg_random(&random, sizeof(random));
|
||||
if (random % 10 >= 3) return false;
|
||||
MG_DEBUG(("dropping TCP segment"));
|
||||
return true;
|
||||
}
|
||||
|
||||
static size_t sock_tx(const void *buf, size_t len, struct mg_tcpip_if *ifp) {
|
||||
if (drop_tcp_segment(buf, len, ifp)) return len;
|
||||
ssize_t res = send(((struct port_driver_data *)ifp->driver_data)->sockfd, buf, len, 0);
|
||||
if (res < 0) {
|
||||
MG_ERROR(("sock_tx failed: %d", errno));
|
||||
|
||||
Reference in new issue
Block a user