diff --git a/src/client/spark_test/System/IcebergAlgoTests.cs b/src/client/spark_test/System/IcebergAlgoTests.cs index e91ca574f..4f7bf1008 100644 --- a/src/client/spark_test/System/IcebergAlgoTests.cs +++ b/src/client/spark_test/System/IcebergAlgoTests.cs @@ -34,7 +34,6 @@ namespace bts.tests { ServiceContext _context; AdminService _admin_service; OrderService _order_service; - Update _orders; Update _fills; public IcebergAlgoTests() { @@ -46,9 +45,6 @@ namespace bts.tests { _context, _admin_service, _order_service, - _order_service.subscribei_orders().Subscribe(ords => { - _orders = ords; - }), _order_service.subscribei_fills().Subscribe(phills => { // lol >8) @@ -146,22 +142,29 @@ namespace bts.tests { var ask_order_def = OrderDefinitionUtilities.create_limit_order_def(s.Value.Id, Side.Sell, test_account, test_user); orders_response_list.Add(await OrderUtilities.send_order(_order_service, bid_order_def, 55.00, 50)); orders_response_list.Add(await OrderUtilities.send_order(_order_service, ask_order_def, 55.05, 50)); - await Task.Delay(new TimeSpan(0, 0, 3)); List test_orders = new List(); foreach (var i in orders_response_list) { - var iret = await OrderUtilities.get_filtered_orders(_orders, x => x.Key == i.value.OrderKey); - Assert.IsTrue(iret.Count() > 0 && iret.First().State.Status == OrderStatus.Working, "This order should have been 'working'"); + var iret = (await OrderUtilities.wait_for_filtered_orders( + _order_service, + rows => rows.Where(o => o.Key == i.value.OrderKey).Count() == 1, + rows => rows.Where(o => o.Key == i.value.OrderKey))).result ?? Enumerable.Empty(); + Assert.IsTrue(iret.Count() == 1 && iret.First().State.Status == OrderStatus.Working, "This order should have been 'working'"); test_orders.AddRange(iret); } // 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_result = await OrderUtilities.send_order(_order_service, ib_order_def, 55.05, 30); - await Task.Delay(new TimeSpan(0, 0, 6)); - var ib_filtered_order = await OrderUtilities.get_filtered_orders(_orders, x => x.Key == ib_order_result.value.OrderKey); - var ib_child_orders = await OrderUtilities.get_filtered_orders(_orders, x => x.ParentKey == ib_order_result.value.OrderKey, 10); + var ib_filtered_order = (await OrderUtilities.wait_for_filtered_orders( + _order_service, + rows => rows.Where(x => x.Key == ib_order_result.value.OrderKey && x.HasState && x.State.Status == OrderStatus.Completed).Count() == 1, + rows => rows.Where(x => x.Key == ib_order_result.value.OrderKey))).result ?? Enumerable.Empty(); + var ib_child_orders = (await OrderUtilities.wait_for_filtered_orders( + _order_service, + rows => rows.Where(x => x.ParentKey == ib_order_result.value.OrderKey && x.HasState && x.State.Status == OrderStatus.Completed).Count() == 10, + rows => rows.Where(x => x.ParentKey == ib_order_result.value.OrderKey))).result ?? Enumerable.Empty(); - Assert.IsTrue(ib_filtered_order.Count() > 0 && ib_filtered_order.First().State.Status == OrderStatus.Completed, "This IB order should have been 'Completed'"); + 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"); diff --git a/src/client/spark_test/Utils/OrderUtilities.cs b/src/client/spark_test/Utils/OrderUtilities.cs index fe099deba..fd1184938 100644 --- a/src/client/spark_test/Utils/OrderUtilities.cs +++ b/src/client/spark_test/Utils/OrderUtilities.cs @@ -13,6 +13,7 @@ using bts.api.orderman; using mbts; using mbts.tests; using bts.co2; +using bts.tests.utils; namespace bts.tests { @@ -44,24 +45,18 @@ namespace bts.tests } } - public static async Task> get_filtered_orders(Update orders, Func predicate, int count = 1) { - int current_count = 0; - int it = 0; - do { - if (orders != null) { - var ords = orders.all.Where(predicate); - current_count = ords.Count(); - - if (current_count >= count) { - return ords; - } - it++; - } - await Task.Delay(100); + public 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); } - while (it < 100 && (orders == null || current_count < count)); - - return new List(); + var tcs = new TaskCompletionSource>(); + var s1 = order_service.subscribei_orders().Subscribe(update => { + if (match(update.all)) { + tcs.TrySetResult(selector(update.all)); + } + }) ; + return tcs.Task.timeout(timeout.Value); } } diff --git a/src/client/spark_test/Utils/TestUtils.cs b/src/client/spark_test/Utils/TestUtils.cs index f469031f3..71669a50f 100644 --- a/src/client/spark_test/Utils/TestUtils.cs +++ b/src/client/spark_test/Utils/TestUtils.cs @@ -2,9 +2,10 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Threading.Tasks; namespace bts.tests.utils { - static class TestUtils { + public static class TestUtils { public static List randomize_values(IReadOnlyList input) { var remaining_indices = Enumerable.Range(0, input.Count).ToList(); var rand = new Random(); @@ -38,5 +39,26 @@ namespace bts.tests.utils { } return reshuffled; } + + // this was copied from GlobalExtensions + public struct TimeoutResult { + public TimeoutResult(T result, TimeSpan ts) { + wait_duration = ts; + this.result = result; + } + public readonly TimeSpan wait_duration; + public readonly T result; + } + + // this was copied from GlobalExtensions + public static async Task> timeout(this Task task, TimeSpan timeout) { + System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); + sw.Start(); + var timeout_task = Task.Delay(timeout); + var completed = await Task.WhenAny(task, timeout_task); + return (completed == timeout_task) ? + new TimeoutResult(default(T), timeout) : + new TimeoutResult(task.Result, sw.Elapsed); + } } } \ No newline at end of file