public void No_Nested_AdvanceTo()
        {
            var s = new HistoricalScheduler();

            s.Schedule(() => s.AdvanceTo(DateTimeOffset.Now.AddDays(1)));

            ReactiveAssert.Throws <InvalidOperationException>(() => s.Start());
        }
        public void No_Nested_AdvanceBy()
        {
            var s = new HistoricalScheduler();

            s.Schedule(() => s.AdvanceBy(TimeSpan.FromSeconds(1)));

            ReactiveAssert.Throws <InvalidOperationException>(() => s.Start());
        }
        public void Start_Stop()
        {
            var s = new HistoricalScheduler();

            var list = new List <Timestamped <int> >();

            var ts = TimeSpan.FromHours(1);

            s.Schedule(Time(0), () => list.Add(new Timestamped <int>(1, s.Now)));
            s.Schedule(Time(1), () => list.Add(new Timestamped <int>(2, s.Now)));
            s.Schedule(Time(2), () => s.Stop());
            s.Schedule(Time(3), () => list.Add(new Timestamped <int>(3, s.Now)));
            s.Schedule(Time(4), () => s.Stop());
            s.Schedule(Time(5), () => s.Start());
            s.Schedule(Time(6), () => list.Add(new Timestamped <int>(4, s.Now)));

            s.Start();

            Assert.Equal(Time(2), s.Now);
            Assert.Equal(Time(2), s.Clock);

            s.Start();

            Assert.Equal(Time(4), s.Now);
            Assert.Equal(Time(4), s.Clock);

            s.Start();

            Assert.Equal(Time(6), s.Now);
            Assert.Equal(Time(6), s.Clock);

            s.Start();

            Assert.Equal(Time(6), s.Now);
            Assert.Equal(Time(6), s.Clock);

            list.AssertEqual(
                new Timestamped <int>(1, Time(0)),
                new Timestamped <int>(2, Time(1)),
                new Timestamped <int>(3, Time(3)),
                new Timestamped <int>(4, Time(6))
                );
        }
示例#4
0
        public async Task T03_ObservableAll()
        {
            var tz        = DateTimeZoneProviders.Tzdb.GetZoneOrNull(ContractDetails.TimeZoneId);
            var scheduler = new HistoricalScheduler();
            var cdt       = new ContractDetailsTime(ContractDetails, scheduler);
            var task      = cdt.ContractTimeObservable.ToList().ToTask();

            scheduler.Start(); // play all
            var list = await task;

            Assert.Equal(cdt.Events.Count + 1, list.Count);
        }
        public void WithComparer()
        {
            var now = DateTimeOffset.Now;

            var s = new HistoricalScheduler(now, new ReverseComparer <DateTimeOffset>(Comparer <DateTimeOffset> .Default));

            var res = new List <int>();

            s.Schedule(now - TimeSpan.FromSeconds(1), () => res.Add(1));
            s.Schedule(now - TimeSpan.FromSeconds(2), () => res.Add(2));

            s.Start();

            Assert.True(new[] { 1, 2 }.SequenceEqual(res));
        }
示例#6
0
        static void Main(string[] args)
        {
            Console.WriteLine("{0} -> Playing...", DateTime.Now);

            //a sourcing sequence
            var sequence = Observable.Interval(TimeSpan.FromSeconds(1)).Take(5);

            var trace = sequence
                        //marks each message with a timestamp
                        .Timestamp()
                        //route messages into a list of timestamped messages
                        .ToList()
                        //materialize the list when the sequence completes
                        //and return only the list
                        .Wait();

            //a scheduler for historical records
            var scheduler = new HistoricalScheduler();

            Console.WriteLine("{0} -> Replaying...", DateTime.Now);

            //generate a new sequence from a collection
            var replay = Observable.Generate(
                //the enumerator to read values from
                trace.GetEnumerator(),
                //the condition to check until False
                x => x.MoveNext(),
                //the item
                x => x,
                //the item's value
                x => x.Current.Value,
                //the item's virtual time
                x => x.Current.Timestamp,
                //the scheduler
                scheduler);

            //some output
            replay.Subscribe(x => Console.WriteLine("{0} -> {1}", scheduler.Now, x));

            //play the record
            scheduler.Start();

            Console.ReadLine();
        }
        public void IsEnabled()
        {
            var s = new HistoricalScheduler();

            Assert.Equal(false, s.IsEnabled);

            s.Schedule(() =>
            {
                Assert.Equal(true, s.IsEnabled);
                s.Stop();
                Assert.Equal(false, s.IsEnabled);
            });

            Assert.Equal(false, s.IsEnabled);

            s.Start();

            Assert.Equal(false, s.IsEnabled);
        }
示例#8
0
        public void HistoricalScheduler()
        {
            // Arrange
            var baseTime = DateTimeOffset.Now;

            var scheduler = new HistoricalScheduler(baseTime);

            var expectedValues = new[]
            {
                Timestamped.Create(0L, baseTime + TimeSpan.FromSeconds(10)),
                Timestamped.Create(1L, baseTime + TimeSpan.FromSeconds(20)),
                Timestamped.Create(4L, baseTime + TimeSpan.FromSeconds(30)),
                Timestamped.Create(9L, baseTime + TimeSpan.FromSeconds(40)),
                Timestamped.Create(16L, baseTime + TimeSpan.FromSeconds(50)),
                Timestamped.Create(25L, baseTime + TimeSpan.FromSeconds(60))
            };

            var actualValues = new List <Timestamped <long> >();

            var source = Observable
                         .Interval(TimeSpan.FromSeconds(10), scheduler)
                         .Select(x => x * x)
                         .Take(6);

            var testSource = source
                             .Timestamp(scheduler)
                             .Do(x => actualValues.Add(x));

            // Act
            testSource.Subscribe();
            scheduler.Start();

            // Assert
            if (expectedValues.SequenceEqual(actualValues, TestDataEqualityComparer.Instance))
            {
                Console.WriteLine("The test was successful");
            }
            else
            {
                Console.WriteLine("The test failed");
            }
        }
        public void Cancellation()
        {
            var s = new HistoricalScheduler();

            var list = new List <Timestamped <int> >();

            var d = s.Schedule(Time(2), () => list.Add(new Timestamped <int>(2, s.Now)));

            s.Schedule(Time(1), () =>
            {
                list.Add(new Timestamped <int>(0, s.Now));
                d.Dispose();
            });

            s.Start();

            list.AssertEqual(
                new Timestamped <int>(0, Time(1))
                );
        }
        public void Order()
        {
            var s = new HistoricalScheduler();

            var list = new List <Timestamped <int> >();

            s.Schedule(Time(2), () => list.Add(new Timestamped <int>(2, s.Now)));

            s.Schedule(Time(3), () => list.Add(new Timestamped <int>(3, s.Now)));

            s.Schedule(Time(1), () => list.Add(new Timestamped <int>(0, s.Now)));
            s.Schedule(Time(1), () => list.Add(new Timestamped <int>(1, s.Now)));

            s.Start();

            list.AssertEqual(
                new Timestamped <int>(0, Time(1)),
                new Timestamped <int>(1, Time(1)),
                new Timestamped <int>(2, Time(2)),
                new Timestamped <int>(3, Time(3))
                );
        }