286b55986b
The Pull*UseCase implementations issued one HTTP request at a time despite
Scraping:MaxConcurrentRequests=4. With 30–80 live events and ~1s per
fetch, a 5–10s live cadence target was unreachable; cycles overflowed
the configured interval.
* New Marathon.Application.Configuration.ScrapingThrottle bound from the
shared Scraping:* section. Exposes only MaxConcurrentRequests so the
Application layer doesn't pull in the Infrastructure-side ScrapingOptions.
* PullLiveOddsUseCase + PullUpcomingEventsUseCase split into two phases:
- Phase 1 — Parallel.ForEachAsync over the event list with
MaxDegreeOfParallelism = throttle.MaxConcurrentRequests. The scraper's
Polly rate limiter still throttles to RequestsPerSecond underneath
this fan-out, so spikes are smoothed before they hit the bookmaker.
- Phase 2 — sequential foreach over the (Event, Snapshot) tuples
captured in Phase 1, doing event upsert + snapshot insert. EF Core
DbContext is not thread-safe so all DB writes stay on a single thread.
* InfrastructureModule binds ScrapingThrottle alongside AnomalyOptions.
* Failed snapshot scrapes in Phase 1 mean the event row is also NOT
persisted in Phase 2 — previously we'd persist the row even when the
snapshot scrape failed, leaving an orphan event with no odds. Updated
the regression test accordingly.
* Test fixture exposes TestFixtures.Throttle(maxConcurrentRequests=1) for
deterministic sequential test runs.
* One existing NSubstitute setup that chained Arg.Is<>() across two
configurations was rewritten to use a single Arg.Any<>() with inline
branching — chained matchers were leaking and returning wrong results.
173 lines
7.0 KiB
C#
173 lines
7.0 KiB
C#
using FluentAssertions;
|
|
using Marathon.Application.Abstractions;
|
|
using Marathon.Application.Configuration;
|
|
using Marathon.Application.UseCases;
|
|
using Marathon.Domain.Entities;
|
|
using Marathon.Domain.Enums;
|
|
using Marathon.Domain.ValueObjects;
|
|
using Marathon.Infrastructure.Configuration;
|
|
using Marathon.Infrastructure.Workers;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using Microsoft.Extensions.Options;
|
|
using NSubstitute;
|
|
using NSubstitute.ExceptionExtensions;
|
|
|
|
namespace Marathon.Infrastructure.Tests.Workers;
|
|
|
|
/// <summary>
|
|
/// Tests for <see cref="LiveOddsPoller"/>.
|
|
/// Uses real <see cref="PullLiveOddsUseCase"/> backed by NSubstitute interfaces,
|
|
/// so the poller's DI scope resolution is exercised end-to-end.
|
|
/// </summary>
|
|
public sealed class LiveOddsPollerTests
|
|
{
|
|
private static readonly TimeSpan MoscowOffset = TimeSpan.FromHours(3);
|
|
|
|
private static OddsSnapshot MakeSnapshot(EventId eventId) =>
|
|
new(eventId, DateTimeOffset.UtcNow, OddsSource.Live, new List<Bet>
|
|
{
|
|
new(MatchScope.Instance, BetType.Win, Side.Side1, null, new OddsRate(1.80m)),
|
|
});
|
|
|
|
/// <summary>
|
|
/// Builds a DI ServiceProvider wiring a real <see cref="PullLiveOddsUseCase"/>
|
|
/// against the provided interface substitutes.
|
|
/// </summary>
|
|
private static IServiceProvider BuildServiceProvider(
|
|
IOddsScraper scraper,
|
|
IEventRepository eventRepo,
|
|
ISnapshotRepository snapshotRepo)
|
|
{
|
|
var services = new ServiceCollection();
|
|
services.AddScoped(_ => scraper);
|
|
services.AddScoped(_ => eventRepo);
|
|
services.AddScoped(_ => snapshotRepo);
|
|
services.AddScoped(sp =>
|
|
new PullLiveOddsUseCase(
|
|
sp.GetRequiredService<IOddsScraper>(),
|
|
sp.GetRequiredService<IEventRepository>(),
|
|
sp.GetRequiredService<ISnapshotRepository>(),
|
|
StaticThrottle(),
|
|
NullLogger<PullLiveOddsUseCase>.Instance));
|
|
|
|
return services.BuildServiceProvider();
|
|
}
|
|
|
|
private static IOptionsMonitor<ScrapingThrottle> StaticThrottle()
|
|
{
|
|
var monitor = Substitute.For<IOptionsMonitor<ScrapingThrottle>>();
|
|
monitor.CurrentValue.Returns(new ScrapingThrottle { MaxConcurrentRequests = 1 });
|
|
return monitor;
|
|
}
|
|
|
|
private static IOptionsMonitor<WorkerOptions> BuildOptions(
|
|
bool enabled = true,
|
|
int intervalSeconds = 0)
|
|
{
|
|
var opts = new WorkerOptions
|
|
{
|
|
LivePollerEnabled = enabled,
|
|
LivePollIntervalSeconds = intervalSeconds,
|
|
};
|
|
var monitor = Substitute.For<IOptionsMonitor<WorkerOptions>>();
|
|
monitor.CurrentValue.Returns(opts);
|
|
return monitor;
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Should_InvokeUseCase_When_PollerIsEnabled()
|
|
{
|
|
// Arrange — scraper returns 1 event; snapshot succeeds
|
|
var eventId = new EventId("10000001");
|
|
var ev = new Event(eventId, new SportCode(6), "BY", "league-1", "Group",
|
|
new DateTimeOffset(2026, 5, 10, 18, 0, 0, MoscowOffset), "A", "B");
|
|
|
|
var scraper = Substitute.For<IOddsScraper>();
|
|
var eventRepo = Substitute.For<IEventRepository>();
|
|
var snapshotRepo = Substitute.For<ISnapshotRepository>();
|
|
|
|
// Use case discovers live events via ScrapeLiveAsync (NOT ListAsync)
|
|
scraper.ScrapeLiveAsync(Arg.Any<CancellationToken>())
|
|
.Returns(new List<Event> { ev }.AsReadOnly());
|
|
eventRepo.GetAsync(eventId, Arg.Any<CancellationToken>())
|
|
.Returns(ev);
|
|
scraper.ScrapeEventOddsAsync(
|
|
Arg.Is<Event>(e => e.Id == eventId), OddsSource.Live, Arg.Any<CancellationToken>())
|
|
.Returns(MakeSnapshot(eventId));
|
|
|
|
var sp = BuildServiceProvider(scraper, eventRepo, snapshotRepo);
|
|
var opts = BuildOptions(enabled: true, intervalSeconds: 0);
|
|
var poller = new LiveOddsPoller(sp, opts, NullLogger<LiveOddsPoller>.Instance);
|
|
|
|
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(3));
|
|
|
|
// Act
|
|
await poller.StartAsync(cts.Token);
|
|
await Task.Delay(300); // allow at least one cycle
|
|
await poller.StopAsync(CancellationToken.None);
|
|
|
|
// Assert — snapshot was added at least once
|
|
await snapshotRepo.Received().AddAsync(Arg.Any<OddsSnapshot>(), Arg.Any<CancellationToken>());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Should_NotInvokeUseCase_When_PollerIsDisabled()
|
|
{
|
|
// Arrange
|
|
var scraper = Substitute.For<IOddsScraper>();
|
|
var eventRepo = Substitute.For<IEventRepository>();
|
|
var snapshotRepo = Substitute.For<ISnapshotRepository>();
|
|
|
|
var sp = BuildServiceProvider(scraper, eventRepo, snapshotRepo);
|
|
var opts = BuildOptions(enabled: false, intervalSeconds: 0);
|
|
var poller = new LiveOddsPoller(sp, opts, NullLogger<LiveOddsPoller>.Instance);
|
|
|
|
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(2));
|
|
|
|
// Act
|
|
await poller.StartAsync(cts.Token);
|
|
await Task.Delay(300);
|
|
await poller.StopAsync(CancellationToken.None);
|
|
|
|
// Assert — no snapshot attempts while disabled
|
|
await snapshotRepo.DidNotReceive().AddAsync(Arg.Any<OddsSnapshot>(), Arg.Any<CancellationToken>());
|
|
await scraper.DidNotReceive().ScrapeLiveAsync(Arg.Any<CancellationToken>());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Should_ContinueRunning_When_UseCaseThrowsException()
|
|
{
|
|
// Arrange — ListAsync always throws; poller must survive
|
|
var scraper = Substitute.For<IOddsScraper>();
|
|
var eventRepo = Substitute.For<IEventRepository>();
|
|
var snapshotRepo = Substitute.For<ISnapshotRepository>();
|
|
|
|
// Use case calls ScrapeLiveAsync first; if it throws, the cycle returns 0
|
|
// without hitting the repo. To exercise the "poller survives failures"
|
|
// path, make ScrapeLiveAsync itself throw.
|
|
scraper.ScrapeLiveAsync(Arg.Any<CancellationToken>())
|
|
.ThrowsAsync(new InvalidOperationException("listing unavailable"));
|
|
|
|
var sp = BuildServiceProvider(scraper, eventRepo, snapshotRepo);
|
|
var opts = BuildOptions(enabled: true, intervalSeconds: 0);
|
|
var poller = new LiveOddsPoller(sp, opts, NullLogger<LiveOddsPoller>.Instance);
|
|
|
|
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(2));
|
|
|
|
// Act — start, let failures occur, stop cleanly
|
|
await poller.StartAsync(cts.Token);
|
|
await Task.Delay(400);
|
|
|
|
var stopAct = async () => await poller.StopAsync(CancellationToken.None);
|
|
|
|
// Assert — StopAsync must not propagate the exception
|
|
await stopAct.Should().NotThrowAsync();
|
|
|
|
// Scraper was hit at least once (poller cycled at least one iteration).
|
|
// The use case swallows ScrapeLiveAsync exceptions and returns 0, so the
|
|
// poller's catch block is not triggered — but it still cycles.
|
|
await scraper.Received().ScrapeLiveAsync(Arg.Any<CancellationToken>());
|
|
}
|
|
}
|