Ejemplo n.º 1
0
        public void Test_Snapshot_Behind_Stream_EmptyCache()
        {
            var cache          = new DefaultLastValueCache <int, MockMessage>();
            var subscriber     = new MockTopicSubscriber();
            var snapshotClient = new MockSnapshotClient
            {
                Data = new[]
                {
                    new MockMessage {
                        Data = new byte[] { 1, 11 }
                    },
                    new MockMessage {
                        Data = new byte[] { 2, 22 }
                    },
                    new MockMessage {
                        Data = new byte[] { 3, 33 }
                    }
                }
            };

            var subscription = new TopicSubscription <MockMessage>
            {
                Topic        = "",
                Deserializer = m => new MockMessage {
                    Data = m
                },
                MessageHandler = m => Debug.WriteLine("[{0},{1}]", m.Data[0], m.Data[1])
            };

            var synchronizer = new TopicSynchronizer <int, MockMessage, IList <MockMessage> >(
                subscriber, subscription, cache,
                s => snapshotClient,
                (newValue, cachedValue) => newValue.Data[1] > cachedValue.Data[1],
                m => m.Data[1] >= 10,
                m => m.Data[0],
                x => x);


            Assert.IsTrue(cache.IsEmpty);

            Task.Run(() =>
            {
                for (byte i = 50; i < 250; i++)
                {
                    subscriber.InvokeSubscriptionAccessor("", new byte[] { 1, i });
                    Thread.Sleep(10);
                }
            });

            synchronizer.Init();
            Assert.AreEqual(3, cache.Count);
            subscriber.InvokeSubscriptionAccessor("", new byte[] { 1, 10 });
            Assert.AreEqual(3, cache.Count);
            Assert.IsTrue(cache[1].Data[1] >= 50);
            subscriber.InvokeSubscriptionAccessor("", new byte[] { 1, 9 });
            Assert.IsTrue(cache[1].Data[1] >= 50);
        }
Ejemplo n.º 2
0
        public void Test_BasicSubscribeUnsubscribe_And_TopicAccessors()
        {
            var subscriber = new MockTopicSubscriber();

            Assert.IsTrue(subscriber.IsEmptyAccessor());
            subscriber.Subscribe("test", m => { });
            Assert.IsFalse(subscriber.IsEmptyAccessor());

            IEnumerable <string> topics = subscriber.GetTopicsAccessor();

            Assert.AreEqual("test", topics.First());
            Assert.AreEqual("test", topics.Last());

            subscriber.Unsubscribe("test");
            Assert.IsTrue(subscriber.IsEmptyAccessor());
            Assert.AreEqual(0, subscriber.GetTopicsAccessor().Count());
        }
Ejemplo n.º 3
0
        public void Test_SubscribeOverloads()
        {
            var subscriber = new MockTopicSubscriber();

            subscriber.Subscribe("test", m => Assert.AreEqual(111, m[0]));
            subscriber.InvokeSubscriptionAccessor("test", new byte[] { 111 });
            subscriber.Unsubscribe("test");
            subscriber.InvokeSubscriptionAccessor("test", new byte[] { 222 });

            subscriber.Subscribe("test1", m => Assert.AreEqual(111, m[0]));
            subscriber.Subscribe("test2", m => Assert.AreEqual(222, m[0]));
            subscriber.InvokeSubscriptionAccessor("test1", new byte[] { 111 });
            subscriber.InvokeSubscriptionAccessor("test2", new byte[] { 222 });


            subscriber.Subscribe(m => Assert.AreEqual(111, m.Data[0]), m => new MockMessage {
                Data = m
            });
            subscriber.InvokeSubscriptionAccessor(typeof(MockMessage).FullName, new byte[] { 111 });

            subscriber.Subscribe(new TopicSubscription <MockMessage>
            {
                MessageHandler = m => Assert.AreEqual(222, m.Data[0]),
                Deserializer   = m => new MockMessage {
                    Data = m
                }
            });
            subscriber.InvokeSubscriptionAccessor(typeof(MockMessage).FullName, new byte[] { 222 });

            subscriber.Subscribe(new TopicSubscription <MockMessage>
            {
                Topic          = string.Empty,
                MessageHandler = m => Assert.AreEqual(123, m.Data[0]),
                Deserializer   = m => new MockMessage {
                    Data = m
                }
            });
            subscriber.InvokeSubscriptionAccessor(string.Empty, new byte[] { 123 });
        }
Ejemplo n.º 4
0
        public void Test_SubscribeException()
        {
            var subscriber = new MockTopicSubscriber();

            subscriber.Subscribe <MockMessage>(null);
        }