Exemple #1
0
        public async Task ShouldWork(
            [Frozen] Stream stream,
            [Frozen] Mock <IProtobufFieldNumberCache> cache,
            TestMessage message,
            List <string> list)
        {
            var protobuf = new Protobuf(Array.Empty <Type>());

            var sut = new ProtobufConnection(stream, cache.Object, protobuf);

            cache.Setup(x => x.GetFieldNumber(typeof(TestMessage)))
            .Returns(2);

            cache.Setup(x => x.GetFieldNumber(typeof(List <string>)))
            .Returns(3);
            cache.Setup(x => x.GetTypeByFieldNumber(3))
            .Returns(typeof(List <string>));

            // Should throw when message not registered.
            await AssertThrowsAsync <InvalidOperationException>(
                () => sut.SendAsync(message, Cts.Token));

            // List should be registered by default.
            await sut.SendAsync(list, Cts.Token);

            // Should send and receive.
            await sut.SendAsync(list, Cts.Token);

            Assert.NotEqual(0, stream.Length);

            stream.Seek(0, SeekOrigin.Begin);

            // Should read first message.
            var result = (List <string>) await sut.ReceiveAsync(Cts.Token);

            Assert.False(ReferenceEquals(list, result));
            Assert.Equal(list, result);

            // Should read second message.
            result = (List <string>) await sut.ReceiveAsync(Cts.Token);

            Assert.False(ReferenceEquals(list, result));
            Assert.Equal(list, result);

            // There should be no more messages.
            await Assert.ThrowsAsync <InvalidOperationException>(
                () => sut.ReceiveAsync(Cts.Token).AsTask());
        }
Exemple #2
0
        public async Task ShouldReceive(
            [Frozen] Stream stream,
            [Frozen] Mock <IProtobuf> protobuf,
            [Frozen] Mock <IProtobufFieldNumberCache> cache,
            TestMessage message,
            ProtobufConnection sut)
        {
            cache.Setup(x => x.GetTypeByFieldNumber(5)).Returns(typeof(TestMessage));

            protobuf.Setup(x => x.Deserialize(stream, It.Is <Func <int, Type> >(
                                                  func => func.Invoke(5) == typeof(TestMessage))))
            .Returns(message);

            var result = await sut.ReceiveAsync(Cts.Token);

            Assert.Equal(message, result);
        }