public void Seek_ThrowsNotSupportedException()
 {
     using (var stream = new BlockingMemoryStream())
     {
         Assert.Throws <NotSupportedException>(() => stream.Seek(0, SeekOrigin.Begin));
     }
 }
        public void ReceiveMessage_should_throw_a_FormatException_when_message_is_an_invalid_size(
            [Values(-1, 48000001)]
            int length,
            [Values(false, true)]
            bool async)
        {
            using (var stream = new BlockingMemoryStream())
            {
                var bytes = BitConverter.GetBytes(length);
                stream.Write(bytes, 0, bytes.Length);
                stream.Seek(0, SeekOrigin.Begin);
                var encoderSelector = new ReplyMessageEncoderSelector <BsonDocument>(BsonDocumentSerializer.Instance);

                Exception exception;
                if (async)
                {
                    _mockStreamFactory
                    .Setup(f => f.CreateStreamAsync(_endPoint, CancellationToken.None))
                    .ReturnsAsync(stream);
                    _subject.OpenAsync(CancellationToken.None).GetAwaiter().GetResult();
                    exception = Record
                                .Exception(() => _subject.ReceiveMessageAsync(10, encoderSelector, _messageEncoderSettings, CancellationToken.None)
                                           .GetAwaiter()
                                           .GetResult());
                }
                else
                {
                    _mockStreamFactory.Setup(f => f.CreateStream(_endPoint, CancellationToken.None))
                    .Returns(stream);
                    _subject.Open(CancellationToken.None);
                    exception = Record.Exception(() => _subject.ReceiveMessage(10, encoderSelector, _messageEncoderSettings, CancellationToken.None));
                }

                exception.Should().BeOfType <MongoConnectionException>();
                var e = exception.InnerException.Should().BeOfType <FormatException>().Subject;
                e.Message.Should().Be("The size of the message is invalid.");
            }
        }