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); } }
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 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."); } }
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); } }
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); } }
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); } }
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 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); } }
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(); } } }
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 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); } }