示例#1
0
        public void Engine_Subject_Dispose()
        {
            var id  = new Uri("tests://bar/foo");
            var svc = new MiniService();
            var sch = new MiniScheduler();

            var subject = GetSubject(id, svc);

            var o = subject.CreateObserver();

            var xs = new List <int>();

            var s1 = subject.Subscribe(Observer.Create <int>(xs.Add, _ => { }, () => { }));

            new SubscriptionInitializeVisitor(s1).Initialize(CreateOperatorContext(new Uri("tests://qux/1"), svc, sch));

            var s2 = subject.Subscribe(Observer.Create <int>(xs.Add, _ => { }, () => { }));

            new SubscriptionInitializeVisitor(s2).Initialize(CreateOperatorContext(new Uri("tests://qux/2"), svc, sch));

            o.OnNext(42);

            subject.Dispose();

            o.OnNext(43);                                     // does not throw; by design

            var s3 = subject.Subscribe(Observer.Nop <int>()); // does not throw; by design

            o.OnNext(44);                                     // does not throw; by design

            Assert.ThrowsException <ObjectDisposedException>(() =>
            {
                new SubscriptionInitializeVisitor(s3).Initialize(CreateOperatorContext(new Uri("tests://qux/3"), svc, sch));
            });
        }
示例#2
0
        public void Engine_Subject_AsIObservable()
        {
            var id  = new Uri("tests://bar/foo");
            var svc = new MiniService();

            var subject = GetSubject(id, svc);

            var xs = new List <int>();

            var s = (ISubscription)((IObservable <int>)subject).Subscribe(xs.Add);

            var o = subject.CreateObserver();

            o.OnNext(41);

            var ctx = CreateOperatorContext(new Uri("tests://qux/1"), svc, new MiniScheduler());

            new SubscriptionInitializeVisitor(s).Initialize(ctx);

            o.OnNext(42);

            s.Dispose();

            o.OnNext(43);

            Assert.IsTrue(new[] { 42 }.SequenceEqual(xs));
        }
示例#3
0
        public void Engine_Subject_NoInputs()
        {
            var id  = new Uri("tests://bar/foo");
            var svc = new MiniService();

            var subject = GetSubject(id, svc);

            Assert.AreEqual(0, subject.Inputs.Count());
        }
示例#4
0
        public void Engine_Subject_NoIReliableObservable()
        {
            var id  = new Uri("tests://bar/foo");
            var svc = new MiniService();

            var subject = GetSubject(id, svc);

            Assert.ThrowsException <NotSupportedException>(() => ((IReliableObservable <int>)subject).Subscribe(null));
        }
示例#5
0
        public void Engine_Subject_NoIReliableMultiSubject()
        {
            var id  = new Uri("tests://bar/foo");
            var svc = new MiniService();

            var subject = GetSubject(id, svc);

            Assert.ThrowsException <NotSupportedException>(() => ((IReliableMultiSubject <int, int>)subject).CreateObserver());
        }
示例#6
0
        public void Engine_Subject_Empty()
        {
            var id  = new Uri("tests://bar/foo");
            var svc = new MiniService();

            var subject = GetSubject(id, svc);

            subject.Seal();

            Assert.IsTrue(svc.DeletedStreams.SequenceEqual(new[] { id }));
        }
示例#7
0
        private static InnerSubject <int> GetSubject(Uri id, MiniService svc)
        {
            var sch = new MiniScheduler();
            var ctx = CreateOperatorContext(id, svc, sch);

            var subject = new InnerSubject <int>();

            subject.SetContext(ctx);
            subject.Start();

            return(subject);
        }
示例#8
0
        public static void Main(string[] args)
        {
            var server = new Grpc.Core.Server
            {
                Services = { MiniService.BindService(new MiniMathImpl()) },
                Ports    = { new ServerPort("localhost", Port, ServerCredentials.Insecure) }
            };

            server.Start();

            Console.WriteLine("Listening on port " + Port);
            Console.WriteLine("Press any key to stop the server...");
            Console.ReadKey();

            server.ShutdownAsync().Wait();
        }
示例#9
0
        public void Engine_Subject_Sealed()
        {
            var id  = new Uri("tests://bar/foo");
            var svc = new MiniService();

            var subject = GetSubject(id, svc);

            for (var i = 0; i < 2; i++)
            {
                subject.Seal();

                var xs = (ISubscribable <int>)subject;
                var iv = Observer.Nop <int>();

                Assert.ThrowsException <InvalidOperationException>(() => xs.Subscribe(iv));
            }
        }
