public void ReadMessage_should_throw_if_binaryReader_was_not_provided() { using (var stream = new MemoryStream()) using (var binaryWriter = new BsonBinaryWriter(stream)) { var subject = new UpdateMessageBinaryEncoder(null, binaryWriter); Action action = () => subject.ReadMessage(); action.ShouldThrow <InvalidOperationException>(); } }
public void ReadMessage_should_read_a_message() { using (var stream = new MemoryStream(__testMessageBytes)) { var subject = new UpdateMessageBinaryEncoder(stream, __messageEncoderSettings); var message = subject.ReadMessage(); message.CollectionNamespace.Should().Be(__collectionNamespace); message.IsMulti.Should().Be(__isMulti); message.IsUpsert.Should().Be(__isUpsert); message.Query.Should().Be(__query); message.RequestId.Should().Be(__requestId); message.Update.Should().Be(__update); } }
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 UpdateMessageBinaryEncoder(stream, __messageEncoderSettings); var exception = Record.Exception(() => subject.ReadMessage()); exception.Should().BeOfType <FormatException>(); exception.Message.Should().Be("Update message opcode is not OP_UPDATE."); } }
public void ReadMessage_should_decode_flags_correctly(int flags, bool isUpsert, bool isMulti) { var bytes = (byte[])__testMessageBytes.Clone(); bytes[__flagsOffset] = (byte)flags; using (var stream = new MemoryStream(bytes)) using (var binaryReader = new BsonBinaryReader(stream)) { var subject = new UpdateMessageBinaryEncoder(stream, __messageEncoderSettings); var message = subject.ReadMessage(); message.IsMulti.Should().Be(isMulti); message.IsUpsert.Should().Be(isUpsert); } }
public void ReadMessage_should_read_a_message() { using (var stream = new MemoryStream(__testMessageBytes)) using (var binaryReader = new BsonBinaryReader(stream)) { var subject = new UpdateMessageBinaryEncoder(binaryReader, null); var message = subject.ReadMessage(); message.DatabaseName.Should().Be(__databaseName); message.CollectionName.Should().Be(__collectionName); message.IsMulti.Should().Be(__isMulti); message.IsUpsert.Should().Be(__isUpsert); message.Query.Should().Be(__query); message.RequestId.Should().Be(__requestId); message.Update.Should().Be(__update); } }
private void ProcessUpdateMessage(UpdateMessage originalMessage, Queue<RequestMessage> messageQueue, ConnectionId connectionId, UpdateMessageBinaryEncoder encoder, Stopwatch stopwatch) { var commandName = "update"; int requestId = originalMessage.RequestId; var operationId = EventContext.OperationId; var expectedResponseType = ExpectedResponseType.None; BsonValue upsertedId = null; int gleRequestId; WriteConcern writeConcern; if (TryGetWriteConcernFromGLE(messageQueue, out gleRequestId, out writeConcern)) { requestId = gleRequestId; expectedResponseType = ExpectedResponseType.GLE; } if (_startedEvent != null) { var decodedMessage = encoder.ReadMessage(RawBsonDocumentSerializer.Instance); try { if (_shouldTrackState) { // GLE result on older versions of the server didn't return // the upserted id when it wasn't an object id, so we'll // attempt to get it from the messages. if (!decodedMessage.Update.TryGetValue("_id", out upsertedId)) { decodedMessage.Query.TryGetValue("_id", out upsertedId); } } var entry = new BsonDocument { { "q", decodedMessage.Query }, { "u", decodedMessage.Update }, { "upsert", decodedMessage.IsUpsert }, { "multi", decodedMessage.IsMulti } }; var command = new BsonDocument { { commandName, decodedMessage.CollectionNamespace.CollectionName }, { "updates", new BsonArray(new [] { entry }) } }; if (writeConcern == null) { command["writeConcern"] = WriteConcern.Unacknowledged.ToBsonDocument(); } else if (!writeConcern.IsServerDefault) { command["writeConcern"] = writeConcern.ToBsonDocument(); } var @event = new CommandStartedEvent( commandName, command, decodedMessage.CollectionNamespace.DatabaseNamespace, operationId, requestId, connectionId); _startedEvent(@event); } finally { var disposable = decodedMessage.Query as IDisposable; if (disposable != null) { disposable.Dispose(); } disposable = decodedMessage.Update as IDisposable; if (disposable != null) { disposable.Dispose(); } } } if (_shouldTrackState) { _state.TryAdd(requestId, new CommandState { CommandName = commandName, OperationId = operationId, Stopwatch = Stopwatch.StartNew(), ExpectedResponseType = expectedResponseType, UpsertedId = upsertedId }); } }
public void ReadMessage_should_throw_if_binaryReader_was_not_provided() { using (var stream = new MemoryStream()) using (var binaryWriter = new BsonBinaryWriter(stream)) { var subject = new UpdateMessageBinaryEncoder(null, binaryWriter); Action action = () => subject.ReadMessage(); action.ShouldThrow<InvalidOperationException>(); } }