示例#1
0
        public void WriteMessage_should_invoke_encoding_post_processor(
            [Values(false, true)] bool wrapped)
        {
            var stream              = new MemoryStream();
            var subject             = new QueryMessageBinaryEncoder(stream, new MessageEncoderSettings());
            var collectionNamespace = new CollectionNamespace(new DatabaseNamespace("databaseName"), "collectionName");
            var command             = BsonDocument.Parse("{ command : \"x\", writeConcern : { w : 0 } }");
            var query = wrapped ? new BsonDocument("$query", command) : command;

#pragma warning disable 618
            var message = new QueryMessage(0, collectionNamespace, query, null, NoOpElementNameValidator.Instance, 0, 0, false, false, false, false, false, false, null)
            {
                PostWriteAction  = encoder => encoder.ChangeWriteConcernFromW0ToW1(),
                ResponseHandling = CommandResponseHandling.Ignore
            };
#pragma warning restore 618

            subject.WriteMessage(message);

            stream.Position = 0;
            var rehydratedMessage = subject.ReadMessage();
            var rehydratedQuery   = rehydratedMessage.Query;
            var rehydratedCommand = wrapped ? rehydratedQuery["$query"].AsBsonDocument : rehydratedQuery;
            rehydratedCommand["writeConcern"]["w"].Should().Be(1);

            // assert that the original message was altered only as expected
            message.ResponseHandling.Should().Be(CommandResponseHandling.Return); // was Ignore before PostWriteAction
            var originalQuery   = message.Query;
            var originalCommand = wrapped ? originalQuery["$query"].AsBsonDocument : originalQuery;
            originalCommand["writeConcern"]["w"].Should().Be(0); // unchanged
        }
 public void WriteMessage_should_throw_if_message_is_null()
 {
     using (var stream = new MemoryStream())
     {
         var    subject = new QueryMessageBinaryEncoder(stream, __messageEncoderSettings);
         Action action  = () => subject.WriteMessage(null);
         action.ShouldThrow <ArgumentNullException>();
     }
 }
 public void WriteMessage_should_write_a_message()
 {
     using (var stream = new MemoryStream())
     {
         var subject = new QueryMessageBinaryEncoder(stream, __messageEncoderSettings);
         subject.WriteMessage(__testMessage);
         var bytes = stream.ToArray();
         bytes.Should().Equal(__testMessageBytes);
     }
 }
示例#4
0
 public void WriteMessage_should_throw_if_binaryWriter_was_not_provided()
 {
     using (var stream = new MemoryStream())
         using (var binaryReader = new BsonBinaryReader(stream))
         {
             var    subject = new QueryMessageBinaryEncoder(binaryReader, null);
             Action action  = () => subject.WriteMessage(__testMessage);
             action.ShouldThrow <InvalidOperationException>();
         }
 }
        public void WriteMessage_should_encode_flags_correctly(int flags, bool tailableCursor, bool slaveOk, bool noCursorTimeout, bool oplogReplay, bool awaitData, bool partialOk)
        {
            var message = new QueryMessage(__requestId, __collectionNamespace, __query, __fields, __queryValidator, __skip, __batchSize, slaveOk, partialOk, noCursorTimeout, oplogReplay, tailableCursor, awaitData);

            using (var stream = new MemoryStream())
            {
                var subject = new QueryMessageBinaryEncoder(stream, __messageEncoderSettings);
                subject.WriteMessage(message);
                var bytes = stream.ToArray();
                bytes[__flagsOffset].Should().Be((byte)flags);
            }
        }
 public void WriteMessage_should_write_a_message()
 {
     using (var stream = new MemoryStream())
     {
         var subject = new QueryMessageBinaryEncoder(stream, __messageEncoderSettings);
         subject.WriteMessage(__testMessage);
         var bytes = stream.ToArray();
         bytes.Should().Equal(__testMessageBytes);
     }
 }
 public void WriteMessage_should_throw_if_message_is_null()
 {
     using (var stream = new MemoryStream())
     {
         var subject = new QueryMessageBinaryEncoder(stream, __messageEncoderSettings);
         Action action = () => subject.WriteMessage(null);
         action.ShouldThrow<ArgumentNullException>();
     }
 }
        public void WriteMessage_should_encode_flags_correctly(int flags, bool tailableCursor, bool slaveOk, bool noCursorTimeout, bool oplogReplay, bool awaitData, bool partialOk)
        {
            var message = new QueryMessage(__requestId, __collectionNamespace, __query, __fields, __queryValidator, __skip, __batchSize, slaveOk, partialOk, noCursorTimeout, oplogReplay, tailableCursor, awaitData);

            using (var stream = new MemoryStream())
            {
                var subject = new QueryMessageBinaryEncoder(stream, __messageEncoderSettings);
                subject.WriteMessage(message);
                var bytes = stream.ToArray();
                bytes[__flagsOffset].Should().Be((byte)flags);
            }
        }
 public void WriteMessage_should_throw_if_binaryWriter_was_not_provided()
 {
     using (var stream = new MemoryStream())
     using (var binaryReader = new BsonBinaryReader(stream))
     {
         var subject = new QueryMessageBinaryEncoder(binaryReader, null);
         Action action = () => subject.WriteMessage(__testMessage);
         action.ShouldThrow<InvalidOperationException>();
     }
 }