示例#10
0
        public void Engine_Subject_Sealed_StartThrows()
        {
            var id  = new Uri("tests://bar/foo");
            var svc = new MiniService();

            var subject = GetSubject(id, svc);

            var s1 = subject.Subscribe(Observer.Nop <int>());

            subject.Seal();

            Assert.ThrowsException <InvalidOperationException>(() =>
            {
                var ctx = CreateOperatorContext(new Uri("tests://qux/1"), svc, new MiniScheduler());
                new SubscriptionInitializeVisitor(s1).Initialize(ctx);
            });

            Assert.ThrowsException <InvalidOperationException>(() => subject.Subscribe(Observer.Nop <int>()));
        }
示例#11
0
        public void Engine_Subject_Single()
        {
            var id  = new Uri("tests://bar/foo");
            var svc = new MiniService();

            var subject = GetSubject(id, svc);

            var res  = new List <int>();
            var done = false;

            var xs = (ISubscribable <int>)subject;
            var iv = Observer.Create <int>(
                x => { res.Add(x); },
                ex => { Assert.Fail(); },
                () => { done = true; }
                );

            var sub = xs.Subscribe(iv);

            var ctx = CreateOperatorContext(new Uri("tests://qux/1"), svc, new MiniScheduler());

            new SubscriptionInitializeVisitor(sub).Initialize(ctx);

            var observer = subject.CreateObserver();

            subject.Seal();

            Assert.IsTrue(svc.DeletedStreams.Count == 0);

            observer.OnNext(42);
            observer.OnNext(43);
            observer.OnCompleted();

            Assert.IsTrue(new[] { 42, 43 }.SequenceEqual(res));
            Assert.IsTrue(done);

            Assert.IsTrue(svc.DeletedStreams.Count == 0);

            sub.Dispose();

            Assert.IsTrue(svc.DeletedStreams.SequenceEqual(new[] { id }));
        }
示例#12
0
        public void Engine_Subject_Checkpoint_Empty()
        {
            var id  = new Uri("tests://bar/foo");
            var svc = new MiniService();

            var mosc = new MockOperatorStateContainer();

            {
                var subject = GetSubject(id, svc);
                subject.Seal();

                Assert.IsTrue(subject.StateChanged);

                using (var oswf = mosc.CreateWriter())
                {
                    oswf.SaveState(subject);
                }

                Assert.IsTrue(subject.StateChanged);

                subject.OnStateSaved();

                Assert.IsFalse(subject.StateChanged);
            }

            {
                var subject = GetSubject(id, svc);

                Assert.IsTrue(subject.StateChanged);

                using (var osrf = mosc.CreateReader())
                {
                    osrf.LoadState(subject);
                }

                Assert.IsFalse(subject.StateChanged);

                // gets sealed immediately upon recovery because it was empty
                Assert.ThrowsException <InvalidOperationException>(() => subject.Subscribe(Observer.Nop <int>()));
            }
        }
示例#13
0
 public Qubject(MiniService parent, Uri uri)
 {
     _parent = parent;
     _uri    = uri;
 }
