From 8fb6f0f5c23ee08cd4b39c2009804e45a8879636 Mon Sep 17 00:00:00 2001
From: Florian Fischer <florian.fischer@muhq.space>
Date: Tue, 16 Nov 2021 17:42:12 +0100
Subject: [PATCH] [ConcurrentNetworkEchoTest] add optional computation per echo

This makes the test closer to our echoserver.
---
 tests/io/ConcurrentNetworkEchoTest.cpp | 37 +++++++++++++++++++++++---
 1 file changed, 34 insertions(+), 3 deletions(-)

diff --git a/tests/io/ConcurrentNetworkEchoTest.cpp b/tests/io/ConcurrentNetworkEchoTest.cpp
index 2b907ed8..f03fdb3d 100644
--- a/tests/io/ConcurrentNetworkEchoTest.cpp
+++ b/tests/io/ConcurrentNetworkEchoTest.cpp
@@ -1,5 +1,10 @@
 // SPDX-License-Identifier: LGPL-3.0-or-later
 // Copyright © 2020-2021 Florian Fischer
+#include <sys/socket.h>
+
+#include <array>
+#include <chrono>
+#include <compare>
 #include <cstdlib>
 #include <iostream>
 #include <string>
@@ -26,11 +31,12 @@ auto main(int argc, char* argv[]) -> int {
 
 	unsigned int client_count = std::thread::hardware_concurrency() * 4;
 	unsigned int echos = EMPER_LOG_LEVEL > Info ? 250 : 25000;
+	unsigned int computation = 0;
 	std::string port = "4241";
 	std::string host = "127.0.0.1";
 
-	if (argc > 3) {
-		std::cerr << "Usage: " << argv[0] << " [clients] [echos]" << std::endl;
+	if (argc > 4) {
+		std::cerr << "Usage: " << argv[0] << " [clients] [echos] [computation]" << std::endl;
 		exit(EXIT_FAILURE);
 	}
 
@@ -43,13 +49,38 @@ auto main(int argc, char* argv[]) -> int {
 		echos = strtol(argv[2], nullptr, DECIMAL);
 	}
 
+	if (argc > 3) {
+		computation = strtol(argv[3], nullptr, DECIMAL);
+	}
+
 	std::cout << "ConcurrentNetworkEchoTest with " << client_count << " clients sending " << echos
 						<< " echos" << std::endl;
 
 	Runtime runtime;
 
+	auto serve_func = [&](int sock) {
+		std::array<char, BUF_SIZE> buf;
+		for (;;) {
+			ssize_t bytes_recv = emper::io::recvAndWait(sock, buf.data(), buf.size(), 0);
+			// socket was shutdown
+			if (bytes_recv == 0) return;
+
+			if (bytes_recv == -1) DIE_MSG_ERRNO("server read failed");
+
+			if (computation) {
+				const auto start = std::chrono::steady_clock::now();
+				const auto deadline = start + std::chrono::microseconds(computation);
+				// NOLINTNEXTLINE(modernize-use-nullptr)
+				while (std::chrono::steady_clock::now() < deadline) {
+				}
+			}
+			if (emper::io::sendAndWait(sock, buf.data(), bytes_recv, MSG_NOSIGNAL, true) == -1)
+				DIE_MSG_ERRNO("send failed");
+		}
+	};
+
 	Fiber* listener =
-			emper::io::tcp_listener(host, port, echo_serve, 1024, {emper::io::SockOpt::ReusePort});
+			emper::io::tcp_listener(host, port, serve_func, 1024, {emper::io::SockOpt::ReusePort});
 	if (!listener) {
 		DIE_MSG("failed to create listener");
 	}
-- 
GitLab