Пример #1
0
        public void CreateTimeSeriesSubscriptionTypesTest()
        {
            //create default endpoint
            DXEndpoint.Create();

            //try to create subscription with invalid event types parameters
            //all attempts to create subscription in this block must be failed with exception
            Assert.Catch(typeof(ArgumentException), () =>
            {
                DXFeed.GetInstance().CreateTimeSeriesSubscription <IDxTimeSeriesEvent>(typeof(IDxCandle), typeof(IDxQuote));
            });
            Assert.Catch(typeof(ArgumentException), () =>
            {
                DXFeed.GetInstance().CreateTimeSeriesSubscription <IDxTimeSeriesEvent>(typeof(IDxOrder), typeof(IDxQuote));
            });
            Assert.Catch(typeof(ArgumentException), () =>
            {
                DXFeed.GetInstance().CreateTimeSeriesSubscription <IDxTimeSeriesEvent>(typeof(string));
            });

            var symbol = "SYMA";
            var s      = DXFeed.GetInstance().CreateTimeSeriesSubscription <IDxTimeSeriesEvent>(typeof(IDxCandle), typeof(IDxGreeks));

            s.AddSymbols(symbol);
            TestListener eventListener = new TestListener();

            s.AddEventListener(eventListener);

            EventPlayer <IDxTimeSeriesEvent> eventPlayer = new EventPlayer <IDxTimeSeriesEvent>(s as DXFeedSubscription <IDxTimeSeriesEvent>);
            var playedCandle = new PlayedCandle(symbol, Tools.DateToUnixTime(DateTime.Now), 123, 100, 12.34, 56.78, 9.0, 43.21, 1000, 999, 1001, 1002, 1, 777, 888, EventFlag.RemoveSymbol);

            eventPlayer.PlayEvents(symbol, playedCandle);
            var playedGreeks = new PlayedGreeks(symbol, EventFlag.RemoveSymbol, 1, Tools.DateToUnixTime(DateTime.Now), 456.789, 11, 555, 666, 777, 888, 999);

            eventPlayer.PlayEvents(symbol, playedGreeks);

            Assert.AreEqual(eventListener.GetEventCount <IDxCandle>(symbol), 1);
            Assert.AreEqual(eventListener.GetEventCount <IDxGreeks>(symbol), 1);

            //thread-safety case
            DXEndpoint.Create();
            Parallel.For(ParallelFrom, ParallelTo, i =>
            {
                Assert.DoesNotThrow(() =>
                {
                    DXFeed.GetInstance().CreateTimeSeriesSubscription <IDxTimeSeriesEvent>(typeof(IDxCandle), typeof(IDxGreeks));
                });
            });
        }
