200 lines
5.8 KiB
C++
200 lines
5.8 KiB
C++
#include <doctest/doctest.h>
|
|
|
|
#include <echoion/core/sources/DirectUrlSource.hpp>
|
|
#include <echoion/core/sources/SourceRouter.hpp>
|
|
|
|
#include <memory>
|
|
|
|
using namespace Echoion;
|
|
using namespace Echoion::Core;
|
|
|
|
namespace
|
|
{
|
|
// Configurable fake source for router-behavior tests.
|
|
class FakeSource : public ITrackSource
|
|
{
|
|
public:
|
|
|
|
FakeSource(std::string name, std::chrono::milliseconds delay,
|
|
bool succeed) :
|
|
name_(std::move(name)), delay_(delay), succeed_(succeed)
|
|
{
|
|
}
|
|
|
|
std::atomic<int> calls{0};
|
|
|
|
[[nodiscard]] std::string_view name() const override
|
|
{
|
|
return name_;
|
|
}
|
|
[[nodiscard]] bool canHandle(const std::string&) const override
|
|
{
|
|
return true;
|
|
}
|
|
[[nodiscard]] bool isAvailable() const override { return true; }
|
|
[[nodiscard]] bool supportsSearch() const override
|
|
{
|
|
return false;
|
|
}
|
|
Api::Result<Api::SearchResult> search(std::string_view) override
|
|
{
|
|
return Api::fail(Api::ErrorCode::NotSupported, "no");
|
|
}
|
|
Api::Result<Api::TrackInfo>
|
|
resolve(const std::string&) override
|
|
{
|
|
return Api::fail(Api::ErrorCode::NotSupported, "no");
|
|
}
|
|
Api::Result<StreamHandle>
|
|
getStream(const Api::TrackInfo&) override
|
|
{
|
|
++calls;
|
|
std::this_thread::sleep_for(delay_);
|
|
if (succeed_) {
|
|
return StreamHandle{.url = "https://x/" + name_,
|
|
.isLocalFile = false,
|
|
.sourceName = name_};
|
|
}
|
|
return Api::fail(Api::ErrorCode::NetworkError, "boom");
|
|
}
|
|
|
|
private:
|
|
|
|
std::string name_;
|
|
std::chrono::milliseconds delay_;
|
|
bool succeed_;
|
|
};
|
|
|
|
Api::TrackInfo hedgeTrack(const std::string& id)
|
|
{
|
|
Api::TrackInfo t;
|
|
t.id = id;
|
|
t.originalUrl = "https://example.com/" + id;
|
|
return t;
|
|
}
|
|
} // namespace
|
|
|
|
TEST_CASE("SourceRouter hedges a slow first source")
|
|
{
|
|
auto slow = std::make_shared<FakeSource>(
|
|
"slow", std::chrono::milliseconds(2000), true);
|
|
auto fast = std::make_shared<FakeSource>(
|
|
"fast", std::chrono::milliseconds(20), true);
|
|
|
|
SourceRouter router;
|
|
router.setSources({slow, fast});
|
|
router.setOrder({"slow", "fast"});
|
|
router.setHedgeDelay(std::chrono::milliseconds(100));
|
|
|
|
const auto start = std::chrono::steady_clock::now();
|
|
auto handle = router.getStream(hedgeTrack("h1"));
|
|
const auto elapsed = std::chrono::steady_clock::now() - start;
|
|
|
|
REQUIRE(handle.has_value());
|
|
CHECK(handle->sourceName == "fast"); // hedge won
|
|
CHECK(elapsed < std::chrono::milliseconds(1000)); // didn't wait 2s
|
|
CHECK(slow->calls.load() == 1);
|
|
CHECK(fast->calls.load() == 1);
|
|
}
|
|
|
|
TEST_CASE("SourceRouter does not hedge when the first source is fast")
|
|
{
|
|
auto fast = std::make_shared<FakeSource>(
|
|
"fast", std::chrono::milliseconds(10), true);
|
|
auto second = std::make_shared<FakeSource>(
|
|
"second", std::chrono::milliseconds(10), true);
|
|
|
|
SourceRouter router;
|
|
router.setSources({fast, second});
|
|
router.setOrder({"fast", "second"});
|
|
router.setHedgeDelay(std::chrono::milliseconds(200));
|
|
|
|
auto handle = router.getStream(hedgeTrack("h2"));
|
|
REQUIRE(handle.has_value());
|
|
CHECK(handle->sourceName == "fast");
|
|
CHECK(second->calls.load() == 0); // never launched
|
|
}
|
|
|
|
TEST_CASE("SourceRouter falls through to a third source when the hedge "
|
|
"pair fails")
|
|
{
|
|
auto bad1 = std::make_shared<FakeSource>(
|
|
"bad1", std::chrono::milliseconds(10), false);
|
|
auto bad2 = std::make_shared<FakeSource>(
|
|
"bad2", std::chrono::milliseconds(10), false);
|
|
auto good = std::make_shared<FakeSource>(
|
|
"good", std::chrono::milliseconds(10), true);
|
|
|
|
SourceRouter router;
|
|
router.setSources({bad1, bad2, good});
|
|
router.setOrder({"bad1", "bad2", "good"});
|
|
router.setHedgeDelay(std::chrono::milliseconds(50));
|
|
|
|
auto handle = router.getStream(hedgeTrack("h3"));
|
|
REQUIRE(handle.has_value());
|
|
CHECK(handle->sourceName == "good");
|
|
}
|
|
|
|
TEST_CASE("SourceRouter hedgeDelay=0 keeps the sequential path")
|
|
{
|
|
auto slow = std::make_shared<FakeSource>(
|
|
"slow", std::chrono::milliseconds(100), true);
|
|
auto fast = std::make_shared<FakeSource>(
|
|
"fast", std::chrono::milliseconds(10), true);
|
|
|
|
SourceRouter router;
|
|
router.setSources({slow, fast});
|
|
router.setOrder({"slow", "fast"});
|
|
router.setHedgeDelay(std::chrono::milliseconds(0));
|
|
|
|
auto handle = router.getStream(hedgeTrack("h4"));
|
|
REQUIRE(handle.has_value());
|
|
CHECK(handle->sourceName == "slow"); // sequential: first wins
|
|
CHECK(fast->calls.load() == 0);
|
|
}
|
|
|
|
TEST_CASE("SourceRouter falls through a failing source and caches streams")
|
|
{
|
|
auto dead = std::make_shared<FakeSource>(
|
|
"dead", std::chrono::milliseconds(10), false);
|
|
auto good = std::make_shared<FakeSource>(
|
|
"good", std::chrono::milliseconds(10), true);
|
|
|
|
SourceRouter router;
|
|
router.setSources({dead, good});
|
|
router.setOrder({"dead", "good"});
|
|
router.setHedgeDelay(std::chrono::milliseconds(0)); // sequential
|
|
|
|
auto track = hedgeTrack("fall1");
|
|
auto handle = router.getStream(track);
|
|
REQUIRE(handle.has_value());
|
|
CHECK(handle->sourceName == "good"); // fell through the dead source
|
|
CHECK(good->calls.load() == 1);
|
|
|
|
// Second getStream must be served from cache — the source is not hit.
|
|
auto cached = router.getStream(track);
|
|
REQUIRE(cached.has_value());
|
|
CHECK(good->calls.load() == 1);
|
|
|
|
// Invalidation drops the cache: the next getStream resolves again.
|
|
router.invalidate(track.id);
|
|
auto refreshed = router.getStream(track);
|
|
REQUIRE(refreshed.has_value());
|
|
CHECK(good->calls.load() == 2);
|
|
}
|
|
|
|
TEST_CASE("DirectUrlSource passes through audio URLs only")
|
|
{
|
|
DirectUrlSource source;
|
|
CHECK(source.canHandle("https://radio.example/stream"));
|
|
CHECK(source.canHandle("http://files.example/song.mp3?token=x"));
|
|
CHECK_FALSE(source.canHandle("https://www.youtube.com/watch?v=abc"));
|
|
CHECK_FALSE(source.canHandle("plain text query"));
|
|
|
|
auto track = source.resolve("https://radio.example/stream");
|
|
REQUIRE(track.has_value());
|
|
auto handle = source.getStream(*track);
|
|
REQUIRE(handle.has_value());
|
|
CHECK(handle->url == "https://radio.example/stream");
|
|
}
|