コード例 #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
        }
コード例 #2
0
 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>();
     }
 }
コード例 #3
0
 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>();
         }
 }
コード例 #5
0
        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);
            }
        }
コード例 #6
0
        public void ReadMessage_should_throw_when_opcode_is_invalid()
        {
            var bytes = (byte[])__testMessageBytes.Clone();

            bytes[12]++;

            using (var stream = new MemoryStream(bytes))
            {
                var subject   = new QueryMessageBinaryEncoder(stream, __messageEncoderSettings);
                var exception = Record.Exception(() => subject.ReadMessage());
                exception.Should().BeOfType <FormatException>();
                exception.Message.Should().Be("Query message opcode is not OP_QUERY.");
            }
        }
コード例 #7
0
        public void ReadMessage_should_decode_flags_correctly(int flags, bool tailableCursor, bool slaveOk, bool noCursorTimeout, bool awaitData, bool partialOk)
        {
            var bytes = (byte[])__testMessageBytes.Clone();

            bytes[__flagsOffset] = (byte)flags;

            using (var stream = new MemoryStream(bytes))
            {
                var subject = new QueryMessageBinaryEncoder(stream, __messageEncoderSettings);
                var message = subject.ReadMessage();
                message.TailableCursor.Should().Be(tailableCursor);
                message.SlaveOk.Should().Be(slaveOk);
                message.NoCursorTimeout.Should().Be(noCursorTimeout);
                message.AwaitData.Should().Be(awaitData);
                message.PartialOk.Should().Be(partialOk);
            }
        }
コード例 #8
0
 public void ReadMessage_should_read_a_message()
 {
     using (var stream = new MemoryStream(__testMessageBytes))
     {
         var subject = new QueryMessageBinaryEncoder(stream, __messageEncoderSettings);
         var message = subject.ReadMessage();
         message.CollectionNamespace.Should().Be(__collectionNamespace);
         message.AwaitData.Should().Be(__awaitData);
         message.BatchSize.Should().Be(__batchSize);
         message.Fields.Should().Be(__fields);
         message.NoCursorTimeout.Should().Be(__noCursorTimeout);
         message.PartialOk.Should().Be(__partialOk);
         message.Query.Should().Be(__query);
         message.RequestId.Should().Be(__requestId);
         message.Skip.Should().Be(__skip);
         message.SlaveOk.Should().Be(__slaveOk);
         message.TailableCursor.Should().Be(__tailableCursor);
     }
 }
コード例 #9
0
        public void ReadMessage_should_decode_flags_correctly(int flags, bool tailableCursor, bool slaveOk, bool noCursorTimeout, bool oplogReplay, bool awaitData, bool partialOk)
        {
            var bytes = (byte[])__testMessageBytes.Clone();
            bytes[__flagsOffset] = (byte)flags;

            using (var stream = new MemoryStream(bytes))
            {
                var subject = new QueryMessageBinaryEncoder(stream, __messageEncoderSettings);
                var message = subject.ReadMessage();
                message.TailableCursor.Should().Be(tailableCursor);
                message.SlaveOk.Should().Be(slaveOk);
                message.NoCursorTimeout.Should().Be(noCursorTimeout);
                message.OplogReplay.Should().Be(oplogReplay);
                message.AwaitData.Should().Be(awaitData);
                message.PartialOk.Should().Be(partialOk);
            }
        }
コード例 #10
0
 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);
     }
 }
コード例 #11
0
 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>();
     }
 }
コード例 #12
0
        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);
            }
        }
コード例 #13
0
 public void ReadMessage_should_read_a_message()
 {
     using (var stream = new MemoryStream(__testMessageBytes))
     {
         var subject = new QueryMessageBinaryEncoder(stream, __messageEncoderSettings);
         var message = subject.ReadMessage();
         message.CollectionNamespace.Should().Be(__collectionNamespace);
         message.AwaitData.Should().Be(__awaitData);
         message.BatchSize.Should().Be(__batchSize);
         message.Fields.Should().Be(__fields);
         message.NoCursorTimeout.Should().Be(__noCursorTimeout);
         message.OplogReplay.Should().Be(__oplogReplay);
         message.PartialOk.Should().Be(__partialOk);
         message.Query.Should().Be(__query);
         message.RequestId.Should().Be(__requestId);
         message.Skip.Should().Be(__skip);
         message.SlaveOk.Should().Be(__slaveOk);
         message.TailableCursor.Should().Be(__tailableCursor);
     }
 }
コード例 #14
0
        private void ProcessQueryMessage(QueryMessage originalMessage, ConnectionId connectionId, QueryMessageBinaryEncoder encoder, Stopwatch stopwatch)
        {
            var requestId = originalMessage.RequestId;
            var operationId = EventContext.OperationId;

            var decodedMessage = encoder.ReadMessage(RawBsonDocumentSerializer.Instance);
            try
            {
                var isCommand = IsCommand(decodedMessage.CollectionNamespace);
                string commandName;
                BsonDocument command;
                if (isCommand)
                {
                    command = decodedMessage.Query;
                    var firstElement = command.GetElement(0);
                    commandName = firstElement.Name;
                    if (__securitySensitiveCommands.Contains(commandName))
                    {
                        command = new BsonDocument();
                    }
                }
                else
                {
                    commandName = "find";
                    command = BuildFindCommandFromQuery(decodedMessage);
                    if (decodedMessage.Query.GetValue("$explain", false).ToBoolean())
                    {
                        commandName = "explain";
                        command = new BsonDocument("explain", command);
                    }
                }

                if (_startedEvent != null)
                {
                    var @event = new CommandStartedEvent(
                        commandName,
                        command,
                        decodedMessage.CollectionNamespace.DatabaseNamespace,
                        operationId,
                        requestId,
                        connectionId);

                    _startedEvent(@event);
                }

                if (_shouldTrackState)
                {
                    _state.TryAdd(requestId, new CommandState
                    {
                        CommandName = commandName,
                        OperationId = operationId,
                        Stopwatch = stopwatch,
                        QueryNamespace = decodedMessage.CollectionNamespace,
                        ExpectedResponseType = isCommand ? ExpectedResponseType.Command : ExpectedResponseType.Query
                    });
                }
            }
            finally
            {
                var disposable = decodedMessage.Query as IDisposable;
                if (disposable != null)
                {
                    disposable.Dispose();
                }
                disposable = decodedMessage.Fields as IDisposable;
                if (disposable != null)
                {
                    disposable.Dispose();
                }
            }
        }
コード例 #15
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>();
     }
 }
コード例 #16
0
 public void ReadMessage_should_read_a_message()
 {
     using (var stream = new MemoryStream(__testMessageBytes))
     using (var binaryReader = new BsonBinaryReader(stream))
     {
         var subject = new QueryMessageBinaryEncoder(binaryReader, null);
         var message = subject.ReadMessage();
         message.DatabaseName.Should().Be(__databaseName);
         message.CollectionName.Should().Be(__collectionName);
         message.AwaitData.Should().Be(__awaitData);
         message.BatchSize.Should().Be(__batchSize);
         message.Fields.Should().Be(__fields);
         message.NoCursorTimeout.Should().Be(__noCursorTimeout);
         message.PartialOk.Should().Be(__partialOk);
         message.Query.Should().Be(__query);
         message.RequestId.Should().Be(__requestId);
         message.Skip.Should().Be(__skip);
         message.SlaveOk.Should().Be(__slaveOk);
         message.TailableCursor.Should().Be(__tailableCursor);
     }
 }