Пример #2
0
        public void GetTimeSeriesPromiseTest()
        {
            //create default endpoint
            DXEndpoint.Create();
            var symbol = "SYMA";

            //try to create promise with invalid parameters
            Assert.Catch(typeof(ArgumentException), () =>
            {
                try
                {
                    DXFeed.GetInstance().GetTimeSeriesPromise <IDxGreeks>(null, 0, 0, CancellationToken.None).Wait();
                }
                catch (AggregateException ae)
                {
                    foreach (var inner in ae.InnerExceptions)
                    {
                        throw inner;
                    }
                }
            });

            //try to cancel promise
            CancellationTokenSource  cancelSource = new CancellationTokenSource();
            Task <List <IDxGreeks> > promise      = DXFeed.GetInstance().GetTimeSeriesPromise <IDxGreeks>(symbol, 0, 0, cancelSource.Token);

            cancelSource.CancelAfter(TimeSpan.FromSeconds(2));
            try
            {
                Task.WaitAll(promise);
            }
            catch (AggregateException ae)
            {
                foreach (var inner in ae.InnerExceptions)
                {
                    if (!(inner is OperationCanceledException))
                    {
                        Assert.Fail("Unexpected exception: " + inner);
                    }
                }
            }

            //try wait promise with timeout
            cancelSource = new CancellationTokenSource(TimeSpan.FromSeconds(2));
            promise      = DXFeed.GetInstance().GetTimeSeriesPromise <IDxGreeks>(symbol, 0, 0, cancelSource.Token);
            try
            {
                Task.WaitAll(promise);
            }
            catch (AggregateException ae)
            {
                foreach (var inner in ae.InnerExceptions)
                {
                    if (!(inner is OperationCanceledException))
                    {
                        Assert.Fail("Unexpected exception: " + inner);
                    }
                }
            }

            //try close endpoint while promise waiting
            Task closeEndpointTask = Task.Run(() =>
            {
                Thread.Sleep(TimeSpan.FromSeconds(2));
                DXEndpoint.GetInstance().Close();
            });

            promise = DXFeed.GetInstance().GetTimeSeriesPromise <IDxGreeks>(symbol, 0, 0, CancellationToken.None);
            try
            {
                Task.WaitAll(promise, closeEndpointTask);
            }
            catch (AggregateException ae)
            {
                foreach (var inner in ae.InnerExceptions)
                {
                    if (!(inner is OperationCanceledException))
                    {
                        Assert.Fail("Unexpected exception: " + inner);
                    }
                }
            }

            //try to get promise on closed endpoint
            promise = DXFeed.GetInstance().GetTimeSeriesPromise <IDxGreeks>(symbol, 0, 0, CancellationToken.None);
            try
            {
                Task.WaitAll(promise);
            }
            catch (AggregateException ae)
            {
                foreach (var inner in ae.InnerExceptions)
                {
                    if (!(inner is OperationCanceledException))
                    {
                        Assert.Fail("Unexpected exception: " + inner);
                    }
                }
            }

            //try to get last event succesfully
            DateTime date         = DateTime.Now;
            var      playedGreeks = new PlayedGreeks[] {
                new PlayedGreeks(symbol, 0, 5, Tools.DateToUnixTime(date), 156.789, 111, 155, 166, 177, 188, 199),
                new PlayedGreeks(symbol, 0, 4, Tools.DateToUnixTime(date.AddMinutes(-1)), 256.789, 211, 255, 266, 277, 288, 299),
                new PlayedGreeks(symbol, 0, 3, Tools.DateToUnixTime(date.AddMinutes(-2)), 356.789, 311, 355, 366, 377, 388, 399),
                new PlayedGreeks(symbol, 0, 2, Tools.DateToUnixTime(date.AddMinutes(-3)), 456.789, 411, 455, 466, 477, 488, 499),
                new PlayedGreeks(symbol, 0, 1, Tools.DateToUnixTime(date.AddMinutes(-4)), 556.789, 511, 555, 566, 577, 588, 599),
            };
            var expectedGreeks = new PlayedGreeks[] {
                new PlayedGreeks(symbol, 0, 4, Tools.DateToUnixTime(date.AddMinutes(-1)), 256.789, 211, 255, 266, 277, 288, 299),
                new PlayedGreeks(symbol, 0, 3, Tools.DateToUnixTime(date.AddMinutes(-2)), 356.789, 311, 355, 366, 377, 388, 399),
                new PlayedGreeks(symbol, 0, 2, Tools.DateToUnixTime(date.AddMinutes(-3)), 456.789, 411, 455, 466, 477, 488, 499)
            };
            Task eventPlayerTask = Task.Run(() =>
            {
                Thread.Sleep(TimeSpan.FromSeconds(2));
                EventPlayer <IDxGreeks> eventPlayer = new EventPlayer <IDxGreeks>(GetSubscriptionFromFeed <IDxGreeks>(symbol));
                eventPlayer.PlaySnapshot(symbol, playedGreeks);
            });

            cancelSource = new CancellationTokenSource(TimeSpan.FromSeconds(10));
            promise      = DXEndpoint.Create().Feed.GetTimeSeriesPromise <IDxGreeks>(symbol, Tools.DateToUnixTime(date.AddMinutes(-3)), Tools.DateToUnixTime(date.AddMinutes(-1)), cancelSource.Token);
            Assert.DoesNotThrow(() =>
            {
                Task.WaitAll(promise, eventPlayerTask);
            });

            var receivedGreeks = promise.Result;

            receivedGreeks.Reverse();
            Assert.AreEqual(expectedGreeks.Length, receivedGreeks.Count);
            for (int i = 0; i < expectedGreeks.Length; i++)
            {
                Assert.AreEqual(symbol, receivedGreeks[i].EventSymbol.ToString());
                CompareGreeks(expectedGreeks[i], receivedGreeks[i]);
            }

            //thread-safety case
            DXEndpoint.Create();
            List <Task> tasks = new List <Task>();

            for (int i = 0; i < 10; i++)
            {
                var threadSymbol       = symbol + i.ToString();
                var threadPlayedGreeks = new PlayedGreeks[] {
                    new PlayedGreeks(symbol, 0, 5, Tools.DateToUnixTime(date), 156.789, 111, 155, 166, 177, 188, 199),
                    new PlayedGreeks(symbol, 0, 4, Tools.DateToUnixTime(date.AddMinutes(-1)), 256.789, 211, 255, 266, 277, 288, 299),
                    new PlayedGreeks(symbol, 0, 3, Tools.DateToUnixTime(date.AddMinutes(-2)), 356.789, 311, 355, 366, 377, 388, 399),
                    new PlayedGreeks(symbol, 0, 2, Tools.DateToUnixTime(date.AddMinutes(-3)), 456.789, 411, 455, 466, 477, 488, 499),
                    new PlayedGreeks(symbol, 0, 1, Tools.DateToUnixTime(date.AddMinutes(-4)), 556.789, 511, 555, 566, 577, 588, 599),
                };
                tasks.Add(Task.Run(() =>
                {
                    Thread.Sleep(TimeSpan.FromSeconds(2));
                    var eventPlayer = new EventPlayer <IDxGreeks>(GetSubscriptionFromFeed <IDxGreeks>(threadSymbol));
                    eventPlayer.PlaySnapshot(threadSymbol, threadPlayedGreeks);
                }));
                var threadPromise = DXEndpoint.GetInstance().Feed
                                    .GetTimeSeriesPromise <IDxGreeks>(threadSymbol, Tools.DateToUnixTime(date.AddMinutes(-3)), Tools.DateToUnixTime(date.AddMinutes(-1)), new CancellationTokenSource(TimeSpan.FromSeconds(20)).Token)
                                    .ContinueWith((resultPromise) =>
                {
                    foreach (var g in resultPromise.Result)
                    {
                        Assert.AreEqual(threadSymbol, g.EventSymbol);
                    }
                });
                tasks.Add(threadPromise);
            }
            ;
            Assert.DoesNotThrow(() =>
            {
                Task.WaitAll(tasks.ToArray());
            });
        }