Ejemplo n.º 1
0
        public void TestReadWithMultipleBlocks()
        {
            ITokenReaderState tokenReaderState = new JsonTokenReaderState();

            ITokenReader tokenReader = new JsonTokenReader();

            String expectedMessage = TestData.HelloRequest.Json;

            (String left, String right)messageBlocks = expectedMessage.Divide();

            ReadOnlySequence <byte> firstBuffer  = new ReadOnlySequence <byte>(JsonSerializationSettings.Encoding.GetBytes(messageBlocks.left));
            ReadOnlySequence <byte> secondBuffer = new ReadOnlySequence <byte>(JsonSerializationSettings.Encoding.GetBytes(messageBlocks.left + messageBlocks.right));

            bool firstReadResult = tokenReader.Read(ref tokenReaderState, ref firstBuffer, out _, out _);

            Assert.IsFalse(firstReadResult);

            bool secondReadResult = tokenReader.Read(ref tokenReaderState, ref secondBuffer, out ReadOnlySequence <byte> token, out _);

            Assert.IsTrue(secondReadResult);

            String actualMessage = JsonSerializationSettings.Encoding.GetString(token.ToArray());

            Assert.IsTrue(JsonComparer.AreEqual(expectedMessage, actualMessage));
        }
Ejemplo n.º 2
0
        public void TestReadWithSingleBlock()
        {
            ITokenReaderState tokenReaderState = new JsonTokenReaderState();

            ITokenReader tokenReader = new JsonTokenReader();

            String expectedMessage = TestData.HelloRequest.Json;

            ReadOnlySequence <byte> buffer = new ReadOnlySequence <byte>(JsonSerializationSettings.Encoding.GetBytes(expectedMessage));

            bool readResult = tokenReader.Read(ref tokenReaderState, ref buffer, out ReadOnlySequence <byte> token, out _);

            Assert.IsTrue(readResult);

            String actualMessage = JsonSerializationSettings.Encoding.GetString(token.ToArray());

            Assert.IsTrue(JsonComparer.AreEqual(expectedMessage, actualMessage));
        }
Ejemplo n.º 3
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);
                    }
                }
            }
        }