Example #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());
        }
Example #2
0
    public async Task ShouldSend(
        [Frozen] Stream stream,
        [Frozen] Mock <IProtobufStreamSerializer> protobuf,
        [Frozen] Mock <IProtobufFieldNumberCache> cache,
        byte[] writtenBytes,
        TestMessage message,
        ProtobufConnection sut)
    {
        cache.Setup(x => x.GetFieldNumber(typeof(TestMessage)))
        .Returns(5);

        Stream writeStream = null !;

        protobuf.Setup(x => x.Serialize(It.IsAny <MemoryStream>(), message, 5))
        .Callback <Stream, object, int>((stream, message, fieldNumber) =>
        {
            writeStream = stream;
            writeStream.Write(writtenBytes, 0, writtenBytes.Length);
        });

        await sut.SendAsync(message, Cts.Token);

        protobuf.Verify(x => x.Serialize(It.IsAny <MemoryStream>(), message, 5));

        // Temporary stream should be disposed.
        Assert.Throws <ObjectDisposedException>(
            () => writeStream.Read(Array.Empty <byte>(), 0, 0));

        // Should write the value to stream.
        Assert.NotEmpty(writtenBytes);
        var resultBytes = new byte[writtenBytes.Length];

        stream.Seek(0, SeekOrigin.Begin);
        await stream.ReadAsync(resultBytes.AsMemory(0, writtenBytes.Length));

        Assert.Equal(writtenBytes, resultBytes);
    }