1 parent
52ab1ca24c
commit
a3715201e1
23 files changed
+2226
-37
No files matched your search
@@ -0,0 +1,97 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <mutex>
|
||||
#include <span>
|
||||
#include <vector>
|
||||
|
||||
namespace Echoion::Test
|
||||
{
|
||||
|
||||
// Captures every chunk the playback pipeline emits through the audio
|
||||
// callback and answers the questions the gapless tests care about:
|
||||
// how many samples arrived, whether any chunk was pure silence, and the
|
||||
// longest wall-clock stall between consecutive chunks.
|
||||
class FakeAudioSink
|
||||
{
|
||||
public:
|
||||
|
||||
void onChunk(std::span<const short> samples)
|
||||
{
|
||||
const auto now = std::chrono::steady_clock::now();
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
|
||||
if (chunkCount_ > 0) {
|
||||
auto gap =
|
||||
std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
now - lastChunkTime_);
|
||||
maxInterChunkGap_ = std::max(maxInterChunkGap_, gap);
|
||||
}
|
||||
lastChunkTime_ = now;
|
||||
++chunkCount_;
|
||||
totalSamples_ += samples.size();
|
||||
|
||||
const bool allZero = std::ranges::all_of(
|
||||
samples, [](short s) { return s == 0; });
|
||||
if (allZero) {
|
||||
++silentChunkCount_;
|
||||
}
|
||||
|
||||
pcm_.insert(pcm_.end(), samples.begin(), samples.end());
|
||||
}
|
||||
|
||||
[[nodiscard]] std::size_t chunkCount() const
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
return chunkCount_;
|
||||
}
|
||||
|
||||
[[nodiscard]] std::size_t totalSamples() const
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
return totalSamples_;
|
||||
}
|
||||
|
||||
[[nodiscard]] std::size_t silentChunkCount() const
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
return silentChunkCount_;
|
||||
}
|
||||
|
||||
[[nodiscard]] std::chrono::milliseconds maxInterChunkGap() const
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
return maxInterChunkGap_;
|
||||
}
|
||||
|
||||
// Full captured PCM stream (interleaved S16), for waveform-level
|
||||
// assertions such as "no silent seam at the track boundary".
|
||||
[[nodiscard]] std::vector<short> pcm() const
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
return pcm_;
|
||||
}
|
||||
|
||||
void reset()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
chunkCount_ = 0;
|
||||
totalSamples_ = 0;
|
||||
silentChunkCount_ = 0;
|
||||
maxInterChunkGap_ = {};
|
||||
pcm_.clear();
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
mutable std::mutex mutex_;
|
||||
std::size_t chunkCount_ = 0;
|
||||
std::size_t totalSamples_ = 0;
|
||||
std::size_t silentChunkCount_ = 0;
|
||||
std::chrono::milliseconds maxInterChunkGap_{0};
|
||||
std::chrono::steady_clock::time_point lastChunkTime_{};
|
||||
std::vector<short> pcm_;
|
||||
};
|
||||
|
||||
} // namespace Echoion::Test
|
||||
@@ -0,0 +1,146 @@
|
||||
#pragma once
|
||||
|
||||
#include <atomic>
|
||||
#include <boost/asio.hpp>
|
||||
#include <boost/beast.hpp>
|
||||
#include <map>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
|
||||
namespace Echoion::Test
|
||||
{
|
||||
|
||||
// Tiny synchronous HTTP/1.1 server for source-router tests. Register
|
||||
// canned responses per path (query string included); unknown paths get
|
||||
// 404. Runs its accept loop on one background thread and handles one
|
||||
// request per connection, which is all the NetworkClient needs.
|
||||
class StubHttpServer
|
||||
{
|
||||
public:
|
||||
|
||||
struct Response {
|
||||
int status = 200;
|
||||
std::string contentType = "application/json";
|
||||
std::string body;
|
||||
};
|
||||
|
||||
StubHttpServer() :
|
||||
acceptor_(io_, {boost::asio::ip::make_address("127.0.0.1"), 0})
|
||||
{
|
||||
port_ = acceptor_.local_endpoint().port();
|
||||
thread_ = std::thread([this] { run(); });
|
||||
}
|
||||
|
||||
~StubHttpServer()
|
||||
{
|
||||
stopping_ = true;
|
||||
// A synchronous accept(2) is NOT interrupted by closing the
|
||||
// fd from another thread on Linux; poke it with a throwaway
|
||||
// connection so the loop observes stopping_.
|
||||
try {
|
||||
boost::asio::io_context io;
|
||||
boost::asio::ip::tcp::socket s(io);
|
||||
s.connect({boost::asio::ip::make_address("127.0.0.1"),
|
||||
port_});
|
||||
} catch (...) {
|
||||
// Server may already be gone; nothing to unblock.
|
||||
}
|
||||
boost::system::error_code ec;
|
||||
acceptor_.close(ec);
|
||||
if (thread_.joinable()) {
|
||||
thread_.join();
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] uint16_t port() const { return port_; }
|
||||
|
||||
[[nodiscard]] std::string baseUrl() const
|
||||
{
|
||||
return "http://127.0.0.1:" + std::to_string(port_);
|
||||
}
|
||||
|
||||
// Exact-match route ("/api/v1/stats") or prefix route when the
|
||||
// registered path ends with '*' ("/api/v1/videos/*").
|
||||
void route(std::string path, Response response)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
routes_[std::move(path)] = std::move(response);
|
||||
}
|
||||
|
||||
[[nodiscard]] std::size_t requestCount() const
|
||||
{
|
||||
return requestCount_.load();
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
void run()
|
||||
{
|
||||
while (!stopping_) {
|
||||
boost::system::error_code ec;
|
||||
boost::asio::ip::tcp::socket socket(io_);
|
||||
acceptor_.accept(socket, ec);
|
||||
if (ec) {
|
||||
break; // closed by destructor
|
||||
}
|
||||
handle(socket);
|
||||
}
|
||||
}
|
||||
|
||||
void handle(boost::asio::ip::tcp::socket& socket)
|
||||
{
|
||||
namespace http = boost::beast::http;
|
||||
try {
|
||||
boost::beast::flat_buffer buffer;
|
||||
http::request<http::string_body> req;
|
||||
http::read(socket, buffer, req);
|
||||
++requestCount_;
|
||||
|
||||
const Response canned = lookup(std::string(req.target()));
|
||||
|
||||
http::response<http::string_body> res{
|
||||
static_cast<http::status>(canned.status),
|
||||
req.version()};
|
||||
res.set(http::field::content_type, canned.contentType);
|
||||
res.body() = canned.body;
|
||||
res.prepare_payload();
|
||||
http::write(socket, res);
|
||||
|
||||
boost::system::error_code ec;
|
||||
socket.shutdown(boost::asio::ip::tcp::socket::shutdown_send,
|
||||
ec);
|
||||
} catch (const std::exception&) {
|
||||
// Connection torn down mid-request; irrelevant for tests.
|
||||
}
|
||||
}
|
||||
|
||||
Response lookup(const std::string& target)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mutex_);
|
||||
if (auto it = routes_.find(target); it != routes_.end()) {
|
||||
return it->second;
|
||||
}
|
||||
for (const auto& [path, response] : routes_) {
|
||||
if (path.ends_with('*') &&
|
||||
target.starts_with(
|
||||
std::string_view(path).substr(0, path.size() - 1))) {
|
||||
return response;
|
||||
}
|
||||
}
|
||||
return Response{.status = 404,
|
||||
.contentType = "text/plain",
|
||||
.body = "not found"};
|
||||
}
|
||||
|
||||
boost::asio::io_context io_;
|
||||
boost::asio::ip::tcp::acceptor acceptor_;
|
||||
uint16_t port_ = 0;
|
||||
std::thread thread_;
|
||||
std::atomic<bool> stopping_{false};
|
||||
std::atomic<std::size_t> requestCount_{0};
|
||||
std::mutex mutex_;
|
||||
std::map<std::string, Response> routes_;
|
||||
};
|
||||
|
||||
} // namespace Echoion::Test
|
||||
Reference in new issue
Block a user