9c5d3df1f2
Implements Phase 8 Amendment 1: marathonbet.by has no public results archive
endpoint, so results must be harvested per-event by re-fetching the event
detail page until eventJsonInfo.matchIsComplete=true.
Backend changes:
* IOddsScraper:
- ScrapeResultsAsync(DateRange) replaced with ScrapeEventResultAsync(Event)
returning a nullable EventResult — null when match still in progress.
- ScrapeEventOddsAsync now takes the full Event (so EventPath drives URL
construction) instead of bare EventId.
- New ScrapeLiveAsync() for the /su/live listing.
* Domain:
- Event gains EventPath (nullable string) — the data-event-path attribute
captured during scraping; required for reliable URL construction.
* Infrastructure:
- New migration 20260506000000_AddEventPath adds the column.
- EventEntity / EventConfiguration / Mapping / model-snapshot updated.
- MarathonbetScraper: new ScrapeLiveAsync + ScrapeEventResultAsync; URL
builder prefers EventPath, falls back to numeric ID for legacy rows.
- EventListingParserBase extracts data-event-path on every listing row.
* Application:
- PullResultsUseCase: branches on selection vs date-range, emits IProgress<
PullResultsProgress>, returns ResultLoadOutcome (Loaded / AlreadyLoaded /
NotYetComplete / Failed); idempotent (skips events whose result already
exists).
- PullLiveOddsUseCase now drives off the live listing (auto-discovers
events that go live without ever appearing in the upcoming list) and
backfills EventPath on legacy rows.
- PullUpcomingEventsUseCase wires EventPath on persisted events.
* Workers: UpcomingEventsPoller updates persistence path accordingly.
* Tests: 17 net-new tests across Application + Infrastructure + Domain;
all 293 still pass.
164 lines
6.7 KiB
C#
164 lines
6.7 KiB
C#
using FluentAssertions;
|
|
using Marathon.Application.Abstractions;
|
|
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>(),
|
|
NullLogger<PullLiveOddsUseCase>.Instance));
|
|
|
|
return services.BuildServiceProvider();
|
|
}
|
|
|
|
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>());
|
|
}
|
|
}
|