Created
June 14, 2021 23:03
-
-
Save Kikimora/47e1ab499bbb8cee1518ed0ba3a7e9f3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Net; | |
using System.Net.Http; | |
using System.Net.Http.Json; | |
using System.Text.RegularExpressions; | |
using System.Threading.Tasks; | |
using Google.Apis.Http; | |
using Google.Apis.Sheets.v4.Data; | |
using Microsoft.Extensions.Configuration; | |
using Microsoft.Extensions.Options; | |
using NUnit.Framework; | |
using PMPA.Monitoring; | |
using Qoden.Test.Util; | |
using IHttpClientFactory = Google.Apis.Http.IHttpClientFactory; | |
namespace PMPA.Test | |
{ | |
[TestFixture] | |
public class ExchangeMonitorTest : PmpaFixture, IHttpClientFactory, IObserver<StubHttpRequest> | |
{ | |
private IDisposable _stubSubscription; | |
private ExchangeMonitor _monitor; | |
private ExchangeMonitoringOptions _options; | |
private MonitoringSheet _monitoringSheet; | |
private StubHttpMessageHandler _httpStub; | |
private HttpResponseMessage _exchangesResponse; | |
private HttpResponseMessage _symbolsResponse; | |
private HttpResponseMessage _sheetUpdateResponse; | |
private Dictionary<string, HttpResponseMessage> _orderBookResponses; | |
[SetUp] | |
public void SetUp() | |
{ | |
_exchangesResponse = new() | |
{ | |
Content = JsonContent.Create(new ValueRange | |
{ | |
Range = "", | |
MajorDimension = "ROWS", | |
Values = new IList<object>[] | |
{ | |
new List<object> {"Exchange", "Url"}, | |
new List<object> {"Test Exchange 1", "https://test1.exchange"}, | |
new List<object> {"Test Exchange 2", "https://test2.exchange"}, | |
} | |
}) | |
}; | |
_symbolsResponse = new() | |
{ | |
Content = JsonContent.Create(new ValueRange | |
{ | |
Range = "", | |
MajorDimension = "ROWS", | |
Values = new IList<object>[] | |
{ | |
new List<object> {"Exchange", "Symbol", "Status", "Interval", "Book Version", "State", "Last Update"}, | |
new List<object> {"Test Exchange 1", "btc_usdt", "On", "00:00:10", "0", "Ok", "2020-10-10"}, | |
new List<object> {"Test Exchange 2", "eth_usdt", "Off", "00:00:10", "1", "Ok", "2020-10-10"}, | |
} | |
}) | |
}; | |
_orderBookResponses = new Dictionary<string, HttpResponseMessage> | |
{ | |
{ | |
"btc_usdt", new() | |
{ | |
Content = JsonContent.Create(new OrderBook(1, true)) | |
} | |
}, | |
{ | |
"eth_usdt", new() | |
{ | |
Content = JsonContent.Create(new OrderBook(1, true)) | |
} | |
} | |
}; | |
_sheetUpdateResponse = new HttpResponseMessage(HttpStatusCode.OK); | |
_httpStub = new StubHttpMessageHandler(); | |
_stubSubscription = _httpStub.Requests.Subscribe(this); | |
_options = Configuration.GetSection("ExchangeMonitoring").Get<ExchangeMonitoringOptions>(); | |
_monitoringSheet = new MonitoringSheet(_options, this); | |
_monitor = new ExchangeMonitor(this, Options.Create(_options)) | |
{ | |
MonitoringSheetFactory = _ => _monitoringSheet | |
}; | |
} | |
[TearDown] | |
public void TearDown() | |
{ | |
_stubSubscription?.Dispose(); | |
} | |
[Test] | |
public async Task Execute() | |
{ | |
await _monitor.ExecuteAsync(); | |
} | |
/// <summary> | |
/// Override Google HTTP client with a stub one. | |
/// </summary> | |
public ConfigurableHttpClient CreateHttpClient(CreateHttpClientArgs args) | |
{ | |
return new(new ConfigurableMessageHandler(_httpStub)); | |
} | |
/// <summary> | |
/// Override <see cref="ExchangeMonitor"/> client with a stub one. | |
/// </summary> | |
public override HttpClient CreateClient(string name) | |
{ | |
return new(_httpStub, false); | |
} | |
/// <summary> | |
/// Generate test responses. | |
/// </summary> | |
void IObserver<StubHttpRequest>.OnNext(StubHttpRequest value) | |
{ | |
var path = value.Message.RequestUri!.PathAndQuery; | |
var method = value.Message.Method.Method; | |
var response = method switch | |
{ | |
"GET" when Regex.IsMatch(path, @".*values/Exchanges.*") => _exchangesResponse, | |
"GET" when Regex.IsMatch(path, @".*values/Symbols.*") => _symbolsResponse, | |
"POST" when Regex.IsMatch(path, @".*values:batch.*") => _sheetUpdateResponse, | |
"GET" when @".*/marketdata/api/v2/marketdata/depth/(.+)".TryMatch(path, out var match) => | |
GetOrderBookResponse(match), | |
_ => BadRequest | |
}; | |
value.Respond(response); | |
HttpResponseMessage GetOrderBookResponse(Match match) | |
{ | |
var symbol = match.Groups[1].Value; | |
return _orderBookResponses.TryGetValue(symbol, out var book) ? book : BadRequest; | |
} | |
} | |
void IObserver<StubHttpRequest>.OnCompleted() | |
{ | |
} | |
void IObserver<StubHttpRequest>.OnError(Exception error) | |
{ | |
} | |
private static readonly HttpResponseMessage BadRequest = new(HttpStatusCode.BadRequest) | |
{ | |
Content = new StringContent("Unexpected request.") | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment