Ejemplo n.º 1
0
        public void TestSubscriptionOfSingleObserver()
        {
            using (ITokenizer tokenizer = new JsonTokenizer(this.BaseStream))
            {
                Mock <IObserver <ReadOnlySequence <byte> > > observerMock = new Mock <IObserver <ReadOnlySequence <byte> > >();

                List <String> actualMessages = new List <String>();

                using (ManualResetEventSlim sync = new ManualResetEventSlim())
                {
                    observerMock.Setup(x => x.OnNext(It.IsAny <ReadOnlySequence <byte> >())).Callback((ReadOnlySequence <byte> token) =>
                    {
                        String message = JsonSerializationSettings.Encoding.GetString(token.ToArray());

                        actualMessages.Add(message);
                    });

                    observerMock.Setup(x => x.OnCompleted()).Callback(() =>
                    {
                        sync.Set();
                    });

                    using (IDisposable subscription = tokenizer.Subscribe(observerMock.Object))
                    {
                        sync.Wait();

                        // Number of calls depends on buffer size.
                        observerMock.Verify(x => x.OnNext(It.IsAny <ReadOnlySequence <byte> >()), Times.Exactly(this.Messages.Count));

                        Assert.AreEqual(actualMessages.Count, this.Messages.Count);

                        for (int i = 0; i < actualMessages.Count; i++)
                        {
                            String expected = this.Messages[i];
                            String actual   = actualMessages[i];

                            JsonComparer.AreEqual(expected, actual);
                        }

                        Assert.IsNotNull(subscription);
                    }
                }
            }
        }