示例#14
0
        public void Engine_Subject_Checkpoint_SealLater()
        {
            var id  = new Uri("tests://bar/foo");
            var svc = new MiniService();
            var sch = new MiniScheduler();

            var mosc1 = new MockOperatorStateContainer();
            var mosc2 = new MockOperatorStateContainer();

            {
                var subject = GetSubject(id, svc);

                var xs = (ISubscribable <int>)subject;

                var d1 = xs.Subscribe(Observer.Nop <int>());
                new SubscriptionInitializeVisitor(d1).Initialize(CreateOperatorContext(new Uri("tests://qux/1"), svc, sch));

                var d2 = xs.Subscribe(Observer.Nop <int>());
                new SubscriptionInitializeVisitor(d2).Initialize(CreateOperatorContext(new Uri("tests://qux/2"), svc, sch));

                d1.Dispose();

                var d3 = xs.Subscribe(Observer.Nop <int>());
                new SubscriptionInitializeVisitor(d3).Initialize(CreateOperatorContext(new Uri("tests://qux/3"), svc, sch));

                Assert.IsTrue(subject.StateChanged);

                using (var oswf = mosc1.CreateWriter())
                {
                    oswf.SaveState(subject);
                }

                Assert.IsTrue(subject.StateChanged);

                subject.OnStateSaved();

                Assert.IsFalse(subject.StateChanged);
            }

            {
                var subject = GetSubject(id, svc);

                Assert.IsTrue(subject.StateChanged);

                using (var osrf = mosc1.CreateReader())
                {
                    osrf.LoadState(subject);
                }

                Assert.IsFalse(subject.StateChanged);

                var xs = (ISubscribable <int>)subject;

                var d2 = xs.Subscribe(Observer.Nop <int>());
                new SubscriptionInitializeVisitor(d2).Initialize(CreateOperatorContext(new Uri("tests://qux/2"), svc, sch));

                var d3 = xs.Subscribe(Observer.Nop <int>());
                new SubscriptionInitializeVisitor(d3).Initialize(CreateOperatorContext(new Uri("tests://qux/3"), svc, sch));

                Assert.IsFalse(subject.StateChanged);

                var d4 = xs.Subscribe(Observer.Nop <int>());
                new SubscriptionInitializeVisitor(d4).Initialize(CreateOperatorContext(new Uri("tests://qux/4"), svc, sch));

                Assert.IsTrue(subject.StateChanged);

                var d5 = xs.Subscribe(Observer.Nop <int>());
                new SubscriptionInitializeVisitor(d5).Initialize(CreateOperatorContext(new Uri("tests://qux/5"), svc, sch));

                d3.Dispose();

                subject.Seal();

                Assert.IsTrue(subject.StateChanged);

                using (var oswf = mosc2.CreateWriter())
                {
                    oswf.SaveState(subject);
                }

                Assert.IsTrue(subject.StateChanged);

                subject.OnStateSaved();

                Assert.IsFalse(subject.StateChanged);
            }

            {
                var subject = GetSubject(id, svc);

                Assert.IsTrue(subject.StateChanged);

                using (var osrf = mosc2.CreateReader())
                {
                    osrf.LoadState(subject);
                }

                Assert.IsFalse(subject.StateChanged);

                var xs = (ISubscribable <int>)subject;

                var d2 = xs.Subscribe(Observer.Nop <int>());
                new SubscriptionInitializeVisitor(d2).Initialize(CreateOperatorContext(new Uri("tests://qux/2"), svc, sch));

                var d4 = xs.Subscribe(Observer.Nop <int>());
                new SubscriptionInitializeVisitor(d4).Initialize(CreateOperatorContext(new Uri("tests://qux/4"), svc, sch));

                var d5 = xs.Subscribe(Observer.Nop <int>());
                new SubscriptionInitializeVisitor(d5).Initialize(CreateOperatorContext(new Uri("tests://qux/5"), svc, sch));

                Assert.IsFalse(subject.StateChanged);

                // gets sealed after all subscriptions have been recreated
                Assert.ThrowsException <InvalidOperationException>(() => subject.Subscribe(Observer.Nop <int>()));
            }
        }
示例#15
0
        private static void Engine_Subject_Multiple_Core(bool fail)
        {
            var id  = new Uri("tests://bar/foo");
            var svc = new MiniService();

            var subject = GetSubject(id, svc);

            var res    = new List <int> [3];
            var done   = new bool[3];
            var errors = new Exception[3];

            var xs = (ISubscribable <int>)subject;

            var subs = new List <ISubscription>();

            for (var i = 0; i < 3; i++)
            {
                var j = i;

                res[j]  = new List <int>();
                done[j] = false;

                var iv = Observer.Create <int>(
                    x =>
                {
                    res[j].Add(x);
                },
                    ex =>
                {
                    Assert.IsNull(errors[j]);
                    errors[j] = ex;
                },
                    () =>
                {
                    Assert.IsFalse(done[j]);
                    done[j] = true;
                }
                    );

                var sub = xs.Subscribe(iv);

                var ctx = CreateOperatorContext(new Uri("tests://qux/" + i), svc, new MiniScheduler());
                new SubscriptionInitializeVisitor(sub).Initialize(ctx);

                subs.Add(sub);
            }

            var observer = subject.CreateObserver();

            subject.Seal();

            Assert.IsTrue(svc.DeletedStreams.Count == 0);

            observer.OnNext(42);

            subs[1].Dispose();

            Assert.IsTrue(svc.DeletedStreams.Count == 0);

            observer.OnNext(43);

            subs[0].Dispose();

            Assert.IsTrue(svc.DeletedStreams.Count == 0);

            var err = new Exception();

            if (fail)
            {
                observer.OnError(err);
                observer.OnError(err);
            }
            else
            {
                observer.OnCompleted();
                observer.OnCompleted();
            }

            subs[2].Dispose();

            Assert.IsTrue(svc.DeletedStreams.SequenceEqual(new[] { id }));

            Assert.IsTrue(new[] { 42, 43 }.SequenceEqual(res[0]));
            Assert.IsTrue(new[] { 42 }.SequenceEqual(res[1]));
            Assert.IsTrue(new[] { 42, 43 }.SequenceEqual(res[2]));

            if (fail)
            {
                Assert.IsFalse(done[0]);
                Assert.IsFalse(done[1]);
                Assert.IsFalse(done[2]);

                Assert.IsNull(errors[0]);
                Assert.IsNull(errors[1]);
                Assert.AreSame(err, errors[2]);
            }
            else
            {
                Assert.IsFalse(done[0]);
                Assert.IsFalse(done[1]);
                Assert.IsTrue(done[2]);

                Assert.IsNull(errors[0]);
                Assert.IsNull(errors[1]);
                Assert.IsNull(errors[2]);
            }
        }