MarketDataService wires the collector, the reference sources, the universes and the corporate actions into the scheduler. Short jobs (rates, calendar, mood, spread samples, corporate actions) run on their schedule; long downloads (catalogues, the first history, thousands of stocks, the Binance archives) run on a background worker the jobs only wake, so they never delay the backup or each other. The live crypto feed follows the crypto universe, or BTC, ETH and SOL until one exists. Alpaca uses only the paper account keys; without them the stock part is off and the sources list says why. Market data is off in demo mode, with ANUBIS_MARKET_DATA=off, and in tests that bring no fake network. New: GET /api/data/quality (JSON or Markdown) and /api/data/sources, and "anubis data report|binance|csv" on the command line. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
49ed73e6f6
commit
1e71038209
10 files changed
+898
-6
No files matched your search
@@ -0,0 +1,67 @@
|
||||
using System.Net;
|
||||
using System.Text.Json;
|
||||
using Anubis.Data;
|
||||
using Anubis.Server;
|
||||
|
||||
namespace Anubis.Tests;
|
||||
|
||||
/// <summary>Market data inside the running server, on a fake network (issues #19, #20, #22).</summary>
|
||||
[TestClass]
|
||||
public sealed class MarketDataServerTests
|
||||
{
|
||||
[TestMethod]
|
||||
[TestCategory(Category.Integration)]
|
||||
public async Task ServerCollectsWithoutAlpacaKeysAndSaysWhatIsOff()
|
||||
{
|
||||
var empty = """{"code":"0","msg":"","data":[]}""";
|
||||
var network = new FakeHttp()
|
||||
.On("/api/v5/public/instruments?instType=SPOT", Fixture.Text("okx/instruments-spot.json"))
|
||||
.On("candles", empty)
|
||||
.On("/market/ticker?", Fixture.Text("okx/ticker-btc-eur.json"))
|
||||
.On(_ => true, _ => FakeHttp.Respond("not here", HttpStatusCode.NotFound));
|
||||
await using var server = await ServerHarness.StartAsync("docker", ServerHarness.DockerSqlite(), marketHttp: network);
|
||||
await server.SignInFirstTimeAsync();
|
||||
|
||||
var upcoming = server.Host.Services!.Scheduler.Upcoming(100).Select(u => u.Name).ToList();
|
||||
CollectionAssert.IsSubsetOf(new[] { "backup", "dati-storico", "dati-cambi-mattina", "dati-cambi-pomeriggio", "dati-calendario", "dati-umore", "dati-spread" },
|
||||
upcoming);
|
||||
Assert.IsFalse(upcoming.Contains("dati-azioni-chiusura"), "without the broker's calendar the stock jobs have no time");
|
||||
|
||||
// The first history round runs at start: OKX answers, Alpaca is off for lack of the paper account keys.
|
||||
JsonElement sources = default;
|
||||
await OkxTests.WaitForAsync(() =>
|
||||
{
|
||||
sources = JsonDocument.Parse(server.TextAsync("/api/data/sources").GetAwaiter().GetResult()).RootElement;
|
||||
return sources.GetProperty("sources").EnumerateArray().Any(s => s.GetProperty("source").GetString() == MarketDataService.HistorySource);
|
||||
}).ConfigureAwait(false);
|
||||
Assert.IsTrue(sources.GetProperty("enabled").GetBoolean());
|
||||
var bySource = sources.GetProperty("sources").EnumerateArray().ToDictionary(s => s.GetProperty("source").GetString()!);
|
||||
Assert.IsFalse(bySource[MarketDataService.AlpacaSource].GetProperty("enabled").GetBoolean());
|
||||
StringAssert.Contains(bySource[MarketDataService.AlpacaSource].GetProperty("note").GetString(), "ALPACA_API_KEY_DEMO");
|
||||
StringAssert.Contains(bySource[MarketDataService.OkxSource].GetProperty("note").GetString(), "3 nuovi");
|
||||
Assert.AreEqual(JsonValueKind.Null, bySource[MarketDataService.OkxSource].GetProperty("lastError").ValueKind);
|
||||
Assert.IsTrue(network.Urls.All(u => u.Host is "eea.okx.com" or "data.binance.vision" or "nfs.faireconomy.media" or "www.ecb.europa.eu"
|
||||
or "api.alternative.me" or "cdn.cboe.com"), string.Join(", ", network.Urls.Select(u => u.Host).Distinct()));
|
||||
Assert.IsFalse(network.Urls.Any(u => u.Host.Contains("alpaca", StringComparison.Ordinal)), "no call to Alpaca without keys");
|
||||
|
||||
var report = await server.TextAsync("/api/data/quality?format=md");
|
||||
StringAssert.Contains(report, "# Qualità dei dati");
|
||||
StringAssert.Contains(report, "| alpaca | no |");
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[TestCategory(Category.Integration)]
|
||||
public async Task MarketDataStaysOffWhenSwitchedOff()
|
||||
{
|
||||
var env = ServerHarness.DockerSqlite();
|
||||
env["ANUBIS_MARKET_DATA"] = "off";
|
||||
var network = new FakeHttp();
|
||||
await using var server = await ServerHarness.StartAsync("docker", env, marketHttp: network);
|
||||
await server.SignInFirstTimeAsync();
|
||||
Assert.IsNull(server.Host.Services!.MarketData);
|
||||
Assert.IsFalse(server.Host.Services.Scheduler.Upcoming(100).Any(u => u.Name.StartsWith("dati-", StringComparison.Ordinal)));
|
||||
var sources = JsonDocument.Parse(await server.TextAsync("/api/data/sources")).RootElement;
|
||||
Assert.IsFalse(sources.GetProperty("enabled").GetBoolean());
|
||||
Assert.AreEqual(0, network.Requests.Count);
|
||||
}
|
||||
}
|
||||
@@ -76,7 +76,7 @@ internal sealed partial class ServerHarness : IAsyncDisposable
|
||||
}
|
||||
|
||||
public static async Task<ServerHarness> StartAsync(string platform, IReadOnlyDictionary<string, string?> environment, string? dataDir = null,
|
||||
string[]? extraArgs = null, bool waitReady = true)
|
||||
string[]? extraArgs = null, bool waitReady = true, HttpMessageHandler? marketHttp = null)
|
||||
{
|
||||
var owns = dataDir is null;
|
||||
dataDir ??= TempFolder.Create("server");
|
||||
@@ -86,6 +86,9 @@ internal sealed partial class ServerHarness : IAsyncDisposable
|
||||
Environment = environment,
|
||||
PasswordIterations = 1000,
|
||||
DatabaseCheckInterval = TimeSpan.FromMilliseconds(100),
|
||||
MarketData = marketHttp is not null,
|
||||
MarketHttp = marketHttp,
|
||||
MarketSockets = marketHttp is null ? null : () => new FakeSocket(),
|
||||
});
|
||||
await app.StartAsync().ConfigureAwait(false);
|
||||
var cookies = new CookieContainer();
|
||||
|
||||
Reference in new issue
Block a user