From 0e92a12e65fc6ddebebeab8895a87c11696a1bc8 Mon Sep 17 00:00:00 2001 From: Jake Norbut Date: Mon, 11 Mar 2019 10:08:13 -0500 Subject: [PATCH] refactoring/reorg --- .../spark_test/System/IcebergAlgoTests.cs | 54 +++---------------- src/client/spark_test/Utils/OrderUtilities.cs | 50 +++++++++++++++-- src/client/spark_test/Utils/TestUtils.cs | 11 +++- 3 files changed, 64 insertions(+), 51 deletions(-) diff --git a/src/client/spark_test/System/IcebergAlgoTests.cs b/src/client/spark_test/System/IcebergAlgoTests.cs index 4f7bf1008..645a8700d 100644 --- a/src/client/spark_test/System/IcebergAlgoTests.cs +++ b/src/client/spark_test/System/IcebergAlgoTests.cs @@ -7,34 +7,20 @@ using System.Threading.Tasks; using bts.api; using bts.api.admin; using bts.api.core; -using bts.api.test; using bts.test; using bts.api.orderman; using mbts; -using mbts.tests; using bts.co2; +using bts.tests.utils; namespace bts.tests { - static class TestUtils { - public static bool test_price_equality(double price_1, double price_2, double tick_size) { - return Math.Abs(price_2 - price_1) < 0.01 * tick_size; - } - - // this is a copy from TickUtils -- ask what's legal for the spark_test project... - public static double ticksize(this ContractSpecificationImmutable spec, double price) => - Math.Abs(price) > spec.PriceBreak ? spec.HighTick : spec.LowTick; - - } - - [TestGroup(TestType = TestType.System)] class IcebergAlgoTests : IDisposable { List _resources; ServiceContext _context; AdminService _admin_service; OrderService _order_service; - Update _fills; public IcebergAlgoTests() { _context = new ServiceContext(); @@ -45,11 +31,6 @@ namespace bts.tests { _context, _admin_service, _order_service, - _order_service.subscribei_fills().Subscribe(phills => - { - // lol >8) - _fills = phills; - }) }; var auth = _context.login("bts", ""); @@ -94,28 +75,6 @@ namespace bts.tests { return security_with_no_market; } - public OrderDefinition create_iceberg_order_def(uint security_id, Side side, (String name, uint id) account, (String name, uint id) user) { - return new OrderDefinition { - SecurityKey = security_id, - Side = side, - TimeInForce = TimeInForce.Day, - CustomDef = new CustomOrderDefinition { - Iceberg = new IcebergOrderDefinition { - ShowQuantity = 3, - Child = new CustomOrderDefinition { - Limit = new LimitOrderDefinition { } - } - } - }, - CtiCode = CTICode.CtiCode1, - CustomerOrFirm = CustomerOrFirm.Firm, - ManualOrderIndicator = ManualOrderIndicator.Yes, - Account = account.name, - AccountId = account.id, - User = user.name, - UserId = user.id - }; - } [Test(Timeout = 25)] public async Task test_ib_create_and_takeout_market() { @@ -153,7 +112,7 @@ namespace bts.tests { } // create an IB that will cross this market - var ib_order_def = create_iceberg_order_def(s.Value.Id, Side.Buy, test_account, test_user); + var ib_order_def = OrderDefinitionUtilities.create_iceberg_order_def(s.Value.Id, Side.Buy, test_account, test_user, 3); var ib_order_result = await OrderUtilities.send_order(_order_service, ib_order_def, 55.05, 30); var ib_filtered_order = (await OrderUtilities.wait_for_filtered_orders( _order_service, @@ -165,10 +124,13 @@ namespace bts.tests { rows => rows.Where(x => x.ParentKey == ib_order_result.value.OrderKey))).result ?? Enumerable.Empty(); Assert.IsTrue(ib_filtered_order.Count() == 1, "This IB order should have been 'Completed'"); - Assert.IsTrue(ib_child_orders.Count() == 10, "There should have been 10 child orders"); - Assert.IsTrue(ib_child_orders.All(x => x.HasState && x.State.Status == OrderStatus.Completed), "All child orders should be completed"); + Assert.IsTrue(ib_child_orders.Count() == 10, "There should have been 10 completed child orders"); - var filled_childen = _fills.all.Where(x => ib_child_orders.Select(o => o.Key).Contains(x.OrderKey)); + var filled_childen = (await OrderUtilities.wait_for_filtered_fills( + _order_service, + rows => rows.Where(x => ib_child_orders.Select(o => o.Key).Contains(x.OrderKey)).Count() == 10, + rows => rows.Where(x => ib_child_orders.Select(o => o.Key).Contains(x.OrderKey)))).result ?? Enumerable.Empty(); + Assert.IsTrue(filled_childen.Count() == 10, "There should have been 10 fills"); Assert.IsTrue(filled_childen.All(x => TestUtils.test_price_equality(x.LastPrice, 55.05, s.Value.ContractSpec.ticksize(x.LastPrice)) && x.LastShares == 3), "All child fills should have been filled at price 55.50 with quantity 3"); diff --git a/src/client/spark_test/Utils/OrderUtilities.cs b/src/client/spark_test/Utils/OrderUtilities.cs index fd1184938..c345554aa 100644 --- a/src/client/spark_test/Utils/OrderUtilities.cs +++ b/src/client/spark_test/Utils/OrderUtilities.cs @@ -45,18 +45,36 @@ namespace bts.tests } } - public static Task>> + public async static Task>> wait_for_filtered_orders(OrderService order_service, Func, bool> match, Func, IEnumerable> selector, TimeSpan? timeout = null) { if (timeout == null) { timeout = TimeSpan.FromSeconds(3); } var tcs = new TaskCompletionSource>(); - var s1 = order_service.subscribei_orders().Subscribe(update => { + var sub = order_service.subscribei_orders().Subscribe(update => { if (match(update.all)) { tcs.TrySetResult(selector(update.all)); } - }) ; - return tcs.Task.timeout(timeout.Value); + }); + var tr = await tcs.Task.timeout(timeout.Value); + sub.Dispose(); + return tr; + } + + public async static Task>> + wait_for_filtered_fills(OrderService order_service, Func, bool> match, Func, IEnumerable> selector, TimeSpan? timeout = null) { + if (timeout == null) { + timeout = TimeSpan.FromSeconds(3); + } + var tcs = new TaskCompletionSource>(); + var sub = order_service.subscribei_fills().Subscribe(update => { + if (match(update.all)) { + tcs.TrySetResult(selector(update.all)); + } + }); + var tr = await tcs.Task.timeout(timeout.Value); + sub.Dispose(); + return tr; } } @@ -80,5 +98,29 @@ namespace bts.tests UserId = user.id }; } + + public static OrderDefinition create_iceberg_order_def(uint security_id, Side side, (String name, uint id) account, (String name, uint id) user, double show_quantity) { + return new OrderDefinition { + SecurityKey = security_id, + Side = side, + TimeInForce = TimeInForce.Day, + CustomDef = new CustomOrderDefinition { + Iceberg = new IcebergOrderDefinition { + ShowQuantity = show_quantity, + Child = new CustomOrderDefinition { + Limit = new LimitOrderDefinition { } + } + } + }, + CtiCode = CTICode.CtiCode1, + CustomerOrFirm = CustomerOrFirm.Firm, + ManualOrderIndicator = ManualOrderIndicator.Yes, + Account = account.name, + AccountId = account.id, + User = user.name, + UserId = user.id + }; + } } + } \ No newline at end of file diff --git a/src/client/spark_test/Utils/TestUtils.cs b/src/client/spark_test/Utils/TestUtils.cs index 71669a50f..dfdcdc5f4 100644 --- a/src/client/spark_test/Utils/TestUtils.cs +++ b/src/client/spark_test/Utils/TestUtils.cs @@ -1,4 +1,5 @@ -using bts.collections; +using bts.api; +using bts.collections; using System; using System.Collections.Generic; using System.Linq; @@ -40,6 +41,14 @@ namespace bts.tests.utils { return reshuffled; } + public static bool test_price_equality(double price_1, double price_2, double tick_size) { + return Math.Abs(price_2 - price_1) < 0.01 * tick_size; + } + + // this is a copy from TickUtils -- ask what's legal for the spark_test project... + public static double ticksize(this ContractSpecificationImmutable spec, double price) => + Math.Abs(price) > spec.PriceBreak ? spec.HighTick : spec.LowTick; + // this was copied from GlobalExtensions public struct TimeoutResult { public TimeoutResult(T result, TimeSpan ts) {